docker_cli_images_test.go 13 KB

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