From 9fcd2a05106af98e6ffd6efb9f124d64426956e4 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Mon, 12 Feb 2018 23:08:25 +0000 Subject: [PATCH] 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 --- integration/container/exec_test.go | 23 ++----- integration/container/health_test.go | 36 ++++------- integration/container/links_linux_test.go | 23 ++----- integration/container/nat_test.go | 61 ++++++------------- integration/container/stats_test.go | 21 ++----- integration/plugin/authz/authz_plugin_test.go | 39 +++++------- .../plugin/authz/authz_plugin_v2_test.go | 13 ++-- 7 files changed, 68 insertions(+), 148 deletions(-) diff --git a/integration/container/exec_test.go b/integration/container/exec_test.go index a14284806d..9c27524696 100644 --- a/integration/container/exec_test.go +++ b/integration/container/exec_test.go @@ -6,9 +6,8 @@ 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/api/types/strslice" + "github.com/docker/docker/integration/internal/container" "github.com/docker/docker/integration/internal/request" "github.com/stretchr/testify/require" ) @@ -18,22 +17,12 @@ func TestExec(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, func(c *container.TestContainerConfig) { + c.Config.Tty = true + c.Config.WorkingDir = "/root" + }) - id, err := client.ContainerExecCreate(ctx, container.ID, + id, err := client.ContainerExecCreate(ctx, cID, types.ExecConfig{ WorkingDir: "/tmp", Env: strslice.StrSlice([]string{"FOO=BAR"}), diff --git a/integration/container/health_test.go b/integration/container/health_test.go index a5c62edb57..ec9604c901 100644 --- a/integration/container/health_test.go +++ b/integration/container/health_test.go @@ -6,13 +6,11 @@ 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/api/types/strslice" + containertypes "github.com/docker/docker/api/types/container" "github.com/docker/docker/client" + "github.com/docker/docker/integration/internal/container" "github.com/docker/docker/integration/internal/request" "github.com/gotestyourself/gotestyourself/poll" - "github.com/stretchr/testify/require" ) // TestHealthCheckWorkdir verifies that health-checks inherit the containers' @@ -22,27 +20,17 @@ func TestHealthCheckWorkdir(t *testing.T) { ctx := context.Background() client := request.NewAPIClient(t) - c, err := client.ContainerCreate(ctx, - &container.Config{ - Image: "busybox", - Tty: true, - WorkingDir: "/foo", - Cmd: strslice.StrSlice([]string{"top"}), - Healthcheck: &container.HealthConfig{ - Test: []string{"CMD-SHELL", "if [ \"$PWD\" = \"/foo\" ]; then exit 0; else exit 1; fi;"}, - Interval: 50 * time.Millisecond, - Retries: 3, - }, - }, - &container.HostConfig{}, - &network.NetworkingConfig{}, - "healthtest", - ) - 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.Config.Tty = true + c.Config.WorkingDir = "/foo" + c.Config.Healthcheck = &containertypes.HealthConfig{ + Test: []string{"CMD-SHELL", "if [ \"$PWD\" = \"/foo\" ]; then exit 0; else exit 1; fi;"}, + Interval: 50 * time.Millisecond, + Retries: 3, + } + }) - poll.WaitOn(t, pollForHealthStatus(ctx, client, c.ID, types.Healthy), poll.WithDelay(100*time.Millisecond)) + poll.WaitOn(t, pollForHealthStatus(ctx, client, cID, types.Healthy), poll.WithDelay(100*time.Millisecond)) } func pollForHealthStatus(ctx context.Context, client client.APIClient, containerID string, healthStatus string) func(log poll.LogT) poll.Result { diff --git a/integration/container/links_linux_test.go b/integration/container/links_linux_test.go index b1dc654c20..550b02d874 100644 --- a/integration/container/links_linux_test.go +++ b/integration/container/links_linux_test.go @@ -9,7 +9,7 @@ import ( "time" "github.com/docker/docker/api/types" - "github.com/docker/docker/api/types/container" + "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" @@ -28,24 +28,13 @@ func TestLinksEtcHostsContentMatch(t *testing.T) { client := request.NewAPIClient(t) ctx := context.Background() - c, err := client.ContainerCreate(ctx, - &container.Config{ - Image: "busybox", - Cmd: []string{"cat", "/etc/hosts"}, - }, - &container.HostConfig{ - NetworkMode: "host", - }, - nil, - "") - require.NoError(t, err) + cID := container.Run(t, ctx, client, container.WithCmd("cat", "/etc/hosts"), func(c *container.TestContainerConfig) { + c.HostConfig.NetworkMode = "host" + }) - err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{}) - require.NoError(t, err) + poll.WaitOn(t, containerIsStopped(ctx, client, cID), poll.WithDelay(100*time.Millisecond)) - poll.WaitOn(t, containerIsStopped(ctx, client, c.ID), poll.WithDelay(100*time.Millisecond)) - - body, err := client.ContainerLogs(ctx, c.ID, types.ContainerLogsOptions{ + body, err := client.ContainerLogs(ctx, cID, types.ContainerLogsOptions{ ShowStdout: true, }) require.NoError(t, err) diff --git a/integration/container/nat_test.go b/integration/container/nat_test.go index 0732e2d852..1b41ae9608 100644 --- a/integration/container/nat_test.go +++ b/integration/container/nat_test.go @@ -12,8 +12,7 @@ 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/integration/internal/container" "github.com/docker/docker/integration/internal/request" "github.com/docker/go-connections/nat" "github.com/gotestyourself/gotestyourself/poll" @@ -67,25 +66,15 @@ func TestNetworkLoopbackNat(t *testing.T) { client := request.NewAPIClient(t) ctx := context.Background() - c, err := client.ContainerCreate(ctx, - &container.Config{ - Image: "busybox", - Cmd: []string{"sh", "-c", fmt.Sprintf("stty raw && nc -w 5 %s 8080", endpoint.String())}, - Tty: true, - }, - &container.HostConfig{ - NetworkMode: "container:server", - }, - nil, - "") - require.NoError(t, err) - err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{}) - require.NoError(t, err) + 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) { + c.Config.Tty = true + c.HostConfig.NetworkMode = "container:server" + }) - poll.WaitOn(t, containerIsStopped(ctx, client, c.ID), poll.WithDelay(100*time.Millisecond)) + poll.WaitOn(t, containerIsStopped(ctx, client, cID), poll.WithDelay(100*time.Millisecond)) - body, err := client.ContainerLogs(ctx, c.ID, types.ContainerLogsOptions{ + body, err := client.ContainerLogs(ctx, cID, types.ContainerLogsOptions{ ShowStdout: true, }) require.NoError(t, err) @@ -102,34 +91,22 @@ func startServerContainer(t *testing.T, msg string, port int) string { client := request.NewAPIClient(t) ctx := context.Background() - c, err := client.ContainerCreate(ctx, - &container.Config{ - Image: "busybox", - Cmd: []string{"sh", "-c", fmt.Sprintf("echo %q | nc -lp %d", msg, port)}, - ExposedPorts: map[nat.Port]struct{}{ - nat.Port(fmt.Sprintf("%d/tcp", port)): {}, - }, - }, - &container.HostConfig{ - PortBindings: nat.PortMap{ - nat.Port(fmt.Sprintf("%d/tcp", port)): []nat.PortBinding{ - { - HostPort: fmt.Sprintf("%d", port), - }, + cID := container.Run(t, ctx, client, container.WithCmd("sh", "-c", fmt.Sprintf("echo %q | nc -lp %d", msg, port)), func(c *container.TestContainerConfig) { + c.Config.ExposedPorts = map[nat.Port]struct{}{ + nat.Port(fmt.Sprintf("%d/tcp", port)): {}, + } + c.HostConfig.PortBindings = nat.PortMap{ + nat.Port(fmt.Sprintf("%d/tcp", port)): []nat.PortBinding{ + { + HostPort: fmt.Sprintf("%d", port), }, }, - }, - &network.NetworkingConfig{}, - "server", - ) - require.NoError(t, err) + } + }) - err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{}) - require.NoError(t, err) + poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond)) - poll.WaitOn(t, containerIsInState(ctx, client, c.ID, "running"), poll.WithDelay(100*time.Millisecond)) - - return c.ID + return cID } func getExternalAddress(t *testing.T) net.IP { diff --git a/integration/container/stats_test.go b/integration/container/stats_test.go index 577d446d15..fdf85f44f6 100644 --- a/integration/container/stats_test.go +++ b/integration/container/stats_test.go @@ -8,8 +8,7 @@ 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/integration/internal/container" "github.com/docker/docker/integration/internal/request" "github.com/gotestyourself/gotestyourself/poll" "github.com/gotestyourself/gotestyourself/skip" @@ -27,23 +26,11 @@ func TestStats(t *testing.T) { info, err := client.Info(ctx) require.NoError(t, err) - c, err := client.ContainerCreate(ctx, - &container.Config{ - Cmd: []string{"top"}, - Image: "busybox", - }, - &container.HostConfig{}, - &network.NetworkingConfig{}, - "", - ) - require.NoError(t, err) + cID := container.Run(t, ctx, client) - err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{}) - require.NoError(t, err) + poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond)) - poll.WaitOn(t, containerIsInState(ctx, client, c.ID, "running"), poll.WithDelay(100*time.Millisecond)) - - resp, err := client.ContainerStats(context.Background(), c.ID, false) + resp, err := client.ContainerStats(ctx, cID, false) require.NoError(t, err) defer resp.Body.Close() diff --git a/integration/plugin/authz/authz_plugin_test.go b/integration/plugin/authz/authz_plugin_test.go index befebe4080..94f7b896a7 100644 --- a/integration/plugin/authz/authz_plugin_test.go +++ b/integration/plugin/authz/authz_plugin_test.go @@ -19,10 +19,9 @@ import ( "time" "github.com/docker/docker/api/types" - "github.com/docker/docker/api/types/container" eventtypes "github.com/docker/docker/api/types/events" - networktypes "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/docker/docker/internal/test/environment" "github.com/docker/docker/pkg/authorization" @@ -91,17 +90,15 @@ func TestAuthZPluginAllowRequest(t *testing.T) { client, err := d.NewClient() require.Nil(t, err) - // Ensure command successful - createResponse, err := client.ContainerCreate(context.Background(), &container.Config{Cmd: []string{"top"}, Image: "busybox"}, &container.HostConfig{}, &networktypes.NetworkingConfig{}, "") - require.Nil(t, err) + ctx := context.Background() - err = client.ContainerStart(context.Background(), createResponse.ID, types.ContainerStartOptions{}) - require.Nil(t, err) + // Ensure command successful + cID := container.Run(t, ctx, client) assertURIRecorded(t, ctrl.requestsURIs, "/containers/create") - assertURIRecorded(t, ctrl.requestsURIs, fmt.Sprintf("/containers/%s/start", createResponse.ID)) + assertURIRecorded(t, ctrl.requestsURIs, fmt.Sprintf("/containers/%s/start", cID)) - _, err = client.ServerVersion(context.Background()) + _, err = client.ServerVersion(ctx) require.Nil(t, err) require.Equal(t, 1, ctrl.versionReqCount) require.Equal(t, 1, ctrl.versionResCount) @@ -213,19 +210,17 @@ func TestAuthZPluginAllowEventStream(t *testing.T) { client, err := d.NewClient() require.Nil(t, err) + ctx := context.Background() + startTime := strconv.FormatInt(systemTime(t, client, testEnv).Unix(), 10) events, errs, cancel := systemEventsSince(client, startTime) defer cancel() // Create a container and wait for the creation events - createResponse, err := client.ContainerCreate(context.Background(), &container.Config{Cmd: []string{"top"}, Image: "busybox"}, &container.HostConfig{}, &networktypes.NetworkingConfig{}, "") - require.Nil(t, err) - - err = client.ContainerStart(context.Background(), createResponse.ID, types.ContainerStartOptions{}) - require.Nil(t, err) + cID := container.Run(t, ctx, client) for i := 0; i < 100; i++ { - c, err := client.ContainerInspect(context.Background(), createResponse.ID) + c, err := client.ContainerInspect(ctx, cID) require.Nil(t, err) if c.State.Running { break @@ -241,7 +236,7 @@ func TestAuthZPluginAllowEventStream(t *testing.T) { for !created && !started { select { case event := <-events: - if event.Type == eventtypes.ContainerEventType && event.Actor.ID == createResponse.ID { + if event.Type == eventtypes.ContainerEventType && event.Actor.ID == cID { if event.Action == "create" { created = true } @@ -264,7 +259,7 @@ func TestAuthZPluginAllowEventStream(t *testing.T) { // authorization plugin assertURIRecorded(t, ctrl.requestsURIs, "/events") assertURIRecorded(t, ctrl.requestsURIs, "/containers/create") - assertURIRecorded(t, ctrl.requestsURIs, fmt.Sprintf("/containers/%s/start", createResponse.ID)) + assertURIRecorded(t, ctrl.requestsURIs, fmt.Sprintf("/containers/%s/start", cID)) } func systemTime(t *testing.T, client client.APIClient, testEnv *environment.Execution) time.Time { @@ -347,6 +342,8 @@ func TestAuthZPluginEnsureLoadImportWorking(t *testing.T) { client, err := d.NewClient() require.Nil(t, err) + ctx := context.Background() + tmp, err := ioutil.TempDir("", "test-authz-load-import") require.Nil(t, err) defer os.RemoveAll(tmp) @@ -360,13 +357,9 @@ func TestAuthZPluginEnsureLoadImportWorking(t *testing.T) { exportedImagePath := filepath.Join(tmp, "export.tar") - createResponse, err := client.ContainerCreate(context.Background(), &container.Config{Cmd: []string{}, Image: "busybox"}, &container.HostConfig{}, &networktypes.NetworkingConfig{}, "") - require.Nil(t, err) + cID := container.Run(t, ctx, client) - err = client.ContainerStart(context.Background(), createResponse.ID, types.ContainerStartOptions{}) - require.Nil(t, err) - - responseReader, err := client.ContainerExport(context.Background(), createResponse.ID) + responseReader, err := client.ContainerExport(context.Background(), cID) require.Nil(t, err) defer responseReader.Close() file, err := os.Create(exportedImagePath) diff --git a/integration/plugin/authz/authz_plugin_v2_test.go b/integration/plugin/authz/authz_plugin_v2_test.go index 3f07f48f01..5efa421e88 100644 --- a/integration/plugin/authz/authz_plugin_v2_test.go +++ b/integration/plugin/authz/authz_plugin_v2_test.go @@ -11,11 +11,10 @@ import ( "testing" "github.com/docker/docker/api/types" - "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/filters" - networktypes "github.com/docker/docker/api/types/network" volumetypes "github.com/docker/docker/api/types/volume" "github.com/docker/docker/client" + "github.com/docker/docker/integration/internal/container" "github.com/docker/docker/integration/internal/requirement" "github.com/gotestyourself/gotestyourself/skip" "github.com/stretchr/testify/require" @@ -47,6 +46,8 @@ func TestAuthZPluginV2AllowNonVolumeRequest(t *testing.T) { client, err := d.NewClient() require.Nil(t, err) + ctx := context.Background() + // Install authz plugin err = pluginInstallGrantAllPermissions(client, authzPluginNameWithTag) require.Nil(t, err) @@ -56,13 +57,9 @@ func TestAuthZPluginV2AllowNonVolumeRequest(t *testing.T) { d.LoadBusybox(t) // Ensure docker run command and accompanying docker ps are successful - createResponse, err := client.ContainerCreate(context.Background(), &container.Config{Cmd: []string{"top"}, Image: "busybox"}, &container.HostConfig{}, &networktypes.NetworkingConfig{}, "") - require.Nil(t, err) + cID := container.Run(t, ctx, client) - err = client.ContainerStart(context.Background(), createResponse.ID, types.ContainerStartOptions{}) - require.Nil(t, err) - - _, err = client.ContainerInspect(context.Background(), createResponse.ID) + _, err = client.ContainerInspect(ctx, cID) require.Nil(t, err) }