github.com/boynux/docker@v1.11.0-rc4/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  	out, _ = dockerCmd(c, "images", "-q", "-f", "dangling=false")
   181  	//dangling=false would not include dangling images
   182  	c.Assert(out, checker.Not(checker.Contains), imageID)
   183  
   184  	out, _ = dockerCmd(c, "images")
   185  	//docker images still include dangling images
   186  	c.Assert(out, checker.Contains, imageID)
   187  
   188  }
   189  
   190  func (s *DockerSuite) TestImagesWithIncorrectFilter(c *check.C) {
   191  	out, _, err := dockerCmdWithError("images", "-f", "dangling=invalid")
   192  	c.Assert(err, check.NotNil)
   193  	c.Assert(out, checker.Contains, "Invalid filter")
   194  }
   195  
   196  func (s *DockerSuite) TestImagesEnsureOnlyHeadsImagesShown(c *check.C) {
   197  	testRequires(c, DaemonIsLinux)
   198  
   199  	dockerfile := `
   200          FROM scratch
   201          MAINTAINER docker
   202          ENV foo bar`
   203  
   204  	head, out, err := buildImageWithOut("scratch-image", dockerfile, false)
   205  	c.Assert(err, check.IsNil)
   206  
   207  	// this is just the output of docker build
   208  	// we're interested in getting the image id of the MAINTAINER instruction
   209  	// and that's located at output, line 5, from 7 to end
   210  	split := strings.Split(out, "\n")
   211  	intermediate := strings.TrimSpace(split[5][7:])
   212  
   213  	out, _ = dockerCmd(c, "images")
   214  	// images shouldn't show non-heads images
   215  	c.Assert(out, checker.Not(checker.Contains), intermediate)
   216  	// images should contain final built images
   217  	c.Assert(out, checker.Contains, stringid.TruncateID(head))
   218  }
   219  
   220  func (s *DockerSuite) TestImagesEnsureImagesFromScratchShown(c *check.C) {
   221  	testRequires(c, DaemonIsLinux)
   222  
   223  	dockerfile := `
   224          FROM scratch
   225          MAINTAINER docker`
   226  
   227  	id, _, err := buildImageWithOut("scratch-image", dockerfile, false)
   228  	c.Assert(err, check.IsNil)
   229  
   230  	out, _ := dockerCmd(c, "images")
   231  	// images should contain images built from scratch
   232  	c.Assert(out, checker.Contains, stringid.TruncateID(id))
   233  }
   234  
   235  // #18181
   236  func (s *DockerSuite) TestImagesFilterNameWithPort(c *check.C) {
   237  	tag := "a.b.c.d:5000/hello"
   238  	dockerCmd(c, "tag", "busybox", tag)
   239  	out, _ := dockerCmd(c, "images", tag)
   240  	c.Assert(out, checker.Contains, tag)
   241  
   242  	out, _ = dockerCmd(c, "images", tag+":latest")
   243  	c.Assert(out, checker.Contains, tag)
   244  
   245  	out, _ = dockerCmd(c, "images", tag+":no-such-tag")
   246  	c.Assert(out, checker.Not(checker.Contains), tag)
   247  }
   248  
   249  func (s *DockerSuite) TestImagesFormat(c *check.C) {
   250  	// testRequires(c, DaemonIsLinux)
   251  	tag := "myimage"
   252  	dockerCmd(c, "tag", "busybox", tag+":v1")
   253  	dockerCmd(c, "tag", "busybox", tag+":v2")
   254  
   255  	out, _ := dockerCmd(c, "images", "--format", "{{.Repository}}", tag)
   256  	lines := strings.Split(strings.TrimSpace(string(out)), "\n")
   257  
   258  	expected := []string{"myimage", "myimage"}
   259  	var names []string
   260  	for _, l := range lines {
   261  		names = append(names, l)
   262  	}
   263  	c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array with truncated names: %v, got: %v", expected, names))
   264  }
   265  
   266  // ImagesDefaultFormatAndQuiet
   267  func (s *DockerSuite) TestImagesFormatDefaultFormat(c *check.C) {
   268  	testRequires(c, DaemonIsLinux)
   269  
   270  	// create container 1
   271  	out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
   272  	containerID1 := strings.TrimSpace(out)
   273  
   274  	// tag as foobox
   275  	out, _ = dockerCmd(c, "commit", containerID1, "myimage")
   276  	imageID := stringid.TruncateID(strings.TrimSpace(out))
   277  
   278  	config := `{
   279  		"imagesFormat": "{{ .ID }} default"
   280  }`
   281  	d, err := ioutil.TempDir("", "integration-cli-")
   282  	c.Assert(err, checker.IsNil)
   283  	defer os.RemoveAll(d)
   284  
   285  	err = ioutil.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0644)
   286  	c.Assert(err, checker.IsNil)
   287  
   288  	out, _ = dockerCmd(c, "--config", d, "images", "-q", "myimage")
   289  	c.Assert(out, checker.Equals, imageID+"\n", check.Commentf("Expected to print only the image id, got %v\n", out))
   290  }