From 4a5cefa1731ae99ee592e1898c6007904fb5943a Mon Sep 17 00:00:00 2001 From: Brice Jaglin Date: Wed, 8 Oct 2014 22:33:01 +0200 Subject: [PATCH] on truncated output, show canonical name rather than first name Docker-DCO-1.1-Signed-off-by: Brice Jaglin (github: bjaglin) --- api/client/commands.go | 23 ++++++++----- integration-cli/docker_cli_ps_test.go | 47 +++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 9 deletions(-) diff --git a/api/client/commands.go b/api/client/commands.go index 03e4293d79..b106dc4ba1 100644 --- a/api/client/commands.go +++ b/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 { diff --git a/integration-cli/docker_cli_ps_test.go b/integration-cli/docker_cli_ps_test.go index 4bcf95bf79..39dd4d680a 100644 --- a/integration-cli/docker_cli_ps_test.go +++ b/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") +}