Browse code

integration: port TestRunModePIDHost from CLI test to API test

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>

Akihiro Suda authored on 2021/01/19 13:28:18
Showing 4 changed files
... ...
@@ -2426,28 +2426,6 @@ func (s *DockerSuite) TestContainerNetworkMode(c *testing.T) {
2426 2426
 	}
2427 2427
 }
2428 2428
 
2429
-func (s *DockerSuite) TestRunModePIDHost(c *testing.T) {
2430
-	// Not applicable on Windows as uses Unix-specific capabilities
2431
-	testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux, NotUserNamespace)
2432
-
2433
-	hostPid, err := os.Readlink("/proc/1/ns/pid")
2434
-	if err != nil {
2435
-		c.Fatal(err)
2436
-	}
2437
-
2438
-	out, _ := dockerCmd(c, "run", "--pid=host", "busybox", "readlink", "/proc/self/ns/pid")
2439
-	out = strings.Trim(out, "\n")
2440
-	if hostPid != out {
2441
-		c.Fatalf("PID different with --pid=host %s != %s\n", hostPid, out)
2442
-	}
2443
-
2444
-	out, _ = dockerCmd(c, "run", "busybox", "readlink", "/proc/self/ns/pid")
2445
-	out = strings.Trim(out, "\n")
2446
-	if hostPid == out {
2447
-		c.Fatalf("PID should be different without --pid=host %s == %s\n", hostPid, out)
2448
-	}
2449
-}
2450
-
2451 2429
 func (s *DockerSuite) TestRunModeUTSHost(c *testing.T) {
2452 2430
 	// Not applicable on Windows as uses Unix-specific capabilities
2453 2431
 	testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux)
2454 2432
new file mode 100644
... ...
@@ -0,0 +1,38 @@
0
+package container // import "github.com/docker/docker/integration/container"
1
+
2
+import (
3
+	"context"
4
+	"os"
5
+	"testing"
6
+	"time"
7
+
8
+	"github.com/docker/docker/integration/internal/container"
9
+	"gotest.tools/v3/assert"
10
+	"gotest.tools/v3/poll"
11
+	"gotest.tools/v3/skip"
12
+)
13
+
14
+func TestPidHost(t *testing.T) {
15
+	skip.If(t, testEnv.DaemonInfo.OSType != "linux")
16
+	skip.If(t, testEnv.IsRemoteDaemon())
17
+	skip.If(t, testEnv.IsRootless, "https://github.com/moby/moby/issues/41457")
18
+
19
+	hostPid, err := os.Readlink("/proc/1/ns/pid")
20
+	assert.NilError(t, err)
21
+
22
+	defer setupTest(t)()
23
+	client := testEnv.APIClient()
24
+	ctx := context.Background()
25
+
26
+	cID := container.Run(ctx, t, client, func(c *container.TestContainerConfig) {
27
+		c.HostConfig.PidMode = "host"
28
+	})
29
+	poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
30
+	cPid := container.GetContainerNS(ctx, t, client, cID, "pid")
31
+	assert.Assert(t, hostPid == cPid)
32
+
33
+	cID = container.Run(ctx, t, client)
34
+	poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
35
+	cPid = container.GetContainerNS(ctx, t, client, cID, "pid")
36
+	assert.Assert(t, hostPid != cPid)
37
+}
... ...
@@ -2,7 +2,6 @@ package container // import "github.com/docker/docker/integration/container"
2 2
 
3 3
 import (
4 4
 	"context"
5
-	"strings"
6 5
 	"testing"
7 6
 	"time"
8 7
 
... ...
@@ -11,20 +10,10 @@ import (
11 11
 	"github.com/docker/docker/integration/internal/requirement"
12 12
 	"github.com/docker/docker/testutil/daemon"
13 13
 	"gotest.tools/v3/assert"
14
-	is "gotest.tools/v3/assert/cmp"
15 14
 	"gotest.tools/v3/poll"
16 15
 	"gotest.tools/v3/skip"
17 16
 )
18 17
 
19
-// Gets the value of the cgroup namespace for pid 1 of a container
20
-func containerCgroupNamespace(ctx context.Context, t *testing.T, client *client.Client, cID string) string {
21
-	res, err := container.Exec(ctx, client, cID, []string{"readlink", "/proc/1/ns/cgroup"})
22
-	assert.NilError(t, err)
23
-	assert.Assert(t, is.Len(res.Stderr(), 0))
24
-	assert.Equal(t, 0, res.ExitCode)
25
-	return strings.TrimSpace(res.Stdout())
26
-}
27
-
28 18
 // Bring up a daemon with the specified default cgroup namespace mode, and then create a container with the container options
29 19
 func testRunWithCgroupNs(t *testing.T, daemonNsMode string, containerOpts ...func(*container.TestContainerConfig)) (string, string) {
30 20
 	d := daemon.New(t, daemon.WithDefaultCgroupNamespaceMode(daemonNsMode))
... ...
@@ -38,7 +27,7 @@ func testRunWithCgroupNs(t *testing.T, daemonNsMode string, containerOpts ...fun
38 38
 	poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
39 39
 
40 40
 	daemonCgroup := d.CgroupNamespace(t)
41
-	containerCgroup := containerCgroupNamespace(ctx, t, client, cID)
41
+	containerCgroup := container.GetContainerNS(ctx, t, client, cID, "cgroup")
42 42
 	return containerCgroup, daemonCgroup
43 43
 }
44 44
 
... ...
@@ -147,7 +136,7 @@ func TestCgroupNamespacesRunOlderClient(t *testing.T) {
147 147
 	poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
148 148
 
149 149
 	daemonCgroup := d.CgroupNamespace(t)
150
-	containerCgroup := containerCgroupNamespace(ctx, t, client, cID)
150
+	containerCgroup := container.GetContainerNS(ctx, t, client, cID, "cgroup")
151 151
 	if testEnv.DaemonInfo.CgroupVersion != "2" {
152 152
 		assert.Assert(t, daemonCgroup == containerCgroup)
153 153
 	} else {
154 154
new file mode 100644
... ...
@@ -0,0 +1,21 @@
0
+package container
1
+
2
+import (
3
+	"context"
4
+	"strings"
5
+	"testing"
6
+
7
+	"github.com/docker/docker/client"
8
+	"gotest.tools/v3/assert"
9
+	is "gotest.tools/v3/assert/cmp"
10
+)
11
+
12
+// GetContainerNS gets the value of the specified namespace of a container
13
+func GetContainerNS(ctx context.Context, t *testing.T, client client.APIClient, cID, nsName string) string {
14
+	t.Helper()
15
+	res, err := Exec(ctx, client, cID, []string{"readlink", "/proc/self/ns/" + nsName})
16
+	assert.NilError(t, err)
17
+	assert.Assert(t, is.Len(res.Stderr(), 0))
18
+	assert.Equal(t, 0, res.ExitCode)
19
+	return strings.TrimSpace(res.Stdout())
20
+}