Skip to content

Commit 5ee17ee

Browse files
committed
cli/command/formatter: fix .Labels format being randomized
The labels are stored as a map, causing the output to be randomized. This patch sorts the result to get a consistent output. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent e6bf6dc commit 5ee17ee

File tree

2 files changed

+53
-21
lines changed

2 files changed

+53
-21
lines changed

cli/command/formatter/container.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ func (c *ContainerContext) Labels() string {
254254
for k, v := range c.c.Labels {
255255
joinLabels = append(joinLabels, k+"="+v)
256256
}
257+
sort.Strings(joinLabels)
257258
return strings.Join(joinLabels, ",")
258259
}
259260

cli/command/formatter/container_test.go

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -497,28 +497,59 @@ func TestContainerContextWriteJSONField(t *testing.T) {
497497
}
498498

499499
func TestContainerBackCompat(t *testing.T) {
500-
containers := []container.Summary{{ID: "brewhaha"}}
501-
cases := []string{
502-
"ID",
503-
"Names",
504-
"Image",
505-
"Command",
506-
"CreatedAt",
507-
"RunningFor",
508-
"Ports",
509-
"Status",
510-
"Size",
511-
"Labels",
512-
"Mounts",
500+
createdAtTime := time.Now().AddDate(-1, 0, 0) // 1 year ago
501+
502+
ctrContext := container.Summary{
503+
ID: "aabbccddeeff",
504+
Names: []string{"/foobar_baz"},
505+
Image: "docker.io/library/ubuntu", // should this have canonical format or not?
506+
ImageID: "sha256:a5a665ff33eced1e0803148700880edab4269067ed77e27737a708d0d293fbf5", // should this have algo-prefix or not?
507+
ImageManifestDescriptor: nil,
508+
Command: "/bin/sh",
509+
Created: createdAtTime.UTC().Unix(),
510+
Ports: []container.Port{{PrivatePort: 8080, PublicPort: 8080, Type: "tcp"}},
511+
SizeRw: 123,
512+
SizeRootFs: 12345,
513+
Labels: map[string]string{"label1": "value1", "label2": "value2"},
514+
State: "running",
515+
Status: "running",
516+
HostConfig: struct {
517+
NetworkMode string `json:",omitempty"`
518+
Annotations map[string]string `json:",omitempty"`
519+
}{
520+
NetworkMode: "bridge",
521+
Annotations: map[string]string{
522+
"com.example.annotation": "hello",
523+
},
524+
},
525+
NetworkSettings: nil,
526+
Mounts: nil,
513527
}
514-
buf := bytes.NewBuffer(nil)
515-
for _, c := range cases {
516-
ctx := Context{Format: Format(fmt.Sprintf("{{ .%s }}", c)), Output: buf}
517-
if err := ContainerWrite(ctx, containers); err != nil {
518-
t.Logf("could not render template for field '%s': %v", c, err)
519-
t.Fail()
520-
}
521-
buf.Reset()
528+
529+
tests := []struct {
530+
field string
531+
expected string
532+
}{
533+
{field: "ID", expected: "aabbccddeeff"},
534+
{field: "Names", expected: "foobar_baz"},
535+
{field: "Image", expected: "docker.io/library/ubuntu"},
536+
{field: "Command", expected: `"/bin/sh"`},
537+
{field: "CreatedAt", expected: time.Unix(createdAtTime.Unix(), 0).String()},
538+
{field: "RunningFor", expected: "12 months ago"},
539+
{field: "Ports", expected: "8080/tcp"},
540+
{field: "Status", expected: "running"},
541+
{field: "Size", expected: "123B (virtual 12.3kB)"},
542+
{field: "Labels", expected: "label1=value1,label2=value2"},
543+
{field: "Mounts", expected: ""},
544+
}
545+
546+
for _, tc := range tests {
547+
t.Run(tc.field, func(t *testing.T) {
548+
buf := new(bytes.Buffer)
549+
ctx := Context{Format: Format(fmt.Sprintf("{{ .%s }}", tc.field)), Output: buf}
550+
assert.NilError(t, ContainerWrite(ctx, []container.Summary{ctrContext}))
551+
assert.Check(t, is.Equal(strings.TrimSpace(buf.String()), tc.expected))
552+
})
522553
}
523554
}
524555

0 commit comments

Comments
 (0)