docker_cli_images_test.go 13 KB

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