docker_cli_images_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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/checker"
  13. "github.com/docker/docker/integration-cli/cli/build"
  14. "github.com/docker/docker/pkg/stringid"
  15. "github.com/go-check/check"
  16. "gotest.tools/assert"
  17. is "gotest.tools/assert/cmp"
  18. "gotest.tools/icmd"
  19. )
  20. func (s *DockerSuite) TestImagesEnsureImageIsListed(c *testing.T) {
  21. imagesOut, _ := dockerCmd(c, "images")
  22. assert.Assert(c, imagesOut, checker.Contains, "busybox")
  23. }
  24. func (s *DockerSuite) TestImagesEnsureImageWithTagIsListed(c *testing.T) {
  25. name := "imagewithtag"
  26. dockerCmd(c, "tag", "busybox", name+":v1")
  27. dockerCmd(c, "tag", "busybox", name+":v1v1")
  28. dockerCmd(c, "tag", "busybox", name+":v2")
  29. imagesOut, _ := dockerCmd(c, "images", name+":v1")
  30. assert.Assert(c, imagesOut, checker.Contains, name)
  31. assert.Assert(c, imagesOut, checker.Contains, "v1")
  32. assert.Assert(c, !strings.Contains(imagesOut, "v2"))
  33. assert.Assert(c, !strings.Contains(imagesOut, "v1v1"))
  34. imagesOut, _ = dockerCmd(c, "images", name)
  35. assert.Assert(c, imagesOut, checker.Contains, name)
  36. assert.Assert(c, imagesOut, checker.Contains, "v1")
  37. assert.Assert(c, imagesOut, checker.Contains, "v1v1")
  38. assert.Assert(c, imagesOut, checker.Contains, "v2")
  39. }
  40. func (s *DockerSuite) TestImagesEnsureImageWithBadTagIsNotListed(c *testing.T) {
  41. imagesOut, _ := dockerCmd(c, "images", "busybox:nonexistent")
  42. assert.Assert(c, !strings.Contains(imagesOut, "busybox"))
  43. }
  44. func (s *DockerSuite) TestImagesOrderedByCreationDate(c *testing.T) {
  45. buildImageSuccessfully(c, "order:test_a", build.WithDockerfile(`FROM busybox
  46. MAINTAINER dockerio1`))
  47. id1 := getIDByName(c, "order:test_a")
  48. time.Sleep(1 * time.Second)
  49. buildImageSuccessfully(c, "order:test_c", build.WithDockerfile(`FROM busybox
  50. MAINTAINER dockerio2`))
  51. id2 := getIDByName(c, "order:test_c")
  52. time.Sleep(1 * time.Second)
  53. buildImageSuccessfully(c, "order:test_b", build.WithDockerfile(`FROM busybox
  54. MAINTAINER dockerio3`))
  55. id3 := getIDByName(c, "order:test_b")
  56. out, _ := dockerCmd(c, "images", "-q", "--no-trunc")
  57. imgs := strings.Split(out, "\n")
  58. assert.Equal(c, imgs[0], id3, check.Commentf("First image must be %s, got %s", id3, imgs[0]))
  59. assert.Equal(c, imgs[1], id2, check.Commentf("First image must be %s, got %s", id2, imgs[1]))
  60. assert.Equal(c, imgs[2], id1, check.Commentf("First image must be %s, got %s", id1, imgs[2]))
  61. }
  62. func (s *DockerSuite) TestImagesErrorWithInvalidFilterNameTest(c *testing.T) {
  63. out, _, err := dockerCmdWithError("images", "-f", "FOO=123")
  64. assert.ErrorContains(c, err, "")
  65. assert.Assert(c, out, checker.Contains, "Invalid filter")
  66. }
  67. func (s *DockerSuite) TestImagesFilterLabelMatch(c *testing.T) {
  68. imageName1 := "images_filter_test1"
  69. imageName2 := "images_filter_test2"
  70. imageName3 := "images_filter_test3"
  71. buildImageSuccessfully(c, imageName1, build.WithDockerfile(`FROM busybox
  72. LABEL match me`))
  73. image1ID := getIDByName(c, imageName1)
  74. buildImageSuccessfully(c, imageName2, build.WithDockerfile(`FROM busybox
  75. LABEL match="me too"`))
  76. image2ID := getIDByName(c, imageName2)
  77. buildImageSuccessfully(c, imageName3, build.WithDockerfile(`FROM busybox
  78. LABEL nomatch me`))
  79. image3ID := getIDByName(c, imageName3)
  80. out, _ := dockerCmd(c, "images", "--no-trunc", "-q", "-f", "label=match")
  81. out = strings.TrimSpace(out)
  82. assert.Assert(c, is.Regexp("^"+
  83. fmt.Sprintf("[\\s\\w:]*%s[\\s\\w:]*", image1ID)+
  84. "$",
  85. out))
  86. assert.Assert(c, is.Regexp("^"+
  87. fmt.Sprintf("[\\s\\w:]*%s[\\s\\w:]*", image2ID)+
  88. "$",
  89. 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 *DockerSuite) 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 *DockerSuite) 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, check.Commentf("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, check.Commentf("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, check.Commentf("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, check.Commentf("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, check.Commentf("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, check.Commentf("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, check.Commentf("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, check.Commentf("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 *DockerSuite) 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, 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 *DockerSuite) 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, out, checker.Contains, imageID)
  214. }
  215. // FIXME(vdemeester) should be a unit test for `docker image ls`
  216. func (s *DockerSuite) TestImagesWithIncorrectFilter(c *testing.T) {
  217. out, _, err := dockerCmdWithError("images", "-f", "dangling=invalid")
  218. assert.ErrorContains(c, err, "")
  219. assert.Assert(c, out, checker.Contains, "Invalid filter")
  220. }
  221. func (s *DockerSuite) 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, out, checker.Contains, stringid.TruncateID(id))
  240. }
  241. func (s *DockerSuite) 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, out, checker.Contains, stringid.TruncateID(id))
  252. }
  253. // For W2W - equivalent to TestImagesEnsureImagesFromScratchShown but Windows
  254. // doesn't support from scratch
  255. func (s *DockerSuite) 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, out, checker.Contains, stringid.TruncateID(id))
  265. }
  266. // #18181
  267. func (s *DockerSuite) 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, out, checker.Contains, tag)
  272. out, _ = dockerCmd(c, "images", tag+":latest")
  273. assert.Assert(c, out, checker.Contains, tag)
  274. out, _ = dockerCmd(c, "images", tag+":no-such-tag")
  275. assert.Assert(c, !strings.Contains(out, tag))
  276. }
  277. func (s *DockerSuite) 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(string(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 *DockerSuite) 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 := ioutil.TempDir("", "integration-cli-")
  302. assert.NilError(c, err)
  303. defer os.RemoveAll(d)
  304. err = ioutil.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. }