docker_api_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "runtime"
  7. "strconv"
  8. "strings"
  9. "testing"
  10. "github.com/docker/docker/api"
  11. "github.com/docker/docker/api/types/versions"
  12. "github.com/docker/docker/testutil/request"
  13. "gotest.tools/v3/assert"
  14. )
  15. type DockerAPISuite struct {
  16. ds *DockerSuite
  17. }
  18. func (s *DockerAPISuite) TearDownTest(c *testing.T) {
  19. s.ds.TearDownTest(c)
  20. }
  21. func (s *DockerAPISuite) OnTimeout(c *testing.T) {
  22. s.ds.OnTimeout(c)
  23. }
  24. func (s *DockerAPISuite) TestAPIOptionsRoute(c *testing.T) {
  25. resp, _, err := request.Do("/", request.Method(http.MethodOptions))
  26. assert.NilError(c, err)
  27. assert.Equal(c, resp.StatusCode, http.StatusOK)
  28. }
  29. func (s *DockerAPISuite) TestAPIGetEnabledCORS(c *testing.T) {
  30. res, body, err := request.Get("/version")
  31. assert.NilError(c, err)
  32. assert.Equal(c, res.StatusCode, http.StatusOK)
  33. body.Close()
  34. // TODO: @runcom incomplete tests, why old integration tests had this headers
  35. // and here none of the headers below are in the response?
  36. //c.Log(res.Header)
  37. //assert.Equal(c, res.Header.Get("Access-Control-Allow-Origin"), "*")
  38. //assert.Equal(c, res.Header.Get("Access-Control-Allow-Headers"), "Origin, X-Requested-With, Content-Type, Accept, X-Registry-Auth")
  39. }
  40. func (s *DockerAPISuite) TestAPIClientVersionOldNotSupported(c *testing.T) {
  41. if testEnv.OSType != runtime.GOOS {
  42. c.Skip("Daemon platform doesn't match test platform")
  43. }
  44. if api.MinVersion == api.DefaultVersion {
  45. c.Skip("API MinVersion==DefaultVersion")
  46. }
  47. v := strings.Split(api.MinVersion, ".")
  48. vMinInt, err := strconv.Atoi(v[1])
  49. assert.NilError(c, err)
  50. vMinInt--
  51. v[1] = strconv.Itoa(vMinInt)
  52. version := strings.Join(v, ".")
  53. resp, body, err := request.Get("/v" + version + "/version")
  54. assert.NilError(c, err)
  55. defer body.Close()
  56. assert.Equal(c, resp.StatusCode, http.StatusBadRequest)
  57. expected := fmt.Sprintf("client version %s is too old. Minimum supported API version is %s, please upgrade your client to a newer version", version, api.MinVersion)
  58. content, err := io.ReadAll(body)
  59. assert.NilError(c, err)
  60. assert.Equal(c, strings.TrimSpace(string(content)), expected)
  61. }
  62. func (s *DockerAPISuite) TestAPIErrorJSON(c *testing.T) {
  63. httpResp, body, err := request.Post("/containers/create", request.JSONBody(struct{}{}))
  64. assert.NilError(c, err)
  65. if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
  66. assert.Equal(c, httpResp.StatusCode, http.StatusInternalServerError)
  67. } else {
  68. assert.Equal(c, httpResp.StatusCode, http.StatusBadRequest)
  69. }
  70. assert.Assert(c, strings.Contains(httpResp.Header.Get("Content-Type"), "application/json"))
  71. b, err := request.ReadBody(body)
  72. assert.NilError(c, err)
  73. assert.Equal(c, getErrorMessage(c, b), "Config cannot be empty in order to create a container")
  74. }
  75. func (s *DockerAPISuite) TestAPIErrorPlainText(c *testing.T) {
  76. // Windows requires API 1.25 or later. This test is validating a behaviour which was present
  77. // in v1.23, but changed in 1.24, hence not applicable on Windows. See apiVersionSupportsJSONErrors
  78. testRequires(c, DaemonIsLinux)
  79. httpResp, body, err := request.Post("/v1.23/containers/create", request.JSONBody(struct{}{}))
  80. assert.NilError(c, err)
  81. if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
  82. assert.Equal(c, httpResp.StatusCode, http.StatusInternalServerError)
  83. } else {
  84. assert.Equal(c, httpResp.StatusCode, http.StatusBadRequest)
  85. }
  86. assert.Assert(c, strings.Contains(httpResp.Header.Get("Content-Type"), "text/plain"))
  87. b, err := request.ReadBody(body)
  88. assert.NilError(c, err)
  89. assert.Equal(c, strings.TrimSpace(string(b)), "Config cannot be empty in order to create a container")
  90. }
  91. func (s *DockerAPISuite) TestAPIErrorNotFoundJSON(c *testing.T) {
  92. // 404 is a different code path to normal errors, so test separately
  93. httpResp, body, err := request.Get("/notfound", request.JSON)
  94. assert.NilError(c, err)
  95. assert.Equal(c, httpResp.StatusCode, http.StatusNotFound)
  96. assert.Assert(c, strings.Contains(httpResp.Header.Get("Content-Type"), "application/json"))
  97. b, err := request.ReadBody(body)
  98. assert.NilError(c, err)
  99. assert.Equal(c, getErrorMessage(c, b), "page not found")
  100. }
  101. func (s *DockerAPISuite) TestAPIErrorNotFoundPlainText(c *testing.T) {
  102. httpResp, body, err := request.Get("/v1.23/notfound", request.JSON)
  103. assert.NilError(c, err)
  104. assert.Equal(c, httpResp.StatusCode, http.StatusNotFound)
  105. assert.Assert(c, strings.Contains(httpResp.Header.Get("Content-Type"), "text/plain"))
  106. b, err := request.ReadBody(body)
  107. assert.NilError(c, err)
  108. assert.Equal(c, strings.TrimSpace(string(b)), "page not found")
  109. }