github.com/endocode/docker@v1.4.2-0.20160113120958-46eb4700391e/integration-cli/docker_cli_images_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"reflect"
     9  	"sort"
    10  	"strings"
    11  	"time"
    12  
    13  	"github.com/docker/docker/pkg/integration/checker"
    14  	"github.com/docker/docker/pkg/stringid"
    15  	"github.com/go-check/check"
    16  )
    17  
    18  func (s *DockerSuite) TestImagesEnsureImageIsListed(c *check.C) {
    19  	testRequires(c, DaemonIsLinux)
    20  	imagesOut, _ := dockerCmd(c, "images")
    21  	c.Assert(imagesOut, checker.Contains, "busybox")
    22  }
    23  
    24  func (s *DockerSuite) TestImagesEnsureImageWithTagIsListed(c *check.C) {
    25  	testRequires(c, DaemonIsLinux)
    26  
    27  	name := "imagewithtag"
    28  	dockerCmd(c, "tag", "busybox", name+":v1")
    29  	dockerCmd(c, "tag", "busybox", name+":v1v1")
    30  	dockerCmd(c, "tag", "busybox", name+":v2")
    31  
    32  	imagesOut, _ := dockerCmd(c, "images", name+":v1")
    33  	c.Assert(imagesOut, checker.Contains, name)
    34  	c.Assert(imagesOut, checker.Contains, "v1")
    35  	c.Assert(imagesOut, checker.Not(checker.Contains), "v2")
    36  	c.Assert(imagesOut, checker.Not(checker.Contains), "v1v1")
    37  
    38  	imagesOut, _ = dockerCmd(c, "images", name)
    39  	c.Assert(imagesOut, checker.Contains, name)
    40  	c.Assert(imagesOut, checker.Contains, "v1")
    41  	c.Assert(imagesOut, checker.Contains, "v1v1")
    42  	c.Assert(imagesOut, checker.Contains, "v2")
    43  }
    44  
    45  func (s *DockerSuite) TestImagesEnsureImageWithBadTagIsNotListed(c *check.C) {
    46  	imagesOut, _ := dockerCmd(c, "images", "busybox:nonexistent")
    47  	c.Assert(imagesOut, checker.Not(checker.Contains), "busybox")
    48  }
    49  
    50  func (s *DockerSuite) TestImagesOrderedByCreationDate(c *check.C) {
    51  	testRequires(c, DaemonIsLinux)
    52  	id1, err := buildImage("order:test_a",
    53  		`FROM scratch
    54                  MAINTAINER dockerio1`, true)
    55  	c.Assert(err, checker.IsNil)
    56  	time.Sleep(1 * time.Second)
    57  	id2, err := buildImage("order:test_c",
    58  		`FROM scratch
    59                  MAINTAINER dockerio2`, true)
    60  	c.Assert(err, checker.IsNil)
    61  	time.Sleep(1 * time.Second)
    62  	id3, err := buildImage("order:test_b",
    63  		`FROM scratch
    64                  MAINTAINER dockerio3`, true)
    65  	c.Assert(err, checker.IsNil)
    66  
    67  	out, _ := dockerCmd(c, "images", "-q", "--no-trunc")
    68  	imgs := strings.Split(out, "\n")
    69  	c.Assert(imgs[0], checker.Equals, id3, check.Commentf("First image must be %s, got %s", id3, imgs[0]))
    70  	c.Assert(imgs[1], checker.Equals, id2, check.Commentf("First image must be %s, got %s", id2, imgs[1]))
    71  	c.Assert(imgs[2], checker.Equals, id1, check.Commentf("First image must be %s, got %s", id1, imgs[2]))
    72  }
    73  
    74  func (s *DockerSuite) TestImagesErrorWithInvalidFilterNameTest(c *check.C) {
    75  	out, _, err := dockerCmdWithError("images", "-f", "FOO=123")
    76  	c.Assert(err, checker.NotNil)
    77  	c.Assert(out, checker.Contains, "Invalid filter")
    78  }
    79  
    80  func (s *DockerSuite) TestImagesFilterLabelMatch(c *check.C) {
    81  	testRequires(c, DaemonIsLinux)
    82  	imageName1 := "images_filter_test1"
    83  	imageName2 := "images_filter_test2"
    84  	imageName3 := "images_filter_test3"
    85  	image1ID, err := buildImage(imageName1,
    86  		`FROM scratch
    87                   LABEL match me`, true)
    88  	c.Assert(err, check.IsNil)
    89  
    90  	image2ID, err := buildImage(imageName2,
    91  		`FROM scratch
    92                   LABEL match="me too"`, true)
    93  	c.Assert(err, check.IsNil)
    94  
    95  	image3ID, err := buildImage(imageName3,
    96  		`FROM scratch
    97                   LABEL nomatch me`, true)
    98  	c.Assert(err, check.IsNil)
    99  
   100  	out, _ := dockerCmd(c, "images", "--no-trunc", "-q", "-f", "label=match")
   101  	out = strings.TrimSpace(out)
   102  	c.Assert(out, check.Matches, fmt.Sprintf("[\\s\\w:]*%s[\\s\\w:]*", image1ID))
   103  	c.Assert(out, check.Matches, fmt.Sprintf("[\\s\\w:]*%s[\\s\\w:]*", image2ID))
   104  	c.Assert(out, check.Not(check.Matches), fmt.Sprintf("[\\s\\w:]*%s[\\s\\w:]*", image3ID))
   105  
   106  	out, _ = dockerCmd(c, "images", "--no-trunc", "-q", "-f", "label=match=me too")
   107  	out = strings.TrimSpace(out)
   108  	c.Assert(out, check.Equals, image2ID)
   109  }
   110  
   111  // Regression : #15659
   112  func (s *DockerSuite) TestImagesFilterLabelWithCommit(c *check.C) {
   113  	// Create a container
   114  	dockerCmd(c, "run", "--name", "bar", "busybox", "/bin/sh")
   115  	// Commit with labels "using changes"
   116  	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")
   117  	imageID := strings.TrimSpace(out)
   118  
   119  	out, _ = dockerCmd(c, "images", "--no-trunc", "-q", "-f", "label=foo.version=1.0.0-1")
   120  	out = strings.TrimSpace(out)
   121  	c.Assert(out, check.Equals, imageID)
   122  }
   123  
   124  func (s *DockerSuite) TestImagesFilterSpaceTrimCase(c *check.C) {
   125  	testRequires(c, DaemonIsLinux)
   126  	imageName := "images_filter_test"
   127  	buildImage(imageName,
   128  		`FROM scratch
   129                   RUN touch /test/foo
   130                   RUN touch /test/bar
   131                   RUN touch /test/baz`, true)
   132  
   133  	filters := []string{
   134  		"dangling=true",
   135  		"Dangling=true",
   136  		" dangling=true",
   137  		"dangling=true ",
   138  		"dangling = true",
   139  	}
   140  
   141  	imageListings := make([][]string, 5, 5)
   142  	for idx, filter := range filters {
   143  		out, _ := dockerCmd(c, "images", "-q", "-f", filter)
   144  		listing := strings.Split(out, "\n")
   145  		sort.Strings(listing)
   146  		imageListings[idx] = listing
   147  	}
   148  
   149  	for idx, listing := range imageListings {
   150  		if idx < 4 && !reflect.DeepEqual(listing, imageListings[idx+1]) {
   151  			for idx, errListing := range imageListings {
   152  				fmt.Printf("out %d", idx)
   153  				for _, image := range errListing {
   154  					fmt.Print(image)
   155  				}
   156  				fmt.Print("")
   157  			}
   158  			c.Fatalf("All output must be the same")
   159  		}
   160  	}
   161  }
   162  
   163  func (s *DockerSuite) TestImagesEnsureDanglingImageOnlyListedOnce(c *check.C) {
   164  	testRequires(c, DaemonIsLinux)
   165  	// create container 1
   166  	out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
   167  	containerID1 := strings.TrimSpace(out)
   168  
   169  	// tag as foobox
   170  	out, _ = dockerCmd(c, "commit", containerID1, "foobox")
   171  	imageID := stringid.TruncateID(strings.TrimSpace(out))
   172  
   173  	// overwrite the tag, making the previous image dangling
   174  	dockerCmd(c, "tag", "-f", "busybox", "foobox")
   175  
   176  	out, _ = dockerCmd(c, "images", "-q", "-f", "dangling=true")
   177  	// Expect one dangling image
   178  	c.Assert(strings.Count(out, imageID), checker.Equals, 1)
   179  }
   180  
   181  func (s *DockerSuite) TestImagesWithIncorrectFilter(c *check.C) {
   182  	out, _, err := dockerCmdWithError("images", "-f", "dangling=invalid")
   183  	c.Assert(err, check.NotNil)
   184  	c.Assert(out, checker.Contains, "Invalid filter")
   185  }
   186  
   187  func (s *DockerSuite) TestImagesEnsureOnlyHeadsImagesShown(c *check.C) {
   188  	testRequires(c, DaemonIsLinux)
   189  
   190  	dockerfile := `
   191          FROM scratch
   192          MAINTAINER docker
   193          ENV foo bar`
   194  
   195  	head, out, err := buildImageWithOut("scratch-image", dockerfile, false)
   196  	c.Assert(err, check.IsNil)
   197  
   198  	// this is just the output of docker build
   199  	// we're interested in getting the image id of the MAINTAINER instruction
   200  	// and that's located at output, line 5, from 7 to end
   201  	split := strings.Split(out, "\n")
   202  	intermediate := strings.TrimSpace(split[5][7:])
   203  
   204  	out, _ = dockerCmd(c, "images")
   205  	// images shouldn't show non-heads images
   206  	c.Assert(out, checker.Not(checker.Contains), intermediate)
   207  	// images should contain final built images
   208  	c.Assert(out, checker.Contains, stringid.TruncateID(head))
   209  }
   210  
   211  func (s *DockerSuite) TestImagesEnsureImagesFromScratchShown(c *check.C) {
   212  	testRequires(c, DaemonIsLinux)
   213  
   214  	dockerfile := `
   215          FROM scratch
   216          MAINTAINER docker`
   217  
   218  	id, _, err := buildImageWithOut("scratch-image", dockerfile, false)
   219  	c.Assert(err, check.IsNil)
   220  
   221  	out, _ := dockerCmd(c, "images")
   222  	// images should contain images built from scratch
   223  	c.Assert(out, checker.Contains, stringid.TruncateID(id))
   224  }
   225  
   226  // #18181
   227  func (s *DockerSuite) TestImagesFilterNameWithPort(c *check.C) {
   228  	tag := "a.b.c.d:5000/hello"
   229  	dockerCmd(c, "tag", "busybox", tag)
   230  	out, _ := dockerCmd(c, "images", tag)
   231  	c.Assert(out, checker.Contains, tag)
   232  
   233  	out, _ = dockerCmd(c, "images", tag+":latest")
   234  	c.Assert(out, checker.Contains, tag)
   235  
   236  	out, _ = dockerCmd(c, "images", tag+":no-such-tag")
   237  	c.Assert(out, checker.Not(checker.Contains), tag)
   238  }
   239  
   240  func (s *DockerSuite) TestImagesFormat(c *check.C) {
   241  	// testRequires(c, DaemonIsLinux)
   242  	tag := "myimage"
   243  	dockerCmd(c, "tag", "busybox", tag+":v1")
   244  	dockerCmd(c, "tag", "busybox", tag+":v2")
   245  
   246  	out, _ := dockerCmd(c, "images", "--format", "{{.Repository}}", tag)
   247  	lines := strings.Split(strings.TrimSpace(string(out)), "\n")
   248  
   249  	expected := []string{"myimage", "myimage"}
   250  	var names []string
   251  	for _, l := range lines {
   252  		names = append(names, l)
   253  	}
   254  	c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array with truncated names: %v, got: %v", expected, names))
   255  }
   256  
   257  // ImagesDefaultFormatAndQuiet
   258  func (s *DockerSuite) TestImagesFormatDefaultFormat(c *check.C) {
   259  	testRequires(c, DaemonIsLinux)
   260  
   261  	// create container 1
   262  	out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
   263  	containerID1 := strings.TrimSpace(out)
   264  
   265  	// tag as foobox
   266  	out, _ = dockerCmd(c, "commit", containerID1, "myimage")
   267  	imageID := stringid.TruncateID(strings.TrimSpace(out))
   268  
   269  	config := `{
   270  		"imagesFormat": "{{ .ID }} default"
   271  }`
   272  	d, err := ioutil.TempDir("", "integration-cli-")
   273  	c.Assert(err, checker.IsNil)
   274  	defer os.RemoveAll(d)
   275  
   276  	err = ioutil.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0644)
   277  	c.Assert(err, checker.IsNil)
   278  
   279  	out, _ = dockerCmd(c, "--config", d, "images", "-q", "myimage")
   280  	c.Assert(out, checker.Equals, imageID+"\n", check.Commentf("Expected to print only the image id, got %v\n", out))
   281  }