docker_cli_images_test.go 13 KB

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