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.
This commit is contained in:
David Calavera 2013-08-17 22:11:34 -07:00
Родитель cd6aeaf979
Коммит e6affb1b1a
3 изменённых файлов: 34 добавлений и 7 удалений

Просмотреть файл

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

Просмотреть файл

@ -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)
}

Просмотреть файл

@ -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.")
}
}