docker_cli_images_test.go 5.0 KB

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