docker_cli_images_test.go 5.1 KB

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