Quellcode durchsuchen

on truncated output, show canonical name rather than first name

Docker-DCO-1.1-Signed-off-by: Brice Jaglin <bjaglin@teads.tv> (github: bjaglin)
Brice Jaglin vor 10 Jahren
Ursprung
Commit
4a5cefa173
2 geänderte Dateien mit 61 neuen und 9 gelöschten Zeilen
  1. 14 9
      api/client/commands.go
  2. 47 0
      integration-cli/docker_cli_ps_test.go

+ 14 - 9
api/client/commands.go

@@ -1558,22 +1558,27 @@ func (cli *DockerCli) CmdPs(args ...string) error {
 			outID = utils.TruncateID(outID)
 		}
 
-		// Remove the leading / from the names
-		for i := 0; i < len(outNames); i++ {
-			outNames[i] = outNames[i][1:]
-		}
-
 		if !*quiet {
 			var (
-				outCommand   = out.Get("Command")
-				ports        = engine.NewTable("", 0)
-				outNamesList = strings.Join(outNames, ",")
+				outCommand = out.Get("Command")
+				ports      = engine.NewTable("", 0)
 			)
 			outCommand = strconv.Quote(outCommand)
 			if !*noTrunc {
 				outCommand = utils.Trunc(outCommand, 20)
-				outNamesList = outNames[0]
+				// Keep only the canonical name
+				for i := 0; i < len(outNames); i++ {
+					if !strings.Contains(outNames[i][1:], "/") {
+						outNames = outNames[i : i+1]
+						break
+					}
+				}
+			}
+			// Remove the leading / from the names
+			for i := 0; i < len(outNames); i++ {
+				outNames[i] = outNames[i][1:]
 			}
+			outNamesList := strings.Join(outNames, ",")
 			ports.ReadListFrom([]byte(out.Get("Ports")))
 			fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\t%s\t%s\t", outID, out.Get("Image"), outCommand, units.HumanDuration(time.Now().UTC().Sub(time.Unix(out.GetInt64("Created"), 0))), out.Get("Status"), api.DisplayablePorts(ports), outNamesList)
 			if *size {

+ 47 - 0
integration-cli/docker_cli_ps_test.go

@@ -282,3 +282,50 @@ func TestPsListContainersFilterStatus(t *testing.T) {
 
 	logDone("ps - test ps filter status")
 }
+
+func TestPsListContainersNames(t *testing.T) {
+	runCmd := exec.Command(dockerBinary, "run", "--name", "link_target", "-d", "busybox", "top")
+	out, _, err := runCommandWithOutput(runCmd)
+	errorOut(err, t, out)
+
+	runCmd = exec.Command(dockerBinary, "run", "--name", "link_source", "--link", "link_target:link_alias", "-d", "busybox", "top")
+	out, _, err = runCommandWithOutput(runCmd)
+	errorOut(err, t, out)
+
+	// non-truncated ps should return all names
+	runCmd = exec.Command(dockerBinary, "ps", "--no-trunc")
+	out, _, err = runCommandWithOutput(runCmd)
+	errorOut(err, t, out)
+	lines := strings.Split(strings.Trim(out, "\n "), "\n")
+	namesIndex := strings.Index(lines[0], "NAMES")
+	expectedNames := "link_source"
+	foundNames := strings.TrimSpace(lines[1][namesIndex:])
+	if foundNames != expectedNames {
+		t.Fatalf("Expected names %q, got %q", expectedNames, foundNames)
+	}
+	expectedNames = "link_source/link_alias,link_target"
+	foundNames = strings.TrimSpace(lines[2][namesIndex:])
+	if foundNames != expectedNames {
+		t.Fatalf("Expected names %q, got %q", expectedNames, foundNames)
+	}
+
+	// truncated ps should return canonical names only
+	runCmd = exec.Command(dockerBinary, "ps")
+	out, _, err = runCommandWithOutput(runCmd)
+	errorOut(err, t, out)
+	lines = strings.Split(strings.Trim(out, "\n "), "\n")
+	namesIndex = strings.Index(lines[0], "NAMES")
+	expectedNames = "link_source"
+	foundNames = strings.TrimSpace(lines[1][namesIndex:])
+	if foundNames != expectedNames {
+		t.Fatalf("Expected names %q, got %q", expectedNames, foundNames)
+	}
+	expectedNames = "link_target"
+	foundNames = strings.TrimSpace(lines[2][namesIndex:])
+	if foundNames != expectedNames {
+		t.Fatalf("Expected names %q, got %q", expectedNames, foundNames)
+	}
+
+	deleteAllContainers()
+	logDone("ps - test ps names")
+}