2014-02-25 16:17:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-11-03 11:11:29 +00:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
2014-02-25 16:17:48 +00:00
|
|
|
"strings"
|
2014-06-23 18:19:52 +00:00
|
|
|
"time"
|
2015-03-18 14:11:01 +00:00
|
|
|
|
2015-03-24 11:25:26 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-04-18 16:46:47 +00:00
|
|
|
"github.com/go-check/check"
|
2014-02-25 16:17:48 +00:00
|
|
|
)
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestImagesEnsureImageIsListed(c *check.C) {
|
2015-07-20 06:55:40 +00:00
|
|
|
out, _ := dockerCmd(c, "images")
|
2014-02-25 16:17:48 +00:00
|
|
|
if !strings.Contains(out, "busybox") {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal("images should've listed busybox")
|
2014-02-25 16:17:48 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-04 00:57:41 +00:00
|
|
|
|
2015-08-08 13:58:48 +00:00
|
|
|
func (s *DockerSuite) TestImagesEnsureImageWithTagIsListed(c *check.C) {
|
|
|
|
_, err := buildImage("imagewithtag:v1",
|
|
|
|
`FROM scratch
|
|
|
|
MAINTAINER dockerio1`, true)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
_, err = buildImage("imagewithtag:v2",
|
|
|
|
`FROM scratch
|
|
|
|
MAINTAINER dockerio1`, true)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
out, _ := dockerCmd(c, "images", "imagewithtag:v1")
|
|
|
|
|
|
|
|
if !strings.Contains(out, "imagewithtag") || !strings.Contains(out, "v1") || strings.Contains(out, "v2") {
|
|
|
|
c.Fatal("images should've listed imagewithtag:v1 and not imagewithtag:v2")
|
|
|
|
}
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "images", "imagewithtag")
|
|
|
|
|
|
|
|
if !strings.Contains(out, "imagewithtag") || !strings.Contains(out, "v1") || !strings.Contains(out, "v2") {
|
|
|
|
c.Fatal("images should've listed imagewithtag:v1 and imagewithtag:v2")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestImagesEnsureImageWithBadTagIsNotListed(c *check.C) {
|
|
|
|
out, _ := dockerCmd(c, "images", "busybox:nonexistent")
|
|
|
|
|
|
|
|
if strings.Contains(out, "busybox") {
|
|
|
|
c.Fatal("images should not have listed busybox")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestImagesOrderedByCreationDate(c *check.C) {
|
2014-06-23 18:19:52 +00:00
|
|
|
id1, err := buildImage("order:test_a",
|
|
|
|
`FROM scratch
|
|
|
|
MAINTAINER dockerio1`, true)
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2014-06-23 18:19:52 +00:00
|
|
|
}
|
2015-08-11 07:41:11 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2014-06-23 18:19:52 +00:00
|
|
|
id2, err := buildImage("order:test_c",
|
|
|
|
`FROM scratch
|
|
|
|
MAINTAINER dockerio2`, true)
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2014-06-23 18:19:52 +00:00
|
|
|
}
|
2015-08-11 07:41:11 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2014-06-23 18:19:52 +00:00
|
|
|
id3, err := buildImage("order:test_b",
|
|
|
|
`FROM scratch
|
|
|
|
MAINTAINER dockerio3`, true)
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2014-06-23 18:19:52 +00:00
|
|
|
}
|
|
|
|
|
2015-07-20 06:55:40 +00:00
|
|
|
out, _ := dockerCmd(c, "images", "-q", "--no-trunc")
|
2014-06-23 18:19:52 +00:00
|
|
|
imgs := strings.Split(out, "\n")
|
|
|
|
if imgs[0] != id3 {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("First image must be %s, got %s", id3, imgs[0])
|
2014-06-23 18:19:52 +00:00
|
|
|
}
|
|
|
|
if imgs[1] != id2 {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Second image must be %s, got %s", id2, imgs[1])
|
2014-06-23 18:19:52 +00:00
|
|
|
}
|
|
|
|
if imgs[2] != id1 {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Third image must be %s, got %s", id1, imgs[2])
|
2014-06-23 18:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-03 11:11:29 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestImagesErrorWithInvalidFilterNameTest(c *check.C) {
|
2015-07-27 18:13:25 +00:00
|
|
|
out, _, err := dockerCmdWithError("images", "-f", "FOO=123")
|
2015-07-20 06:55:40 +00:00
|
|
|
if err == nil || !strings.Contains(out, "Invalid filter") {
|
|
|
|
c.Fatalf("error should occur when listing images with invalid filter name FOO, %s", out)
|
2014-11-03 11:11:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestImagesFilterLabel(c *check.C) {
|
2015-01-07 00:04:10 +00:00
|
|
|
imageName1 := "images_filter_test1"
|
|
|
|
imageName2 := "images_filter_test2"
|
|
|
|
imageName3 := "images_filter_test3"
|
|
|
|
image1ID, err := buildImage(imageName1,
|
|
|
|
`FROM scratch
|
|
|
|
LABEL match me`, true)
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-01-07 00:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
image2ID, err := buildImage(imageName2,
|
|
|
|
`FROM scratch
|
|
|
|
LABEL match="me too"`, true)
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-01-07 00:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
image3ID, err := buildImage(imageName3,
|
|
|
|
`FROM scratch
|
|
|
|
LABEL nomatch me`, true)
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-01-07 00:04:10 +00:00
|
|
|
}
|
|
|
|
|
2015-07-20 06:55:40 +00:00
|
|
|
out, _ := dockerCmd(c, "images", "--no-trunc", "-q", "-f", "label=match")
|
2015-01-07 00:04:10 +00:00
|
|
|
out = strings.TrimSpace(out)
|
|
|
|
if (!strings.Contains(out, image1ID) && !strings.Contains(out, image2ID)) || strings.Contains(out, image3ID) {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Expected ids %s,%s got %s", image1ID, image2ID, out)
|
2015-01-07 00:04:10 +00:00
|
|
|
}
|
|
|
|
|
2015-07-20 06:55:40 +00:00
|
|
|
out, _ = dockerCmd(c, "images", "--no-trunc", "-q", "-f", "label=match=me too")
|
2015-01-07 00:04:10 +00:00
|
|
|
out = strings.TrimSpace(out)
|
|
|
|
if out != image2ID {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Expected %s got %s", image2ID, out)
|
2015-01-07 00:04:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestImagesFilterSpaceTrimCase(c *check.C) {
|
2014-11-03 11:11:29 +00:00
|
|
|
imageName := "images_filter_test"
|
|
|
|
buildImage(imageName,
|
|
|
|
`FROM scratch
|
|
|
|
RUN touch /test/foo
|
|
|
|
RUN touch /test/bar
|
|
|
|
RUN touch /test/baz`, true)
|
|
|
|
|
|
|
|
filters := []string{
|
|
|
|
"dangling=true",
|
|
|
|
"Dangling=true",
|
|
|
|
" dangling=true",
|
|
|
|
"dangling=true ",
|
|
|
|
"dangling = true",
|
|
|
|
}
|
|
|
|
|
|
|
|
imageListings := make([][]string, 5, 5)
|
|
|
|
for idx, filter := range filters {
|
2015-07-20 06:55:40 +00:00
|
|
|
out, _ := dockerCmd(c, "images", "-q", "-f", filter)
|
2014-11-03 11:11:29 +00:00
|
|
|
listing := strings.Split(out, "\n")
|
|
|
|
sort.Strings(listing)
|
|
|
|
imageListings[idx] = listing
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx, listing := range imageListings {
|
|
|
|
if idx < 4 && !reflect.DeepEqual(listing, imageListings[idx+1]) {
|
|
|
|
for idx, errListing := range imageListings {
|
|
|
|
fmt.Printf("out %d", idx)
|
|
|
|
for _, image := range errListing {
|
|
|
|
fmt.Print(image)
|
|
|
|
}
|
|
|
|
fmt.Print("")
|
|
|
|
}
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("All output must be the same")
|
2014-11-03 11:11:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-18 14:11:01 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestImagesEnsureDanglingImageOnlyListedOnce(c *check.C) {
|
2015-03-18 14:11:01 +00:00
|
|
|
// create container 1
|
2015-07-20 06:55:40 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
|
2015-07-22 12:59:24 +00:00
|
|
|
containerID1 := strings.TrimSpace(out)
|
2015-03-18 14:11:01 +00:00
|
|
|
|
|
|
|
// tag as foobox
|
2015-07-22 12:59:24 +00:00
|
|
|
out, _ = dockerCmd(c, "commit", containerID1, "foobox")
|
|
|
|
imageID := stringid.TruncateID(strings.TrimSpace(out))
|
2015-03-18 14:11:01 +00:00
|
|
|
|
|
|
|
// overwrite the tag, making the previous image dangling
|
2015-07-20 06:55:40 +00:00
|
|
|
dockerCmd(c, "tag", "-f", "busybox", "foobox")
|
2015-03-18 14:11:01 +00:00
|
|
|
|
2015-07-20 06:55:40 +00:00
|
|
|
out, _ = dockerCmd(c, "images", "-q", "-f", "dangling=true")
|
2015-07-22 12:59:24 +00:00
|
|
|
if e, a := 1, strings.Count(out, imageID); e != a {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("expected 1 dangling image, got %d: %s", a, out)
|
2015-03-18 14:11:01 +00:00
|
|
|
}
|
|
|
|
}
|