diff --git a/tag/store.go b/tag/store.go index 0565cf335b..f09db74b53 100644 --- a/tag/store.go +++ b/tag/store.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "os" "path/filepath" + "sort" "sync" "github.com/docker/distribution/digest" @@ -55,6 +56,18 @@ type store struct { // including the repository name. type repository map[string]image.ID +type lexicalRefs []reference.Named + +func (a lexicalRefs) Len() int { return len(a) } +func (a lexicalRefs) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a lexicalRefs) Less(i, j int) bool { return a[i].String() < a[j].String() } + +type lexicalAssociations []Association + +func (a lexicalAssociations) Len() int { return len(a) } +func (a lexicalAssociations) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a lexicalAssociations) Less(i, j int) bool { return a[i].Ref.String() < a[j].Ref.String() } + func defaultTagIfNameOnly(ref reference.Named) reference.Named { switch ref.(type) { case reference.Tagged: @@ -218,6 +231,8 @@ func (store *store) References(id image.ID) []reference.Named { references = append(references, ref) } + sort.Sort(lexicalRefs(references)) + return references } @@ -247,6 +262,8 @@ func (store *store) ReferencesByName(ref reference.Named) []Association { }) } + sort.Sort(lexicalAssociations(associations)) + return associations } diff --git a/tag/store_test.go b/tag/store_test.go index a424325616..80a36bf84c 100644 --- a/tag/store_test.go +++ b/tag/store_test.go @@ -5,7 +5,6 @@ import ( "io/ioutil" "os" "path/filepath" - "sort" "strings" "testing" @@ -103,18 +102,6 @@ func TestSave(t *testing.T) { } } -type LexicalRefs []reference.Named - -func (a LexicalRefs) Len() int { return len(a) } -func (a LexicalRefs) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a LexicalRefs) Less(i, j int) bool { return a[i].String() < a[j].String() } - -type LexicalAssociations []Association - -func (a LexicalAssociations) Len() int { return len(a) } -func (a LexicalAssociations) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a LexicalAssociations) Less(i, j int) bool { return a[i].Ref.String() < a[j].Ref.String() } - func TestAddDeleteGet(t *testing.T) { jsonFile, err := ioutil.TempFile("", "tag-store-test") if err != nil { @@ -261,10 +248,11 @@ func TestAddDeleteGet(t *testing.T) { // Check References refs := store.References(testImageID1) - sort.Sort(LexicalRefs(refs)) if len(refs) != 3 { t.Fatal("unexpected number of references") } + // Looking for the references in this order verifies that they are + // returned lexically sorted. if refs[0].String() != ref3.String() { t.Fatalf("unexpected reference: %v", refs[0].String()) } @@ -281,10 +269,11 @@ func TestAddDeleteGet(t *testing.T) { t.Fatalf("could not parse reference: %v", err) } associations := store.ReferencesByName(repoName) - sort.Sort(LexicalAssociations(associations)) if len(associations) != 3 { t.Fatal("unexpected number of associations") } + // Looking for the associations in this order verifies that they are + // returned lexically sorted. if associations[0].Ref.String() != ref3.String() { t.Fatalf("unexpected reference: %v", associations[0].Ref.String()) }