mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
on truncated output, show canonical name rather than first name
Docker-DCO-1.1-Signed-off-by: Brice Jaglin <bjaglin@teads.tv> (github: bjaglin)
This commit is contained in:
parent
0099b5e68c
commit
4a5cefa173
2 changed files with 61 additions and 9 deletions
|
@ -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 {
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue