docker_cli_images_test.go 12 KB

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