docker_cli_images_test.go 5.6 KB

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