2018-02-05 16:05:59 -05:00
|
|
|
package registry // import "github.com/docker/docker/registry"
|
2013-07-24 20:44:55 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-11-11 16:31:15 -05:00
|
|
|
"errors"
|
2013-07-24 20:44:55 -04:00
|
|
|
"io"
|
2014-11-11 16:31:15 -05:00
|
|
|
"net"
|
2013-07-24 20:44:55 -04:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2014-07-24 16:37:44 -04:00
|
|
|
|
2022-02-26 13:13:43 -05:00
|
|
|
"github.com/docker/docker/api/types/registry"
|
2014-07-24 16:37:44 -04:00
|
|
|
"github.com/gorilla/mux"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-02-26 16:53:06 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2013-07-24 20:44:55 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-10-06 21:54:52 -04:00
|
|
|
testHTTPServer *httptest.Server
|
|
|
|
testHTTPSServer *httptest.Server
|
2013-07-24 20:44:55 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
r := mux.NewRouter()
|
2014-10-01 21:26:06 -04:00
|
|
|
|
|
|
|
// /v1/
|
2019-10-12 14:42:44 -04:00
|
|
|
r.HandleFunc("/v1/_ping", handlerGetPing).Methods(http.MethodGet)
|
|
|
|
r.HandleFunc("/v1/search", handlerSearch).Methods(http.MethodGet)
|
2014-10-01 21:26:06 -04:00
|
|
|
|
|
|
|
// /v2/
|
2019-10-12 14:42:44 -04:00
|
|
|
r.HandleFunc("/v2/version", handlerGetPing).Methods(http.MethodGet)
|
2014-10-01 21:26:06 -04:00
|
|
|
|
2014-10-06 15:34:39 -04:00
|
|
|
testHTTPServer = httptest.NewServer(handlerAccessLog(r))
|
2014-10-06 21:54:52 -04:00
|
|
|
testHTTPSServer = httptest.NewTLSServer(handlerAccessLog(r))
|
2014-11-11 16:31:15 -05: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 16:53:06 -05: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 16:31:15 -05: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 15:57:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func handlerAccessLog(handler http.Handler) http.Handler {
|
|
|
|
logHandler := func(w http.ResponseWriter, r *http.Request) {
|
2022-02-26 16:53:06 -05:00
|
|
|
logrus.Debugf(`%s "%s %s"`, r.RemoteAddr, r.Method, r.URL)
|
2013-07-25 15:57:09 -04:00
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(logHandler)
|
2013-07-24 20:44:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func makeURL(req string) string {
|
2014-10-06 15:34:39 -04:00
|
|
|
return testHTTPServer.URL + req
|
2013-07-24 20:44:55 -04:00
|
|
|
}
|
|
|
|
|
2015-07-21 15:40:36 -04:00
|
|
|
func makeHTTPSURL(req string) string {
|
2014-10-06 21:54:52 -04:00
|
|
|
return testHTTPSServer.URL + req
|
|
|
|
}
|
|
|
|
|
2022-02-26 13:13:43 -05:00
|
|
|
func makeIndex(req string) *registry.IndexInfo {
|
|
|
|
index := ®istry.IndexInfo{
|
2014-10-06 21:54:52 -04:00
|
|
|
Name: makeURL(req),
|
|
|
|
}
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
|
2022-02-26 13:13:43 -05:00
|
|
|
func makeHTTPSIndex(req string) *registry.IndexInfo {
|
|
|
|
index := ®istry.IndexInfo{
|
2015-07-21 15:40:36 -04:00
|
|
|
Name: makeHTTPSURL(req),
|
2014-10-06 21:54:52 -04:00
|
|
|
}
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
|
2022-02-26 13:13:43 -05:00
|
|
|
func makePublicIndex() *registry.IndexInfo {
|
|
|
|
index := ®istry.IndexInfo{
|
2015-07-21 15:40:36 -04:00
|
|
|
Name: IndexServer,
|
2014-10-06 21:54:52 -04:00
|
|
|
Secure: true,
|
|
|
|
Official: true,
|
|
|
|
}
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
|
2017-09-01 10:35:04 -04:00
|
|
|
func makeServiceConfig(mirrors []string, insecureRegistries []string) (*serviceConfig, error) {
|
2016-03-08 16:03:37 -05:00
|
|
|
options := ServiceOptions{
|
|
|
|
Mirrors: mirrors,
|
|
|
|
InsecureRegistries: insecureRegistries,
|
2014-10-06 21:54:52 -04:00
|
|
|
}
|
|
|
|
|
2016-03-08 16:03:37 -05:00
|
|
|
return newServiceConfig(options)
|
2014-10-06 21:54:52 -04:00
|
|
|
}
|
|
|
|
|
2013-07-24 20:44:55 -04: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")
|
|
|
|
h.Add("X-Docker-Registry-Version", "0.0.0")
|
|
|
|
h.Add("X-Docker-Registry-Config", "mock")
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeResponse(w http.ResponseWriter, message interface{}, code int) {
|
|
|
|
writeHeaders(w)
|
|
|
|
w.WriteHeader(code)
|
|
|
|
body, err := json.Marshal(message)
|
|
|
|
if err != nil {
|
|
|
|
io.WriteString(w, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlerGetPing(w http.ResponseWriter, r *http.Request) {
|
2019-10-13 11:25:25 -04:00
|
|
|
writeResponse(w, true, http.StatusOK)
|
2013-07-24 20:44:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func handlerSearch(w http.ResponseWriter, r *http.Request) {
|
2022-02-26 13:13:43 -05:00
|
|
|
result := ®istry.SearchResults{
|
2014-03-13 13:40:34 -04:00
|
|
|
Query: "fakequery",
|
|
|
|
NumResults: 1,
|
2022-02-26 13:13:43 -05:00
|
|
|
Results: []registry.SearchResult{{Name: "fakeimage", StarCount: 42}},
|
2014-03-13 13:40:34 -04:00
|
|
|
}
|
2019-10-13 11:25:25 -04:00
|
|
|
writeResponse(w, result, http.StatusOK)
|
2013-07-24 20:44:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPing(t *testing.T) {
|
|
|
|
res, err := http.Get(makeURL("/v1/_ping"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-02-26 16:53:06 -05:00
|
|
|
assert.Equal(t, res.StatusCode, http.StatusOK, "")
|
|
|
|
assert.Equal(t, res.Header.Get("X-Docker-Registry-Config"), "mock", "This is not a Mocked Registry")
|
2013-07-24 20:44:55 -04:00
|
|
|
}
|