Browse code

Update more tests to use new errors

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2019/01/09 22:47:06
Showing 2 changed files
... ...
@@ -4,7 +4,6 @@ import (
4 4
 	"context"
5 5
 	"encoding/json"
6 6
 	"fmt"
7
-	"net/http"
8 7
 	"strconv"
9 8
 	"testing"
10 9
 	"time"
... ...
@@ -14,6 +13,7 @@ import (
14 14
 	"github.com/docker/docker/api/types/network"
15 15
 	"github.com/docker/docker/api/types/versions"
16 16
 	"github.com/docker/docker/client"
17
+	"github.com/docker/docker/errdefs"
17 18
 	ctr "github.com/docker/docker/integration/internal/container"
18 19
 	"github.com/docker/docker/internal/test/request"
19 20
 	"github.com/docker/docker/oci"
... ...
@@ -60,6 +60,7 @@ func TestCreateFailsWhenIdentifierDoesNotExist(t *testing.T) {
60 60
 				"",
61 61
 			)
62 62
 			assert.Check(t, is.ErrorContains(err, tc.expectedError))
63
+			assert.Check(t, errdefs.IsNotFound(err))
63 64
 		})
64 65
 	}
65 66
 }
... ...
@@ -100,6 +101,7 @@ func TestCreateWithInvalidEnv(t *testing.T) {
100 100
 				"",
101 101
 			)
102 102
 			assert.Check(t, is.ErrorContains(err, tc.expectedError))
103
+			assert.Check(t, errdefs.IsInvalidParameter(err))
103 104
 		})
104 105
 	}
105 106
 }
... ...
@@ -145,6 +147,7 @@ func TestCreateTmpfsMountsTarget(t *testing.T) {
145 145
 			"",
146 146
 		)
147 147
 		assert.Check(t, is.ErrorContains(err, tc.expectedError))
148
+		assert.Check(t, errdefs.IsInvalidParameter(err))
148 149
 	}
149 150
 }
150 151
 func TestCreateWithCustomMaskedPaths(t *testing.T) {
... ...
@@ -346,6 +349,7 @@ func TestCreateWithCapabilities(t *testing.T) {
346 346
 				assert.DeepEqual(t, tc.expected, ci.HostConfig.Capabilities)
347 347
 			} else {
348 348
 				assert.ErrorContains(t, err, tc.expectedError)
349
+				assert.Check(t, errdefs.IsInvalidParameter(err))
349 350
 			}
350 351
 		})
351 352
 	}
