github.com/tsuna/docker@v1.7.0-rc3/integration-cli/docker_cli_images_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os/exec"
     6  	"reflect"
     7  	"sort"
     8  	"strings"
     9  	"time"
    10  
    11  	"github.com/docker/docker/pkg/stringid"
    12  	"github.com/go-check/check"
    13  )
    14  
    15  func (s *DockerSuite) TestImagesEnsureImageIsListed(c *check.C) {
    16  	imagesCmd := exec.Command(dockerBinary, "images")
    17  	out, _, err := runCommandWithOutput(imagesCmd)
    18  	if err != nil {
    19  		c.Fatalf("listing images failed with errors: %s, %v", out, err)
    20  	}
    21  
    22  	if !strings.Contains(out, "busybox") {
    23  		c.Fatal("images should've listed busybox")
    24  	}
    25  
    26  }
    27  
    28  func (s *DockerSuite) TestImagesOrderedByCreationDate(c *check.C) {
    29  	id1, err := buildImage("order:test_a",
    30  		`FROM scratch
    31  		MAINTAINER dockerio1`, true)
    32  	if err != nil {
    33  		c.Fatal(err)
    34  	}
    35  	time.Sleep(time.Second)
    36  	id2, err := buildImage("order:test_c",
    37  		`FROM scratch
    38  		MAINTAINER dockerio2`, true)
    39  	if err != nil {
    40  		c.Fatal(err)
    41  	}
    42  	time.Sleep(time.Second)
    43  	id3, err := buildImage("order:test_b",
    44  		`FROM scratch
    45  		MAINTAINER dockerio3`, true)
    46  	if err != nil {
    47  		c.Fatal(err)
    48  	}
    49  
    50  	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "images", "-q", "--no-trunc"))
    51  	if err != nil {
    52  		c.Fatalf("listing images failed with errors: %s, %v", out, err)
    53  	}
    54  	imgs := strings.Split(out, "\n")
    55  	if imgs[0] != id3 {
    56  		c.Fatalf("First image must be %s, got %s", id3, imgs[0])
    57  	}
    58  	if imgs[1] != id2 {
    59  		c.Fatalf("Second image must be %s, got %s", id2, imgs[1])
    60  	}
    61  	if imgs[2] != id1 {
    62  		c.Fatalf("Third image must be %s, got %s", id1, imgs[2])
    63  	}
    64  
    65  }
    66  
    67  func (s *DockerSuite) TestImagesErrorWithInvalidFilterNameTest(c *check.C) {
    68  	imagesCmd := exec.Command(dockerBinary, "images", "-f", "FOO=123")
    69  	out, _, err := runCommandWithOutput(imagesCmd)
    70  	if !strings.Contains(out, "Invalid filter") {
    71  		c.Fatalf("error should occur when listing images with invalid filter name FOO, %s, %v", out, err)
    72  	}
    73  
    74  }
    75  
    76  func (s *DockerSuite) TestImagesFilterLabel(c *check.C) {
    77  	imageName1 := "images_filter_test1"
    78  	imageName2 := "images_filter_test2"
    79  	imageName3 := "images_filter_test3"
    80  	image1ID, err := buildImage(imageName1,
    81  		`FROM scratch
    82  		 LABEL match me`, true)
    83  	if err != nil {
    84  		c.Fatal(err)
    85  	}
    86  
    87  	image2ID, err := buildImage(imageName2,
    88  		`FROM scratch
    89  		 LABEL match="me too"`, true)
    90  	if err != nil {
    91  		c.Fatal(err)
    92  	}
    93  
    94  	image3ID, err := buildImage(imageName3,
    95  		`FROM scratch
    96  		 LABEL nomatch me`, true)
    97  	if err != nil {
    98  		c.Fatal(err)
    99  	}
   100  
   101  	cmd := exec.Command(dockerBinary, "images", "--no-trunc", "-q", "-f", "label=match")
   102  	out, _, err := runCommandWithOutput(cmd)
   103  	if err != nil {
   104  		c.Fatal(out, err)
   105  	}
   106  	out = strings.TrimSpace(out)
   107  
   108  	if (!strings.Contains(out, image1ID) && !strings.Contains(out, image2ID)) || strings.Contains(out, image3ID) {
   109  		c.Fatalf("Expected ids %s,%s got %s", image1ID, image2ID, out)
   110  	}
   111  
   112  	cmd = exec.Command(dockerBinary, "images", "--no-trunc", "-q", "-f", "label=match=me too")
   113  	out, _, err = runCommandWithOutput(cmd)
   114  	if err != nil {
   115  		c.Fatal(out, err)
   116  	}
   117  	out = strings.TrimSpace(out)
   118  
   119  	if out != image2ID {
   120  		c.Fatalf("Expected %s got %s", image2ID, out)
   121  	}
   122  
   123  }
   124  
   125  func (s *DockerSuite) TestImagesFilterSpaceTrimCase(c *check.C) {
   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  		cmd := exec.Command(dockerBinary, "images", "-q", "-f", filter)
   144  		out, _, err := runCommandWithOutput(cmd)
   145  		if err != nil {
   146  			c.Fatal(err)
   147  		}
   148  		listing := strings.Split(out, "\n")
   149  		sort.Strings(listing)
   150  		imageListings[idx] = listing
   151  	}
   152  
   153  	for idx, listing := range imageListings {
   154  		if idx < 4 && !reflect.DeepEqual(listing, imageListings[idx+1]) {
   155  			for idx, errListing := range imageListings {
   156  				fmt.Printf("out %d", idx)
   157  				for _, image := range errListing {
   158  					fmt.Print(image)
   159  				}
   160  				fmt.Print("")
   161  			}
   162  			c.Fatalf("All output must be the same")
   163  		}
   164  	}
   165  
   166  }
   167  
   168  func (s *DockerSuite) TestImagesEnsureDanglingImageOnlyListedOnce(c *check.C) {
   169  
   170  	// create container 1
   171  	cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
   172  	out, _, err := runCommandWithOutput(cmd)
   173  	if err != nil {
   174  		c.Fatalf("error running busybox: %s, %v", out, err)
   175  	}
   176  	containerId1 := strings.TrimSpace(out)
   177  
   178  	// tag as foobox
   179  	cmd = exec.Command(dockerBinary, "commit", containerId1, "foobox")
   180  	out, _, err = runCommandWithOutput(cmd)
   181  	if err != nil {
   182  		c.Fatalf("error tagging foobox: %s", err)
   183  	}
   184  	imageId := stringid.TruncateID(strings.TrimSpace(out))
   185  
   186  	// overwrite the tag, making the previous image dangling
   187  	cmd = exec.Command(dockerBinary, "tag", "-f", "busybox", "foobox")
   188  	out, _, err = runCommandWithOutput(cmd)
   189  	if err != nil {
   190  		c.Fatalf("error tagging foobox: %s", err)
   191  	}
   192  
   193  	cmd = exec.Command(dockerBinary, "images", "-q", "-f", "dangling=true")
   194  	out, _, err = runCommandWithOutput(cmd)
   195  	if err != nil {
   196  		c.Fatalf("listing images failed with errors: %s, %v", out, err)
   197  	}
   198  
   199  	if e, a := 1, strings.Count(out, imageId); e != a {
   200  		c.Fatalf("expected 1 dangling image, got %d: %s", a, out)
   201  	}
   202  
   203  }