docker_cli_images_test.go 5.9 KB

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