Selaa lähdekoodia

Sort images by tag name when the creation date is the same.

This establishes a strict alphabetical order for tags with the same creation date.
David Calavera 12 vuotta sitten
vanhempi
commit
e6affb1b1a
3 muutettua tiedostoa jossa 34 lisäystä ja 7 poistoa
  1. 1 1
      server.go
  2. 5 5
      sorter.go
  3. 28 1
      sorter_test.go

+ 1 - 1
server.go

@@ -242,7 +242,7 @@ func (srv *Server) Images(all bool, filter string) ([]APIImages, error) {
 		}
 	}
 
-	sortImagesByCreation(outs)
+	sortImagesByCreationAndTag(outs)
 	return outs, nil
 }
 

+ 5 - 5
sorter.go

@@ -22,15 +22,15 @@ func (s *imageSorter) Less(i, j int) bool {
 	return s.by(&s.images[i], &s.images[j])
 }
 
-// Sort []ApiImages by most recent creation date.
-func sortImagesByCreation(images []APIImages) {
-	creation := func(i1, i2 *APIImages) bool {
-		return i1.Created > i2.Created
+// Sort []ApiImages by most recent creation date and tag name.
+func sortImagesByCreationAndTag(images []APIImages) {
+	creationAndTag := func(i1, i2 *APIImages) bool {
+		return i1.Created > i2.Created || (i1.Created == i2.Created && i2.Tag > i1.Tag)
 	}
 
 	sorter := &imageSorter{
 		images: images,
-		by:     creation}
+		by:     creationAndTag}
 
 	sort.Sort(sorter)
 }

+ 28 - 1
sorter_test.go

@@ -4,7 +4,7 @@ import (
 	"testing"
 )
 
-func TestServerListOrderedImages(t *testing.T) {
+func TestServerListOrderedImagesByCreationDate(t *testing.T) {
 	runtime := mkRuntime(t)
 	defer nuke(runtime)
 
@@ -28,3 +28,30 @@ func TestServerListOrderedImages(t *testing.T) {
 		t.Error("Expected []APIImges to be ordered by most recent creation date.")
 	}
 }
+
+func TestServerListOrderedImagesByCreationDateAndTag(t *testing.T) {
+	runtime := mkRuntime(t)
+	defer nuke(runtime)
+
+	archive, err := fakeTar()
+	if err != nil {
+		t.Fatal(err)
+	}
+	image, err := runtime.graph.Create(archive, nil, "Testing", "", nil)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	srv := &Server{runtime: runtime}
+	srv.ContainerTag(image.ID, "repo", "foo", false)
+	srv.ContainerTag(image.ID, "repo", "bar", false)
+
+	images, err := srv.Images(true, "")
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if images[0].Created != images[1].Created || images[0].Tag >= images[1].Tag {
+		t.Error("Expected []APIImges to be ordered by most recent creation date and tag name.")
+	}
+}