docker_cli_images_test.go 14 KB

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