Skip to content

Commit

Permalink
fix ps format names
Browse files Browse the repository at this point in the history
Signed-off-by: Kay Yan <kay.yan@daocloud.io>
  • Loading branch information
yankay committed Oct 25, 2023
1 parent 16384fa commit caea0f9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
6 changes: 1 addition & 5 deletions cmd/nerdctl/container_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,6 @@ func formatAndPrintContainerInfo(containers []container.ListItem, options Format
return err
}
} else {
var name string
if len(c.Names) > 0 {
name = c.Names[0]
}
format := "%s\t%s\t%s\t%s\t%s\t%s\t%s"
args := []interface{}{
c.ID,
Expand All @@ -214,7 +210,7 @@ func formatAndPrintContainerInfo(containers []container.ListItem, options Format
formatter.TimeSinceInHuman(c.CreatedAt),
c.Status,
c.Ports,
name,
c.Names,
}
if wide {
format += "\t%s\t%s\t%s\n"
Expand Down
18 changes: 18 additions & 0 deletions cmd/nerdctl/container_list_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,24 @@ func TestContainerListWithLabels(t *testing.T) {
})
}

func TestContainerListWithNames(t *testing.T) {
base, testContainer := preparePsTestContainer(t, "listWithNames", true)

// hope there are no tests running parallel
base.Cmd("ps", "-n", "1", "--format", "{{.Names}}").AssertOutWithFunc(func(stdout string) error {

// An example of nerdctl ps --format "{{.Names}}"
lines := strings.Split(strings.TrimSpace(stdout), "\n")
if len(lines) != 1 {
return fmt.Errorf("expected 1 line, got %d", len(lines))
}

assert.Equal(t, lines[0], testContainer.name)

return nil
})
}

func TestContainerListWithFilter(t *testing.T) {
base, testContainerA := preparePsTestContainer(t, "listWithFilterA", true)
_, testContainerB := preparePsTestContainer(t, "listWithFilterB", true)
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/container/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type ListItem struct {
ID string
Image string
Platform string // nerdctl extension
Names []string
Names string
Ports string
Status string
Runtime string // nerdctl extension
Expand Down Expand Up @@ -130,7 +130,7 @@ func prepareContainers(ctx context.Context, client *containerd.Client, container
ID: id,
Image: info.Image,
Platform: info.Labels[labels.Platform],
Names: []string{getContainerName(info.Labels)},
Names: getContainerName(info.Labels),
Ports: formatter.FormatPorts(info.Labels),
Status: formatter.ContainerStatus(ctx, c),
Runtime: info.Runtime.Name,
Expand Down

0 comments on commit caea0f9

Please sign in to comment.