Browse code

Fix --link to a container which net mode is container mode

Signed-off-by: Lei Jitang <leijitang@huawei.com>

Lei Jitang authored on 2015/03/17 23:50:12
Showing 2 changed files
... ...
@@ -774,6 +774,13 @@ func (daemon *Daemon) RegisterLinks(container *Container, hostConfig *runconfig.
774 774
 				//An error from daemon.Get() means this name could not be found
775 775
 				return fmt.Errorf("Could not get container for %s", parts["name"])
776 776
 			}
777
+			for child.hostConfig.NetworkMode.IsContainer() {
778
+				parts := strings.SplitN(string(child.hostConfig.NetworkMode), ":", 2)
779
+				child, err = daemon.Get(parts[1])
780
+				if err != nil {
781
+					return fmt.Errorf("Could not get container for %s", parts[1])
782
+				}
783
+			}
777 784
 			if child.hostConfig.NetworkMode.IsHost() {
778 785
 				return runconfig.ErrConflictHostNetworkAndLinks
779 786
 			}
... ...
@@ -379,6 +379,39 @@ func TestRunLinksContainerWithContainerId(t *testing.T) {
379 379
 	logDone("run - use a container id to link target work")
380 380
 }
381 381
 
382
+func TestRunLinkToContainerNetMode(t *testing.T) {
383
+	defer deleteAllContainers()
384
+
385
+	cmd := exec.Command(dockerBinary, "run", "--name", "test", "-d", "busybox", "top")
386
+	out, _, err := runCommandWithOutput(cmd)
387
+	if err != nil {
388
+		t.Fatalf("failed to run container: %v, output: %q", err, out)
389
+	}
390
+	cmd = exec.Command(dockerBinary, "run", "--name", "parent", "-d", "--net=container:test", "busybox", "top")
391
+	out, _, err = runCommandWithOutput(cmd)
392
+	if err != nil {
393
+		t.Fatalf("failed to run container: %v, output: %q", err, out)
394
+	}
395
+	cmd = exec.Command(dockerBinary, "run", "-d", "--link=parent:parent", "busybox", "top")
396
+	out, _, err = runCommandWithOutput(cmd)
397
+	if err != nil {
398
+		t.Fatalf("failed to run container: %v, output: %q", err, out)
399
+	}
400
+
401
+	cmd = exec.Command(dockerBinary, "run", "--name", "child", "-d", "--net=container:parent", "busybox", "top")
402
+	out, _, err = runCommandWithOutput(cmd)
403
+	if err != nil {
404
+		t.Fatalf("failed to run container: %v, output: %q", err, out)
405
+	}
406
+	cmd = exec.Command(dockerBinary, "run", "-d", "--link=child:child", "busybox", "top")
407
+	out, _, err = runCommandWithOutput(cmd)
408
+	if err != nil {
409
+		t.Fatalf("failed to run container: %v, output: %q", err, out)
410
+	}
411
+
412
+	logDone("run - link to a container which net mode is container success")
413
+}
414
+
382 415
 // Regression test for #4741
383 416
 func TestRunWithVolumesAsFiles(t *testing.T) {
384 417
 	defer deleteAllContainers()