docker_cli_images_test.go 5.3 KB

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