github.com/rsampaio/docker@v0.7.2-0.20150827203920-fdc73cc3fc31/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  	out, _ := dockerCmd(c, "images")
    16  	if !strings.Contains(out, "busybox") {
    17  		c.Fatal("images should've listed busybox")
    18  	}
    19  }
    20  
    21  func (s *DockerSuite) TestImagesEnsureImageWithTagIsListed(c *check.C) {
    22  	_, err := buildImage("imagewithtag:v1",
    23  		`FROM scratch
    24  		MAINTAINER dockerio1`, true)
    25  	c.Assert(err, check.IsNil)
    26  
    27  	_, err = buildImage("imagewithtag:v2",
    28  		`FROM scratch
    29  		MAINTAINER dockerio1`, true)
    30  	c.Assert(err, check.IsNil)
    31  
    32  	out, _ := dockerCmd(c, "images", "imagewithtag:v1")
    33  
    34  	if !strings.Contains(out, "imagewithtag") || !strings.Contains(out, "v1") || strings.Contains(out, "v2") {
    35  		c.Fatal("images should've listed imagewithtag:v1 and not imagewithtag:v2")
    36  	}
    37  
    38  	out, _ = dockerCmd(c, "images", "imagewithtag")
    39  
    40  	if !strings.Contains(out, "imagewithtag") || !strings.Contains(out, "v1") || !strings.Contains(out, "v2") {
    41  		c.Fatal("images should've listed imagewithtag:v1 and imagewithtag:v2")
    42  	}
    43  }
    44  
    45  func (s *DockerSuite) TestImagesEnsureImageWithBadTagIsNotListed(c *check.C) {
    46  	out, _ := dockerCmd(c, "images", "busybox:nonexistent")
    47  
    48  	if strings.Contains(out, "busybox") {
    49  		c.Fatal("images should not have listed busybox")
    50  	}
    51  
    52  }
    53  
    54  func (s *DockerSuite) TestImagesOrderedByCreationDate(c *check.C) {
    55  	id1, err := buildImage("order:test_a",
    56  		`FROM scratch
    57  		MAINTAINER dockerio1`, true)
    58  	if err != nil {
    59  		c.Fatal(err)
    60  	}
    61  	time.Sleep(1 * time.Second)
    62  	id2, err := buildImage("order:test_c",
    63  		`FROM scratch
    64  		MAINTAINER dockerio2`, true)
    65  	if err != nil {
    66  		c.Fatal(err)
    67  	}
    68  	time.Sleep(1 * time.Second)
    69  	id3, err := buildImage("order:test_b",
    70  		`FROM scratch
    71  		MAINTAINER dockerio3`, true)
    72  	if err != nil {
    73  		c.Fatal(err)
    74  	}
    75  
    76  	out, _ := dockerCmd(c, "images", "-q", "--no-trunc")
    77  	imgs := strings.Split(out, "\n")
    78  	if imgs[0] != id3 {
    79  		c.Fatalf("First image must be %s, got %s", id3, imgs[0])
    80  	}
    81  	if imgs[1] != id2 {
    82  		c.Fatalf("Second image must be %s, got %s", id2, imgs[1])
    83  	}
    84  	if imgs[2] != id1 {
    85  		c.Fatalf("Third image must be %s, got %s", id1, imgs[2])
    86  	}
    87  }
    88  
    89  func (s *DockerSuite) TestImagesErrorWithInvalidFilterNameTest(c *check.C) {
    90  	out, _, err := dockerCmdWithError("images", "-f", "FOO=123")
    91  	if err == nil || !strings.Contains(out, "Invalid filter") {
    92  		c.Fatalf("error should occur when listing images with invalid filter name FOO, %s", out)
    93  	}
    94  }
    95  
    96  func (s *DockerSuite) TestImagesFilterLabel(c *check.C) {
    97  	imageName1 := "images_filter_test1"
    98  	imageName2 := "images_filter_test2"
    99  	imageName3 := "images_filter_test3"
   100  	image1ID, err := buildImage(imageName1,
   101  		`FROM scratch
   102  		 LABEL match me`, true)
   103  	if err != nil {
   104  		c.Fatal(err)
   105  	}
   106  
   107  	image2ID, err := buildImage(imageName2,
   108  		`FROM scratch
   109  		 LABEL match="me too"`, true)
   110  	if err != nil {
   111  		c.Fatal(err)
   112  	}
   113  
   114  	image3ID, err := buildImage(imageName3,
   115  		`FROM scratch
   116  		 LABEL nomatch me`, true)
   117  	if err != nil {
   118  		c.Fatal(err)
   119  	}
   120  
   121  	out, _ := dockerCmd(c, "images", "--no-trunc", "-q", "-f", "label=match")
   122  	out = strings.TrimSpace(out)
   123  	if (!strings.Contains(out, image1ID) && !strings.Contains(out, image2ID)) || strings.Contains(out, image3ID) {
   124  		c.Fatalf("Expected ids %s,%s got %s", image1ID, image2ID, out)
   125  	}
   126  
   127  	out, _ = dockerCmd(c, "images", "--no-trunc", "-q", "-f", "label=match=me too")
   128  	out = strings.TrimSpace(out)
   129  	if out != image2ID {
   130  		c.Fatalf("Expected %s got %s", image2ID, out)
   131  	}
   132  }
   133  
   134  func (s *DockerSuite) TestImagesFilterSpaceTrimCase(c *check.C) {
   135  	imageName := "images_filter_test"
   136  	buildImage(imageName,
   137  		`FROM scratch
   138  		 RUN touch /test/foo
   139  		 RUN touch /test/bar
   140  		 RUN touch /test/baz`, true)
   141  
   142  	filters := []string{
   143  		"dangling=true",
   144  		"Dangling=true",
   145  		" dangling=true",
   146  		"dangling=true ",
   147  		"dangling = true",
   148  	}
   149  
   150  	imageListings := make([][]string, 5, 5)
   151  	for idx, filter := range filters {
   152  		out, _ := dockerCmd(c, "images", "-q", "-f", filter)
   153  		listing := strings.Split(out, "\n")
   154  		sort.Strings(listing)
   155  		imageListings[idx] = listing
   156  	}
   157  
   158  	for idx, listing := range imageListings {
   159  		if idx < 4 && !reflect.DeepEqual(listing, imageListings[idx+1]) {
   160  			for idx, errListing := range imageListings {
   161  				fmt.Printf("out %d", idx)
   162  				for _, image := range errListing {
   163  					fmt.Print(image)
   164  				}
   165  				fmt.Print("")
   166  			}
   167  			c.Fatalf("All output must be the same")
   168  		}
   169  	}
   170  }
   171  
   172  func (s *DockerSuite) TestImagesEnsureDanglingImageOnlyListedOnce(c *check.C) {
   173  	// create container 1
   174  	out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
   175  	containerID1 := strings.TrimSpace(out)
   176  
   177  	// tag as foobox
   178  	out, _ = dockerCmd(c, "commit", containerID1, "foobox")
   179  	imageID := stringid.TruncateID(strings.TrimSpace(out))
   180  
   181  	// overwrite the tag, making the previous image dangling
   182  	dockerCmd(c, "tag", "-f", "busybox", "foobox")
   183  
   184  	out, _ = dockerCmd(c, "images", "-q", "-f", "dangling=true")
   185  	if e, a := 1, strings.Count(out, imageID); e != a {
   186  		c.Fatalf("expected 1 dangling image, got %d: %s", a, out)
   187  	}
   188  }