Browse Source

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 7 năm trước cách đây
mục cha
commit
e9f19df6a9

+ 8 - 35
integration/container/stop_test.go

@@ -8,9 +8,8 @@ import (
 	"time"
 
 	"github.com/docker/docker/api/types"
-	"github.com/docker/docker/api/types/container"
-	"github.com/docker/docker/api/types/network"
 	"github.com/docker/docker/client"
+	"github.com/docker/docker/integration/internal/container"
 	"github.com/docker/docker/integration/internal/request"
 	"github.com/gotestyourself/gotestyourself/icmd"
 	"github.com/gotestyourself/gotestyourself/poll"
@@ -25,23 +24,9 @@ func TestStopContainerWithRestartPolicyAlways(t *testing.T) {
 
 	names := []string{"verifyRestart1", "verifyRestart2"}
 	for _, name := range names {
-		resp, err := client.ContainerCreate(ctx,
-			&container.Config{
-				Cmd:   []string{"false"},
-				Image: "busybox",
-			},
-			&container.HostConfig{
-				RestartPolicy: container.RestartPolicy{
-					Name: "always",
-				},
-			},
-			&network.NetworkingConfig{},
-			name,
-		)
-		require.NoError(t, err)
-
-		err = client.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
-		require.NoError(t, err)
+		container.Run(t, ctx, client, container.WithName(name), container.WithCmd("false"), func(c *container.TestContainerConfig) {
+			c.HostConfig.RestartPolicy.Name = "always"
+		})
 	}
 
 	for _, name := range names {
@@ -65,25 +50,13 @@ func TestDeleteDevicemapper(t *testing.T) {
 	client := request.NewAPIClient(t)
 	ctx := context.Background()
 
-	foo, err := client.ContainerCreate(ctx,
-		&container.Config{
-			Cmd:   []string{"echo"},
-			Image: "busybox",
-		},
-		&container.HostConfig{},
-		&network.NetworkingConfig{},
-		"foo",
-	)
-	require.NoError(t, err)
+	id := container.Run(t, ctx, client, container.WithName("foo"), container.WithCmd("echo"))
 
-	err = client.ContainerStart(ctx, foo.ID, types.ContainerStartOptions{})
-	require.NoError(t, err)
+	poll.WaitOn(t, containerIsStopped(ctx, client, id), poll.WithDelay(100*time.Millisecond))
 
-	inspect, err := client.ContainerInspect(ctx, foo.ID)
+	inspect, err := client.ContainerInspect(ctx, id)
 	require.NoError(t, err)
 
-	poll.WaitOn(t, containerIsStopped(ctx, client, foo.ID), poll.WithDelay(100*time.Millisecond))
-
 	deviceID := inspect.GraphDriver.Data["DeviceId"]
 
 	// Find pool name from device name
@@ -94,7 +67,7 @@ func TestDeleteDevicemapper(t *testing.T) {
 	result := icmd.RunCommand("dmsetup", "message", devicePool, "0", fmt.Sprintf("delete %s", deviceID))
 	result.Assert(t, icmd.Success)
 
-	err = client.ContainerRemove(ctx, foo.ID, types.ContainerRemoveOptions{})
+	err = client.ContainerRemove(ctx, id, types.ContainerRemoveOptions{})
 	require.NoError(t, err)
 }
 

+ 19 - 45
integration/container/update_linux_test.go

@@ -3,7 +3,6 @@ package container // import "github.com/docker/docker/integration/container"
 import (
 	"bytes"
 	"context"
-	"fmt"
 	"io/ioutil"
 	"strconv"
 	"strings"
@@ -11,9 +10,10 @@ import (
 	"time"
 
 	"github.com/docker/docker/api/types"
-	"github.com/docker/docker/api/types/container"
+	containertypes "github.com/docker/docker/api/types/container"
 	"github.com/docker/docker/api/types/strslice"
 	"github.com/docker/docker/client"
+	"github.com/docker/docker/integration/internal/container"
 	"github.com/docker/docker/integration/internal/request"
 	"github.com/docker/docker/pkg/stdcopy"
 	"github.com/gotestyourself/gotestyourself/poll"
@@ -31,44 +31,32 @@ func TestUpdateMemory(t *testing.T) {
 	client := request.NewAPIClient(t)
 	ctx := context.Background()
 
-	c, err := client.ContainerCreate(ctx,
-		&container.Config{
-			Cmd:   []string{"top"},
-			Image: "busybox",
-		},
-		&container.HostConfig{
-			Resources: container.Resources{
-				Memory: 200 * 1024 * 1024,
-			},
-		},
-		nil,
-		"",
-	)
-	require.NoError(t, err)
-
-	err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
-	require.NoError(t, err)
+	cID := container.Run(t, ctx, client, func(c *container.TestContainerConfig) {
+		c.HostConfig.Resources = containertypes.Resources{
+			Memory: 200 * 1024 * 1024,
+		}
+	})
 
-	poll.WaitOn(t, containerIsInState(ctx, client, c.ID, "running"), poll.WithDelay(100*time.Millisecond))
+	poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
 
-	_, err = client.ContainerUpdate(ctx, c.ID, container.UpdateConfig{
-		Resources: container.Resources{
+	_, err := client.ContainerUpdate(ctx, cID, containertypes.UpdateConfig{
+		Resources: containertypes.Resources{
 			Memory:     314572800,
 			MemorySwap: 524288000,
 		},
 	})
 	require.NoError(t, err)
 
-	inspect, err := client.ContainerInspect(ctx, c.ID)
+	inspect, err := client.ContainerInspect(ctx, cID)
 	require.NoError(t, err)
 	assert.Equal(t, inspect.HostConfig.Memory, int64(314572800))
 	assert.Equal(t, inspect.HostConfig.MemorySwap, int64(524288000))
 
-	body, err := getContainerSysFSValue(ctx, client, c.ID, "/sys/fs/cgroup/memory/memory.limit_in_bytes")
+	body, err := getContainerSysFSValue(ctx, client, cID, "/sys/fs/cgroup/memory/memory.limit_in_bytes")
 	require.NoError(t, err)
 	assert.Equal(t, strings.TrimSpace(body), "314572800")
 
-	body, err = getContainerSysFSValue(ctx, client, c.ID, "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes")
+	body, err = getContainerSysFSValue(ctx, client, cID, "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes")
 	require.NoError(t, err)
 	assert.Equal(t, strings.TrimSpace(body), "524288000")
 }
@@ -76,25 +64,11 @@ func TestUpdateMemory(t *testing.T) {
 func TestUpdateCPUQUota(t *testing.T) {
 	t.Parallel()
 
+	defer setupTest(t)()
 	client := request.NewAPIClient(t)
 	ctx := context.Background()
 
-	c, err := client.ContainerCreate(ctx, &container.Config{
-		Image: "busybox",
-		Cmd:   []string{"top"},
-	}, nil, nil, "")
-	if err != nil {
-		t.Fatal(err)
-	}
-	defer func() {
-		if err := client.ContainerRemove(ctx, c.ID, types.ContainerRemoveOptions{Force: true}); err != nil {
-			panic(fmt.Sprintf("failed to clean up after test: %v", err))
-		}
-	}()
-
-	if err := client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{}); err != nil {
-		t.Fatal(err)
-	}
+	cID := container.Run(t, ctx, client)
 
 	for _, test := range []struct {
 		desc   string
@@ -105,15 +79,15 @@ func TestUpdateCPUQUota(t *testing.T) {
 		{desc: "a lower value", update: 10000},
 		{desc: "unset value", update: -1},
 	} {
-		if _, err := client.ContainerUpdate(ctx, c.ID, container.UpdateConfig{
-			Resources: container.Resources{
+		if _, err := client.ContainerUpdate(ctx, cID, containertypes.UpdateConfig{
+			Resources: containertypes.Resources{
 				CPUQuota: test.update,
 			},
 		}); err != nil {
 			t.Fatal(err)
 		}
 
-		inspect, err := client.ContainerInspect(ctx, c.ID)
+		inspect, err := client.ContainerInspect(ctx, cID)
 		if err != nil {
 			t.Fatal(err)
 		}
@@ -122,7 +96,7 @@ func TestUpdateCPUQUota(t *testing.T) {
 			t.Fatalf("quota not updated in the API, expected %d, got: %d", test.update, inspect.HostConfig.CPUQuota)
 		}
 
-		execCreate, err := client.ContainerExecCreate(ctx, c.ID, types.ExecConfig{
+		execCreate, err := client.ContainerExecCreate(ctx, cID, types.ExecConfig{
 			Cmd:          []string{"/bin/cat", "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"},
 			AttachStdout: true,
 			AttachStderr: true,

+ 14 - 25
integration/service/network_test.go

@@ -5,9 +5,9 @@ import (
 	"testing"
 
 	"github.com/docker/docker/api/types"
-	"github.com/docker/docker/api/types/container"
 	"github.com/docker/docker/api/types/network"
 	"github.com/docker/docker/client"
+	"github.com/docker/docker/integration/internal/container"
 	"github.com/docker/docker/integration/internal/swarm"
 	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/require"
@@ -27,20 +27,15 @@ func TestDockerNetworkConnectAlias(t *testing.T) {
 		Attachable: true,
 	})
 	require.NoError(t, err)
-	_, err = client.ContainerCreate(ctx,
-		&container.Config{
-			Cmd:   []string{"top"},
-			Image: "busybox",
-		},
-		&container.HostConfig{},
-		&network.NetworkingConfig{
-			EndpointsConfig: map[string]*network.EndpointSettings{
+
+	container.Create(t, ctx, client, container.WithName("ng1"), func(c *container.TestContainerConfig) {
+		c.NetworkingConfig = &network.NetworkingConfig{
+			map[string]*network.EndpointSettings{
 				name: {},
 			},
-		},
-		"ng1",
-	)
-	require.NoError(t, err)
+		}
+	})
+
 	err = client.NetworkConnect(ctx, name, "ng1", &network.EndpointSettings{
 		Aliases: []string{
 			"aaa",
@@ -56,20 +51,14 @@ func TestDockerNetworkConnectAlias(t *testing.T) {
 	assert.Equal(t, len(ng1.NetworkSettings.Networks[name].Aliases), 2)
 	assert.Equal(t, ng1.NetworkSettings.Networks[name].Aliases[0], "aaa")
 
-	_, err = client.ContainerCreate(ctx,
-		&container.Config{
-			Cmd:   []string{"top"},
-			Image: "busybox",
-		},
-		&container.HostConfig{},
-		&network.NetworkingConfig{
-			EndpointsConfig: map[string]*network.EndpointSettings{
+	container.Create(t, ctx, client, container.WithName("ng2"), func(c *container.TestContainerConfig) {
+		c.NetworkingConfig = &network.NetworkingConfig{
+			map[string]*network.EndpointSettings{
 				name: {},
 			},
-		},
-		"ng2",
-	)
-	require.NoError(t, err)
+		}
+	})
+
 	err = client.NetworkConnect(ctx, name, "ng2", &network.EndpointSettings{
 		Aliases: []string{
 			"bbb",

+ 5 - 19
integration/system/event_test.go

@@ -7,10 +7,9 @@ import (
 	"time"
 
 	"github.com/docker/docker/api/types"
-	"github.com/docker/docker/api/types/container"
 	"github.com/docker/docker/api/types/filters"
-	"github.com/docker/docker/api/types/network"
 	"github.com/docker/docker/api/types/strslice"
+	"github.com/docker/docker/integration/internal/container"
 	"github.com/docker/docker/integration/internal/request"
 	"github.com/stretchr/testify/require"
 )
@@ -20,22 +19,9 @@ func TestEvents(t *testing.T) {
 	ctx := context.Background()
 	client := request.NewAPIClient(t)
 
-	container, err := client.ContainerCreate(ctx,
-		&container.Config{
-			Image:      "busybox",
-			Tty:        true,
-			WorkingDir: "/root",
-			Cmd:        strslice.StrSlice([]string{"top"}),
-		},
-		&container.HostConfig{},
-		&network.NetworkingConfig{},
-		"foo",
-	)
-	require.NoError(t, err)
-	err = client.ContainerStart(ctx, container.ID, types.ContainerStartOptions{})
-	require.NoError(t, err)
+	cID := container.Run(t, ctx, client)
 
-	id, err := client.ContainerExecCreate(ctx, container.ID,
+	id, err := client.ContainerExecCreate(ctx, cID,
 		types.ExecConfig{
 			Cmd: strslice.StrSlice([]string{"echo", "hello"}),
 		},
@@ -43,7 +29,7 @@ func TestEvents(t *testing.T) {
 	require.NoError(t, err)
 
 	filters := filters.NewArgs(
-		filters.Arg("container", container.ID),
+		filters.Arg("container", cID),
 		filters.Arg("event", "exec_die"),
 	)
 	msg, errors := client.Events(ctx, types.EventsOptions{
@@ -61,7 +47,7 @@ func TestEvents(t *testing.T) {
 	select {
 	case m := <-msg:
 		require.Equal(t, m.Type, "container")
-		require.Equal(t, m.Actor.ID, container.ID)
+		require.Equal(t, m.Actor.ID, cID)
 		require.Equal(t, m.Action, "exec_die")
 		require.Equal(t, m.Actor.Attributes["execID"], id.ID)
 		require.Equal(t, m.Actor.Attributes["exitCode"], "0")