docker_cli_images_test.go 13 KB

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