Browse code

Add WithNetworkMode, WithExposedPorts, WithTty, WithWorkingDir to container helper functions

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

Yong Tang authored on 2018/02/14 00:45:40
Showing 5 changed files
... ...
@@ -17,10 +17,7 @@ func TestExec(t *testing.T) {
17 17
 	ctx := context.Background()
18 18
 	client := request.NewAPIClient(t)
19 19
 
20
-	cID := container.Run(t, ctx, client, func(c *container.TestContainerConfig) {
21
-		c.Config.Tty = true
22
-		c.Config.WorkingDir = "/root"
23
-	})
20
+	cID := container.Run(t, ctx, client, container.WithTty(true), container.WithWorkingDir("/root"))
24 21
 
25 22
 	id, err := client.ContainerExecCreate(ctx, cID,
26 23
 		types.ExecConfig{
... ...
@@ -20,9 +20,7 @@ func TestHealthCheckWorkdir(t *testing.T) {
20 20
 	ctx := context.Background()
21 21
 	client := request.NewAPIClient(t)
22 22
 
23
-	cID := container.Run(t, ctx, client, func(c *container.TestContainerConfig) {
24
-		c.Config.Tty = true
25
-		c.Config.WorkingDir = "/foo"
23
+	cID := container.Run(t, ctx, client, container.WithTty(true), container.WithWorkingDir("/foo"), func(c *container.TestContainerConfig) {
26 24
 		c.Config.Healthcheck = &containertypes.HealthConfig{
27 25
 			Test:     []string{"CMD-SHELL", "if [ \"$PWD\" = \"/foo\" ]; then exit 0; else exit 1; fi;"},
28 26
 			Interval: 50 * time.Millisecond,
... ...
@@ -28,9 +28,7 @@ func TestLinksEtcHostsContentMatch(t *testing.T) {
28 28
 	client := request.NewAPIClient(t)
29 29
 	ctx := context.Background()
30 30
 
31
-	cID := container.Run(t, ctx, client, container.WithCmd("cat", "/etc/hosts"), func(c *container.TestContainerConfig) {
32
-		c.HostConfig.NetworkMode = "host"
33
-	})
31
+	cID := container.Run(t, ctx, client, container.WithCmd("cat", "/etc/hosts"), container.WithNetworkMode("host"))
34 32
 
35 33
 	poll.WaitOn(t, containerIsStopped(ctx, client, cID), poll.WithDelay(100*time.Millisecond))
36 34
 
... ...
@@ -67,10 +67,7 @@ func TestNetworkLoopbackNat(t *testing.T) {
67 67
 	client := request.NewAPIClient(t)
68 68
 	ctx := context.Background()
69 69
 
70
-	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) {
71
-		c.Config.Tty = true
72
-		c.HostConfig.NetworkMode = "container:server"
73
-	})
70
+	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"))
74 71
 
75 72
 	poll.WaitOn(t, containerIsStopped(ctx, client, cID), poll.WithDelay(100*time.Millisecond))
76 73
 
... ...
@@ -91,10 +88,7 @@ func startServerContainer(t *testing.T, msg string, port int) string {
91 91
 	client := request.NewAPIClient(t)
92 92
 	ctx := context.Background()
93 93
 
94
-	cID := container.Run(t, ctx, client, container.WithCmd("sh", "-c", fmt.Sprintf("echo %q | nc -lp %d", msg, port)), func(c *container.TestContainerConfig) {
95
-		c.Config.ExposedPorts = map[nat.Port]struct{}{
96
-			nat.Port(fmt.Sprintf("%d/tcp", port)): {},
97
-		}
94
+	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) {
98 95
 		c.HostConfig.PortBindings = nat.PortMap{
99 96
 			nat.Port(fmt.Sprintf("%d/tcp", port)): []nat.PortBinding{
100 97
 				{
... ...
@@ -1,6 +1,10 @@
1 1
 package container
2 2
 
3
-import "github.com/docker/docker/api/types/strslice"
3
+import (
4
+	containertypes "github.com/docker/docker/api/types/container"
5
+	"github.com/docker/docker/api/types/strslice"
6
+	"github.com/docker/go-connections/nat"
7
+)
4 8
 
5 9
 // WithName sets the name of the container
6 10
 func WithName(name string) func(*TestContainerConfig) {
... ...
@@ -22,3 +26,34 @@ func WithCmd(cmds ...string) func(*TestContainerConfig) {
22 22
 		c.Config.Cmd = strslice.StrSlice(cmds)
23 23
 	}
24 24
 }
25
+
26
+// WithNetworkMode sets the network mode of the container
27
+func WithNetworkMode(mode string) func(*TestContainerConfig) {
28
+	return func(c *TestContainerConfig) {
29
+		c.HostConfig.NetworkMode = containertypes.NetworkMode(mode)
30
+	}
31
+}
32
+
33
+// WithExposedPorts sets the exposed ports of the container
34
+func WithExposedPorts(ports ...string) func(*TestContainerConfig) {
35
+	return func(c *TestContainerConfig) {
36
+		c.Config.ExposedPorts = map[nat.Port]struct{}{}
37
+		for _, port := range ports {
38
+			c.Config.ExposedPorts[nat.Port(port)] = struct{}{}
39
+		}
40
+	}
41
+}
42
+
43
+// WithTty sets the TTY mode of the container
44
+func WithTty(tty bool) func(*TestContainerConfig) {
45
+	return func(c *TestContainerConfig) {
46
+		c.Config.Tty = tty
47
+	}
48
+}
49
+
50
+// WithWorkingDir sets the working dir of the container
51
+func WithWorkingDir(dir string) func(*TestContainerConfig) {
52
+	return func(c *TestContainerConfig) {
53
+		c.Config.WorkingDir = dir
54
+	}
55
+}