Browse code

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 authored on 2014/10/09 05:33:01
Showing 2 changed files
... ...
@@ -1558,22 +1558,27 @@ func (cli *DockerCli) CmdPs(args ...string) error {
1558 1558
 			outID = utils.TruncateID(outID)
1559 1559
 		}
1560 1560
 
1561
-		// Remove the leading / from the names
1562
-		for i := 0; i < len(outNames); i++ {
1563
-			outNames[i] = outNames[i][1:]
1564
-		}
1565
-
1566 1561
 		if !*quiet {
1567 1562
 			var (
1568
-				outCommand   = out.Get("Command")
1569
-				ports        = engine.NewTable("", 0)
1570
-				outNamesList = strings.Join(outNames, ",")
1563
+				outCommand = out.Get("Command")
1564
+				ports      = engine.NewTable("", 0)
1571 1565
 			)
1572 1566
 			outCommand = strconv.Quote(outCommand)
1573 1567
 			if !*noTrunc {
1574 1568
 				outCommand = utils.Trunc(outCommand, 20)
1575
-				outNamesList = outNames[0]
1569
+				// Keep only the canonical name
1570
+				for i := 0; i < len(outNames); i++ {
1571
+					if !strings.Contains(outNames[i][1:], "/") {
1572
+						outNames = outNames[i : i+1]
1573
+						break
1574
+					}
1575
+				}
1576
+			}
1577
+			// Remove the leading / from the names
1578
+			for i := 0; i < len(outNames); i++ {
1579
+				outNames[i] = outNames[i][1:]
1576 1580
 			}
1581
+			outNamesList := strings.Join(outNames, ",")
1577 1582
 			ports.ReadListFrom([]byte(out.Get("Ports")))
1578 1583
 			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)
1579 1584
 			if *size {
... ...
@@ -282,3 +282,50 @@ func TestPsListContainersFilterStatus(t *testing.T) {
282 282
 
283 283
 	logDone("ps - test ps filter status")
284 284
 }
285
+
286
+func TestPsListContainersNames(t *testing.T) {
287
+	runCmd := exec.Command(dockerBinary, "run", "--name", "link_target", "-d", "busybox", "top")
288
+	out, _, err := runCommandWithOutput(runCmd)
289
+	errorOut(err, t, out)
290
+
291
+	runCmd = exec.Command(dockerBinary, "run", "--name", "link_source", "--link", "link_target:link_alias", "-d", "busybox", "top")
292
+	out, _, err = runCommandWithOutput(runCmd)
293
+	errorOut(err, t, out)
294
+
295
+	// non-truncated ps should return all names
296
+	runCmd = exec.Command(dockerBinary, "ps", "--no-trunc")
297
+	out, _, err = runCommandWithOutput(runCmd)
298
+	errorOut(err, t, out)
299
+	lines := strings.Split(strings.Trim(out, "\n "), "\n")
300
+	namesIndex := strings.Index(lines[0], "NAMES")
301
+	expectedNames := "link_source"
302
+	foundNames := strings.TrimSpace(lines[1][namesIndex:])
303
+	if foundNames != expectedNames {
304
+		t.Fatalf("Expected names %q, got %q", expectedNames, foundNames)
305
+	}
306
+	expectedNames = "link_source/link_alias,link_target"
307
+	foundNames = strings.TrimSpace(lines[2][namesIndex:])
308
+	if foundNames != expectedNames {
309
+		t.Fatalf("Expected names %q, got %q", expectedNames, foundNames)
310
+	}
311
+
312
+	// truncated ps should return canonical names only
313
+	runCmd = exec.Command(dockerBinary, "ps")
314
+	out, _, err = runCommandWithOutput(runCmd)
315
+	errorOut(err, t, out)
316
+	lines = strings.Split(strings.Trim(out, "\n "), "\n")
317
+	namesIndex = strings.Index(lines[0], "NAMES")
318
+	expectedNames = "link_source"
319
+	foundNames = strings.TrimSpace(lines[1][namesIndex:])
320
+	if foundNames != expectedNames {
321
+		t.Fatalf("Expected names %q, got %q", expectedNames, foundNames)
322
+	}
323
+	expectedNames = "link_target"
324
+	foundNames = strings.TrimSpace(lines[2][namesIndex:])
325
+	if foundNames != expectedNames {
326
+		t.Fatalf("Expected names %q, got %q", expectedNames, foundNames)
327
+	}
328
+
329
+	deleteAllContainers()
330
+	logDone("ps - test ps names")
331
+}