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>
This commit is contained in:
Sebastiaan van Stijn 2025-05-16 14:11:56 +02:00
parent e6bf6dcd90
commit 5ee17eefe6
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
2 changed files with 53 additions and 21 deletions

View File

@ -254,6 +254,7 @@ func (c *ContainerContext) Labels() string {
for k, v := range c.c.Labels { for k, v := range c.c.Labels {
joinLabels = append(joinLabels, k+"="+v) joinLabels = append(joinLabels, k+"="+v)
} }
sort.Strings(joinLabels)
return strings.Join(joinLabels, ",") return strings.Join(joinLabels, ",")
} }

View File

@ -497,28 +497,59 @@ func TestContainerContextWriteJSONField(t *testing.T) {
} }
func TestContainerBackCompat(t *testing.T) { func TestContainerBackCompat(t *testing.T) {
containers := []container.Summary{{ID: "brewhaha"}} createdAtTime := time.Now().AddDate(-1, 0, 0) // 1 year ago
cases := []string{
"ID", ctrContext := container.Summary{
"Names", ID: "aabbccddeeff",
"Image", Names: []string{"/foobar_baz"},
"Command", Image: "docker.io/library/ubuntu", // should this have canonical format or not?
"CreatedAt", ImageID: "sha256:a5a665ff33eced1e0803148700880edab4269067ed77e27737a708d0d293fbf5", // should this have algo-prefix or not?
"RunningFor", ImageManifestDescriptor: nil,
"Ports", Command: "/bin/sh",
"Status", Created: createdAtTime.UTC().Unix(),
"Size", Ports: []container.Port{{PrivatePort: 8080, PublicPort: 8080, Type: "tcp"}},
"Labels", SizeRw: 123,
"Mounts", SizeRootFs: 12345,
Labels: map[string]string{"label1": "value1", "label2": "value2"},
State: "running",
Status: "running",
HostConfig: struct {
NetworkMode string `json:",omitempty"`
Annotations map[string]string `json:",omitempty"`
}{
NetworkMode: "bridge",
Annotations: map[string]string{
"com.example.annotation": "hello",
},
},
NetworkSettings: nil,
Mounts: nil,
} }
buf := bytes.NewBuffer(nil)
for _, c := range cases { tests := []struct {
ctx := Context{Format: Format(fmt.Sprintf("{{ .%s }}", c)), Output: buf} field string
if err := ContainerWrite(ctx, containers); err != nil { expected string
t.Logf("could not render template for field '%s': %v", c, err) }{
t.Fail() {field: "ID", expected: "aabbccddeeff"},
} {field: "Names", expected: "foobar_baz"},
buf.Reset() {field: "Image", expected: "docker.io/library/ubuntu"},
{field: "Command", expected: `"/bin/sh"`},
{field: "CreatedAt", expected: time.Unix(createdAtTime.Unix(), 0).String()},
{field: "RunningFor", expected: "12 months ago"},
{field: "Ports", expected: "8080/tcp"},
{field: "Status", expected: "running"},
{field: "Size", expected: "123B (virtual 12.3kB)"},
{field: "Labels", expected: "label1=value1,label2=value2"},
{field: "Mounts", expected: ""},
}
for _, tc := range tests {
t.Run(tc.field, func(t *testing.T) {
buf := new(bytes.Buffer)
ctx := Context{Format: Format(fmt.Sprintf("{{ .%s }}", tc.field)), Output: buf}
assert.NilError(t, ContainerWrite(ctx, []container.Summary{ctrContext}))
assert.Check(t, is.Equal(strings.TrimSpace(buf.String()), tc.expected))
})
} }
} }