... ...
@@ -432,6 +436,8 @@ func TestCreateWithCustomReadonlyPaths(t *testing.T) {
432 432
 
433 433
 func TestCreateWithInvalidHealthcheckParams(t *testing.T) {
434 434
 	defer setupTest(t)()
435
+	client := testEnv.APIClient()
436
+	ctx := context.Background()
435 437
 
436 438
 	testCases := []struct {
437 439
 		doc         string
... ...
@@ -479,38 +485,31 @@ func TestCreateWithInvalidHealthcheckParams(t *testing.T) {
479 479
 		},
480 480
 	}
481 481
 
482
-	for i, tc := range testCases {
483
-		i := i
482
+	for _, tc := range testCases {
484 483
 		tc := tc
485 484
 		t.Run(tc.doc, func(t *testing.T) {
486 485
 			t.Parallel()
487
-			healthCheck := map[string]interface{}{
488
-				"Interval": tc.interval,
489
-				"Timeout":  tc.timeout,
490
-				"Retries":  tc.retries,
486
+			cfg := container.Config{
487
+				Image: "busybox",
488
+				Healthcheck: &container.HealthConfig{
489
+					Interval: tc.interval,
490
+					Timeout:  tc.timeout,
491
+					Retries:  tc.retries,
492
+				},
491 493
 			}
492 494
 			if tc.startPeriod != 0 {
493
-				healthCheck["StartPeriod"] = tc.startPeriod
494
-			}
495
-
496
-			config := map[string]interface{}{
497
-				"Image":       "busybox",
498
-				"Healthcheck": healthCheck,
495
+				cfg.Healthcheck.StartPeriod = tc.startPeriod
499 496
 			}
500 497
 
501
-			res, body, err := request.Post("/containers/create?name="+fmt.Sprintf("test_%d_", i)+t.Name(), request.JSONBody(config))
502
-			assert.NilError(t, err)
498
+			resp, err := client.ContainerCreate(ctx, &cfg, &container.HostConfig{}, nil, "")
499
+			assert.Check(t, is.Equal(len(resp.Warnings), 0))
503 500
 
504 501
 			if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
505
-				assert.Check(t, is.Equal(http.StatusInternalServerError, res.StatusCode))
502
+				assert.Check(t, errdefs.IsSystem(err))
506 503
 			} else {
507
-				assert.Check(t, is.Equal(http.StatusBadRequest, res.StatusCode))
504
+				assert.Check(t, errdefs.IsInvalidParameter(err))
508 505
 			}
509
-
510
-			buf, err := request.ReadBody(body)
511
-			assert.NilError(t, err)
512
-
513
-			assert.Check(t, is.Contains(string(buf), tc.expectedErr))
506
+			assert.ErrorContains(t, err, tc.expectedErr)
514 507
 		})
515 508
 	}
516 509
 }
... ...
@@ -4,7 +4,6 @@ import (
4 4
 	"context"
5 5
 	"fmt"
6 6
 	"io/ioutil"
7
-	"net/http"
8 7
 	"testing"
9 8
 	"time"
10 9
 
... ...
@@ -13,10 +12,10 @@ import (
13 13
 	swarmtypes "github.com/docker/docker/api/types/swarm"
14 14
 	"github.com/docker/docker/api/types/versions"
15 15
 	"github.com/docker/docker/client"
16
+	"github.com/docker/docker/errdefs"
16 17
 	"github.com/docker/docker/integration/internal/network"
17 18
 	"github.com/docker/docker/integration/internal/swarm"
18 19
 	"github.com/docker/docker/internal/test/daemon"
19
-	"github.com/docker/docker/internal/test/request"
20 20
 	"gotest.tools/assert"
21 21
 	is "gotest.tools/assert/cmp"
22 22
 	"gotest.tools/poll"
... ...
@@ -129,6 +128,9 @@ func TestCreateServiceConflict(t *testing.T) {
129 129
 	defer setupTest(t)()
130 130
 	d := swarm.NewSwarm(t, testEnv)
131 131
 	defer d.Stop(t)
132
+	c := d.NewClientT(t)
133
+	defer c.Close()
134
+	ctx := context.Background()
132 135
 
133 136
 	serviceName := "TestService_" + t.Name()
134 137
 	serviceSpec := []swarm.ServiceSpecOpt{
... ...
@@ -138,18 +140,9 @@ func TestCreateServiceConflict(t *testing.T) {
138 138
 	swarm.CreateService(t, d, serviceSpec...)
139 139
 
140 140
 	spec := swarm.CreateServiceSpec(t, serviceSpec...)
141
-	res, body, err := request.Post(
142
-		"/services/create",
143
-		request.Host(d.Sock()),
144
-		request.JSONBody(spec),
145
-		request.JSON,
146
-	)
147
-	assert.NilError(t, err)
148
-	assert.Equal(t, res.StatusCode, http.StatusConflict)
149
-
150
-	buf, err := request.ReadBody(body)
151
-	assert.NilError(t, err)
152
-	assert.Check(t, is.Contains(string(buf), "service "+serviceName+" already exists"))
141
+	_, err := c.ServiceCreate(ctx, spec, types.ServiceCreateOptions{})
142
+	assert.Check(t, errdefs.IsConflict(err))
143
+	assert.ErrorContains(t, err, "service "+serviceName+" already exists")
153 144
 }
154 145
 
155 146
 func TestCreateServiceMaxReplicas(t *testing.T) {