docker_cli_images_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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/integration-cli/checker"
  12. "github.com/docker/docker/pkg/stringid"
  13. icmd "github.com/docker/docker/pkg/testutil/cmd"
  14. "github.com/go-check/check"
  15. )
  16. func (s *DockerSuite) TestImagesEnsureImageIsListed(c *check.C) {
  17. imagesOut, _ := dockerCmd(c, "images")
  18. c.Assert(imagesOut, checker.Contains, "busybox")
  19. }
  20. func (s *DockerSuite) TestImagesEnsureImageWithTagIsListed(c *check.C) {
  21. name := "imagewithtag"
  22. dockerCmd(c, "tag", "busybox", name+":v1")
  23. dockerCmd(c, "tag", "busybox", name+":v1v1")
  24. dockerCmd(c, "tag", "busybox", name+":v2")
  25. imagesOut, _ := dockerCmd(c, "images", name+":v1")
  26. c.Assert(imagesOut, checker.Contains, name)
  27. c.Assert(imagesOut, checker.Contains, "v1")
  28. c.Assert(imagesOut, checker.Not(checker.Contains), "v2")
  29. c.Assert(imagesOut, checker.Not(checker.Contains), "v1v1")
  30. imagesOut, _ = dockerCmd(c, "images", name)
  31. c.Assert(imagesOut, checker.Contains, name)
  32. c.Assert(imagesOut, checker.Contains, "v1")
  33. c.Assert(imagesOut, checker.Contains, "v1v1")
  34. c.Assert(imagesOut, checker.Contains, "v2")
  35. }
  36. func (s *DockerSuite) TestImagesEnsureImageWithBadTagIsNotListed(c *check.C) {
  37. imagesOut, _ := dockerCmd(c, "images", "busybox:nonexistent")
  38. c.Assert(imagesOut, checker.Not(checker.Contains), "busybox")
  39. }
  40. func (s *DockerSuite) TestImagesOrderedByCreationDate(c *check.C) {
  41. buildImageSuccessfully(c, "order:test_a", withDockerfile(`FROM busybox
  42. MAINTAINER dockerio1`))
  43. id1 := getIDByName(c, "order:test_a")
  44. time.Sleep(1 * time.Second)
  45. buildImageSuccessfully(c, "order:test_c", withDockerfile(`FROM busybox
  46. MAINTAINER dockerio2`))
  47. id2 := getIDByName(c, "order:test_c")
  48. time.Sleep(1 * time.Second)
  49. buildImageSuccessfully(c, "order:test_b", withDockerfile(`FROM busybox
  50. MAINTAINER dockerio3`))
  51. id3 := getIDByName(c, "order:test_b")
  52. out, _ := dockerCmd(c, "images", "-q", "--no-trunc")
  53. imgs := strings.Split(out, "\n")
  54. c.Assert(imgs[0], checker.Equals, id3, check.Commentf("First image must be %s, got %s", id3, imgs[0]))
  55. c.Assert(imgs[1], checker.Equals, id2, check.Commentf("First image must be %s, got %s", id2, imgs[1]))
  56. c.Assert(imgs[2], checker.Equals, id1, check.Commentf("First image must be %s, got %s", id1, imgs[2]))
  57. }
  58. func (s *DockerSuite) TestImagesErrorWithInvalidFilterNameTest(c *check.C) {
  59. out, _, err := dockerCmdWithError("images", "-f", "FOO=123")
  60. c.Assert(err, checker.NotNil)
  61. c.Assert(out, checker.Contains, "Invalid filter")
  62. }
  63. func (s *DockerSuite) TestImagesFilterLabelMatch(c *check.C) {
  64. imageName1 := "images_filter_test1"
  65. imageName2 := "images_filter_test2"
  66. imageName3 := "images_filter_test3"
  67. buildImageSuccessfully(c, imageName1, withDockerfile(`FROM busybox
  68. LABEL match me`))
  69. image1ID := getIDByName(c, imageName1)
  70. buildImageSuccessfully(c, imageName2, withDockerfile(`FROM busybox
  71. LABEL match="me too"`))
  72. image2ID := getIDByName(c, imageName2)
  73. buildImageSuccessfully(c, imageName3, withDockerfile(`FROM busybox
  74. LABEL nomatch me`))
  75. image3ID := getIDByName(c, imageName3)
  76. out, _ := dockerCmd(c, "images", "--no-trunc", "-q", "-f", "label=match")
  77. out = strings.TrimSpace(out)
  78. c.Assert(out, check.Matches, fmt.Sprintf("[\\s\\w:]*%s[\\s\\w:]*", image1ID))
  79. c.Assert(out, check.Matches, fmt.Sprintf("[\\s\\w:]*%s[\\s\\w:]*", image2ID))
  80. c.Assert(out, check.Not(check.Matches), fmt.Sprintf("[\\s\\w:]*%s[\\s\\w:]*", image3ID))
  81. out, _ = dockerCmd(c, "images", "--no-trunc", "-q", "-f", "label=match=me too")
  82. out = strings.TrimSpace(out)
  83. c.Assert(out, check.Equals, image2ID)
  84. }
  85. // Regression : #15659
  86. func (s *DockerSuite) TestImagesFilterLabelWithCommit(c *check.C) {
  87. // Create a container
  88. dockerCmd(c, "run", "--name", "bar", "busybox", "/bin/sh")
  89. // Commit with labels "using changes"
  90. 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")
  91. imageID := strings.TrimSpace(out)
  92. out, _ = dockerCmd(c, "images", "--no-trunc", "-q", "-f", "label=foo.version=1.0.0-1")
  93. out = strings.TrimSpace(out)
  94. c.Assert(out, check.Equals, imageID)
  95. }
  96. func (s *DockerSuite) TestImagesFilterSinceAndBefore(c *check.C) {
  97. buildImageSuccessfully(c, "image:1", withDockerfile(`FROM `+minimalBaseImage()+`
  98. LABEL number=1`))
  99. imageID1 := getIDByName(c, "image:1")
  100. buildImageSuccessfully(c, "image:2", withDockerfile(`FROM `+minimalBaseImage()+`
  101. LABEL number=2`))
  102. imageID2 := getIDByName(c, "image:2")
  103. buildImageSuccessfully(c, "image:3", withDockerfile(`FROM `+minimalBaseImage()+`
  104. LABEL number=3`))
  105. imageID3 := getIDByName(c, "image:3")
  106. expected := []string{imageID3, imageID2}
  107. out, _ := dockerCmd(c, "images", "-f", "since=image:1", "image")
  108. c.Assert(assertImageList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Image list is not in the correct order: %v\n%s", expected, out))
  109. out, _ = dockerCmd(c, "images", "-f", "since="+imageID1, "image")
  110. c.Assert(assertImageList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Image list is not in the correct order: %v\n%s", expected, out))
  111. expected = []string{imageID3}
  112. out, _ = dockerCmd(c, "images", "-f", "since=image:2", "image")
  113. c.Assert(assertImageList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Image list is not in the correct order: %v\n%s", expected, out))
  114. out, _ = dockerCmd(c, "images", "-f", "since="+imageID2, "image")
  115. c.Assert(assertImageList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Image list is not in the correct order: %v\n%s", expected, out))
  116. expected = []string{imageID2, imageID1}
  117. out, _ = dockerCmd(c, "images", "-f", "before=image:3", "image")
  118. c.Assert(assertImageList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter: Image list is not in the correct order: %v\n%s", expected, out))
  119. out, _ = dockerCmd(c, "images", "-f", "before="+imageID3, "image")
  120. c.Assert(assertImageList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter: Image list is not in the correct order: %v\n%s", expected, out))
  121. expected = []string{imageID1}
  122. out, _ = dockerCmd(c, "images", "-f", "before=image:2", "image")
  123. c.Assert(assertImageList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter: Image list is not in the correct order: %v\n%s", expected, out))
  124. out, _ = dockerCmd(c, "images", "-f", "before="+imageID2, "image")
  125. c.Assert(assertImageList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter: Image list is not in the correct order: %v\n%s", expected, out))
  126. }
  127. func assertImageList(out string, expected []string) bool {
  128. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  129. if len(lines)-1 != len(expected) {
  130. return false
  131. }
  132. imageIDIndex := strings.Index(lines[0], "IMAGE ID")
  133. for i := 0; i < len(expected); i++ {
  134. imageID := lines[i+1][imageIDIndex : imageIDIndex+12]
  135. found := false
  136. for _, e := range expected {
  137. if imageID == e[7:19] {
  138. found = true
  139. break
  140. }
  141. }
  142. if !found {
  143. return false
  144. }
  145. }
  146. return true
  147. }
  148. // FIXME(vdemeester) should be a unit test on `docker image ls`
  149. func (s *DockerSuite) TestImagesFilterSpaceTrimCase(c *check.C) {
  150. imageName := "images_filter_test"
  151. // Build a image and fail to build so that we have dangling images ?
  152. buildImage(imageName, withDockerfile(`FROM busybox
  153. RUN touch /test/foo
  154. RUN touch /test/bar
  155. RUN touch /test/baz`)).Assert(c, icmd.Expected{
  156. ExitCode: 1,
  157. })
  158. filters := []string{
  159. "dangling=true",
  160. "Dangling=true",
  161. " dangling=true",
  162. "dangling=true ",
  163. "dangling = true",
  164. }
  165. imageListings := make([][]string, 5, 5)
  166. for idx, filter := range filters {
  167. out, _ := dockerCmd(c, "images", "-q", "-f", filter)
  168. listing := strings.Split(out, "\n")
  169. sort.Strings(listing)
  170. imageListings[idx] = listing
  171. }
  172. for idx, listing := range imageListings {
  173. if idx < 4 && !reflect.DeepEqual(listing, imageListings[idx+1]) {
  174. for idx, errListing := range imageListings {
  175. fmt.Printf("out %d\n", idx)
  176. for _, image := range errListing {
  177. fmt.Print(image)
  178. }
  179. fmt.Print("")
  180. }
  181. c.Fatalf("All output must be the same")
  182. }
  183. }
  184. }
  185. func (s *DockerSuite) TestImagesEnsureDanglingImageOnlyListedOnce(c *check.C) {
  186. testRequires(c, DaemonIsLinux)
  187. // create container 1
  188. out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
  189. containerID1 := strings.TrimSpace(out)
  190. // tag as foobox
  191. out, _ = dockerCmd(c, "commit", containerID1, "foobox")
  192. imageID := stringid.TruncateID(strings.TrimSpace(out))
  193. // overwrite the tag, making the previous image dangling
  194. dockerCmd(c, "tag", "busybox", "foobox")
  195. out, _ = dockerCmd(c, "images", "-q", "-f", "dangling=true")
  196. // Expect one dangling image
  197. c.Assert(strings.Count(out, imageID), checker.Equals, 1)
  198. out, _ = dockerCmd(c, "images", "-q", "-f", "dangling=false")
  199. //dangling=false would not include dangling images
  200. c.Assert(out, checker.Not(checker.Contains), imageID)
  201. out, _ = dockerCmd(c, "images")
  202. //docker images still include dangling images
  203. c.Assert(out, checker.Contains, imageID)
  204. }
  205. // FIXME(vdemeester) should be a unit test for `docker image ls`
  206. func (s *DockerSuite) TestImagesWithIncorrectFilter(c *check.C) {
  207. out, _, err := dockerCmdWithError("images", "-f", "dangling=invalid")
  208. c.Assert(err, check.NotNil)
  209. c.Assert(out, checker.Contains, "Invalid filter")
  210. }
  211. func (s *DockerSuite) TestImagesEnsureOnlyHeadsImagesShown(c *check.C) {
  212. dockerfile := `
  213. FROM busybox
  214. MAINTAINER docker
  215. ENV foo bar`
  216. name := "scratch-image"
  217. result := buildImage(name, withDockerfile(dockerfile))
  218. result.Assert(c, icmd.Success)
  219. id := getIDByName(c, name)
  220. // this is just the output of docker build
  221. // we're interested in getting the image id of the MAINTAINER instruction
  222. // and that's located at output, line 5, from 7 to end
  223. split := strings.Split(result.Combined(), "\n")
  224. intermediate := strings.TrimSpace(split[5][7:])
  225. out, _ := dockerCmd(c, "images")
  226. // images shouldn't show non-heads images
  227. c.Assert(out, checker.Not(checker.Contains), intermediate)
  228. // images should contain final built images
  229. c.Assert(out, checker.Contains, stringid.TruncateID(id))
  230. }
  231. func (s *DockerSuite) TestImagesEnsureImagesFromScratchShown(c *check.C) {
  232. testRequires(c, DaemonIsLinux) // Windows does not support FROM scratch
  233. dockerfile := `
  234. FROM scratch
  235. MAINTAINER docker`
  236. name := "scratch-image"
  237. buildImageSuccessfully(c, name, withDockerfile(dockerfile))
  238. id := getIDByName(c, name)
  239. out, _ := dockerCmd(c, "images")
  240. // images should contain images built from scratch
  241. c.Assert(out, checker.Contains, stringid.TruncateID(id))
  242. }
  243. // For W2W - equivalent to TestImagesEnsureImagesFromScratchShown but Windows
  244. // doesn't support from scratch
  245. func (s *DockerSuite) TestImagesEnsureImagesFromBusyboxShown(c *check.C) {
  246. dockerfile := `
  247. FROM busybox
  248. MAINTAINER docker`
  249. name := "busybox-image"
  250. buildImageSuccessfully(c, name, withDockerfile(dockerfile))
  251. id := getIDByName(c, name)
  252. out, _ := dockerCmd(c, "images")
  253. // images should contain images built from busybox
  254. c.Assert(out, checker.Contains, stringid.TruncateID(id))
  255. }
  256. // #18181
  257. func (s *DockerSuite) TestImagesFilterNameWithPort(c *check.C) {
  258. tag := "a.b.c.d:5000/hello"
  259. dockerCmd(c, "tag", "busybox", tag)
  260. out, _ := dockerCmd(c, "images", tag)
  261. c.Assert(out, checker.Contains, tag)
  262. out, _ = dockerCmd(c, "images", tag+":latest")
  263. c.Assert(out, checker.Contains, tag)
  264. out, _ = dockerCmd(c, "images", tag+":no-such-tag")
  265. c.Assert(out, checker.Not(checker.Contains), tag)
  266. }
  267. func (s *DockerSuite) TestImagesFormat(c *check.C) {
  268. // testRequires(c, DaemonIsLinux)
  269. tag := "myimage"
  270. dockerCmd(c, "tag", "busybox", tag+":v1")
  271. dockerCmd(c, "tag", "busybox", tag+":v2")
  272. out, _ := dockerCmd(c, "images", "--format", "{{.Repository}}", tag)
  273. lines := strings.Split(strings.TrimSpace(string(out)), "\n")
  274. expected := []string{"myimage", "myimage"}
  275. var names []string
  276. names = append(names, lines...)
  277. c.Assert(names, checker.DeepEquals, expected, check.Commentf("Expected array with truncated names: %v, got: %v", expected, names))
  278. }
  279. // ImagesDefaultFormatAndQuiet
  280. func (s *DockerSuite) TestImagesFormatDefaultFormat(c *check.C) {
  281. testRequires(c, DaemonIsLinux)
  282. // create container 1
  283. out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
  284. containerID1 := strings.TrimSpace(out)
  285. // tag as foobox
  286. out, _ = dockerCmd(c, "commit", containerID1, "myimage")
  287. imageID := stringid.TruncateID(strings.TrimSpace(out))
  288. config := `{
  289. "imagesFormat": "{{ .ID }} default"
  290. }`
  291. d, err := ioutil.TempDir("", "integration-cli-")
  292. c.Assert(err, checker.IsNil)
  293. defer os.RemoveAll(d)
  294. err = ioutil.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0644)
  295. c.Assert(err, checker.IsNil)
  296. out, _ = dockerCmd(c, "--config", d, "images", "-q", "myimage")
  297. c.Assert(out, checker.Equals, imageID+"\n", check.Commentf("Expected to print only the image id, got %v\n", out))
  298. }