mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #1396 from calavera/985-ordered-api-images
Sort APIImages by most recent creation date.
This commit is contained in:
commit
04c16f347b
3 changed files with 95 additions and 0 deletions
|
@ -241,6 +241,8 @@ func (srv *Server) Images(all bool, filter string) ([]APIImages, error) {
|
|||
outs = append(outs, out)
|
||||
}
|
||||
}
|
||||
|
||||
sortImagesByCreationAndTag(outs)
|
||||
return outs, nil
|
||||
}
|
||||
|
||||
|
|
36
sorter.go
Normal file
36
sorter.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
package docker
|
||||
|
||||
import "sort"
|
||||
|
||||
type imageSorter struct {
|
||||
images []APIImages
|
||||
by func(i1, i2 *APIImages) bool // Closure used in the Less method.
|
||||
}
|
||||
|
||||
// Len is part of sort.Interface.
|
||||
func (s *imageSorter) Len() int {
|
||||
return len(s.images)
|
||||
}
|
||||
|
||||
// Swap is part of sort.Interface.
|
||||
func (s *imageSorter) Swap(i, j int) {
|
||||
s.images[i], s.images[j] = s.images[j], s.images[i]
|
||||
}
|
||||
|
||||
// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
|
||||
func (s *imageSorter) Less(i, j int) bool {
|
||||
return s.by(&s.images[i], &s.images[j])
|
||||
}
|
||||
|
||||
// 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: creationAndTag}
|
||||
|
||||
sort.Sort(sorter)
|
||||
}
|
57
sorter_test.go
Normal file
57
sorter_test.go
Normal file
|
@ -0,0 +1,57 @@
|
|||
package docker
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestServerListOrderedImagesByCreationDate(t *testing.T) {
|
||||
runtime := mkRuntime(t)
|
||||
defer nuke(runtime)
|
||||
|
||||
archive, err := fakeTar()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = runtime.graph.Create(archive, nil, "Testing", "", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
srv := &Server{runtime: runtime}
|
||||
|
||||
images, err := srv.Images(true, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if images[0].Created < images[1].Created {
|
||||
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.")
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue