Browse code

Update api tests to use container.Run/Create in helper package

This fix is a sync up with 36266 so that relevant api tests
use the newly added container.Run/Create in helper package

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

Yong Tang authored on 2018/02/13 08:08:25
Showing 7 changed files
... ...
@@ -6,9 +6,8 @@ import (
6 6
 	"testing"
7 7
 
8 8
 	"github.com/docker/docker/api/types"
9
-	"github.com/docker/docker/api/types/container"
10
-	"github.com/docker/docker/api/types/network"
11 9
 	"github.com/docker/docker/api/types/strslice"
10
+	"github.com/docker/docker/integration/internal/container"
12 11
 	"github.com/docker/docker/integration/internal/request"
13 12
 	"github.com/stretchr/testify/require"
14 13
 )
... ...
@@ -18,22 +17,12 @@ func TestExec(t *testing.T) {
18 18
 	ctx := context.Background()
19 19
 	client := request.NewAPIClient(t)
20 20
 
21
-	container, err := client.ContainerCreate(ctx,
22
-		&container.Config{
23
-			Image:      "busybox",
24
-			Tty:        true,
25
-			WorkingDir: "/root",
26
-			Cmd:        strslice.StrSlice([]string{"top"}),
27
-		},
28
-		&container.HostConfig{},
29
-		&network.NetworkingConfig{},
30
-		"foo",
31
-	)
32
-	require.NoError(t, err)
33
-	err = client.ContainerStart(ctx, container.ID, types.ContainerStartOptions{})
34
-	require.NoError(t, err)
21
+	cID := container.Run(t, ctx, client, func(c *container.TestContainerConfig) {
22
+		c.Config.Tty = true
23
+		c.Config.WorkingDir = "/root"
24
+	})
35 25
 
36
-	id, err := client.ContainerExecCreate(ctx, container.ID,
26
+	id, err := client.ContainerExecCreate(ctx, cID,
37 27
 		types.ExecConfig{
38 28
 			WorkingDir:   "/tmp",
39 29
 			Env:          strslice.StrSlice([]string{"FOO=BAR"}),
... ...
@@ -6,13 +6,11 @@ import (
6 6
 	"time"
7 7
 
8 8
 	"github.com/docker/docker/api/types"
9
-	"github.com/docker/docker/api/types/container"
10
-	"github.com/docker/docker/api/types/network"
11
-	"github.com/docker/docker/api/types/strslice"
9
+	containertypes "github.com/docker/docker/api/types/container"
12 10
 	"github.com/docker/docker/client"
11
+	"github.com/docker/docker/integration/internal/container"
13 12
 	"github.com/docker/docker/integration/internal/request"
14 13
 	"github.com/gotestyourself/gotestyourself/poll"
15
-	"github.com/stretchr/testify/require"
16 14
 )
17 15
 
18 16
 // TestHealthCheckWorkdir verifies that health-checks inherit the containers'
... ...
@@ -22,27 +20,17 @@ func TestHealthCheckWorkdir(t *testing.T) {
22 22
 	ctx := context.Background()
23 23
 	client := request.NewAPIClient(t)
24 24
 
25
-	c, err := client.ContainerCreate(ctx,
26
-		&container.Config{
27
-			Image:      "busybox",
28
-			Tty:        true,
29
-			WorkingDir: "/foo",
30
-			Cmd:        strslice.StrSlice([]string{"top"}),
31
-			Healthcheck: &container.HealthConfig{
32
-				Test:     []string{"CMD-SHELL", "if [ \"$PWD\" = \"/foo\" ]; then exit 0; else exit 1; fi;"},
33
-				Interval: 50 * time.Millisecond,
34
-				Retries:  3,
35
-			},
36
-		},
37
-		&container.HostConfig{},
38
-		&network.NetworkingConfig{},
39
-		"healthtest",
40
-	)
41
-	require.NoError(t, err)
42
-	err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
43
-	require.NoError(t, err)
25
+	cID := container.Run(t, ctx, client, func(c *container.TestContainerConfig) {
26
+		c.Config.Tty = true
27
+		c.Config.WorkingDir = "/foo"
28
+		c.Config.Healthcheck = &containertypes.HealthConfig{
29
+			Test:     []string{"CMD-SHELL", "if [ \"$PWD\" = \"/foo\" ]; then exit 0; else exit 1; fi;"},
30
+			Interval: 50 * time.Millisecond,
31
+			Retries:  3,
32
+		}
33
+	})
44 34
 
45
-	poll.WaitOn(t, pollForHealthStatus(ctx, client, c.ID, types.Healthy), poll.WithDelay(100*time.Millisecond))
35
+	poll.WaitOn(t, pollForHealthStatus(ctx, client, cID, types.Healthy), poll.WithDelay(100*time.Millisecond))
46 36
 }
47 37
 
48 38
 func pollForHealthStatus(ctx context.Context, client client.APIClient, containerID string, healthStatus string) func(log poll.LogT) poll.Result {
... ...
@@ -9,7 +9,7 @@ import (
9 9
 	"time"
10 10
 
11 11
 	"github.com/docker/docker/api/types"
12
-	"github.com/docker/docker/api/types/container"
12
+	"github.com/docker/docker/integration/internal/container"
13 13
 	"github.com/docker/docker/integration/internal/request"
14 14
 	"github.com/docker/docker/pkg/stdcopy"
15 15
 	"github.com/gotestyourself/gotestyourself/poll"
... ...
@@ -28,24 +28,13 @@ func TestLinksEtcHostsContentMatch(t *testing.T) {
28 28
 	client := request.NewAPIClient(t)
29 29
 	ctx := context.Background()
30 30
 
31
-	c, err := client.ContainerCreate(ctx,
32
-		&container.Config{
33
-			Image: "busybox",
34
-			Cmd:   []string{"cat", "/etc/hosts"},
35
-		},
36
-		&container.HostConfig{
37
-			NetworkMode: "host",
38
-		},
39
-		nil,
40
-		"")
41
-	require.NoError(t, err)
42
-
43
-	err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
44
-	require.NoError(t, err)
31
+	cID := container.Run(t, ctx, client, container.WithCmd("cat", "/etc/hosts"), func(c *container.TestContainerConfig) {
32
+		c.HostConfig.NetworkMode = "host"
33
+	})
45 34
 
46
-	poll.WaitOn(t, containerIsStopped(ctx, client, c.ID), poll.WithDelay(100*time.Millisecond))
35
+	poll.WaitOn(t, containerIsStopped(ctx, client, cID), poll.WithDelay(100*time.Millisecond))
47 36
 
48
-	body, err := client.ContainerLogs(ctx, c.ID, types.ContainerLogsOptions{
37
+	body, err := client.ContainerLogs(ctx, cID, types.ContainerLogsOptions{
49 38
 		ShowStdout: true,
50 39
 	})
51 40
 	require.NoError(t, err)
... ...
@@ -12,8 +12,7 @@ import (
12 12
 	"time"
13 13
 
14 14
 	"github.com/docker/docker/api/types"
15
-	"github.com/docker/docker/api/types/container"
16
-	"github.com/docker/docker/api/types/network"
15
+	"github.com/docker/docker/integration/internal/container"
17 16
 	"github.com/docker/docker/integration/internal/request"
18 17
 	"github.com/docker/go-connections/nat"
19 18
 	"github.com/gotestyourself/gotestyourself/poll"
... ...
@@ -67,25 +66,15 @@ func TestNetworkLoopbackNat(t *testing.T) {
67 67
 
68 68
 	client := request.NewAPIClient(t)
69 69
 	ctx := context.Background()
70
-	c, err := client.ContainerCreate(ctx,
71
-		&container.Config{
72
-			Image: "busybox",
73
-			Cmd:   []string{"sh", "-c", fmt.Sprintf("stty raw && nc -w 5 %s 8080", endpoint.String())},
74
-			Tty:   true,
75
-		},
76
-		&container.HostConfig{
77
-			NetworkMode: "container:server",
78
-		},
79
-		nil,
80
-		"")
81
-	require.NoError(t, err)
82 70
 
83
-	err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
84
-	require.NoError(t, err)
71
+	cID := container.Run(t, ctx, client, container.WithCmd("sh", "-c", fmt.Sprintf("stty raw && nc -w 5 %s 8080", endpoint.String())), func(c *container.TestContainerConfig) {
72
+		c.Config.Tty = true
73
+		c.HostConfig.NetworkMode = "container:server"
74
+	})
85 75
 
86
-	poll.WaitOn(t, containerIsStopped(ctx, client, c.ID), poll.WithDelay(100*time.Millisecond))
76
+	poll.WaitOn(t, containerIsStopped(ctx, client, cID), poll.WithDelay(100*time.Millisecond))
87 77
 
88
-	body, err := client.ContainerLogs(ctx, c.ID, types.ContainerLogsOptions{
78
+	body, err := client.ContainerLogs(ctx, cID, types.ContainerLogsOptions{
89 79
 		ShowStdout: true,
90 80
 	})
91 81
 	require.NoError(t, err)
... ...
@@ -102,34 +91,22 @@ func startServerContainer(t *testing.T, msg string, port int) string {
102 102
 	client := request.NewAPIClient(t)
103 103
 	ctx := context.Background()
104 104
 
105
-	c, err := client.ContainerCreate(ctx,
106
-		&container.Config{
107
-			Image: "busybox",
108
-			Cmd:   []string{"sh", "-c", fmt.Sprintf("echo %q | nc -lp %d", msg, port)},
109
-			ExposedPorts: map[nat.Port]struct{}{
110
-				nat.Port(fmt.Sprintf("%d/tcp", port)): {},
111
-			},
112
-		},
113
-		&container.HostConfig{
114
-			PortBindings: nat.PortMap{
115
-				nat.Port(fmt.Sprintf("%d/tcp", port)): []nat.PortBinding{
116
-					{
117
-						HostPort: fmt.Sprintf("%d", port),
118
-					},
105
+	cID := container.Run(t, ctx, client, container.WithCmd("sh", "-c", fmt.Sprintf("echo %q | nc -lp %d", msg, port)), func(c *container.TestContainerConfig) {
106
+		c.Config.ExposedPorts = map[nat.Port]struct{}{
107
+			nat.Port(fmt.Sprintf("%d/tcp", port)): {},
108
+		}
109
+		c.HostConfig.PortBindings = nat.PortMap{
110
+			nat.Port(fmt.Sprintf("%d/tcp", port)): []nat.PortBinding{
111
+				{
112
+					HostPort: fmt.Sprintf("%d", port),
119 113
 				},
120 114
 			},
121
-		},
122
-		&network.NetworkingConfig{},
123
-		"server",
124
-	)
125
-	require.NoError(t, err)
126
-
127
-	err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
128
-	require.NoError(t, err)
115
+		}
116
+	})
129 117
 
130
-	poll.WaitOn(t, containerIsInState(ctx, client, c.ID, "running"), poll.WithDelay(100*time.Millisecond))
118
+	poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
131 119
 
132
-	return c.ID
120
+	return cID
133 121
 }
134 122
 
135 123
 func getExternalAddress(t *testing.T) net.IP {
... ...
@@ -8,8 +8,7 @@ import (
8 8
 	"time"
9 9
 
10 10
 	"github.com/docker/docker/api/types"
11
-	"github.com/docker/docker/api/types/container"
12
-	"github.com/docker/docker/api/types/network"
11
+	"github.com/docker/docker/integration/internal/container"
13 12
 	"github.com/docker/docker/integration/internal/request"
14 13
 	"github.com/gotestyourself/gotestyourself/poll"
15 14
 	"github.com/gotestyourself/gotestyourself/skip"
... ...
@@ -27,23 +26,11 @@ func TestStats(t *testing.T) {
27 27
 	info, err := client.Info(ctx)
28 28
 	require.NoError(t, err)
29 29
 
30
-	c, err := client.ContainerCreate(ctx,
31
-		&container.Config{
32
-			Cmd:   []string{"top"},
33
-			Image: "busybox",
34
-		},
35
-		&container.HostConfig{},
36
-		&network.NetworkingConfig{},
37
-		"",
38
-	)
39
-	require.NoError(t, err)
40
-
41
-	err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
42
-	require.NoError(t, err)
30
+	cID := container.Run(t, ctx, client)
43 31
 
44
-	poll.WaitOn(t, containerIsInState(ctx, client, c.ID, "running"), poll.WithDelay(100*time.Millisecond))
32
+	poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
45 33
 
46
-	resp, err := client.ContainerStats(context.Background(), c.ID, false)
34
+	resp, err := client.ContainerStats(ctx, cID, false)
47 35
 	require.NoError(t, err)
48 36
 	defer resp.Body.Close()
49 37
 
... ...
@@ -19,10 +19,9 @@ import (
19 19
 	"time"
20 20
 
21 21
 	"github.com/docker/docker/api/types"
22
-	"github.com/docker/docker/api/types/container"
23 22
 	eventtypes "github.com/docker/docker/api/types/events"
24
-	networktypes "github.com/docker/docker/api/types/network"
25 23
 	"github.com/docker/docker/client"
24
+	"github.com/docker/docker/integration/internal/container"
26 25
 	"github.com/docker/docker/integration/internal/request"
27 26
 	"github.com/docker/docker/internal/test/environment"
28 27
 	"github.com/docker/docker/pkg/authorization"
... ...
@@ -91,17 +90,15 @@ func TestAuthZPluginAllowRequest(t *testing.T) {
91 91
 	client, err := d.NewClient()
92 92
 	require.Nil(t, err)
93 93
 
94
-	// Ensure command successful
95
-	createResponse, err := client.ContainerCreate(context.Background(), &container.Config{Cmd: []string{"top"}, Image: "busybox"}, &container.HostConfig{}, &networktypes.NetworkingConfig{}, "")
96
-	require.Nil(t, err)
94
+	ctx := context.Background()
97 95
 
98
-	err = client.ContainerStart(context.Background(), createResponse.ID, types.ContainerStartOptions{})
99
-	require.Nil(t, err)
96
+	// Ensure command successful
97
+	cID := container.Run(t, ctx, client)
100 98
 
101 99
 	assertURIRecorded(t, ctrl.requestsURIs, "/containers/create")
102
-	assertURIRecorded(t, ctrl.requestsURIs, fmt.Sprintf("/containers/%s/start", createResponse.ID))
100
+	assertURIRecorded(t, ctrl.requestsURIs, fmt.Sprintf("/containers/%s/start", cID))
103 101
 
104
-	_, err = client.ServerVersion(context.Background())
102
+	_, err = client.ServerVersion(ctx)
105 103
 	require.Nil(t, err)
106 104
 	require.Equal(t, 1, ctrl.versionReqCount)
107 105
 	require.Equal(t, 1, ctrl.versionResCount)
... ...
@@ -213,19 +210,17 @@ func TestAuthZPluginAllowEventStream(t *testing.T) {
213 213
 	client, err := d.NewClient()
214 214
 	require.Nil(t, err)
215 215
 
216
+	ctx := context.Background()
217
+
216 218
 	startTime := strconv.FormatInt(systemTime(t, client, testEnv).Unix(), 10)
217 219
 	events, errs, cancel := systemEventsSince(client, startTime)
218 220
 	defer cancel()
219 221
 
220 222
 	// Create a container and wait for the creation events
221
-	createResponse, err := client.ContainerCreate(context.Background(), &container.Config{Cmd: []string{"top"}, Image: "busybox"}, &container.HostConfig{}, &networktypes.NetworkingConfig{}, "")
222
-	require.Nil(t, err)
223
-
224
-	err = client.ContainerStart(context.Background(), createResponse.ID, types.ContainerStartOptions{})
225
-	require.Nil(t, err)
223
+	cID := container.Run(t, ctx, client)
226 224
 
227 225
 	for i := 0; i < 100; i++ {
228
-		c, err := client.ContainerInspect(context.Background(), createResponse.ID)
226
+		c, err := client.ContainerInspect(ctx, cID)
229 227
 		require.Nil(t, err)
230 228
 		if c.State.Running {
231 229
 			break
... ...
@@ -241,7 +236,7 @@ func TestAuthZPluginAllowEventStream(t *testing.T) {
241 241
 	for !created && !started {
242 242
 		select {
243 243
 		case event := <-events:
244
-			if event.Type == eventtypes.ContainerEventType && event.Actor.ID == createResponse.ID {
244
+			if event.Type == eventtypes.ContainerEventType && event.Actor.ID == cID {
245 245
 				if event.Action == "create" {
246 246
 					created = true
247 247
 				}
... ...
@@ -264,7 +259,7 @@ func TestAuthZPluginAllowEventStream(t *testing.T) {
264 264
 	// authorization plugin
265 265
 	assertURIRecorded(t, ctrl.requestsURIs, "/events")
266 266
 	assertURIRecorded(t, ctrl.requestsURIs, "/containers/create")
267
-	assertURIRecorded(t, ctrl.requestsURIs, fmt.Sprintf("/containers/%s/start", createResponse.ID))
267
+	assertURIRecorded(t, ctrl.requestsURIs, fmt.Sprintf("/containers/%s/start", cID))
268 268
 }
269 269
 
270 270
 func systemTime(t *testing.T, client client.APIClient, testEnv *environment.Execution) time.Time {
... ...
@@ -347,6 +342,8 @@ func TestAuthZPluginEnsureLoadImportWorking(t *testing.T) {
347 347
 	client, err := d.NewClient()
348 348
 	require.Nil(t, err)
349 349
 
350
+	ctx := context.Background()
351
+
350 352
 	tmp, err := ioutil.TempDir("", "test-authz-load-import")
351 353
 	require.Nil(t, err)
352 354
 	defer os.RemoveAll(tmp)
... ...
@@ -360,13 +357,9 @@ func TestAuthZPluginEnsureLoadImportWorking(t *testing.T) {
360 360
 
361 361
 	exportedImagePath := filepath.Join(tmp, "export.tar")
362 362
 
363
-	createResponse, err := client.ContainerCreate(context.Background(), &container.Config{Cmd: []string{}, Image: "busybox"}, &container.HostConfig{}, &networktypes.NetworkingConfig{}, "")
364
-	require.Nil(t, err)
365
-
366
-	err = client.ContainerStart(context.Background(), createResponse.ID, types.ContainerStartOptions{})
367
-	require.Nil(t, err)
363
+	cID := container.Run(t, ctx, client)
368 364
 
369
-	responseReader, err := client.ContainerExport(context.Background(), createResponse.ID)
365
+	responseReader, err := client.ContainerExport(context.Background(), cID)
370 366
 	require.Nil(t, err)
371 367
 	defer responseReader.Close()
372 368
 	file, err := os.Create(exportedImagePath)
... ...
@@ -11,11 +11,10 @@ import (
11 11
 	"testing"
12 12
 
13 13
 	"github.com/docker/docker/api/types"
14
-	"github.com/docker/docker/api/types/container"
15 14
 	"github.com/docker/docker/api/types/filters"
16
-	networktypes "github.com/docker/docker/api/types/network"
17 15
 	volumetypes "github.com/docker/docker/api/types/volume"
18 16
 	"github.com/docker/docker/client"
17
+	"github.com/docker/docker/integration/internal/container"
19 18
 	"github.com/docker/docker/integration/internal/requirement"
20 19
 	"github.com/gotestyourself/gotestyourself/skip"
21 20
 	"github.com/stretchr/testify/require"
... ...
@@ -47,6 +46,8 @@ func TestAuthZPluginV2AllowNonVolumeRequest(t *testing.T) {
47 47
 	client, err := d.NewClient()
48 48
 	require.Nil(t, err)
49 49
 
50
+	ctx := context.Background()
51
+
50 52
 	// Install authz plugin
51 53
 	err = pluginInstallGrantAllPermissions(client, authzPluginNameWithTag)
52 54
 	require.Nil(t, err)
... ...
@@ -56,13 +57,9 @@ func TestAuthZPluginV2AllowNonVolumeRequest(t *testing.T) {
56 56
 	d.LoadBusybox(t)
57 57
 
58 58
 	// Ensure docker run command and accompanying docker ps are successful
59
-	createResponse, err := client.ContainerCreate(context.Background(), &container.Config{Cmd: []string{"top"}, Image: "busybox"}, &container.HostConfig{}, &networktypes.NetworkingConfig{}, "")
60
-	require.Nil(t, err)
61
-
62
-	err = client.ContainerStart(context.Background(), createResponse.ID, types.ContainerStartOptions{})
63
-	require.Nil(t, err)
59
+	cID := container.Run(t, ctx, client)
64 60
 
65
-	_, err = client.ContainerInspect(context.Background(), createResponse.ID)
61
+	_, err = client.ContainerInspect(ctx, cID)
66 62
 	require.Nil(t, err)
67 63
 }
68 64