docker_api_images_test.go 4.8 KB

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