formatter: Sort labels for stable output

Several Labels() methods iterated over maps without sorting, producing
non-deterministic output.

In early versions of Go, map iteration order happened to be stable in
practice, so the original code appeared to work correctly.
Since Go 1.12, the runtime intentionally randomizes map iteration order,
making the output unpredictable between runs.

The API response produces sorted labels and the container formatter
already sorted its labels (changed in 5ee17eef).

Apply the same fix to the volume, network, config, and secret
formatters, and update tests to assert exact ordering instead of using
order-independent comparison.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski
2026-04-24 16:47:11 +02:00
parent 977ee838e0
commit 7059ef4c9c
6 changed files with 22 additions and 6 deletions
+5
View File
@@ -1,7 +1,11 @@
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
//go:build go1.25
package config
import (
"fmt"
"slices"
"strings"
"time"
@@ -102,6 +106,7 @@ func (c *configContext) Labels() string {
for k, v := range mapLabels {
joinLabels = append(joinLabels, k+"="+v)
}
slices.Sort(joinLabels)
return strings.Join(joinLabels, ",")
}
+5
View File
@@ -1,7 +1,11 @@
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
//go:build go1.25
package formatter
import (
"fmt"
"slices"
"strconv"
"strings"
@@ -104,6 +108,7 @@ func (c *volumeContext) Labels() string {
for k, v := range c.v.Labels {
joinLabels = append(joinLabels, k+"="+v)
}
slices.Sort(joinLabels)
return strings.Join(joinLabels, ",")
}
+1 -3
View File
@@ -48,9 +48,7 @@ func TestVolumeContext(t *testing.T) {
for _, c := range cases {
ctx = c.volumeCtx
v := c.call()
if strings.Contains(v, ",") {
test.CompareMultipleValues(t, v, c.expValue)
} else if v != c.expValue {
if v != c.expValue {
t.Fatalf("Expected %s, was %s\n", c.expValue, v)
}
}
+5
View File
@@ -1,6 +1,10 @@
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
//go:build go1.25
package network
import (
"slices"
"strconv"
"strings"
@@ -115,6 +119,7 @@ func (c *networkContext) Labels() string {
for k, v := range c.n.Labels {
joinLabels = append(joinLabels, k+"="+v)
}
slices.Sort(joinLabels)
return strings.Join(joinLabels, ",")
}
+1 -3
View File
@@ -68,9 +68,7 @@ func TestNetworkContext(t *testing.T) {
for _, c := range cases {
ctx = c.networkCtx
v := c.call()
if strings.Contains(v, ",") {
test.CompareMultipleValues(t, v, c.expValue)
} else if v != c.expValue {
if v != c.expValue {
t.Fatalf("Expected %s, was %s\n", c.expValue, v)
}
}
+5
View File
@@ -1,7 +1,11 @@
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
//go:build go1.25
package secret
import (
"fmt"
"slices"
"strings"
"time"
@@ -109,6 +113,7 @@ func (c *secretContext) Labels() string {
for k, v := range mapLabels {
joinLabels = append(joinLabels, k+"="+v)
}
slices.Sort(joinLabels)
return strings.Join(joinLabels, ",")
}