docker_cli_images_test.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "reflect"
  8. "sort"
  9. "strings"
  10. "time"
  11. "github.com/docker/docker/pkg/integration/checker"
  12. "github.com/docker/docker/pkg/stringid"
  13. "github.com/go-check/check"
  14. )
  15. func (s *DockerSuite) TestImagesEnsureImageIsListed(c *check.C) {
  16. testRequires(c, DaemonIsLinux)
  17. imagesOut, _ := dockerCmd(c, "images")
  18. c.Assert(imagesOut, checker.Contains, "busybox")
  19. }
  20. func (s *DockerSuite) TestImagesEnsureImageWithTagIsListed(c *check.C) {
  21. testRequires(c, DaemonIsLinux)
  22. name := "imagewithtag"
  23. dockerCmd(c, "tag", "busybox", name+":v1")
  24. dockerCmd(c, "tag", "busybox", name+":v1v1")
  25. dockerCmd(c, "tag", "busybox", name+":v2")
  26. imagesOut, _ := dockerCmd(c, "images", name+":v1")
  27. c.Assert(imagesOut, checker.Contains, name)
  28. c.Assert(imagesOut, checker.Contains, "v1")
  29. c.Assert(imagesOut, checker.Not(checker.Contains), "v2")
  30. c.Assert(imagesOut, checker.Not(checker.Contains), "v1v1")
  31. imagesOut, _ = dockerCmd(c, "images", name)
  32. c.Assert(imagesOut, checker.Contains, name)
  33. c.Assert(imagesOut, checker.Contains, "v1")
  34. c.Assert(imagesOut, checker.Contains, "v1v1")
  35. c.Assert(imagesOut, checker.Contains, "v2")
  36. }
  37. func (s *DockerSuite) TestImagesEnsureImageWithBadTagIsNotListed(c *check.C) {
  38. imagesOut, _ := dockerCmd(c, "images", "busybox:nonexistent")
  39. c.Assert(imagesOut, checker.Not(checker.Contains), "busybox")
  40. }
  41. func (s *DockerSuite) TestImagesOrderedByCreationDate(c *check.C) {
  42. testRequires(c, DaemonIsLinux)
  43. id1, err := buildImage("order:test_a",
  44. `FROM scratch
  45. MAINTAINER dockerio1`, true)
  46. c.Assert(err, checker.IsNil)
  47. time.Sleep(1 * time.Second)
  48. id2, err := buildImage("order:test_c",
  49. `FROM scratch
  50. MAINTAINER dockerio2`, true)
  51. c.Assert(err, checker.IsNil)
  52. time.Sleep(1 * time.Second)
  53. id3, err := buildImage("order:test_b",
  54. `FROM scratch
  55. MAINTAINER dockerio3`, true)
  56. c.Assert(err, checker.IsNil)
  57. out, _ := dockerCmd(c, "images", "-q", "--no-trunc")
  58. imgs := strings.Split(out, "\n")
  59. c.Assert(imgs[0], checker.Equals, id3, check.Commentf("First image must be %s, got %s", id3, imgs[0]))
  60. c.Assert(imgs[1], checker.Equals, id2, check.Commentf("First image must be %s, got %s", id2, imgs[1]))
  61. c.Assert(imgs[2], checker.Equals, id1, check.Commentf("First image must be %s, got %s", id1, imgs[2]))
  62. }
  63. func (s *DockerSuite) TestImagesErrorWithInvalidFilterNameTest(c *check.C) {
  64. out, _, err := dockerCmdWithError("images", "-f", "FOO=123")
  65. c.Assert(err, checker.NotNil)
  66. c.Assert(out, checker.Contains, "Invalid filter")
  67. }
  68. func (s *DockerSuite) TestImagesFilterLabelMatch(c *check.C) {
  69. testRequires(c, DaemonIsLinux)
  70. imageName1 := "images_filter_test1"
  71. imageName2 := "images_filter_test2"
  72. imageName3 := "images_filter_test3"
  73. image1ID, err := buildImage(imageName1,
  74. `FROM scratch
  75. LABEL match me`, true)
  76. c.Assert(err, check.IsNil)
  77. image2ID, err := buildImage(imageName2,
  78. `FROM scratch
  79. LABEL match="me too"`, true)
  80. c.Assert(err, check.IsNil)
  81. image3ID, err := buildImage(imageName3,
  82. `FROM scratch
  83. LABEL nomatch me`, true)
  84. c.Assert(err, check.IsNil)
  85. out, _ := dockerCmd(c, "images", "--no-trunc", "-q", "-f", "label=match")
  86. out = strings.TrimSpace(out)
  87. c.Assert(out, check.Matches, fmt.Sprintf("[\\s\\w:]*%s[\\s\\w:]*", image1ID))
  88. c.Assert(out, check.Matches, fmt.Sprintf("[\\s\\w:]*%s[\\s\\w:]*", image2ID))
  89. c.Assert(out, check.Not(check.Matches), fmt.Sprintf("[\\s\\w:]*%s[\\s\\w:]*", image3ID))
  90. out, _ = dockerCmd(c, "images", "--no-trunc", "-q", "-f", "label=match=me too")
  91. out = strings.TrimSpace(out)
  92. c.Assert(out, check.Equals, image2ID)
  93. }
  94. // Regression : #15659
  95. func (s *DockerSuite) TestImagesFilterLabelWithCommit(c *check.C) {
  96. // Create a container
  97. dockerCmd(c, "run", "--name", "bar", "busybox", "/bin/sh")
  98. // Commit with labels "using changes"
  99. 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")
  100. imageID := strings.TrimSpace(out)
  101. out, _ = dockerCmd(c, "images", "--no-trunc", "-q", "-f", "label=foo.version=1.0.0-1")
  102. out = strings.TrimSpace(out)
  103. c.Assert(out, check.Equals, imageID)
  104. }
  105. func (s *DockerSuite) TestImagesFilterSpaceTrimCase(c *check.C) {
  106. testRequires(c, DaemonIsLinux)
  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. out, _ := dockerCmd(c, "images", "-q", "-f", filter)
  123. listing := strings.Split(out, "\n")
  124. sort.Strings(listing)
  125. imageListings[idx] = listing
  126. }
  127. for idx, listing := range imageListings {
  128. if idx < 4 && !reflect.DeepEqual(listing, imageListings[idx+1]) {
  129. for idx, errListing := range imageListings {
  130. fmt.Printf("out %d", idx)
  131. for _, image := range errListing {
  132. fmt.Print(image)
  133. }
  134. fmt.Print("")
  135. }
  136. c.Fatalf("All output must be the same")
  137. }
  138. }
  139. }
  140. func (s *DockerSuite) TestImagesEnsureDanglingImageOnlyListedOnce(c *check.C) {
  141. testRequires(c, DaemonIsLinux)
  142. // create container 1
  143. out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
  144. containerID1 := strings.TrimSpace(out)
  145. // tag as foobox
  146. out, _ = dockerCmd(c, "commit", containerID1, "foobox")
  147. imageID := stringid.TruncateID(strings.TrimSpace(out))
  148. // overwrite the tag, making the previous image dangling
  149. dockerCmd(c, "tag", "-f", "busybox", "foobox")
  150. out, _ = dockerCmd(c, "images", "-q", "-f", "dangling=true")
  151. // Expect one dangling image
  152. c.Assert(strings.Count(out, imageID), checker.Equals, 1)
  153. out, _ = dockerCmd(c, "images", "-q", "-f", "dangling=false")
  154. //dangling=false would not include dangling images
  155. c.Assert(out, checker.Not(checker.Contains), imageID)
  156. out, _ = dockerCmd(c, "images")
  157. //docker images still include dangling images
  158. c.Assert(out, checker.Contains, imageID)
  159. }
  160. func (s *DockerSuite) TestImagesWithIncorrectFilter(c *check.C) {
  161. out, _, err := dockerCmdWithError("images", "-f", "dangling=invalid")
  162. c.Assert(err, check.NotNil)
  163. c.Assert(out, checker.Contains, "Invalid filter")
  164. }
  165. func (s *DockerSuite) TestImagesEnsureOnlyHeadsImagesShown(c *check.C) {
  166. testRequires(c, DaemonIsLinux)
  167. dockerfile := `
  168. FROM scratch
  169. MAINTAINER docker
  170. ENV foo bar`
  171. head, out, err := buildImageWithOut("scratch-image", dockerfile, false)
  172. c.Assert(err, check.IsNil)
  173. // this is just the output of docker build
  174. // we're interested in getting the image id of the MAINTAINER instruction
  175. // and that's located at output, line 5, from 7 to end
  176. split := strings.Split(out, "\n")
  177. intermediate := strings.TrimSpace(split[5][7:])
  178. out, _ = dockerCmd(c, "images")
  179. // images shouldn't show non-heads images
  180. c.Assert(out, checker.Not(checker.Contains), intermediate)
  181. // images should contain final built images
  182. c.Assert(out, checker.Contains, stringid.TruncateID(head))
  183. }
  184. func (s *DockerSuite) TestImagesEnsureImagesFromScratchShown(c *check.C) {
  185. testRequires(c, DaemonIsLinux)
  186. dockerfile := `
  187. FROM scratch
  188. MAINTAINER docker`
  189. id, _, err := buildImageWithOut("scratch-image", dockerfile, false)
  190. c.Assert(err, check.IsNil)
  191. out, _ := dockerCmd(c, "images")
  192. // images should contain images built from scratch
  193. c.Assert(out, checker.Contains, stringid.TruncateID(id))
  194. }
  195. // #18181
  196. func (s *DockerSuite) TestImagesFilterNameWithPort(c *check.C) {
  197. tag := "a.b.c.d:5000/hello"
  198. dockerCmd(c, "tag", "busybox", tag)
  199. out, _ := dockerCmd(c, "images", tag)
  200. c.Assert(out, checker.Contains, tag)
  201. out, _ = dockerCmd(c, "images", tag+":latest")
  202. c.Assert(out, checker.Contains, tag)
  203. out, _ = dockerCmd(c, "images", tag+":no-such-tag")
  204. c.Assert(out, checker.Not(checker.Contains), tag)
  205. }
  206. func (s *DockerSuite) TestImagesFormat(c *check.C) {
  207. // testRequires(c, DaemonIsLinux)
  208. tag := "myimage"
  209. dockerCmd(c, "tag", "busybox", tag+":v1")
  210. dockerCmd(c, "tag", "busybox", tag+":v2")
  211. out, _ := dockerCmd(c, "images", "--format", "{{.Repository}}", tag)
  212. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  213. expected := []string{"myimage", "myimage"}
  214. var names []string
  215. for _, l := range lines {
  216. names = append(names, l)
  217. }
  218. c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array with truncated names: %v, got: %v", expected, names))
  219. }
  220. // ImagesDefaultFormatAndQuiet
  221. func (s *DockerSuite) TestImagesFormatDefaultFormat(c *check.C) {
  222. testRequires(c, DaemonIsLinux)
  223. // create container 1
  224. out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
  225. containerID1 := strings.TrimSpace(out)
  226. // tag as foobox
  227. out, _ = dockerCmd(c, "commit", containerID1, "myimage")
  228. imageID := stringid.TruncateID(strings.TrimSpace(out))
  229. config := `{
  230. "imagesFormat": "{{ .ID }} default"
  231. }`
  232. d, err := ioutil.TempDir("", "integration-cli-")
  233. c.Assert(err, checker.IsNil)
  234. defer os.RemoveAll(d)
  235. err = ioutil.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0644)
  236. c.Assert(err, checker.IsNil)
  237. out, _ = dockerCmd(c, "--config", d, "images", "-q", "myimage")
  238. c.Assert(out, checker.Equals, imageID+"\n", check.Commentf("Expected to print only the image id, got %v\n", out))
  239. }