diff --git a/cli/command/trust/common.go b/cli/command/trust/common.go index 751259d5cb..4cc876a121 100644 --- a/cli/command/trust/common.go +++ b/cli/command/trust/common.go @@ -14,6 +14,7 @@ import ( "github.com/theupdateframework/notary" "github.com/theupdateframework/notary/client" "github.com/theupdateframework/notary/tuf/data" + "vbom.ml/util/sortorder" ) // trustTagKey represents a unique signed tag and hex-encoded hash pair @@ -149,7 +150,7 @@ func matchReleasedSignatures(allTargets []client.TargetSignedStruct) []trustTagR signatureRows = append(signatureRows, trustTagRow{targetKey, signers}) } sort.Slice(signatureRows, func(i, j int) bool { - return signatureRows[i].SignedTag < signatureRows[j].SignedTag + return sortorder.NaturalLess(signatureRows[i].SignedTag, signatureRows[j].SignedTag) }) return signatureRows } diff --git a/cli/command/trust/common_test.go b/cli/command/trust/common_test.go new file mode 100644 index 0000000000..279e7fa79c --- /dev/null +++ b/cli/command/trust/common_test.go @@ -0,0 +1,33 @@ +package trust + +import ( + "testing" + + "github.com/docker/cli/cli/trust" + "github.com/theupdateframework/notary/client" + "github.com/theupdateframework/notary/tuf/data" + "gotest.tools/assert" + is "gotest.tools/assert/cmp" +) + +func TestMatchReleasedSignaturesSortOrder(t *testing.T) { + var releasesRole = data.DelegationRole{BaseRole: data.BaseRole{Name: trust.ReleasesRole}} + targets := []client.TargetSignedStruct{ + {Target: client.Target{Name: "target10-foo"}, Role: releasesRole}, + {Target: client.Target{Name: "target1-foo"}, Role: releasesRole}, + {Target: client.Target{Name: "target2-foo"}, Role: releasesRole}, + } + + rows := matchReleasedSignatures(targets) + + var targetNames []string + for _, r := range rows { + targetNames = append(targetNames, r.SignedTag) + } + expected := []string{ + "target1-foo", + "target2-foo", + "target10-foo", + } + assert.Check(t, is.DeepEqual(expected, targetNames)) +} diff --git a/cli/command/trust/inspect_pretty.go b/cli/command/trust/inspect_pretty.go index f9104ce2c0..24c811b904 100644 --- a/cli/command/trust/inspect_pretty.go +++ b/cli/command/trust/inspect_pretty.go @@ -8,6 +8,7 @@ import ( "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/formatter" "github.com/theupdateframework/notary/client" + "vbom.ml/util/sortorder" ) func prettyPrintTrustInfo(cli command.Cli, remote string) error { @@ -86,7 +87,7 @@ func printSignerInfo(out io.Writer, roleToKeyIDs map[string][]string) error { }) } sort.Slice(formattedSignerInfo, func(i, j int) bool { - return formattedSignerInfo[i].Name < formattedSignerInfo[j].Name + return sortorder.NaturalLess(formattedSignerInfo[i].Name, formattedSignerInfo[j].Name) }) return formatter.SignerInfoWrite(signerInfoCtx, formattedSignerInfo) } diff --git a/cli/command/trust/inspect_pretty_test.go b/cli/command/trust/inspect_pretty_test.go index 8c5141b331..703dac02d0 100644 --- a/cli/command/trust/inspect_pretty_test.go +++ b/cli/command/trust/inspect_pretty_test.go @@ -1,6 +1,7 @@ package trust import ( + "bytes" "encoding/hex" "io/ioutil" "testing" @@ -440,3 +441,20 @@ func TestFormatAdminRole(t *testing.T) { targetsRoleWithSigs := client.RoleWithSignatures{Role: targetsRole, Signatures: nil} assert.Check(t, is.Equal("Repository Key:\tabc, key11, key99\n", formatAdminRole(targetsRoleWithSigs))) } + +func TestPrintSignerInfoSortOrder(t *testing.T) { + roleToKeyIDs := map[string][]string{ + "signer2-foo": {"B"}, + "signer10-foo": {"C"}, + "signer1-foo": {"A"}, + } + + expected := `SIGNER KEYS +signer1-foo A +signer2-foo B +signer10-foo C +` + buf := new(bytes.Buffer) + assert.NilError(t, printSignerInfo(buf, roleToKeyIDs)) + assert.Check(t, is.Equal(expected, buf.String())) +}