Browse code

Merge pull request #10056 from coolljt0725/add_link_accept_ID

Add --link accept container ID

Michael Crosby authored on 2015/01/15 05:50:10
Showing 3 changed files
... ...
@@ -753,10 +753,7 @@ func (daemon *Daemon) RegisterLinks(container *Container, hostConfig *runconfig.
753 753
 			if err != nil {
754 754
 				return err
755 755
 			}
756
-			child, err := daemon.GetByName(parts["name"])
757
-			if err != nil {
758
-				return err
759
-			}
756
+			child := daemon.Get(parts["name"])
760 757
 			if child == nil {
761 758
 				return fmt.Errorf("Could not get container for %s", parts["name"])
762 759
 			}
... ...
@@ -314,6 +314,60 @@ func TestRunWithoutNetworking(t *testing.T) {
314 314
 	logDone("run - disable networking with -n=false")
315 315
 }
316 316
 
317
+//test --link use container name to link target
318
+func TestRunLinksContainerWithContainerName(t *testing.T) {
319
+	cmd := exec.Command(dockerBinary, "run", "-t", "-d", "--name", "parent", "busybox")
320
+	out, _, _, err := runCommandWithStdoutStderr(cmd)
321
+	if err != nil {
322
+		t.Fatal("failed to run container: %v, output: %q", err, out)
323
+	}
324
+	cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.NetworkSettings.IPAddress}}", "parent")
325
+	ip, _, _, err := runCommandWithStdoutStderr(cmd)
326
+	if err != nil {
327
+		t.Fatal("failed to inspect container: %v, output: %q", err, ip)
328
+	}
329
+	ip = strings.TrimSpace(ip)
330
+	cmd = exec.Command(dockerBinary, "run", "--link", "parent:test", "busybox", "/bin/cat", "/etc/hosts")
331
+	out, _, err = runCommandWithOutput(cmd)
332
+	if err != nil {
333
+		t.Fatal("failed to run container: %v, output: %q", err, out)
334
+	}
335
+	if !strings.Contains(out, ip+"	test") {
336
+		t.Fatalf("use a container name to link target failed")
337
+	}
338
+	deleteAllContainers()
339
+
340
+	logDone("run - use a container name to link target work")
341
+}
342
+
343
+//test --link use container id to link target
344
+func TestRunLinksContainerWithContainerId(t *testing.T) {
345
+	cmd := exec.Command(dockerBinary, "run", "-t", "-d", "busybox")
346
+	cID, _, _, err := runCommandWithStdoutStderr(cmd)
347
+	if err != nil {
348
+		t.Fatal("failed to run container: %v, output: %q", err, cID)
349
+	}
350
+	cID = strings.TrimSpace(cID)
351
+	cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.NetworkSettings.IPAddress}}", cID)
352
+	ip, _, _, err := runCommandWithStdoutStderr(cmd)
353
+	if err != nil {
354
+		t.Fatal("faild to inspect container: %v, output: %q", err, ip)
355
+	}
356
+	ip = strings.TrimSpace(ip)
357
+	cmd = exec.Command(dockerBinary, "run", "--link", cID+":test", "busybox", "/bin/cat", "/etc/hosts")
358
+	out, _, err := runCommandWithOutput(cmd)
359
+	if err != nil {
360
+		t.Fatal("failed to run container: %v, output: %q", err, out)
361
+	}
362
+	if !strings.Contains(out, ip+"	test") {
363
+		t.Fatalf("use a container id to link target failed")
364
+	}
365
+
366
+	deleteAllContainers()
367
+
368
+	logDone("run - use a container id to link target work")
369
+}
370
+
317 371
 // Regression test for #4741
318 372
 func TestRunWithVolumesAsFiles(t *testing.T) {
319 373
 	runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/etc/hosts:/target-file", "busybox", "true")
... ...
@@ -67,7 +67,7 @@ func Parse(cmd *flag.FlagSet, args []string) (*Config, *HostConfig, *flag.FlagSe
67 67
 
68 68
 	cmd.Var(&flAttach, []string{"a", "-attach"}, "Attach to STDIN, STDOUT or STDERR.")
69 69
 	cmd.Var(&flVolumes, []string{"v", "-volume"}, "Bind mount a volume (e.g., from the host: -v /host:/container, from Docker: -v /container)")
70
-	cmd.Var(&flLinks, []string{"#link", "-link"}, "Add link to another container in the form of name:alias")
70
+	cmd.Var(&flLinks, []string{"#link", "-link"}, "Add link to another container in the form of <name|id>:alias")
71 71
 	cmd.Var(&flDevices, []string{"-device"}, "Add a host device to the container (e.g. --device=/dev/sdc:/dev/xvdc:rwm)")
72 72
 
73 73
 	cmd.Var(&flEnv, []string{"e", "-env"}, "Set environment variables")