registry_mock_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package registry // import "github.com/docker/docker/registry"
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "io"
  7. "net"
  8. "net/http"
  9. "net/http/httptest"
  10. "testing"
  11. "github.com/containerd/log"
  12. "github.com/docker/docker/api/types/registry"
  13. "gotest.tools/v3/assert"
  14. )
  15. var (
  16. testHTTPServer *httptest.Server
  17. testHTTPSServer *httptest.Server
  18. )
  19. func init() {
  20. r := http.NewServeMux()
  21. // /v1/
  22. r.HandleFunc("/v1/_ping", handlerGetPing)
  23. r.HandleFunc("/v1/search", handlerSearch)
  24. // /v2/
  25. r.HandleFunc("/v2/version", handlerGetPing)
  26. testHTTPServer = httptest.NewServer(handlerAccessLog(r))
  27. testHTTPSServer = httptest.NewTLSServer(handlerAccessLog(r))
  28. // override net.LookupIP
  29. lookupIP = func(host string) ([]net.IP, error) {
  30. if host == "127.0.0.1" {
  31. // I believe in future Go versions this will fail, so let's fix it later
  32. return net.LookupIP(host)
  33. }
  34. mockHosts := map[string][]net.IP{
  35. "": {net.ParseIP("0.0.0.0")},
  36. "localhost": {net.ParseIP("127.0.0.1"), net.ParseIP("::1")},
  37. "example.com": {net.ParseIP("42.42.42.42")},
  38. "other.com": {net.ParseIP("43.43.43.43")},
  39. }
  40. for h, addrs := range mockHosts {
  41. if host == h {
  42. return addrs, nil
  43. }
  44. for _, addr := range addrs {
  45. if addr.String() == host {
  46. return []net.IP{addr}, nil
  47. }
  48. }
  49. }
  50. return nil, errors.New("lookup: no such host")
  51. }
  52. }
  53. func handlerAccessLog(handler http.Handler) http.Handler {
  54. logHandler := func(w http.ResponseWriter, r *http.Request) {
  55. log.G(context.TODO()).Debugf(`%s "%s %s"`, r.RemoteAddr, r.Method, r.URL)
  56. handler.ServeHTTP(w, r)
  57. }
  58. return http.HandlerFunc(logHandler)
  59. }
  60. func makeURL(req string) string {
  61. return testHTTPServer.URL + req
  62. }
  63. func makeHTTPSURL(req string) string {
  64. return testHTTPSServer.URL + req
  65. }
  66. func makeIndex(req string) *registry.IndexInfo {
  67. return &registry.IndexInfo{
  68. Name: makeURL(req),
  69. }
  70. }
  71. func makeHTTPSIndex(req string) *registry.IndexInfo {
  72. return &registry.IndexInfo{
  73. Name: makeHTTPSURL(req),
  74. }
  75. }
  76. func makePublicIndex() *registry.IndexInfo {
  77. return &registry.IndexInfo{
  78. Name: IndexServer,
  79. Secure: true,
  80. Official: true,
  81. }
  82. }
  83. func makeServiceConfig(mirrors []string, insecureRegistries []string) (*serviceConfig, error) {
  84. return newServiceConfig(ServiceOptions{
  85. Mirrors: mirrors,
  86. InsecureRegistries: insecureRegistries,
  87. })
  88. }
  89. func writeHeaders(w http.ResponseWriter) {
  90. h := w.Header()
  91. h.Add("Server", "docker-tests/mock")
  92. h.Add("Expires", "-1")
  93. h.Add("Content-Type", "application/json")
  94. h.Add("Pragma", "no-cache")
  95. h.Add("Cache-Control", "no-cache")
  96. }
  97. func writeResponse(w http.ResponseWriter, message interface{}, code int) {
  98. writeHeaders(w)
  99. w.WriteHeader(code)
  100. body, err := json.Marshal(message)
  101. if err != nil {
  102. _, _ = io.WriteString(w, err.Error())
  103. return
  104. }
  105. _, _ = w.Write(body)
  106. }
  107. func handlerGetPing(w http.ResponseWriter, r *http.Request) {
  108. if r.Method != http.MethodGet {
  109. writeResponse(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
  110. return
  111. }
  112. writeResponse(w, true, http.StatusOK)
  113. }
  114. func handlerSearch(w http.ResponseWriter, r *http.Request) {
  115. if r.Method != http.MethodGet {
  116. writeResponse(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
  117. return
  118. }
  119. result := &registry.SearchResults{
  120. Query: "fakequery",
  121. NumResults: 1,
  122. Results: []registry.SearchResult{{Name: "fakeimage", StarCount: 42}},
  123. }
  124. writeResponse(w, result, http.StatusOK)
  125. }
  126. func TestPing(t *testing.T) {
  127. res, err := http.Get(makeURL("/v1/_ping"))
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. assert.Equal(t, res.StatusCode, http.StatusOK, "")
  132. assert.Equal(t, res.Header.Get("Server"), "docker-tests/mock")
  133. }