github.com/damirazo/docker@v1.9.0/integration-cli/docker_cli_images_test.go (about)

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