Browse code

Fix regression on --link on bridge network

Signed-off-by: Alessandro Boch <aboch@docker.com>

Alessandro Boch authored on 2016/07/23 07:42:26
Showing 2 changed files
... ...
@@ -177,11 +177,12 @@ func (daemon *Daemon) buildSandboxOptions(container *container.Container) ([]lib
177 177
 	// Legacy Link feature is supported only for the default bridge network.
178 178
 	// return if this call to build join options is not for default bridge network
179 179
 	// Legacy Link is only supported by docker run --link
180
-	if _, ok := container.NetworkSettings.Networks[defaultNetName]; !container.HostConfig.NetworkMode.IsDefault() || !ok {
180
+	bridgeSettings, ok := container.NetworkSettings.Networks[defaultNetName]
181
+	if !ok {
181 182
 		return sboxOptions, nil
182 183
 	}
183 184
 
184
-	if container.NetworkSettings.Networks[defaultNetName].EndpointID == "" {
185
+	if bridgeSettings.EndpointID == "" {
185 186
 		return sboxOptions, nil
186 187
 	}
187 188
 
... ...
@@ -209,7 +210,6 @@ func (daemon *Daemon) buildSandboxOptions(container *container.Container) ([]lib
209 209
 		}
210 210
 	}
211 211
 
212
-	bridgeSettings := container.NetworkSettings.Networks[defaultNetName]
213 212
 	for alias, parent := range daemon.parents(container) {
214 213
 		if daemon.configStore.DisableBridge || !container.HostConfig.NetworkMode.IsPrivate() {
215 214
 			continue
... ...
@@ -31,10 +31,33 @@ func (s *DockerSuite) TestLinksInvalidContainerTarget(c *check.C) {
31 31
 
32 32
 func (s *DockerSuite) TestLinksPingLinkedContainers(c *check.C) {
33 33
 	testRequires(c, DaemonIsLinux)
34
-	dockerCmd(c, "run", "-d", "--name", "container1", "--hostname", "fred", "busybox", "top")
35
-	dockerCmd(c, "run", "-d", "--name", "container2", "--hostname", "wilma", "busybox", "top")
34
+	// Test with the three different ways of specifying the default network on Linux
35
+	testLinkPingOnNetwork(c, "")
36
+	testLinkPingOnNetwork(c, "default")
37
+	testLinkPingOnNetwork(c, "bridge")
38
+}
39
+
40
+func testLinkPingOnNetwork(c *check.C, network string) {
41
+	var postArgs []string
42
+	if network != "" {
43
+		postArgs = append(postArgs, []string{"--net", network}...)
44
+	}
45
+	postArgs = append(postArgs, []string{"busybox", "top"}...)
46
+	runArgs1 := append([]string{"run", "-d", "--name", "container1", "--hostname", "fred"}, postArgs...)
47
+	runArgs2 := append([]string{"run", "-d", "--name", "container2", "--hostname", "wilma"}, postArgs...)
48
+
49
+	// Run the two named containers
50
+	dockerCmd(c, runArgs1...)
51
+	dockerCmd(c, runArgs2...)
52
+
53
+	postArgs = []string{}
54
+	if network != "" {
55
+		postArgs = append(postArgs, []string{"--net", network}...)
56
+	}
57
+	postArgs = append(postArgs, []string{"busybox", "sh", "-c"}...)
36 58
 
37
-	runArgs := []string{"run", "--rm", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "sh", "-c"}
59
+	// Format a run for a container which links to the other two
60
+	runArgs := append([]string{"run", "--rm", "--link", "container1:alias1", "--link", "container2:alias2"}, postArgs...)
38 61
 	pingCmd := "ping -c 1 %s -W 1 && ping -c 1 %s -W 1"
39 62
 
40 63
 	// test ping by alias, ping by name, and ping by hostname
... ...
@@ -45,6 +68,9 @@ func (s *DockerSuite) TestLinksPingLinkedContainers(c *check.C) {
45 45
 	// 3. Ping by hostname
46 46
 	dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "fred", "wilma"))...)
47 47
 
48
+	// Clean for next round
49
+	dockerCmd(c, "rm", "-f", "container1")
50
+	dockerCmd(c, "rm", "-f", "container2")
48 51
 }
49 52
 
50 53
 func (s *DockerSuite) TestLinksPingLinkedContainersAfterRename(c *check.C) {