docker_api_images_test.go 4.6 KB

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