version_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package middleware // import "github.com/docker/docker/api/server/middleware"
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/http/httptest"
  7. "runtime"
  8. "testing"
  9. "github.com/docker/docker/api"
  10. "github.com/docker/docker/api/server/httputils"
  11. "gotest.tools/v3/assert"
  12. is "gotest.tools/v3/assert/cmp"
  13. )
  14. func TestNewVersionMiddlewareValidation(t *testing.T) {
  15. tests := []struct {
  16. doc, defaultVersion, minVersion, expectedErr string
  17. }{
  18. {
  19. doc: "defaults",
  20. defaultVersion: api.DefaultVersion,
  21. minVersion: api.MinSupportedAPIVersion,
  22. },
  23. {
  24. doc: "invalid default lower than min",
  25. defaultVersion: api.MinSupportedAPIVersion,
  26. minVersion: api.DefaultVersion,
  27. expectedErr: fmt.Sprintf("invalid API version: the minimum API version (%s) is higher than the default version (%s)", api.DefaultVersion, api.MinSupportedAPIVersion),
  28. },
  29. {
  30. doc: "invalid default too low",
  31. defaultVersion: "0.1",
  32. minVersion: api.MinSupportedAPIVersion,
  33. expectedErr: fmt.Sprintf("invalid default API version (0.1): must be between %s and %s", api.MinSupportedAPIVersion, api.DefaultVersion),
  34. },
  35. {
  36. doc: "invalid default too high",
  37. defaultVersion: "9999.9999",
  38. minVersion: api.DefaultVersion,
  39. expectedErr: fmt.Sprintf("invalid default API version (9999.9999): must be between %s and %s", api.MinSupportedAPIVersion, api.DefaultVersion),
  40. },
  41. {
  42. doc: "invalid minimum too low",
  43. defaultVersion: api.MinSupportedAPIVersion,
  44. minVersion: "0.1",
  45. expectedErr: fmt.Sprintf("invalid minimum API version (0.1): must be between %s and %s", api.MinSupportedAPIVersion, api.DefaultVersion),
  46. },
  47. {
  48. doc: "invalid minimum too high",
  49. defaultVersion: api.DefaultVersion,
  50. minVersion: "9999.9999",
  51. expectedErr: fmt.Sprintf("invalid minimum API version (9999.9999): must be between %s and %s", api.MinSupportedAPIVersion, api.DefaultVersion),
  52. },
  53. }
  54. for _, tc := range tests {
  55. tc := tc
  56. t.Run(tc.doc, func(t *testing.T) {
  57. _, err := NewVersionMiddleware("1.2.3", tc.defaultVersion, tc.minVersion)
  58. if tc.expectedErr == "" {
  59. assert.Check(t, err)
  60. } else {
  61. assert.Check(t, is.Error(err, tc.expectedErr))
  62. }
  63. })
  64. }
  65. }
  66. func TestVersionMiddlewareVersion(t *testing.T) {
  67. expectedVersion := "<not set>"
  68. handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  69. v := httputils.VersionFromContext(ctx)
  70. assert.Check(t, is.Equal(expectedVersion, v))
  71. return nil
  72. }
  73. m, err := NewVersionMiddleware("1.2.3", api.DefaultVersion, api.MinSupportedAPIVersion)
  74. assert.NilError(t, err)
  75. h := m.WrapHandler(handler)
  76. req, _ := http.NewRequest(http.MethodGet, "/containers/json", nil)
  77. resp := httptest.NewRecorder()
  78. ctx := context.Background()
  79. tests := []struct {
  80. reqVersion string
  81. expectedVersion string
  82. errString string
  83. }{
  84. {
  85. expectedVersion: api.DefaultVersion,
  86. },
  87. {
  88. reqVersion: api.MinSupportedAPIVersion,
  89. expectedVersion: api.MinSupportedAPIVersion,
  90. },
  91. {
  92. reqVersion: "0.1",
  93. errString: fmt.Sprintf("client version 0.1 is too old. Minimum supported API version is %s, please upgrade your client to a newer version", api.MinSupportedAPIVersion),
  94. },
  95. {
  96. reqVersion: "9999.9999",
  97. errString: fmt.Sprintf("client version 9999.9999 is too new. Maximum supported API version is %s", api.DefaultVersion),
  98. },
  99. }
  100. for _, test := range tests {
  101. expectedVersion = test.expectedVersion
  102. err := h(ctx, resp, req, map[string]string{"version": test.reqVersion})
  103. if test.errString != "" {
  104. assert.Check(t, is.Error(err, test.errString))
  105. } else {
  106. assert.Check(t, err)
  107. }
  108. }
  109. }
  110. func TestVersionMiddlewareWithErrorsReturnsHeaders(t *testing.T) {
  111. handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  112. v := httputils.VersionFromContext(ctx)
  113. assert.Check(t, len(v) != 0)
  114. return nil
  115. }
  116. m, err := NewVersionMiddleware("1.2.3", api.DefaultVersion, api.MinSupportedAPIVersion)
  117. assert.NilError(t, err)
  118. h := m.WrapHandler(handler)
  119. req, _ := http.NewRequest(http.MethodGet, "/containers/json", nil)
  120. resp := httptest.NewRecorder()
  121. ctx := context.Background()
  122. vars := map[string]string{"version": "0.1"}
  123. err = h(ctx, resp, req, vars)
  124. assert.Check(t, is.ErrorContains(err, ""))
  125. hdr := resp.Result().Header
  126. assert.Check(t, is.Contains(hdr.Get("Server"), "Docker/1.2.3"))
  127. assert.Check(t, is.Contains(hdr.Get("Server"), runtime.GOOS))
  128. assert.Check(t, is.Equal(hdr.Get("API-Version"), api.DefaultVersion))
  129. assert.Check(t, is.Equal(hdr.Get("OSType"), runtime.GOOS))
  130. }