github.com/goern/docker@v1.9.0-rc1/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/stringid"
    11  	"github.com/go-check/check"
    12  )
    13  
    14  func (s *DockerSuite) TestImagesEnsureImageIsListed(c *check.C) {
    15  	testRequires(c, DaemonIsLinux)
    16  	out, _ := dockerCmd(c, "images")
    17  	if !strings.Contains(out, "busybox") {
    18  		c.Fatal("images should've listed busybox")
    19  	}
    20  }
    21  
    22  func (s *DockerSuite) TestImagesEnsureImageWithTagIsListed(c *check.C) {
    23  	testRequires(c, DaemonIsLinux)
    24  	_, err := buildImage("imagewithtag:v1",
    25  		`FROM scratch
    26  		MAINTAINER dockerio1`, true)
    27  	c.Assert(err, check.IsNil)
    28  
    29  	_, err = buildImage("imagewithtag:v2",
    30  		`FROM scratch
    31  		MAINTAINER dockerio1`, true)
    32  	c.Assert(err, check.IsNil)
    33  
    34  	out, _ := dockerCmd(c, "images", "imagewithtag:v1")
    35  
    36  	if !strings.Contains(out, "imagewithtag") || !strings.Contains(out, "v1") || strings.Contains(out, "v2") {
    37  		c.Fatal("images should've listed imagewithtag:v1 and not imagewithtag:v2")
    38  	}
    39  
    40  	out, _ = dockerCmd(c, "images", "imagewithtag")
    41  
    42  	if !strings.Contains(out, "imagewithtag") || !strings.Contains(out, "v1") || !strings.Contains(out, "v2") {
    43  		c.Fatal("images should've listed imagewithtag:v1 and imagewithtag:v2")
    44  	}
    45  }
    46  
    47  func (s *DockerSuite) TestImagesEnsureImageWithBadTagIsNotListed(c *check.C) {
    48  	out, _ := dockerCmd(c, "images", "busybox:nonexistent")
    49  
    50  	if strings.Contains(out, "busybox") {
    51  		c.Fatal("images should not have listed busybox")
    52  	}
    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  }