docker_cli_images_test.go 4.9 KB

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