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