Browse code

Migrate some ipcmode tests to integration

This fix migrates some ipcmode tests in integration-cli
to integration tests.

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

Yong Tang authored on 2018/07/04 13:14:23
Showing 2 changed files
... ...
@@ -46,72 +46,6 @@ func testIpcCheckDevExists(mm string) (bool, error) {
46 46
 	return false, s.Err()
47 47
 }
48 48
 
49
-// testIpcNonePrivateShareable is a helper function to test "none",
50
-// "private" and "shareable" modes.
51
-func testIpcNonePrivateShareable(c *check.C, mode string, mustBeMounted bool, mustBeShared bool) {
52
-	cfg := container.Config{
53
-		Image: "busybox",
54
-		Cmd:   []string{"top"},
55
-	}
56
-	hostCfg := container.HostConfig{
57
-		IpcMode: container.IpcMode(mode),
58
-	}
59
-	ctx := context.Background()
60
-
61
-	client := testEnv.APIClient()
62
-
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
-
67
-	err = client.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
68
-	c.Assert(err, checker.IsNil)
69
-
70
-	// get major:minor pair for /dev/shm from container's /proc/self/mountinfo
71
-	cmd := "awk '($5 == \"/dev/shm\") {printf $3}' /proc/self/mountinfo"
72
-	mm := cli.DockerCmd(c, "exec", "-i", resp.ID, "sh", "-c", cmd).Combined()
73
-	if !mustBeMounted {
74
-		c.Assert(mm, checker.Equals, "")
75
-		// no more checks to perform
76
-		return
77
-	}
78
-	c.Assert(mm, checker.Matches, "^[0-9]+:[0-9]+$")
79
-
80
-	shared, err := testIpcCheckDevExists(mm)
81
-	c.Assert(err, checker.IsNil)
82
-	c.Logf("[testIpcPrivateShareable] ipcmode: %v, ipcdev: %v, shared: %v, mustBeShared: %v\n", mode, mm, shared, mustBeShared)
83
-	c.Assert(shared, checker.Equals, mustBeShared)
84
-}
85
-
86
-/* TestAPIIpcModeNone checks the container "none" IPC mode
87
- * (--ipc none) works as expected. It makes sure there is no
88
- * /dev/shm mount inside the container.
89
- */
90
-func (s *DockerSuite) TestAPIIpcModeNone(c *check.C) {
91
-	testRequires(c, DaemonIsLinux, MinimumAPIVersion("1.32"))
92
-	testIpcNonePrivateShareable(c, "none", false, false)
93
-}
94
-
95
-/* TestAPIIpcModePrivate checks the container private IPC mode
96
- * (--ipc private) works as expected. It gets the minor:major pair
97
- * of /dev/shm mount from the container, and makes sure there is no
98
- * such pair on the host.
99
- */
100
-func (s *DockerSuite) TestAPIIpcModePrivate(c *check.C) {
101
-	testRequires(c, DaemonIsLinux, SameHostDaemon)
102
-	testIpcNonePrivateShareable(c, "private", true, false)
103
-}
104
-
105
-/* TestAPIIpcModeShareable checks the container shareable IPC mode
106
- * (--ipc shareable) works as expected. It gets the minor:major pair
107
- * of /dev/shm mount from the container, and makes sure such pair
108
- * also exists on the host.
109
- */
110
-func (s *DockerSuite) TestAPIIpcModeShareable(c *check.C) {
111
-	testRequires(c, DaemonIsLinux, SameHostDaemon)
112
-	testIpcNonePrivateShareable(c, "shareable", true, true)
113
-}
114
-
115 49
 // testIpcContainer is a helper function to test --ipc container:NNN mode in various scenarios
