integration-cli/docker_cli_images_test.go
6db32fde
 package main
 
 import (
4deac03c
 	"fmt"
34a3c3ca
 	"io/ioutil"
 	"os"
 	"path/filepath"
4deac03c
 	"reflect"
 	"sort"
6db32fde
 	"strings"
386d1ecc
 	"time"
c680dd9e
 
33968e6c
 	"github.com/docker/docker/integration-cli/checker"
b80fae73
 	"github.com/docker/docker/pkg/stringid"
c10f6ef4
 	icmd "github.com/docker/docker/pkg/testutil/cmd"
dc944ea7
 	"github.com/go-check/check"
6db32fde
 )
 
dc944ea7
 func (s *DockerSuite) TestImagesEnsureImageIsListed(c *check.C) {
5814d67f
 	imagesOut, _ := dockerCmd(c, "images")
 	c.Assert(imagesOut, checker.Contains, "busybox")
6db32fde
 }
30f22ee9
 
4fb88d2e
 func (s *DockerSuite) TestImagesEnsureImageWithTagIsListed(c *check.C) {
8819b530
 	name := "imagewithtag"
 	dockerCmd(c, "tag", "busybox", name+":v1")
 	dockerCmd(c, "tag", "busybox", name+":v1v1")
 	dockerCmd(c, "tag", "busybox", name+":v2")
4fb88d2e
 
8819b530
 	imagesOut, _ := dockerCmd(c, "images", name+":v1")
 	c.Assert(imagesOut, checker.Contains, name)
5814d67f
 	c.Assert(imagesOut, checker.Contains, "v1")
 	c.Assert(imagesOut, checker.Not(checker.Contains), "v2")
8819b530
 	c.Assert(imagesOut, checker.Not(checker.Contains), "v1v1")
4fb88d2e
 
8819b530
 	imagesOut, _ = dockerCmd(c, "images", name)
 	c.Assert(imagesOut, checker.Contains, name)
5814d67f
 	c.Assert(imagesOut, checker.Contains, "v1")
8819b530
 	c.Assert(imagesOut, checker.Contains, "v1v1")
5814d67f
 	c.Assert(imagesOut, checker.Contains, "v2")
4fb88d2e
 }
 
 func (s *DockerSuite) TestImagesEnsureImageWithBadTagIsNotListed(c *check.C) {
5814d67f
 	imagesOut, _ := dockerCmd(c, "images", "busybox:nonexistent")
 	c.Assert(imagesOut, checker.Not(checker.Contains), "busybox")
4fb88d2e
 }
 
dc944ea7
 func (s *DockerSuite) TestImagesOrderedByCreationDate(c *check.C) {
c10f6ef4
 	buildImageSuccessfully(c, "order:test_a", withDockerfile(`FROM busybox
                 MAINTAINER dockerio1`))
 	id1 := getIDByName(c, "order:test_a")
799d9605
 	time.Sleep(1 * time.Second)
c10f6ef4
 	buildImageSuccessfully(c, "order:test_c", withDockerfile(`FROM busybox
                 MAINTAINER dockerio2`))
 	id2 := getIDByName(c, "order:test_c")
799d9605
 	time.Sleep(1 * time.Second)
c10f6ef4
 	buildImageSuccessfully(c, "order:test_b", withDockerfile(`FROM busybox
                 MAINTAINER dockerio3`))
 	id3 := getIDByName(c, "order:test_b")
386d1ecc
 
668e2369
 	out, _ := dockerCmd(c, "images", "-q", "--no-trunc")
386d1ecc
 	imgs := strings.Split(out, "\n")
5814d67f
 	c.Assert(imgs[0], checker.Equals, id3, check.Commentf("First image must be %s, got %s", id3, imgs[0]))
 	c.Assert(imgs[1], checker.Equals, id2, check.Commentf("First image must be %s, got %s", id2, imgs[1]))
 	c.Assert(imgs[2], checker.Equals, id1, check.Commentf("First image must be %s, got %s", id1, imgs[2]))
386d1ecc
 }
4deac03c
 
dc944ea7
 func (s *DockerSuite) TestImagesErrorWithInvalidFilterNameTest(c *check.C) {
693ba98c
 	out, _, err := dockerCmdWithError("images", "-f", "FOO=123")
5814d67f
 	c.Assert(err, checker.NotNil)
 	c.Assert(out, checker.Contains, "Invalid filter")
4deac03c
 }
 
