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