docker_api_images_test.go 5.6 KB

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