93d1dd80
 func (s *DockerSuite) TestImagesFilterLabelMatch(c *check.C) {
abb5e9a0
 	imageName1 := "images_filter_test1"
 	imageName2 := "images_filter_test2"
 	imageName3 := "images_filter_test3"
c10f6ef4
 	buildImageSuccessfully(c, imageName1, withDockerfile(`FROM busybox
                  LABEL match me`))
 	image1ID := getIDByName(c, imageName1)
abb5e9a0
 
c10f6ef4
 	buildImageSuccessfully(c, imageName2, withDockerfile(`FROM busybox
                  LABEL match="me too"`))
 	image2ID := getIDByName(c, imageName2)
abb5e9a0
 
c10f6ef4
 	buildImageSuccessfully(c, imageName3, withDockerfile(`FROM busybox
                  LABEL nomatch me`))
 	image3ID := getIDByName(c, imageName3)
abb5e9a0
 
668e2369
 	out, _ := dockerCmd(c, "images", "--no-trunc", "-q", "-f", "label=match")
abb5e9a0
 	out = strings.TrimSpace(out)
4352da78
 	c.Assert(out, check.Matches, fmt.Sprintf("[\\s\\w:]*%s[\\s\\w:]*", image1ID))
 	c.Assert(out, check.Matches, fmt.Sprintf("[\\s\\w:]*%s[\\s\\w:]*", image2ID))
 	c.Assert(out, check.Not(check.Matches), fmt.Sprintf("[\\s\\w:]*%s[\\s\\w:]*", image3ID))
abb5e9a0
 
668e2369
 	out, _ = dockerCmd(c, "images", "--no-trunc", "-q", "-f", "label=match=me too")
abb5e9a0
 	out = strings.TrimSpace(out)
c71a99af
 	c.Assert(out, check.Equals, image2ID)
 }
 
 // Regression : #15659
 func (s *DockerSuite) TestImagesFilterLabelWithCommit(c *check.C) {
 	// Create a container
 	dockerCmd(c, "run", "--name", "bar", "busybox", "/bin/sh")
 	// Commit with labels "using changes"
 	out, _ := dockerCmd(c, "commit", "-c", "LABEL foo.version=1.0.0-1", "-c", "LABEL foo.name=bar", "-c", "LABEL foo.author=starlord", "bar", "bar:1.0.0-1")
 	imageID := strings.TrimSpace(out)
 
 	out, _ = dockerCmd(c, "images", "--no-trunc", "-q", "-f", "label=foo.version=1.0.0-1")
 	out = strings.TrimSpace(out)
 	c.Assert(out, check.Equals, imageID)
abb5e9a0
 }
 
