Bläddra i källkod

Merge pull request #18488 from aaronlehmann/docker-images-order

Make order of items in "docker images" deterministic
Vincent Demeester 9 år sedan
förälder
incheckning
6eeff9288b
2 ändrade filer med 21 tillägg och 15 borttagningar
  1. 17 0
      tag/store.go
  2. 4 15
      tag/store_test.go

+ 17 - 0
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
 }
 

+ 4 - 15
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())
 	}