Skip to content

Commit

Permalink
Merge pull request moby#36294 from yongtang/02122018-update
Browse files Browse the repository at this point in the history
Update api tests to use container.Run/Create in helper package
  • Loading branch information
dnephin authored Feb 13, 2018
2 parents 82d1aed + eaa1a0c commit bbd9b7f
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 149 deletions.
20 changes: 3 additions & 17 deletions integration/container/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -18,22 +17,9 @@ 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, container.WithTty(true), container.WithWorkingDir("/root"))

id, err := client.ContainerExecCreate(ctx, container.ID,
id, err := client.ContainerExecCreate(ctx, cID,
types.ExecConfig{
WorkingDir: "/tmp",
Env: strslice.StrSlice([]string{"FOO=BAR"}),
Expand Down
34 changes: 10 additions & 24 deletions integration/container/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -22,27 +20,15 @@ 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, container.WithTty(true), container.WithWorkingDir("/foo"), func(c *container.TestContainerConfig) {
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 {
Expand Down
21 changes: 4 additions & 17 deletions integration/container/links_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -28,24 +28,11 @@ 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)

err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
require.NoError(t, err)
cID := container.Run(t, ctx, client, container.WithCmd("cat", "/etc/hosts"), container.WithNetworkMode("host"))

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)
Expand Down
55 changes: 13 additions & 42 deletions integration/container/nat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -67,25 +66,12 @@ 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())), container.WithTty(true), container.WithNetworkMode("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)
Expand All @@ -102,34 +88,19 @@ 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.WithName("server"), container.WithCmd("sh", "-c", fmt.Sprintf("echo %q | nc -lp %d", msg, port)), container.WithExposedPorts(fmt.Sprintf("%d/tcp", port)), func(c *container.TestContainerConfig) {
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, c.ID, "running"), poll.WithDelay(100*time.Millisecond))
poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))

return c.ID
return cID
}

func getExternalAddress(t *testing.T) net.IP {
Expand Down
21 changes: 4 additions & 17 deletions integration/container/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)

err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
require.NoError(t, err)
cID := container.Run(t, ctx, client)

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))

resp, err := client.ContainerStats(context.Background(), c.ID, false)
resp, err := client.ContainerStats(ctx, cID, false)
require.NoError(t, err)
defer resp.Body.Close()

Expand Down
37 changes: 36 additions & 1 deletion integration/internal/container/ops.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package container

import "github.com/docker/docker/api/types/strslice"
import (
containertypes "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/strslice"
"github.com/docker/go-connections/nat"
)

// WithName sets the name of the container
func WithName(name string) func(*TestContainerConfig) {
Expand All @@ -22,3 +26,34 @@ func WithCmd(cmds ...string) func(*TestContainerConfig) {
c.Config.Cmd = strslice.StrSlice(cmds)
}
}

// WithNetworkMode sets the network mode of the container
func WithNetworkMode(mode string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.HostConfig.NetworkMode = containertypes.NetworkMode(mode)
}
}

// WithExposedPorts sets the exposed ports of the container
func WithExposedPorts(ports ...string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.Config.ExposedPorts = map[nat.Port]struct{}{}
for _, port := range ports {
c.Config.ExposedPorts[nat.Port(port)] = struct{}{}
}
}
}

// WithTty sets the TTY mode of the container
func WithTty(tty bool) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.Config.Tty = tty
}
}

// WithWorkingDir sets the working dir of the container
func WithWorkingDir(dir string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.Config.WorkingDir = dir
}
}
39 changes: 16 additions & 23 deletions integration/plugin/authz/authz_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)

err = client.ContainerStart(context.Background(), createResponse.ID, types.ContainerStartOptions{})
require.Nil(t, err)
cID := container.Run(t, ctx, client)

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)
Expand Down
Loading

0 comments on commit bbd9b7f

Please sign in to comment.