docker_api_images_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package main
  2. import (
  3. "context"
  4. "net/http"
  5. "net/http/httptest"
  6. "strings"
  7. "testing"
  8. "github.com/docker/docker/api/types"
  9. "github.com/docker/docker/api/types/filters"
  10. "github.com/docker/docker/client"
  11. "github.com/docker/docker/integration-cli/cli"
  12. "github.com/docker/docker/integration-cli/cli/build"
  13. "github.com/docker/docker/testutil/request"
  14. "gotest.tools/v3/assert"
  15. )
  16. func (s *DockerAPISuite) TestAPIImagesFilter(c *testing.T) {
  17. cli, err := client.NewClientWithOpts(client.FromEnv)
  18. assert.NilError(c, err)
  19. defer cli.Close()
  20. name := "utest:tag1"
  21. name2 := "utest/docker:tag2"
  22. name3 := "utest:5000/docker:tag3"
  23. for _, n := range []string{name, name2, name3} {
  24. dockerCmd(c, "tag", "busybox", n)
  25. }
  26. getImages := func(filter string) []types.ImageSummary {
  27. filters := filters.NewArgs()
  28. filters.Add("reference", filter)
  29. options := types.ImageListOptions{
  30. All: false,
  31. Filters: filters,
  32. }
  33. images, err := cli.ImageList(context.Background(), options)
  34. assert.NilError(c, err)
  35. return images
  36. }
  37. // incorrect number of matches returned
  38. images := getImages("utest*/*")
  39. assert.Equal(c, len(images[0].RepoTags), 2)
  40. images = getImages("utest")
  41. assert.Equal(c, len(images[0].RepoTags), 1)
  42. images = getImages("utest*")
  43. assert.Equal(c, len(images[0].RepoTags), 1)
  44. images = getImages("*5000*/*")
  45. assert.Equal(c, len(images[0].RepoTags), 1)
  46. }
  47. func (s *DockerAPISuite) TestAPIImagesSaveAndLoad(c *testing.T) {
  48. testRequires(c, Network)
  49. buildImageSuccessfully(c, "saveandload", build.WithDockerfile("FROM busybox\nENV FOO bar"))
  50. id := getIDByName(c, "saveandload")
  51. res, body, err := request.Get("/images/" + id + "/get")
  52. assert.NilError(c, err)
  53. defer body.Close()
  54. assert.Equal(c, res.StatusCode, http.StatusOK)
  55. dockerCmd(c, "rmi", id)
  56. res, loadBody, err := request.Post("/images/load", request.RawContent(body), request.ContentType("application/x-tar"))
  57. assert.NilError(c, err)
  58. defer loadBody.Close()
  59. assert.Equal(c, res.StatusCode, http.StatusOK)
  60. inspectOut := cli.InspectCmd(c, id, cli.Format(".Id")).Combined()
  61. assert.Equal(c, strings.TrimSpace(inspectOut), id, "load did not work properly")
  62. }
  63. func (s *DockerAPISuite) TestAPIImagesDelete(c *testing.T) {
  64. cli, err := client.NewClientWithOpts(client.FromEnv)
  65. assert.NilError(c, err)
  66. defer cli.Close()
  67. if testEnv.OSType != "windows" {
  68. testRequires(c, Network)
  69. }
  70. name := "test-api-images-delete"
  71. buildImageSuccessfully(c, name, build.WithDockerfile("FROM busybox\nENV FOO bar"))
  72. id := getIDByName(c, name)
  73. dockerCmd(c, "tag", name, "test:tag1")
  74. _, err = cli.ImageRemove(context.Background(), id, types.ImageRemoveOptions{})
  75. assert.ErrorContains(c, err, "unable to delete")
  76. _, err = cli.ImageRemove(context.Background(), "test:noexist", types.ImageRemoveOptions{})
  77. assert.ErrorContains(c, err, "No such image")
  78. _, err = cli.ImageRemove(context.Background(), "test:tag1", types.ImageRemoveOptions{})
  79. assert.NilError(c, err)
  80. }
  81. func (s *DockerAPISuite) TestAPIImagesHistory(c *testing.T) {
  82. cli, err := client.NewClientWithOpts(client.FromEnv)
  83. assert.NilError(c, err)
  84. defer cli.Close()
  85. if testEnv.OSType != "windows" {
  86. testRequires(c, Network)
  87. }
  88. name := "test-api-images-history"
  89. buildImageSuccessfully(c, name, build.WithDockerfile("FROM busybox\nENV FOO bar"))
  90. id := getIDByName(c, name)
  91. historydata, err := cli.ImageHistory(context.Background(), id)
  92. assert.NilError(c, err)
  93. assert.Assert(c, len(historydata) != 0)
  94. var found bool
  95. for _, tag := range historydata[0].Tags {
  96. if tag == "test-api-images-history:latest" {
  97. found = true
  98. break
  99. }
  100. }
  101. assert.Assert(c, found)
  102. }
  103. func (s *DockerAPISuite) TestAPIImagesImportBadSrc(c *testing.T) {
  104. testRequires(c, Network, testEnv.IsLocalDaemon)
  105. server := httptest.NewServer(http.NewServeMux())
  106. defer server.Close()
  107. tt := []struct {
  108. statusExp int
  109. fromSrc string
  110. }{
  111. {http.StatusNotFound, server.URL + "/nofile.tar"},
  112. {http.StatusNotFound, strings.TrimPrefix(server.URL, "http://") + "/nofile.tar"},
  113. {http.StatusNotFound, strings.TrimPrefix(server.URL, "http://") + "%2Fdata%2Ffile.tar"},
  114. {http.StatusInternalServerError, "%2Fdata%2Ffile.tar"},
  115. }
  116. for _, te := range tt {
  117. res, _, err := request.Post(strings.Join([]string{"/images/create?fromSrc=", te.fromSrc}, ""), request.JSON)
  118. assert.NilError(c, err)
  119. assert.Equal(c, res.StatusCode, te.statusExp)
  120. assert.Equal(c, res.Header.Get("Content-Type"), "application/json")
  121. }
  122. }
  123. // #14846
  124. func (s *DockerAPISuite) TestAPIImagesSearchJSONContentType(c *testing.T) {
  125. testRequires(c, Network)
  126. res, b, err := request.Get("/images/search?term=test", request.JSON)
  127. assert.NilError(c, err)
  128. b.Close()
  129. assert.Equal(c, res.StatusCode, http.StatusOK)
  130. assert.Equal(c, res.Header.Get("Content-Type"), "application/json")
  131. }
  132. // Test case for 30027: image size reported as -1 in v1.12 client against v1.13 daemon.
  133. // This test checks to make sure both v1.12 and v1.13 client against v1.13 daemon get correct `Size` after the fix.
  134. func (s *DockerAPISuite) TestAPIImagesSizeCompatibility(c *testing.T) {
  135. apiclient := testEnv.APIClient()
  136. defer apiclient.Close()
  137. images, err := apiclient.ImageList(context.Background(), types.ImageListOptions{})
  138. assert.NilError(c, err)
  139. assert.Assert(c, len(images) != 0)
  140. for _, image := range images {
  141. assert.Assert(c, image.Size != int64(-1))
  142. }
  143. apiclient, err = client.NewClientWithOpts(client.FromEnv, client.WithVersion("v1.24"))
  144. assert.NilError(c, err)
  145. defer apiclient.Close()
  146. v124Images, err := apiclient.ImageList(context.Background(), types.ImageListOptions{})
  147. assert.NilError(c, err)
  148. assert.Assert(c, len(v124Images) != 0)
  149. for _, image := range v124Images {
  150. assert.Assert(c, image.Size != int64(-1))
  151. }
  152. }