116 50
 func testIpcContainer(s *DockerSuite, c *check.C, donorMode string, mustWork bool) {
117 51
 	cfg := container.Config{
118 52
new file mode 100644
... ...
@@ -0,0 +1,116 @@
0
+package container // import "github.com/docker/docker/integration/container"
1
+
2
+import (
3
+	"bufio"
4
+	"context"
5
+	"os"
6
+	"regexp"
7
+	"strings"
8
+	"testing"
9
+
10
+	"github.com/docker/docker/api/types"
11
+	containertypes "github.com/docker/docker/api/types/container"
12
+	"github.com/docker/docker/integration/internal/container"
13
+	"github.com/docker/docker/internal/test/request"
14
+	"gotest.tools/assert"
15
+	is "gotest.tools/assert/cmp"
16
+	"gotest.tools/skip"
17
+)
18
+
19
+// testIpcCheckDevExists checks whether a given mount (identified by its
20
+// major:minor pair from /proc/self/mountinfo) exists on the host system.
21
+//
22
+// The format of /proc/self/mountinfo is like:
23
+//
24
+// 29 23 0:24 / /dev/shm rw,nosuid,nodev shared:4 - tmpfs tmpfs rw
25
+//       ^^^^\
26
+//            - this is the minor:major we look for
27
+func testIpcCheckDevExists(mm string) (bool, error) {
28
+	f, err := os.Open("/proc/self/mountinfo")
29
+	if err != nil {
30
+		return false, err
31
+	}
32
+	defer f.Close()
33
+
34
+	s := bufio.NewScanner(f)
35
+	for s.Scan() {
36
+		fields := strings.Fields(s.Text())
37
+		if len(fields) < 7 {
38
+			continue
39
+		}
40
+		if fields[2] == mm {
41
+			return true, nil
42
+		}
43
+	}
44
+
45
+	return false, s.Err()
46
+}
47
+
48
+// testIpcNonePrivateShareable is a helper function to test "none",
49
+// "private" and "shareable" modes.
50
+func testIpcNonePrivateShareable(t *testing.T, mode string, mustBeMounted bool, mustBeShared bool) {
51
+	defer setupTest(t)()
52
+
53
+	cfg := containertypes.Config{
54
+		Image: "busybox",
55
+		Cmd:   []string{"top"},
56
+	}
57
+	hostCfg := containertypes.HostConfig{
58
+		IpcMode: containertypes.IpcMode(mode),
59
+	}
60
+	client := request.NewAPIClient(t)
61
+	ctx := context.Background()
62
+
63
+	resp, err := client.ContainerCreate(ctx, &cfg, &hostCfg, nil, "")
64
+	assert.NilError(t, err)
65
+	assert.Check(t, is.Equal(len(resp.Warnings), 0))
66
+
67
+	err = client.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
68
+	assert.NilError(t, err)
69
+
70
+	// get major:minor pair for /dev/shm from container's /proc/self/mountinfo
71
+	cmd := "awk '($5 == \"/dev/shm\") {printf $3}' /proc/self/mountinfo"
72
+	result, err := container.Exec(ctx, client, resp.ID, []string{"sh", "-c", cmd})
73
+	assert.NilError(t, err)
74
+	mm := result.Combined()
75
+	if !mustBeMounted {
76
+		assert.Check(t, is.Equal(mm, ""))
77
+		// no more checks to perform
78
+		return
79
+	}
80
+	assert.Check(t, is.Equal(true, regexp.MustCompile("^[0-9]+:[0-9]+$").MatchString(mm)))
81
+
82
+	shared, err := testIpcCheckDevExists(mm)
83
+	assert.NilError(t, err)
84
+	t.Logf("[testIpcPrivateShareable] ipcmode: %v, ipcdev: %v, shared: %v, mustBeShared: %v\n", mode, mm, shared, mustBeShared)
85
+	assert.Check(t, is.Equal(shared, mustBeShared))
86
+}
87
+
88
+// TestIpcModeNone checks the container "none" IPC mode
89
+// (--ipc none) works as expected. It makes sure there is no
90
+// /dev/shm mount inside the container.
91
+func TestIpcModeNone(t *testing.T) {
92
+	skip.If(t, testEnv.DaemonInfo.OSType != "linux" || testEnv.IsRemoteDaemon())
93
+
94
+	testIpcNonePrivateShareable(t, "none", false, false)
95
+}
96
+
97
+// TestAPIIpcModePrivate checks the container private IPC mode
98
+// (--ipc private) works as expected. It gets the minor:major pair
99
+// of /dev/shm mount from the container, and makes sure there is no
100
+// such pair on the host.
101
+func TestIpcModePrivate(t *testing.T) {
102
+	skip.If(t, testEnv.DaemonInfo.OSType != "linux" || testEnv.IsRemoteDaemon())
103
+
104
+	testIpcNonePrivateShareable(t, "private", true, false)
105
+}
106
+
107
+// TestAPIIpcModeShareable checks the container shareable IPC mode
108
+// (--ipc shareable) works as expected. It gets the minor:major pair
109
+// of /dev/shm mount from the container, and makes sure such pair
110
+// also exists on the host.
111
+func TestIpcModeShareable(t *testing.T) {
112
+	skip.If(t, testEnv.DaemonInfo.OSType != "linux" || testEnv.IsRemoteDaemon())
113
+
114
+	testIpcNonePrivateShareable(t, "shareable", true, true)
115
+}