Browse code

Update api tests to use the newly added container helper package

This fix is a follow up to 36266 to update some api tests
to use the newly added container helper package.

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

Yong Tang authored on 2018/02/11 08:01:37
Showing 4 changed files
... ...
@@ -8,9 +8,8 @@ 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"
13 11
 	"github.com/docker/docker/client"
12
+	"github.com/docker/docker/integration/internal/container"
14 13
 	"github.com/docker/docker/integration/internal/request"
15 14
 	"github.com/gotestyourself/gotestyourself/icmd"
16 15
 	"github.com/gotestyourself/gotestyourself/poll"
... ...
@@ -25,23 +24,9 @@ func TestStopContainerWithRestartPolicyAlways(t *testing.T) {
25 25
 
26 26
 	names := []string{"verifyRestart1", "verifyRestart2"}
27 27
 	for _, name := range names {
28
-		resp, err := client.ContainerCreate(ctx,
29
-			&container.Config{
30
-				Cmd:   []string{"false"},
31
-				Image: "busybox",
32
-			},
33
-			&container.HostConfig{
34
-				RestartPolicy: container.RestartPolicy{
35
-					Name: "always",
36
-				},
37
-			},
38
-			&network.NetworkingConfig{},
39
-			name,
40
-		)
41
-		require.NoError(t, err)
42
-
43
-		err = client.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
44
-		require.NoError(t, err)
28
+		container.Run(t, ctx, client, container.WithName(name), container.WithCmd("false"), func(c *container.TestContainerConfig) {
29
+			c.HostConfig.RestartPolicy.Name = "always"
30
+		})
45 31
 	}
46 32
 
47 33
 	for _, name := range names {
... ...
@@ -65,25 +50,13 @@ func TestDeleteDevicemapper(t *testing.T) {
65 65
 	client := request.NewAPIClient(t)
66 66
 	ctx := context.Background()
67 67
 
68
-	foo, err := client.ContainerCreate(ctx,
69
-		&container.Config{
70
-			Cmd:   []string{"echo"},
71
-			Image: "busybox",
72
-		},
73
-		&container.HostConfig{},
74
-		&network.NetworkingConfig{},
75
-		"foo",
76
-	)
77
-	require.NoError(t, err)
68
+	id := container.Run(t, ctx, client, container.WithName("foo"), container.WithCmd("echo"))
78 69
 
79
-	err = client.ContainerStart(ctx, foo.ID, types.ContainerStartOptions{})
80
-	require.NoError(t, err)
70
+	poll.WaitOn(t, containerIsStopped(ctx, client, id), poll.WithDelay(100*time.Millisecond))
81 71
 
82
-	inspect, err := client.ContainerInspect(ctx, foo.ID)
72
+	inspect, err := client.ContainerInspect(ctx, id)
83 73
 	require.NoError(t, err)
84 74
 
85
-	poll.WaitOn(t, containerIsStopped(ctx, client, foo.ID), poll.WithDelay(100*time.Millisecond))
86
-
87 75
 	deviceID := inspect.GraphDriver.Data["DeviceId"]
88 76
 
89 77
 	// Find pool name from device name
... ...
@@ -94,7 +67,7 @@ func TestDeleteDevicemapper(t *testing.T) {
94 94
 	result := icmd.RunCommand("dmsetup", "message", devicePool, "0", fmt.Sprintf("delete %s", deviceID))
95 95
 	result.Assert(t, icmd.Success)
96 96
 
97
-	err = client.ContainerRemove(ctx, foo.ID, types.ContainerRemoveOptions{})
97
+	err = client.ContainerRemove(ctx, id, types.ContainerRemoveOptions{})
98 98
 	require.NoError(t, err)
99 99
 }
100 100
 
... ...
@@ -3,7 +3,6 @@ package container // import "github.com/docker/docker/integration/container"
3 3
 import (
4 4
 	"bytes"
5 5
 	"context"
6
-	"fmt"
7 6
 	"io/ioutil"
8 7
 	"strconv"
9 8
 	"strings"
... ...
@@ -11,9 +10,10 @@ import (
11 11
 	"time"
12 12
 
13 13
 	"github.com/docker/docker/api/types"
14
-	"github.com/docker/docker/api/types/container"
14
+	containertypes "github.com/docker/docker/api/types/container"
15 15
 	"github.com/docker/docker/api/types/strslice"
16 16
 	"github.com/docker/docker/client"
17
+	"github.com/docker/docker/integration/internal/container"
17 18
 	"github.com/docker/docker/integration/internal/request"
18 19
 	"github.com/docker/docker/pkg/stdcopy"
19 20
 	"github.com/gotestyourself/gotestyourself/poll"
... ...
@@ -31,44 +31,32 @@ func TestUpdateMemory(t *testing.T) {
31 31
 	client := request.NewAPIClient(t)
32 32
 	ctx := context.Background()
33 33
 
34
-	c, err := client.ContainerCreate(ctx,
35
-		&container.Config{
36
-			Cmd:   []string{"top"},
37
-			Image: "busybox",
38
-		},
39
-		&container.HostConfig{
40
-			Resources: container.Resources{
41
-				Memory: 200 * 1024 * 1024,
42
-			},
43
-		},
44
-		nil,
45
-		"",
46
-	)
47
-	require.NoError(t, err)
48
-
49
-	err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
50
-	require.NoError(t, err)
34
+	cID := container.Run(t, ctx, client, func(c *container.TestContainerConfig) {
35
+		c.HostConfig.Resources = containertypes.Resources{
36
+			Memory: 200 * 1024 * 1024,
37
+		}
38
+	})
51 39
 
52
-	poll.WaitOn(t, containerIsInState(ctx, client, c.ID, "running"), poll.WithDelay(100*time.Millisecond))
40
+	poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
53 41
 
54
-	_, err = client.ContainerUpdate(ctx, c.ID, container.UpdateConfig{
55
-		Resources: container.Resources{
42
+	_, err := client.ContainerUpdate(ctx, cID, containertypes.UpdateConfig{
43
+		Resources: containertypes.Resources{
56 44
 			Memory:     314572800,
57 45
 			MemorySwap: 524288000,
58 46
 		},
59 47
 	})
60 48
 	require.NoError(t, err)
61 49
 
62
-	inspect, err := client.ContainerInspect(ctx, c.ID)
50
+	inspect, err := client.ContainerInspect(ctx, cID)
63 51
 	require.NoError(t, err)
64 52
 	assert.Equal(t, inspect.HostConfig.Memory, int64(314572800))
65 53
 	assert.Equal(t, inspect.HostConfig.MemorySwap, int64(524288000))
66 54
 
67
-	body, err := getContainerSysFSValue(ctx, client, c.ID, "/sys/fs/cgroup/memory/memory.limit_in_bytes")
55
+	body, err := getContainerSysFSValue(ctx, client, cID, "/sys/fs/cgroup/memory/memory.limit_in_bytes")
68 56
 	require.NoError(t, err)
69 57
 	assert.Equal(t, strings.TrimSpace(body), "314572800")
70 58
 
71
-	body, err = getContainerSysFSValue(ctx, client, c.ID, "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes")
59
+	body, err = getContainerSysFSValue(ctx, client, cID, "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes")
72 60
 	require.NoError(t, err)
73 61
 	assert.Equal(t, strings.TrimSpace(body), "524288000")
74 62
 }
... ...
@@ -76,25 +64,11 @@ func TestUpdateMemory(t *testing.T) {
76 76
 func TestUpdateCPUQUota(t *testing.T) {
77 77
 	t.Parallel()
78 78
 
79
+	defer setupTest(t)()
79 80
 	client := request.NewAPIClient(t)
80 81
 	ctx := context.Background()
81 82
 
82
-	c, err := client.ContainerCreate(ctx, &container.Config{
83
-		Image: "busybox",
84
-		Cmd:   []string{"top"},
85
-	}, nil, nil, "")
86
-	if err != nil {
87
-		t.Fatal(err)
88
-	}
89
-	defer func() {
90
-		if err := client.ContainerRemove(ctx, c.ID, types.ContainerRemoveOptions{Force: true}); err != nil {
91
-			panic(fmt.Sprintf("failed to clean up after test: %v", err))
92
-		}
93
-	}()
94
-
95
-	if err := client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{}); err != nil {
96
-		t.Fatal(err)
97
-	}
83
+	cID := container.Run(t, ctx, client)
98 84
 
99 85
 	for _, test := range []struct {
100 86
 		desc   string
... ...
@@ -105,15 +79,15 @@ func TestUpdateCPUQUota(t *testing.T) {
105 105
 		{desc: "a lower value", update: 10000},
106 106
 		{desc: "unset value", update: -1},
107 107
 	} {
108
-		if _, err := client.ContainerUpdate(ctx, c.ID, container.UpdateConfig{
109
-			Resources: container.Resources{
108
+		if _, err := client.ContainerUpdate(ctx, cID, containertypes.UpdateConfig{
109
+			Resources: containertypes.Resources{
110 110
 				CPUQuota: test.update,
111 111
 			},
112 112
 		}); err != nil {
113 113
 			t.Fatal(err)
114 114
 		}
115 115
 
116
-		inspect, err := client.ContainerInspect(ctx, c.ID)
116
+		inspect, err := client.ContainerInspect(ctx, cID)
117 117
 		if err != nil {
118 118
 			t.Fatal(err)
119 119
 		}
... ...
@@ -122,7 +96,7 @@ func TestUpdateCPUQUota(t *testing.T) {
122 122
 			t.Fatalf("quota not updated in the API, expected %d, got: %d", test.update, inspect.HostConfig.CPUQuota)
123 123
 		}
124 124
 
125
-		execCreate, err := client.ContainerExecCreate(ctx, c.ID, types.ExecConfig{
125
+		execCreate, err := client.ContainerExecCreate(ctx, cID, types.ExecConfig{
126 126
 			Cmd:          []string{"/bin/cat", "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"},
127 127
 			AttachStdout: true,
128 128
 			AttachStderr: true,
... ...
@@ -5,9 +5,9 @@ import (
5 5
 	"testing"
6 6
 
7 7
 	"github.com/docker/docker/api/types"
8
-	"github.com/docker/docker/api/types/container"
9 8
 	"github.com/docker/docker/api/types/network"
10 9
 	"github.com/docker/docker/client"
10
+	"github.com/docker/docker/integration/internal/container"
11 11
 	"github.com/docker/docker/integration/internal/swarm"
12 12
 	"github.com/stretchr/testify/assert"
13 13
 	"github.com/stretchr/testify/require"
... ...
@@ -27,20 +27,15 @@ func TestDockerNetworkConnectAlias(t *testing.T) {
27 27
 		Attachable: true,
28 28
 	})
29 29
 	require.NoError(t, err)
30
-	_, err = client.ContainerCreate(ctx,
31
-		&container.Config{
32
-			Cmd:   []string{"top"},
33
-			Image: "busybox",
34
-		},
35
-		&container.HostConfig{},
36
-		&network.NetworkingConfig{
37
-			EndpointsConfig: map[string]*network.EndpointSettings{
30
+
31
+	container.Create(t, ctx, client, container.WithName("ng1"), func(c *container.TestContainerConfig) {
32
+		c.NetworkingConfig = &network.NetworkingConfig{
33
+			map[string]*network.EndpointSettings{
38 34
 				name: {},
39 35
 			},
40
-		},
41
-		"ng1",
42
-	)
43
-	require.NoError(t, err)
36
+		}
37
+	})
38
+
44 39
 	err = client.NetworkConnect(ctx, name, "ng1", &network.EndpointSettings{
45 40
 		Aliases: []string{
46 41
 			"aaa",
... ...
@@ -56,20 +51,14 @@ func TestDockerNetworkConnectAlias(t *testing.T) {
56 56
 	assert.Equal(t, len(ng1.NetworkSettings.Networks[name].Aliases), 2)
57 57
 	assert.Equal(t, ng1.NetworkSettings.Networks[name].Aliases[0], "aaa")
58 58
 
59
-	_, err = client.ContainerCreate(ctx,
60
-		&container.Config{
61
-			Cmd:   []string{"top"},
62
-			Image: "busybox",
63
-		},
64
-		&container.HostConfig{},
65
-		&network.NetworkingConfig{
66
-			EndpointsConfig: map[string]*network.EndpointSettings{
59
+	container.Create(t, ctx, client, container.WithName("ng2"), func(c *container.TestContainerConfig) {
60
+		c.NetworkingConfig = &network.NetworkingConfig{
61
+			map[string]*network.EndpointSettings{
67 62
 				name: {},
68 63
 			},
69
-		},
70
-		"ng2",
71
-	)
72
-	require.NoError(t, err)
64
+		}
65
+	})
66
+
73 67
 	err = client.NetworkConnect(ctx, name, "ng2", &network.EndpointSettings{
74 68
 		Aliases: []string{
75 69
 			"bbb",
... ...
@@ -7,10 +7,9 @@ import (
7 7
 	"time"
8 8
 
9 9
 	"github.com/docker/docker/api/types"
10
-	"github.com/docker/docker/api/types/container"
11 10
 	"github.com/docker/docker/api/types/filters"
12
-	"github.com/docker/docker/api/types/network"
13 11
 	"github.com/docker/docker/api/types/strslice"
12
+	"github.com/docker/docker/integration/internal/container"
14 13
 	"github.com/docker/docker/integration/internal/request"
15 14
 	"github.com/stretchr/testify/require"
16 15
 )
... ...
@@ -20,22 +19,9 @@ func TestEvents(t *testing.T) {
20 20
 	ctx := context.Background()
21 21
 	client := request.NewAPIClient(t)
22 22
 
23
-	container, err := client.ContainerCreate(ctx,
24
-		&container.Config{
25
-			Image:      "busybox",
26
-			Tty:        true,
27
-			WorkingDir: "/root",
28
-			Cmd:        strslice.StrSlice([]string{"top"}),
29
-		},
30
-		&container.HostConfig{},
31
-		&network.NetworkingConfig{},
32
-		"foo",
33
-	)
34
-	require.NoError(t, err)
35
-	err = client.ContainerStart(ctx, container.ID, types.ContainerStartOptions{})
36
-	require.NoError(t, err)
23
+	cID := container.Run(t, ctx, client)
37 24
 
38
-	id, err := client.ContainerExecCreate(ctx, container.ID,
25
+	id, err := client.ContainerExecCreate(ctx, cID,
39 26
 		types.ExecConfig{
40 27
 			Cmd: strslice.StrSlice([]string{"echo", "hello"}),
41 28
 		},
... ...
@@ -43,7 +29,7 @@ func TestEvents(t *testing.T) {
43 43
 	require.NoError(t, err)
44 44
 
45 45
 	filters := filters.NewArgs(
46
-		filters.Arg("container", container.ID),
46
+		filters.Arg("container", cID),
47 47
 		filters.Arg("event", "exec_die"),
48 48
 	)
49 49
 	msg, errors := client.Events(ctx, types.EventsOptions{
... ...
@@ -61,7 +47,7 @@ func TestEvents(t *testing.T) {
61 61
 	select {
62 62
 	case m := <-msg:
63 63
 		require.Equal(t, m.Type, "container")
64
-		require.Equal(t, m.Actor.ID, container.ID)
64
+		require.Equal(t, m.Actor.ID, cID)
65 65
 		require.Equal(t, m.Action, "exec_die")
66 66
 		require.Equal(t, m.Actor.Attributes["execID"], id.ID)
67 67
 		require.Equal(t, m.Actor.Attributes["exitCode"], "0")