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