Browse code

Migrate some ipc container test from integration-cli to integration

This fix migrates some ipc container tests from integration-cli
to integration test.

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

Yong Tang authored on 2018/07/27 16:18:12
Showing 2 changed files
... ...
@@ -4,7 +4,6 @@ package main
4 4
 import (
5 5
 	"bufio"
6 6
 	"context"
7
-	"fmt"
8 7
 	"io/ioutil"
9 8
 	"os"
10 9
 	"strings"
... ...
@@ -46,71 +45,6 @@ func testIpcCheckDevExists(mm string) (bool, error) {
46 46
 	return false, s.Err()
47 47
 }
48 48
 
49
-// testIpcContainer is a helper function to test --ipc container:NNN mode in various scenarios
50
-func testIpcContainer(s *DockerSuite, c *check.C, donorMode string, mustWork bool) {
51
-	cfg := container.Config{
52
-		Image: "busybox",
53
-		Cmd:   []string{"top"},
54
-	}
55
-	hostCfg := container.HostConfig{
56
-		IpcMode: container.IpcMode(donorMode),
57
-	}
58
-	ctx := context.Background()
59
-
60
-	client := testEnv.APIClient()
61
-
62
-	// create and start the "donor" container
63
-	resp, err := client.ContainerCreate(ctx, &cfg, &hostCfg, nil, "")
64
-	c.Assert(err, checker.IsNil)
65
-	c.Assert(len(resp.Warnings), checker.Equals, 0)
66
-	name1 := resp.ID
67
-
68
-	err = client.ContainerStart(ctx, name1, types.ContainerStartOptions{})
69
-	c.Assert(err, checker.IsNil)
70
-
71
-	// create and start the second container
72
-	hostCfg.IpcMode = container.IpcMode("container:" + name1)
73
-	resp, err = client.ContainerCreate(ctx, &cfg, &hostCfg, nil, "")
74
-	c.Assert(err, checker.IsNil)
75
-	c.Assert(len(resp.Warnings), checker.Equals, 0)
76
-	name2 := resp.ID
77
-
78
-	err = client.ContainerStart(ctx, name2, types.ContainerStartOptions{})
79
-	if !mustWork {
80
-		// start should fail with a specific error
81
-		c.Assert(err, checker.NotNil)
82
-		c.Assert(fmt.Sprintf("%v", err), checker.Contains, "non-shareable IPC")
83
-		// no more checks to perform here
84
-		return
85
-	}
86
-
87
-	// start should succeed
88
-	c.Assert(err, checker.IsNil)
89
-
90
-	// check that IPC is shared
91
-	// 1. create a file in the first container
92
-	cli.DockerCmd(c, "exec", name1, "sh", "-c", "printf covfefe > /dev/shm/bar")
93
-	// 2. check it's the same file in the second one
94
-	out := cli.DockerCmd(c, "exec", "-i", name2, "cat", "/dev/shm/bar").Combined()
95
-	c.Assert(out, checker.Matches, "^covfefe$")
96
-}
97
-
98
-/* TestAPIIpcModeShareableAndContainer checks that a container created with
99
- * --ipc container:ID can use IPC of another shareable container.
100
- */
101
-func (s *DockerSuite) TestAPIIpcModeShareableAndContainer(c *check.C) {
102
-	testRequires(c, DaemonIsLinux)
103
-	testIpcContainer(s, c, "shareable", true)
104
-}
105
-
106
-/* TestAPIIpcModePrivateAndContainer checks that a container created with
107
- * --ipc container:ID can NOT use IPC of another private container.
108
- */
109
-func (s *DockerSuite) TestAPIIpcModePrivateAndContainer(c *check.C) {
110
-	testRequires(c, DaemonIsLinux, MinimumAPIVersion("1.32"))
111
-	testIpcContainer(s, c, "private", false)
112
-}
113
-
114 49
 /* TestAPIIpcModeHost checks that a container created with --ipc host
115 50
  * can use IPC of the host system.
116 51
  */
... ...
@@ -114,3 +114,68 @@ func TestIpcModeShareable(t *testing.T) {
114 114
 
115 115
 	testIpcNonePrivateShareable(t, "shareable", true, true)
116 116
 }
117
+
118
+// testIpcContainer is a helper function to test --ipc container:NNN mode in various scenarios
119
+func testIpcContainer(t *testing.T, donorMode string, mustWork bool) {
120
+	t.Helper()
121
+
122
+	defer setupTest(t)()
123
+
124
+	cfg := containertypes.Config{
125
+		Image: "busybox",
126
+		Cmd:   []string{"top"},
127
+	}
128
+	hostCfg := containertypes.HostConfig{
129
+		IpcMode: containertypes.IpcMode(donorMode),
130
+	}
131
+	ctx := context.Background()
132
+	client := request.NewAPIClient(t)
133
+
134
+	// create and start the "donor" container
135
+	resp, err := client.ContainerCreate(ctx, &cfg, &hostCfg, nil, "")
136
+	assert.NilError(t, err)
137
+	assert.Check(t, is.Equal(len(resp.Warnings), 0))
138
+	name1 := resp.ID
139
+
140
+	err = client.ContainerStart(ctx, name1, types.ContainerStartOptions{})
141
+	assert.NilError(t, err)
142
+
143
+	// create and start the second container
144
+	hostCfg.IpcMode = containertypes.IpcMode("container:" + name1)
145
+	resp, err = client.ContainerCreate(ctx, &cfg, &hostCfg, nil, "")
146
+	assert.NilError(t, err)
147
+	assert.Check(t, is.Equal(len(resp.Warnings), 0))
148
+	name2 := resp.ID
149
+
150
+	err = client.ContainerStart(ctx, name2, types.ContainerStartOptions{})
151
+	if !mustWork {
152
+		// start should fail with a specific error
153
+		assert.Check(t, is.ErrorContains(err, "non-shareable IPC"))
154
+		// no more checks to perform here
155
+		return
156
+	}
157
+
158
+	// start should succeed
159
+	assert.NilError(t, err)
160
+
161
+	// check that IPC is shared
162
+	// 1. create a file in the first container
163
+	_, err = container.Exec(ctx, client, name1, []string{"sh", "-c", "printf covfefe > /dev/shm/bar"})
164
+	assert.NilError(t, err)
165
+	// 2. check it's the same file in the second one
166
+	result, err := container.Exec(ctx, client, name2, []string{"cat", "/dev/shm/bar"})
167
+	assert.NilError(t, err)
168
+	out := result.Combined()
169
+	assert.Check(t, is.Equal(true, regexp.MustCompile("^covfefe$").MatchString(out)))
170
+}
171
+
172
+// TestAPIIpcModeShareableAndPrivate checks that
173
+// 1) a container created with --ipc container:ID can use IPC of another shareable container.
174
+// 2) a container created with --ipc container:ID can NOT use IPC of another private container.
175
+func TestAPIIpcModeShareableAndContainer(t *testing.T) {
176
+	skip.If(t, testEnv.DaemonInfo.OSType != "linux")
177
+
178
+	testIpcContainer(t, "shareable", true)
179
+
180
+	testIpcContainer(t, "private", false)
181
+}