docker_api_images_test.go 6.6 KB

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