750e16f5
 func (s *DockerSuite) TestImagesFilterSinceAndBefore(c *check.C) {
c10f6ef4
 	buildImageSuccessfully(c, "image:1", withDockerfile(`FROM `+minimalBaseImage()+`
 LABEL number=1`))
 	imageID1 := getIDByName(c, "image:1")
 	buildImageSuccessfully(c, "image:2", withDockerfile(`FROM `+minimalBaseImage()+`
 LABEL number=2`))
 	imageID2 := getIDByName(c, "image:2")
 	buildImageSuccessfully(c, "image:3", withDockerfile(`FROM `+minimalBaseImage()+`
 LABEL number=3`))
 	imageID3 := getIDByName(c, "image:3")
750e16f5
 
 	expected := []string{imageID3, imageID2}
 
 	out, _ := dockerCmd(c, "images", "-f", "since=image:1", "image")
 	c.Assert(assertImageList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Image list is not in the correct order: %v\n%s", expected, out))
 
 	out, _ = dockerCmd(c, "images", "-f", "since="+imageID1, "image")
 	c.Assert(assertImageList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Image list is not in the correct order: %v\n%s", expected, out))
 
 	expected = []string{imageID3}
 
 	out, _ = dockerCmd(c, "images", "-f", "since=image:2", "image")
 	c.Assert(assertImageList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Image list is not in the correct order: %v\n%s", expected, out))
 
 	out, _ = dockerCmd(c, "images", "-f", "since="+imageID2, "image")
 	c.Assert(assertImageList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Image list is not in the correct order: %v\n%s", expected, out))
 
 	expected = []string{imageID2, imageID1}
 
 	out, _ = dockerCmd(c, "images", "-f", "before=image:3", "image")
 	c.Assert(assertImageList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter: Image list is not in the correct order: %v\n%s", expected, out))
 
 	out, _ = dockerCmd(c, "images", "-f", "before="+imageID3, "image")
 	c.Assert(assertImageList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter: Image list is not in the correct order: %v\n%s", expected, out))
 
 	expected = []string{imageID1}
 
 	out, _ = dockerCmd(c, "images", "-f", "before=image:2", "image")
 	c.Assert(assertImageList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter: Image list is not in the correct order: %v\n%s", expected, out))
 
 	out, _ = dockerCmd(c, "images", "-f", "before="+imageID2, "image")
 	c.Assert(assertImageList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter: Image list is not in the correct order: %v\n%s", expected, out))
 }
 
 func assertImageList(out string, expected []string) bool {
 	lines := strings.Split(strings.Trim(out, "\n "), "\n")
 
 	if len(lines)-1 != len(expected) {
 		return false
 	}
 
 	imageIDIndex := strings.Index(lines[0], "IMAGE ID")
 	for i := 0; i < len(expected); i++ {
 		imageID := lines[i+1][imageIDIndex : imageIDIndex+12]
 		found := false
 		for _, e := range expected {
 			if imageID == e[7:19] {
 				found = true
 				break
 			}
 		}
 		if !found {
 			return false
 		}
 	}
 
 	return true
 }
 
c10f6ef4
 // FIXME(vdemeester) should be a unit test on `docker image ls`
dc944ea7
 func (s *DockerSuite) TestImagesFilterSpaceTrimCase(c *check.C) {
4deac03c
 	imageName := "images_filter_test"
c10f6ef4
 	// Build a image and fail to build so that we have dangling images ?
 	buildImage(imageName, withDockerfile(`FROM busybox
34a3c3ca
                  RUN touch /test/foo
                  RUN touch /test/bar
c10f6ef4
                  RUN touch /test/baz`)).Assert(c, icmd.Expected{
 		ExitCode: 1,
 	})
4deac03c
 
 	filters := []string{
 		"dangling=true",
 		"Dangling=true",
 		" dangling=true",
 		"dangling=true ",
 		"dangling = true",
 	}
 
 	imageListings := make([][]string, 5, 5)
 	for idx, filter := range filters {
668e2369
 		out, _ := dockerCmd(c, "images", "-q", "-f", filter)
4deac03c
 		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 {
9b015bd4
 				fmt.Printf("out %d\n", idx)
4deac03c
 				for _, image := range errListing {
 					fmt.Print(image)
 				}
 				fmt.Print("")
 			}
dc944ea7
 			c.Fatalf("All output must be the same")
4deac03c
 		}
 	}
 }
c680dd9e
 
dc944ea7
 func (s *DockerSuite) TestImagesEnsureDanglingImageOnlyListedOnce(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
c680dd9e
 	// create container 1
668e2369
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
6b3c9281
 	containerID1 := strings.TrimSpace(out)
c680dd9e
 
 	// tag as foobox
6b3c9281
 	out, _ = dockerCmd(c, "commit", containerID1, "foobox")
 	imageID := stringid.TruncateID(strings.TrimSpace(out))
c680dd9e
 
 	// overwrite the tag, making the previous image dangling
4455ec14
 	dockerCmd(c, "tag", "busybox", "foobox")
c680dd9e
 
668e2369
 	out, _ = dockerCmd(c, "images", "-q", "-f", "dangling=true")
927b334e
 	// Expect one dangling image
5814d67f
 	c.Assert(strings.Count(out, imageID), checker.Equals, 1)
5ee69eb4
 
 	out, _ = dockerCmd(c, "images", "-q", "-f", "dangling=false")
 	//dangling=false would not include dangling images
 	c.Assert(out, checker.Not(checker.Contains), imageID)
 
 	out, _ = dockerCmd(c, "images")
 	//docker images still include dangling images
 	c.Assert(out, checker.Contains, imageID)
 
c680dd9e
 }
98c1a412
 
c10f6ef4
 // FIXME(vdemeester) should be a unit test for `docker image ls`
98c1a412
 func (s *DockerSuite) TestImagesWithIncorrectFilter(c *check.C) {
 	out, _, err := dockerCmdWithError("images", "-f", "dangling=invalid")
 	c.Assert(err, check.NotNil)
 	c.Assert(out, checker.Contains, "Invalid filter")
 }
56f5e345
 
 func (s *DockerSuite) TestImagesEnsureOnlyHeadsImagesShown(c *check.C) {
 	dockerfile := `
7ea707d5
         FROM busybox
56f5e345
         MAINTAINER docker
         ENV foo bar`
c10f6ef4
 	name := "scratch-image"
 	result := buildImage(name, withDockerfile(dockerfile))
 	result.Assert(c, icmd.Success)
 	id := getIDByName(c, name)
56f5e345
 
f6577be1
 	// this is just the output of docker build
 	// we're interested in getting the image id of the MAINTAINER instruction
 	// and that's located at output, line 5, from 7 to end
c10f6ef4
 	split := strings.Split(result.Combined(), "\n")
56f5e345
 	intermediate := strings.TrimSpace(split[5][7:])
 
c10f6ef4
 	out, _ := dockerCmd(c, "images")
5814d67f
 	// images shouldn't show non-heads images
 	c.Assert(out, checker.Not(checker.Contains), intermediate)
 	// images should contain final built images
c10f6ef4
 	c.Assert(out, checker.Contains, stringid.TruncateID(id))
56f5e345
 }
 
 func (s *DockerSuite) TestImagesEnsureImagesFromScratchShown(c *check.C) {
7ea707d5
 	testRequires(c, DaemonIsLinux) // Windows does not support FROM scratch
56f5e345
 	dockerfile := `
         FROM scratch
         MAINTAINER docker`
 
c10f6ef4
 	name := "scratch-image"
 	buildImageSuccessfully(c, name, withDockerfile(dockerfile))
 	id := getIDByName(c, name)
56f5e345
 
 	out, _ := dockerCmd(c, "images")
5814d67f
 	// images should contain images built from scratch
4352da78
 	c.Assert(out, checker.Contains, stringid.TruncateID(id))
56f5e345
 }
8819b530
 
7ea707d5
 // For W2W - equivalent to TestImagesEnsureImagesFromScratchShown but Windows
 // doesn't support from scratch
 func (s *DockerSuite) TestImagesEnsureImagesFromBusyboxShown(c *check.C) {
 	dockerfile := `
         FROM busybox
         MAINTAINER docker`
c10f6ef4
 	name := "busybox-image"
7ea707d5
 
c10f6ef4
 	buildImageSuccessfully(c, name, withDockerfile(dockerfile))
 	id := getIDByName(c, name)
7ea707d5
 
 	out, _ := dockerCmd(c, "images")
 	// images should contain images built from busybox
 	c.Assert(out, checker.Contains, stringid.TruncateID(id))
 }
 
8819b530
 // #18181
 func (s *DockerSuite) TestImagesFilterNameWithPort(c *check.C) {
 	tag := "a.b.c.d:5000/hello"
 	dockerCmd(c, "tag", "busybox", tag)
 	out, _ := dockerCmd(c, "images", tag)
 	c.Assert(out, checker.Contains, tag)
 
 	out, _ = dockerCmd(c, "images", tag+":latest")
 	c.Assert(out, checker.Contains, tag)
 
 	out, _ = dockerCmd(c, "images", tag+":no-such-tag")
 	c.Assert(out, checker.Not(checker.Contains), tag)
 }
34a3c3ca
 
 func (s *DockerSuite) TestImagesFormat(c *check.C) {
 	// testRequires(c, DaemonIsLinux)
 	tag := "myimage"
 	dockerCmd(c, "tag", "busybox", tag+":v1")
 	dockerCmd(c, "tag", "busybox", tag+":v2")
 
 	out, _ := dockerCmd(c, "images", "--format", "{{.Repository}}", tag)
 	lines := strings.Split(strings.TrimSpace(string(out)), "\n")
 
 	expected := []string{"myimage", "myimage"}
 	var names []string
64238fef
 	names = append(names, lines...)
3a127939
 	c.Assert(names, checker.DeepEquals, expected, check.Commentf("Expected array with truncated names: %v, got: %v", expected, names))
34a3c3ca
 }
 
 // ImagesDefaultFormatAndQuiet
 func (s *DockerSuite) TestImagesFormatDefaultFormat(c *check.C) {
 	testRequires(c, DaemonIsLinux)
 
 	// create container 1
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
 	containerID1 := strings.TrimSpace(out)
 
 	// tag as foobox
 	out, _ = dockerCmd(c, "commit", containerID1, "myimage")
 	imageID := stringid.TruncateID(strings.TrimSpace(out))
 
 	config := `{
 		"imagesFormat": "{{ .ID }} default"
 }`
 	d, err := ioutil.TempDir("", "integration-cli-")
 	c.Assert(err, checker.IsNil)
 	defer os.RemoveAll(d)
 
 	err = ioutil.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0644)
 	c.Assert(err, checker.IsNil)
 
 	out, _ = dockerCmd(c, "--config", d, "images", "-q", "myimage")
 	c.Assert(out, checker.Equals, imageID+"\n", check.Commentf("Expected to print only the image id, got %v\n", out))
 }