2018-02-05 21:05:59 +00:00
|
|
|
package registry // import "github.com/docker/docker/registry"
|
2013-07-25 00:44:55 +00:00
|
|
|
|
|
|
|
import (
|
2023-06-23 00:33:17 +00:00
|
|
|
"context"
|
2013-07-25 00:44:55 +00:00
|
|
|
"encoding/json"
|
2014-11-11 21:31:15 +00:00
|
|
|
"errors"
|
2013-07-25 00:44:55 +00:00
|
|
|
"io"
|
2014-11-11 21:31:15 +00:00
|
|
|
"net"
|
2013-07-25 00:44:55 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2014-07-24 20:37:44 +00:00
|
|
|
|
2023-09-13 15:41:45 +00:00
|
|
|
"github.com/containerd/log"
|
2022-02-26 18:13:43 +00:00
|
|
|
"github.com/docker/docker/api/types/registry"
|
2022-02-26 21:53:06 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2013-07-25 00:44:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-10-07 01:54:52 +00:00
|
|
|
testHTTPServer *httptest.Server
|
|
|
|
testHTTPSServer *httptest.Server
|
2013-07-25 00:44:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2022-12-19 12:21:20 +00:00
|
|
|
r := http.NewServeMux()
|
2014-10-02 01:26:06 +00:00
|
|
|
|
|
|
|
// /v1/
|
2022-12-19 12:21:20 +00:00
|
|
|
r.HandleFunc("/v1/_ping", handlerGetPing)
|
|
|
|
r.HandleFunc("/v1/search", handlerSearch)
|
2014-10-02 01:26:06 +00:00
|
|
|
|
|
|
|
// /v2/
|
2022-12-19 12:21:20 +00:00
|
|
|
r.HandleFunc("/v2/version", handlerGetPing)
|
2014-10-02 01:26:06 +00:00
|
|
|
|
2014-10-06 19:34:39 +00:00
|
|
|
testHTTPServer = httptest.NewServer(handlerAccessLog(r))
|
2014-10-07 01:54:52 +00:00
|
|
|
testHTTPSServer = httptest.NewTLSServer(handlerAccessLog(r))
|
2014-11-11 21:31:15 +00:00
|
|
|
|
|
|
|
// override net.LookupIP
|
|
|
|
lookupIP = func(host string) ([]net.IP, error) {
|
|
|
|
if host == "127.0.0.1" {
|
|
|
|
// I believe in future Go versions this will fail, so let's fix it later
|
|
|
|
return net.LookupIP(host)
|
|
|
|
}
|
2022-02-26 21:53:06 +00:00
|
|
|
mockHosts := map[string][]net.IP{
|
|
|
|
"": {net.ParseIP("0.0.0.0")},
|
|
|
|
"localhost": {net.ParseIP("127.0.0.1"), net.ParseIP("::1")},
|
|
|
|
"example.com": {net.ParseIP("42.42.42.42")},
|
|
|
|
"other.com": {net.ParseIP("43.43.43.43")},
|
|
|
|
}
|
2014-11-11 21:31:15 +00:00
|
|
|
for h, addrs := range mockHosts {
|
|
|
|
if host == h {
|
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
for _, addr := range addrs {
|
|
|
|
if addr.String() == host {
|
|
|
|
return []net.IP{addr}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errors.New("lookup: no such host")
|
|
|
|
}
|
2013-07-25 19:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handlerAccessLog(handler http.Handler) http.Handler {
|
|
|
|
logHandler := func(w http.ResponseWriter, r *http.Request) {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Debugf(`%s "%s %s"`, r.RemoteAddr, r.Method, r.URL)
|
2013-07-25 19:57:09 +00:00
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(logHandler)
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func makeURL(req string) string {
|
2014-10-06 19:34:39 +00:00
|
|
|
return testHTTPServer.URL + req
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 19:40:36 +00:00
|
|
|
func makeHTTPSURL(req string) string {
|
2014-10-07 01:54:52 +00:00
|
|
|
return testHTTPSServer.URL + req
|
|
|
|
}
|
|
|
|
|
2022-02-26 18:13:43 +00:00
|
|
|
func makeIndex(req string) *registry.IndexInfo {
|
2023-09-08 12:22:21 +00:00
|
|
|
return ®istry.IndexInfo{
|
2014-10-07 01:54:52 +00:00
|
|
|
Name: makeURL(req),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-26 18:13:43 +00:00
|
|
|
func makeHTTPSIndex(req string) *registry.IndexInfo {
|
2023-09-08 12:22:21 +00:00
|
|
|
return ®istry.IndexInfo{
|
2015-07-21 19:40:36 +00:00
|
|
|
Name: makeHTTPSURL(req),
|
2014-10-07 01:54:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-26 18:13:43 +00:00
|
|
|
func makePublicIndex() *registry.IndexInfo {
|
2023-09-08 12:22:21 +00:00
|
|
|
return ®istry.IndexInfo{
|
2015-07-21 19:40:36 +00:00
|
|
|
Name: IndexServer,
|
2014-10-07 01:54:52 +00:00
|
|
|
Secure: true,
|
|
|
|
Official: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-01 14:35:04 +00:00
|
|
|
func makeServiceConfig(mirrors []string, insecureRegistries []string) (*serviceConfig, error) {
|
2023-09-08 12:22:21 +00:00
|
|
|
return newServiceConfig(ServiceOptions{
|
2016-03-08 21:03:37 +00:00
|
|
|
Mirrors: mirrors,
|
|
|
|
InsecureRegistries: insecureRegistries,
|
2023-09-08 12:22:21 +00:00
|
|
|
})
|
2014-10-07 01:54:52 +00:00
|
|
|
}
|
|
|
|
|
2013-07-25 00:44:55 +00:00
|
|
|
func writeHeaders(w http.ResponseWriter) {
|
|
|
|
h := w.Header()
|
|
|
|
h.Add("Server", "docker-tests/mock")
|
|
|
|
h.Add("Expires", "-1")
|
|
|
|
h.Add("Content-Type", "application/json")
|
|
|
|
h.Add("Pragma", "no-cache")
|
|
|
|
h.Add("Cache-Control", "no-cache")
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeResponse(w http.ResponseWriter, message interface{}, code int) {
|
|
|
|
writeHeaders(w)
|
|
|
|
w.WriteHeader(code)
|
|
|
|
body, err := json.Marshal(message)
|
|
|
|
if err != nil {
|
2022-12-19 12:21:20 +00:00
|
|
|
_, _ = io.WriteString(w, err.Error())
|
2013-07-25 00:44:55 +00:00
|
|
|
return
|
|
|
|
}
|
2022-12-19 12:21:20 +00:00
|
|
|
_, _ = w.Write(body)
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handlerGetPing(w http.ResponseWriter, r *http.Request) {
|
2022-12-19 12:21:20 +00:00
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
writeResponse(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
2019-10-13 15:25:25 +00:00
|
|
|
writeResponse(w, true, http.StatusOK)
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handlerSearch(w http.ResponseWriter, r *http.Request) {
|
2022-12-19 12:21:20 +00:00
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
writeResponse(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
2022-02-26 18:13:43 +00:00
|
|
|
result := ®istry.SearchResults{
|
2014-03-13 17:40:34 +00:00
|
|
|
Query: "fakequery",
|
|
|
|
NumResults: 1,
|
2022-02-26 18:13:43 +00:00
|
|
|
Results: []registry.SearchResult{{Name: "fakeimage", StarCount: 42}},
|
2014-03-13 17:40:34 +00:00
|
|
|
}
|
2019-10-13 15:25:25 +00:00
|
|
|
writeResponse(w, result, http.StatusOK)
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPing(t *testing.T) {
|
|
|
|
res, err := http.Get(makeURL("/v1/_ping"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-02-26 21:53:06 +00:00
|
|
|
assert.Equal(t, res.StatusCode, http.StatusOK, "")
|
2023-09-08 09:55:53 +00:00
|
|
|
assert.Equal(t, res.Header.Get("Server"), "docker-tests/mock")
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|