Browse code

Merge pull request #37438 from adshmh/fix-flaky-test-TestRunContainerWithBridgeNone

fix the race condition in the integration test TestRunContainerWithBridgeNone

Vincent Demeester authored on 2018/08/01 16:57:29
Showing 3 changed files
... ...
@@ -1488,34 +1488,6 @@ func (s *DockerDaemonSuite) TestCleanupMountsAfterGracefulShutdown(c *check.C) {
1488 1488
 	c.Assert(strings.Contains(string(mountOut), id), check.Equals, false, comment)
1489 1489
 }
1490 1490
 
1491
-func (s *DockerDaemonSuite) TestRunContainerWithBridgeNone(c *check.C) {
1492
-	testRequires(c, DaemonIsLinux, NotUserNamespace)
1493
-	s.d.StartWithBusybox(c, "-b", "none")
1494
-
1495
-	out, err := s.d.Cmd("run", "--rm", "busybox", "ip", "l")
1496
-	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
1497
-	c.Assert(strings.Contains(out, "eth0"), check.Equals, false,
1498
-		check.Commentf("There shouldn't be eth0 in container in default(bridge) mode when bridge network is disabled: %s", out))
1499
-
1500
-	out, err = s.d.Cmd("run", "--rm", "--net=bridge", "busybox", "ip", "l")
1501
-	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
1502
-	c.Assert(strings.Contains(out, "eth0"), check.Equals, false,
1503
-		check.Commentf("There shouldn't be eth0 in container in bridge mode when bridge network is disabled: %s", out))
1504
-	// the extra grep and awk clean up the output of `ip` to only list the number and name of
1505
-	// interfaces, allowing for different versions of ip (e.g. inside and outside the container) to
1506
-	// be used while still verifying that the interface list is the exact same
1507
-	cmd := exec.Command("sh", "-c", "ip l | grep -E '^[0-9]+:' | awk -F: ' { print $1\":\"$2 } '")
1508
-	stdout := bytes.NewBuffer(nil)
1509
-	cmd.Stdout = stdout
1510
-	if err := cmd.Run(); err != nil {
1511
-		c.Fatal("Failed to get host network interface")
1512
-	}
1513
-	out, err = s.d.Cmd("run", "--rm", "--net=host", "busybox", "sh", "-c", "ip l | grep -E '^[0-9]+:' | awk -F: ' { print $1\":\"$2 } '")
1514
-	c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
1515
-	c.Assert(out, check.Equals, fmt.Sprintf("%s", stdout),
1516
-		check.Commentf("The network interfaces in container should be the same with host when --net=host when bridge network is disabled: %s", out))
1517
-}
1518
-
1519 1491
 func (s *DockerDaemonSuite) TestDaemonRestartWithContainerRunning(t *check.C) {
1520 1492
 	s.d.StartWithBusybox(t)
1521 1493
 	if out, err := s.d.Cmd("run", "-d", "--name", "test", "busybox", "top"); err != nil {
... ...
@@ -3,6 +3,7 @@ package network
3 3
 import (
4 4
 	"context"
5 5
 	"fmt"
6
+	"os"
6 7
 	"testing"
7 8
 
8 9
 	"github.com/docker/docker/api/types"
... ...
@@ -83,3 +84,9 @@ func CheckKernelMajorVersionGreaterOrEqualThen(kernelVersion int, majorVersion i
83 83
 	}
84 84
 	return true
85 85
 }
86
+
87
+// IsUserNamespace returns whether the user namespace remapping is enabled
88
+func IsUserNamespace() bool {
89
+	root := os.Getenv("DOCKER_REMAP_ROOT")
90
+	return root != ""
91
+}
86 92
new file mode 100644
... ...
@@ -0,0 +1,58 @@
0
+package network // import "github.com/docker/docker/integration/network"
1
+
2
+import (
3
+	"bytes"
4
+	"context"
5
+	"os/exec"
6
+	"strings"
7
+	"testing"
8
+
9
+	"github.com/docker/docker/api/types"
10
+	"github.com/docker/docker/integration/internal/container"
11
+	"github.com/docker/docker/internal/test/daemon"
12
+	"gotest.tools/assert"
13
+	is "gotest.tools/assert/cmp"
14
+	"gotest.tools/skip"
15
+)
16
+
17
+func TestRunContainerWithBridgeNone(t *testing.T) {
18
+	skip.If(t, testEnv.IsRemoteDaemon, "cannot start daemon on remote test run")
19
+	skip.If(t, testEnv.DaemonInfo.OSType != "linux")
20
+	skip.If(t, IsUserNamespace())
21
+
22
+	d := daemon.New(t)
23
+	d.StartWithBusybox(t, "-b", "none")
24
+	defer d.Stop(t)
25
+
26
+	client, err := d.NewClient()
27
+	assert.Check(t, err, "error creating client")
28
+
29
+	ctx := context.Background()
30
+	id1 := container.Run(t, ctx, client)
31
+	defer client.ContainerRemove(ctx, id1, types.ContainerRemoveOptions{Force: true})
32
+
33
+	result, err := container.Exec(ctx, client, id1, []string{"ip", "l"})
34
+	assert.NilError(t, err)
35
+	assert.Check(t, is.Equal(false, strings.Contains(result.Combined(), "eth0")), "There shouldn't be eth0 in container in default(bridge) mode when bridge network is disabled")
36
+
37
+	id2 := container.Run(t, ctx, client, container.WithNetworkMode("bridge"))
38
+	defer client.ContainerRemove(ctx, id2, types.ContainerRemoveOptions{Force: true})
39
+
40
+	result, err = container.Exec(ctx, client, id2, []string{"ip", "l"})
41
+	assert.NilError(t, err)
42
+	assert.Check(t, is.Equal(false, strings.Contains(result.Combined(), "eth0")), "There shouldn't be eth0 in container in bridge mode when bridge network is disabled")
43
+
44
+	nsCommand := "ls -l /proc/self/ns/net | awk -F '->' '{print $2}'"
45
+	cmd := exec.Command("sh", "-c", nsCommand)
46
+	stdout := bytes.NewBuffer(nil)
47
+	cmd.Stdout = stdout
48
+	err = cmd.Run()
49
+	assert.NilError(t, err, "Failed to get current process network namespace: %+v", err)
50
+
51
+	id3 := container.Run(t, ctx, client, container.WithNetworkMode("host"))
52
+	defer client.ContainerRemove(ctx, id3, types.ContainerRemoveOptions{Force: true})
53
+
54
+	result, err = container.Exec(ctx, client, id3, []string{"sh", "-c", nsCommand})
55
+	assert.NilError(t, err)
56
+	assert.Check(t, is.Equal(stdout.String(), result.Combined()), "The network namspace of container should be the same with host when --net=host and bridge network is disabled")
57
+}