mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
registry: tests: remove unused mock code and use gotest.tools
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
0798f5f5cf
commit
746c51b54f
3 changed files with 39 additions and 362 deletions
|
@ -5,6 +5,7 @@ import (
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
registrytypes "github.com/docker/docker/api/types/registry"
|
registrytypes "github.com/docker/docker/api/types/registry"
|
||||||
|
"gotest.tools/v3/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func buildAuthConfigs() map[string]types.AuthConfig {
|
func buildAuthConfigs() map[string]types.AuthConfig {
|
||||||
|
@ -32,10 +33,10 @@ func TestResolveAuthConfigIndexServer(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
resolved := ResolveAuthConfig(authConfigs, officialIndex)
|
resolved := ResolveAuthConfig(authConfigs, officialIndex)
|
||||||
assertEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to return IndexServer")
|
assert.Equal(t, resolved, indexConfig, "Expected ResolveAuthConfig to return IndexServer")
|
||||||
|
|
||||||
resolved = ResolveAuthConfig(authConfigs, privateIndex)
|
resolved = ResolveAuthConfig(authConfigs, privateIndex)
|
||||||
assertNotEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to not return IndexServer")
|
assert.Check(t, resolved != indexConfig, "Expected ResolveAuthConfig to not return IndexServer")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResolveAuthConfigFullURL(t *testing.T) {
|
func TestResolveAuthConfigFullURL(t *testing.T) {
|
||||||
|
|
|
@ -3,93 +3,21 @@ package registry // import "github.com/docker/docker/registry"
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/docker/distribution/reference"
|
|
||||||
registrytypes "github.com/docker/docker/api/types/registry"
|
registrytypes "github.com/docker/docker/api/types/registry"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
"gotest.tools/v3/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
testHTTPServer *httptest.Server
|
testHTTPServer *httptest.Server
|
||||||
testHTTPSServer *httptest.Server
|
testHTTPSServer *httptest.Server
|
||||||
testLayers = map[string]map[string]string{
|
|
||||||
"77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20": {
|
|
||||||
"json": `{"id":"77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
|
|
||||||
"comment":"test base image","created":"2013-03-23T12:53:11.10432-07:00",
|
|
||||||
"container_config":{"Hostname":"","User":"","Memory":0,"MemorySwap":0,
|
|
||||||
"CpuShares":0,"AttachStdin":false,"AttachStdout":false,"AttachStderr":false,
|
|
||||||
"Tty":false,"OpenStdin":false,"StdinOnce":false,
|
|
||||||
"Env":null,"Cmd":null,"Dns":null,"Image":"","Volumes":null,
|
|
||||||
"VolumesFrom":"","Entrypoint":null},"Size":424242}`,
|
|
||||||
"checksum_simple": "sha256:1ac330d56e05eef6d438586545ceff7550d3bdcb6b19961f12c5ba714ee1bb37",
|
|
||||||
"checksum_tarsum": "tarsum+sha256:4409a0685741ca86d38df878ed6f8cbba4c99de5dc73cd71aef04be3bb70be7c",
|
|
||||||
"ancestry": `["77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20"]`,
|
|
||||||
"layer": string([]byte{
|
|
||||||
0x1f, 0x8b, 0x08, 0x08, 0x0e, 0xb0, 0xee, 0x51, 0x02, 0x03, 0x6c, 0x61, 0x79, 0x65,
|
|
||||||
0x72, 0x2e, 0x74, 0x61, 0x72, 0x00, 0xed, 0xd2, 0x31, 0x0e, 0xc2, 0x30, 0x0c, 0x05,
|
|
||||||
0x50, 0xcf, 0x9c, 0xc2, 0x27, 0x48, 0xed, 0x38, 0x4e, 0xce, 0x13, 0x44, 0x2b, 0x66,
|
|
||||||
0x62, 0x24, 0x8e, 0x4f, 0xa0, 0x15, 0x63, 0xb6, 0x20, 0x21, 0xfc, 0x96, 0xbf, 0x78,
|
|
||||||
0xb0, 0xf5, 0x1d, 0x16, 0x98, 0x8e, 0x88, 0x8a, 0x2a, 0xbe, 0x33, 0xef, 0x49, 0x31,
|
|
||||||
0xed, 0x79, 0x40, 0x8e, 0x5c, 0x44, 0x85, 0x88, 0x33, 0x12, 0x73, 0x2c, 0x02, 0xa8,
|
|
||||||
0xf0, 0x05, 0xf7, 0x66, 0xf5, 0xd6, 0x57, 0x69, 0xd7, 0x7a, 0x19, 0xcd, 0xf5, 0xb1,
|
|
||||||
0x6d, 0x1b, 0x1f, 0xf9, 0xba, 0xe3, 0x93, 0x3f, 0x22, 0x2c, 0xb6, 0x36, 0x0b, 0xf6,
|
|
||||||
0xb0, 0xa9, 0xfd, 0xe7, 0x94, 0x46, 0xfd, 0xeb, 0xd1, 0x7f, 0x2c, 0xc4, 0xd2, 0xfb,
|
|
||||||
0x97, 0xfe, 0x02, 0x80, 0xe4, 0xfd, 0x4f, 0x77, 0xae, 0x6d, 0x3d, 0x81, 0x73, 0xce,
|
|
||||||
0xb9, 0x7f, 0xf3, 0x04, 0x41, 0xc1, 0xab, 0xc6, 0x00, 0x0a, 0x00, 0x00,
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
"42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d": {
|
|
||||||
"json": `{"id":"42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d",
|
|
||||||
"parent":"77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
|
|
||||||
"comment":"test base image","created":"2013-03-23T12:55:11.10432-07:00",
|
|
||||||
"container_config":{"Hostname":"","User":"","Memory":0,"MemorySwap":0,
|
|
||||||
"CpuShares":0,"AttachStdin":false,"AttachStdout":false,"AttachStderr":false,
|
|
||||||
"Tty":false,"OpenStdin":false,"StdinOnce":false,
|
|
||||||
"Env":null,"Cmd":null,"Dns":null,"Image":"","Volumes":null,
|
|
||||||
"VolumesFrom":"","Entrypoint":null},"Size":424242}`,
|
|
||||||
"checksum_simple": "sha256:bea7bf2e4bacd479344b737328db47b18880d09096e6674165533aa994f5e9f2",
|
|
||||||
"checksum_tarsum": "tarsum+sha256:68fdb56fb364f074eec2c9b3f85ca175329c4dcabc4a6a452b7272aa613a07a2",
|
|
||||||
"ancestry": `["42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d",
|
|
||||||
"77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20"]`,
|
|
||||||
"layer": string([]byte{
|
|
||||||
0x1f, 0x8b, 0x08, 0x08, 0xbd, 0xb3, 0xee, 0x51, 0x02, 0x03, 0x6c, 0x61, 0x79, 0x65,
|
|
||||||
0x72, 0x2e, 0x74, 0x61, 0x72, 0x00, 0xed, 0xd1, 0x31, 0x0e, 0xc2, 0x30, 0x0c, 0x05,
|
|
||||||
0x50, 0xcf, 0x9c, 0xc2, 0x27, 0x48, 0x9d, 0x38, 0x8e, 0xcf, 0x53, 0x51, 0xaa, 0x56,
|
|
||||||
0xea, 0x44, 0x82, 0xc4, 0xf1, 0x09, 0xb4, 0xea, 0x98, 0x2d, 0x48, 0x08, 0xbf, 0xe5,
|
|
||||||
0x2f, 0x1e, 0xfc, 0xf5, 0xdd, 0x00, 0xdd, 0x11, 0x91, 0x8a, 0xe0, 0x27, 0xd3, 0x9e,
|
|
||||||
0x14, 0xe2, 0x9e, 0x07, 0xf4, 0xc1, 0x2b, 0x0b, 0xfb, 0xa4, 0x82, 0xe4, 0x3d, 0x93,
|
|
||||||
0x02, 0x0a, 0x7c, 0xc1, 0x23, 0x97, 0xf1, 0x5e, 0x5f, 0xc9, 0xcb, 0x38, 0xb5, 0xee,
|
|
||||||
0xea, 0xd9, 0x3c, 0xb7, 0x4b, 0xbe, 0x7b, 0x9c, 0xf9, 0x23, 0xdc, 0x50, 0x6e, 0xb9,
|
|
||||||
0xb8, 0xf2, 0x2c, 0x5d, 0xf7, 0x4f, 0x31, 0xb6, 0xf6, 0x4f, 0xc7, 0xfe, 0x41, 0x55,
|
|
||||||
0x63, 0xdd, 0x9f, 0x89, 0x09, 0x90, 0x6c, 0xff, 0xee, 0xae, 0xcb, 0xba, 0x4d, 0x17,
|
|
||||||
0x30, 0xc6, 0x18, 0xf3, 0x67, 0x5e, 0xc1, 0xed, 0x21, 0x5d, 0x00, 0x0a, 0x00, 0x00,
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
testRepositories = map[string]map[string]string{
|
|
||||||
"foo42/bar": {
|
|
||||||
"latest": "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d",
|
|
||||||
"test": "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
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")},
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -97,14 +25,6 @@ func init() {
|
||||||
|
|
||||||
// /v1/
|
// /v1/
|
||||||
r.HandleFunc("/v1/_ping", handlerGetPing).Methods(http.MethodGet)
|
r.HandleFunc("/v1/_ping", handlerGetPing).Methods(http.MethodGet)
|
||||||
r.HandleFunc("/v1/images/{image_id:[^/]+}/{action:json|layer|ancestry}", handlerGetImage).Methods(http.MethodGet)
|
|
||||||
r.HandleFunc("/v1/images/{image_id:[^/]+}/{action:json|layer|checksum}", handlerPutImage).Methods(http.MethodPut)
|
|
||||||
r.HandleFunc("/v1/repositories/{repository:.+}/tags", handlerGetDeleteTags).Methods(http.MethodGet, http.MethodDelete)
|
|
||||||
r.HandleFunc("/v1/repositories/{repository:.+}/tags/{tag:.+}", handlerGetTag).Methods(http.MethodGet)
|
|
||||||
r.HandleFunc("/v1/repositories/{repository:.+}/tags/{tag:.+}", handlerPutTag).Methods(http.MethodPut)
|
|
||||||
r.HandleFunc("/v1/users{null:.*}", handlerUsers).Methods(http.MethodGet, http.MethodPost, http.MethodPut)
|
|
||||||
r.HandleFunc("/v1/repositories/{repository:.+}{action:/images|/}", handlerImages).Methods(http.MethodGet, http.MethodPut, http.MethodDelete)
|
|
||||||
r.HandleFunc("/v1/repositories/{repository:.+}/auth", handlerAuth).Methods(http.MethodPut)
|
|
||||||
r.HandleFunc("/v1/search", handlerSearch).Methods(http.MethodGet)
|
r.HandleFunc("/v1/search", handlerSearch).Methods(http.MethodGet)
|
||||||
|
|
||||||
// /v2/
|
// /v2/
|
||||||
|
@ -119,6 +39,12 @@ func init() {
|
||||||
// I believe in future Go versions this will fail, so let's fix it later
|
// I believe in future Go versions this will fail, so let's fix it later
|
||||||
return net.LookupIP(host)
|
return net.LookupIP(host)
|
||||||
}
|
}
|
||||||
|
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")},
|
||||||
|
}
|
||||||
for h, addrs := range mockHosts {
|
for h, addrs := range mockHosts {
|
||||||
if host == h {
|
if host == h {
|
||||||
return addrs, nil
|
return addrs, nil
|
||||||
|
@ -135,7 +61,7 @@ func init() {
|
||||||
|
|
||||||
func handlerAccessLog(handler http.Handler) http.Handler {
|
func handlerAccessLog(handler http.Handler) http.Handler {
|
||||||
logHandler := func(w http.ResponseWriter, r *http.Request) {
|
logHandler := func(w http.ResponseWriter, r *http.Request) {
|
||||||
logrus.Debugf("%s \"%s %s\"", r.RemoteAddr, r.Method, r.URL)
|
logrus.Debugf(`%s "%s %s"`, r.RemoteAddr, r.Method, r.URL)
|
||||||
handler.ServeHTTP(w, r)
|
handler.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
return http.HandlerFunc(logHandler)
|
return http.HandlerFunc(logHandler)
|
||||||
|
@ -203,247 +129,10 @@ func writeResponse(w http.ResponseWriter, message interface{}, code int) {
|
||||||
w.Write(body)
|
w.Write(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
func readJSON(r *http.Request, dest interface{}) error {
|
|
||||||
body, err := io.ReadAll(r.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return json.Unmarshal(body, dest)
|
|
||||||
}
|
|
||||||
|
|
||||||
func apiError(w http.ResponseWriter, message string, code int) {
|
|
||||||
body := map[string]string{
|
|
||||||
"error": message,
|
|
||||||
}
|
|
||||||
writeResponse(w, body, code)
|
|
||||||
}
|
|
||||||
|
|
||||||
func assertEqual(t *testing.T, a interface{}, b interface{}, message string) {
|
|
||||||
if a == b {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(message) == 0 {
|
|
||||||
message = fmt.Sprintf("%v != %v", a, b)
|
|
||||||
}
|
|
||||||
t.Fatal(message)
|
|
||||||
}
|
|
||||||
|
|
||||||
func assertNotEqual(t *testing.T, a interface{}, b interface{}, message string) {
|
|
||||||
if a != b {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(message) == 0 {
|
|
||||||
message = fmt.Sprintf("%v == %v", a, b)
|
|
||||||
}
|
|
||||||
t.Fatal(message)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Similar to assertEqual, but does not stop test
|
|
||||||
func checkEqual(t *testing.T, a interface{}, b interface{}, messagePrefix string) {
|
|
||||||
if a == b {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
message := fmt.Sprintf("%v != %v", a, b)
|
|
||||||
if len(messagePrefix) != 0 {
|
|
||||||
message = messagePrefix + ": " + message
|
|
||||||
}
|
|
||||||
t.Error(message)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Similar to assertNotEqual, but does not stop test
|
|
||||||
func checkNotEqual(t *testing.T, a interface{}, b interface{}, messagePrefix string) {
|
|
||||||
if a != b {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
message := fmt.Sprintf("%v == %v", a, b)
|
|
||||||
if len(messagePrefix) != 0 {
|
|
||||||
message = messagePrefix + ": " + message
|
|
||||||
}
|
|
||||||
t.Error(message)
|
|
||||||
}
|
|
||||||
|
|
||||||
func requiresAuth(w http.ResponseWriter, r *http.Request) bool {
|
|
||||||
writeCookie := func() {
|
|
||||||
value := fmt.Sprintf("FAKE-SESSION-%d", time.Now().UnixNano())
|
|
||||||
cookie := &http.Cookie{Name: "session", Value: value, MaxAge: 3600}
|
|
||||||
http.SetCookie(w, cookie)
|
|
||||||
// FIXME(sam): this should be sent only on Index routes
|
|
||||||
value = fmt.Sprintf("FAKE-TOKEN-%d", time.Now().UnixNano())
|
|
||||||
w.Header().Add("X-Docker-Token", value)
|
|
||||||
}
|
|
||||||
if len(r.Cookies()) > 0 {
|
|
||||||
writeCookie()
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if len(r.Header.Get("Authorization")) > 0 {
|
|
||||||
writeCookie()
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
w.Header().Add("WWW-Authenticate", "token")
|
|
||||||
apiError(w, "Wrong auth", http.StatusUnauthorized)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func handlerGetPing(w http.ResponseWriter, r *http.Request) {
|
func handlerGetPing(w http.ResponseWriter, r *http.Request) {
|
||||||
writeResponse(w, true, http.StatusOK)
|
writeResponse(w, true, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handlerGetImage(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if !requiresAuth(w, r) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
vars := mux.Vars(r)
|
|
||||||
layer, exists := testLayers[vars["image_id"]]
|
|
||||||
if !exists {
|
|
||||||
http.NotFound(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
writeHeaders(w)
|
|
||||||
layerSize := len(layer["layer"])
|
|
||||||
w.Header().Add("X-Docker-Size", strconv.Itoa(layerSize))
|
|
||||||
io.WriteString(w, layer[vars["action"]])
|
|
||||||
}
|
|
||||||
|
|
||||||
func handlerPutImage(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if !requiresAuth(w, r) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
vars := mux.Vars(r)
|
|
||||||
imageID := vars["image_id"]
|
|
||||||
action := vars["action"]
|
|
||||||
layer, exists := testLayers[imageID]
|
|
||||||
if !exists {
|
|
||||||
if action != "json" {
|
|
||||||
http.NotFound(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
layer = make(map[string]string)
|
|
||||||
testLayers[imageID] = layer
|
|
||||||
}
|
|
||||||
if checksum := r.Header.Get("X-Docker-Checksum"); checksum != "" {
|
|
||||||
if checksum != layer["checksum_simple"] && checksum != layer["checksum_tarsum"] {
|
|
||||||
apiError(w, "Wrong checksum", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
body, err := io.ReadAll(r.Body)
|
|
||||||
if err != nil {
|
|
||||||
apiError(w, fmt.Sprintf("Error: %s", err), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
layer[action] = string(body)
|
|
||||||
writeResponse(w, true, http.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
func handlerGetDeleteTags(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if !requiresAuth(w, r) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
repositoryName, err := reference.WithName(mux.Vars(r)["repository"])
|
|
||||||
if err != nil {
|
|
||||||
apiError(w, "Could not parse repository", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
tags, exists := testRepositories[repositoryName.String()]
|
|
||||||
if !exists {
|
|
||||||
apiError(w, "Repository not found", http.StatusNotFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if r.Method == http.MethodDelete {
|
|
||||||
delete(testRepositories, repositoryName.String())
|
|
||||||
writeResponse(w, true, http.StatusOK)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
writeResponse(w, tags, http.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
func handlerGetTag(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if !requiresAuth(w, r) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
vars := mux.Vars(r)
|
|
||||||
repositoryName, err := reference.WithName(vars["repository"])
|
|
||||||
if err != nil {
|
|
||||||
apiError(w, "Could not parse repository", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
tagName := vars["tag"]
|
|
||||||
tags, exists := testRepositories[repositoryName.String()]
|
|
||||||
if !exists {
|
|
||||||
apiError(w, "Repository not found", http.StatusNotFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
tag, exists := tags[tagName]
|
|
||||||
if !exists {
|
|
||||||
apiError(w, "Tag not found", http.StatusNotFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
writeResponse(w, tag, http.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
func handlerPutTag(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if !requiresAuth(w, r) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
vars := mux.Vars(r)
|
|
||||||
repositoryName, err := reference.WithName(vars["repository"])
|
|
||||||
if err != nil {
|
|
||||||
apiError(w, "Could not parse repository", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
tagName := vars["tag"]
|
|
||||||
tags, exists := testRepositories[repositoryName.String()]
|
|
||||||
if !exists {
|
|
||||||
tags = make(map[string]string)
|
|
||||||
testRepositories[repositoryName.String()] = tags
|
|
||||||
}
|
|
||||||
tagValue := ""
|
|
||||||
readJSON(r, tagValue)
|
|
||||||
tags[tagName] = tagValue
|
|
||||||
writeResponse(w, true, http.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
func handlerUsers(w http.ResponseWriter, r *http.Request) {
|
|
||||||
code := http.StatusOK
|
|
||||||
if r.Method == http.MethodPost {
|
|
||||||
code = http.StatusCreated
|
|
||||||
} else if r.Method == http.MethodPut {
|
|
||||||
code = http.StatusNoContent
|
|
||||||
}
|
|
||||||
writeResponse(w, "", code)
|
|
||||||
}
|
|
||||||
|
|
||||||
func handlerImages(w http.ResponseWriter, r *http.Request) {
|
|
||||||
u, _ := url.Parse(testHTTPServer.URL)
|
|
||||||
w.Header().Add("X-Docker-Endpoints", fmt.Sprintf("%s , %s ", u.Host, "test.example.com"))
|
|
||||||
w.Header().Add("X-Docker-Token", fmt.Sprintf("FAKE-SESSION-%d", time.Now().UnixNano()))
|
|
||||||
if r.Method == http.MethodPut {
|
|
||||||
if strings.HasSuffix(r.URL.Path, "images") {
|
|
||||||
writeResponse(w, "", http.StatusNoContent)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
writeResponse(w, "", http.StatusOK)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if r.Method == http.MethodDelete {
|
|
||||||
writeResponse(w, "", http.StatusNoContent)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var images []map[string]string
|
|
||||||
for imageID, layer := range testLayers {
|
|
||||||
image := make(map[string]string)
|
|
||||||
image["id"] = imageID
|
|
||||||
image["checksum"] = layer["checksum_tarsum"]
|
|
||||||
image["Tag"] = "latest"
|
|
||||||
images = append(images, image)
|
|
||||||
}
|
|
||||||
writeResponse(w, images, http.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
func handlerAuth(w http.ResponseWriter, r *http.Request) {
|
|
||||||
writeResponse(w, "OK", http.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
func handlerSearch(w http.ResponseWriter, r *http.Request) {
|
func handlerSearch(w http.ResponseWriter, r *http.Request) {
|
||||||
result := ®istrytypes.SearchResults{
|
result := ®istrytypes.SearchResults{
|
||||||
Query: "fakequery",
|
Query: "fakequery",
|
||||||
|
@ -458,18 +147,6 @@ func TestPing(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
assertEqual(t, res.StatusCode, http.StatusOK, "")
|
assert.Equal(t, res.StatusCode, http.StatusOK, "")
|
||||||
assertEqual(t, res.Header.Get("X-Docker-Registry-Config"), "mock",
|
assert.Equal(t, res.Header.Get("X-Docker-Registry-Config"), "mock", "This is not a Mocked Registry")
|
||||||
"This is not a Mocked Registry")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Uncomment this to test Mocked Registry locally with curl
|
|
||||||
* WARNING: Don't push on the repos uncommented, it'll block the tests
|
|
||||||
*
|
|
||||||
func TestWait(t *testing.T) {
|
|
||||||
logrus.Println("Test HTTP server ready and waiting:", testHTTPServer.URL)
|
|
||||||
c := make(chan int)
|
|
||||||
<-c
|
|
||||||
}
|
|
||||||
|
|
||||||
//*/
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
registrytypes "github.com/docker/docker/api/types/registry"
|
registrytypes "github.com/docker/docker/api/types/registry"
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
|
is "gotest.tools/v3/assert/cmp"
|
||||||
"gotest.tools/v3/skip"
|
"gotest.tools/v3/skip"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -55,7 +56,7 @@ func TestPingRegistryEndpoint(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEqual(t, regInfo.Standalone, expectedStandalone, assertMessage)
|
assert.Equal(t, regInfo.Standalone, expectedStandalone, assertMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
testPing(makeIndex("/v1/"), true, "Expected standalone to be true (default)")
|
testPing(makeIndex("/v1/"), true, "Expected standalone to be true (default)")
|
||||||
|
@ -77,50 +78,48 @@ func TestEndpoint(t *testing.T) {
|
||||||
assertInsecureIndex := func(index *registrytypes.IndexInfo) {
|
assertInsecureIndex := func(index *registrytypes.IndexInfo) {
|
||||||
index.Secure = true
|
index.Secure = true
|
||||||
_, err := NewV1Endpoint(index, "", nil)
|
_, err := NewV1Endpoint(index, "", nil)
|
||||||
assertNotEqual(t, err, nil, index.Name+": Expected error for insecure index")
|
assert.ErrorContains(t, err, "insecure-registry", index.Name+": Expected insecure-registry error for insecure index")
|
||||||
assertEqual(t, strings.Contains(err.Error(), "insecure-registry"), true, index.Name+": Expected insecure-registry error for insecure index")
|
|
||||||
index.Secure = false
|
index.Secure = false
|
||||||
}
|
}
|
||||||
|
|
||||||
assertSecureIndex := func(index *registrytypes.IndexInfo) {
|
assertSecureIndex := func(index *registrytypes.IndexInfo) {
|
||||||
index.Secure = true
|
index.Secure = true
|
||||||
_, err := NewV1Endpoint(index, "", nil)
|
_, err := NewV1Endpoint(index, "", nil)
|
||||||
assertNotEqual(t, err, nil, index.Name+": Expected cert error for secure index")
|
assert.ErrorContains(t, err, "certificate signed by unknown authority", index.Name+": Expected cert error for secure index")
|
||||||
assertEqual(t, strings.Contains(err.Error(), "certificate signed by unknown authority"), true, index.Name+": Expected cert error for secure index")
|
|
||||||
index.Secure = false
|
index.Secure = false
|
||||||
}
|
}
|
||||||
|
|
||||||
index := ®istrytypes.IndexInfo{}
|
index := ®istrytypes.IndexInfo{}
|
||||||
index.Name = makeURL("/v1/")
|
index.Name = makeURL("/v1/")
|
||||||
endpoint := expandEndpoint(index)
|
endpoint := expandEndpoint(index)
|
||||||
assertEqual(t, endpoint.String(), index.Name, "Expected endpoint to be "+index.Name)
|
assert.Equal(t, endpoint.String(), index.Name, "Expected endpoint to be "+index.Name)
|
||||||
assertInsecureIndex(index)
|
assertInsecureIndex(index)
|
||||||
|
|
||||||
index.Name = makeURL("")
|
index.Name = makeURL("")
|
||||||
endpoint = expandEndpoint(index)
|
endpoint = expandEndpoint(index)
|
||||||
assertEqual(t, endpoint.String(), index.Name+"/v1/", index.Name+": Expected endpoint to be "+index.Name+"/v1/")
|
assert.Equal(t, endpoint.String(), index.Name+"/v1/", index.Name+": Expected endpoint to be "+index.Name+"/v1/")
|
||||||
assertInsecureIndex(index)
|
assertInsecureIndex(index)
|
||||||
|
|
||||||
httpURL := makeURL("")
|
httpURL := makeURL("")
|
||||||
index.Name = strings.SplitN(httpURL, "://", 2)[1]
|
index.Name = strings.SplitN(httpURL, "://", 2)[1]
|
||||||
endpoint = expandEndpoint(index)
|
endpoint = expandEndpoint(index)
|
||||||
assertEqual(t, endpoint.String(), httpURL+"/v1/", index.Name+": Expected endpoint to be "+httpURL+"/v1/")
|
assert.Equal(t, endpoint.String(), httpURL+"/v1/", index.Name+": Expected endpoint to be "+httpURL+"/v1/")
|
||||||
assertInsecureIndex(index)
|
assertInsecureIndex(index)
|
||||||
|
|
||||||
index.Name = makeHTTPSURL("/v1/")
|
index.Name = makeHTTPSURL("/v1/")
|
||||||
endpoint = expandEndpoint(index)
|
endpoint = expandEndpoint(index)
|
||||||
assertEqual(t, endpoint.String(), index.Name, "Expected endpoint to be "+index.Name)
|
assert.Equal(t, endpoint.String(), index.Name, "Expected endpoint to be "+index.Name)
|
||||||
assertSecureIndex(index)
|
assertSecureIndex(index)
|
||||||
|
|
||||||
index.Name = makeHTTPSURL("")
|
index.Name = makeHTTPSURL("")
|
||||||
endpoint = expandEndpoint(index)
|
endpoint = expandEndpoint(index)
|
||||||
assertEqual(t, endpoint.String(), index.Name+"/v1/", index.Name+": Expected endpoint to be "+index.Name+"/v1/")
|
assert.Equal(t, endpoint.String(), index.Name+"/v1/", index.Name+": Expected endpoint to be "+index.Name+"/v1/")
|
||||||
assertSecureIndex(index)
|
assertSecureIndex(index)
|
||||||
|
|
||||||
httpsURL := makeHTTPSURL("")
|
httpsURL := makeHTTPSURL("")
|
||||||
index.Name = strings.SplitN(httpsURL, "://", 2)[1]
|
index.Name = strings.SplitN(httpsURL, "://", 2)[1]
|
||||||
endpoint = expandEndpoint(index)
|
endpoint = expandEndpoint(index)
|
||||||
assertEqual(t, endpoint.String(), httpsURL+"/v1/", index.Name+": Expected endpoint to be "+httpsURL+"/v1/")
|
assert.Equal(t, endpoint.String(), httpsURL+"/v1/", index.Name+": Expected endpoint to be "+httpsURL+"/v1/")
|
||||||
assertSecureIndex(index)
|
assertSecureIndex(index)
|
||||||
|
|
||||||
badEndpoints := []string{
|
badEndpoints := []string{
|
||||||
|
@ -133,7 +132,7 @@ func TestEndpoint(t *testing.T) {
|
||||||
for _, address := range badEndpoints {
|
for _, address := range badEndpoints {
|
||||||
index.Name = address
|
index.Name = address
|
||||||
_, err := NewV1Endpoint(index, "", nil)
|
_, err := NewV1Endpoint(index, "", nil)
|
||||||
checkNotEqual(t, err, nil, "Expected error while expanding bad endpoint")
|
assert.Check(t, err != nil, "Expected error while expanding bad endpoint: %s", address)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -359,12 +358,12 @@ func TestParseRepositoryInfo(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
} else {
|
} else {
|
||||||
checkEqual(t, repoInfo.Index.Name, expectedRepoInfo.Index.Name, reposName)
|
assert.Check(t, is.Equal(repoInfo.Index.Name, expectedRepoInfo.Index.Name), reposName)
|
||||||
checkEqual(t, reference.Path(repoInfo.Name), expectedRepoInfo.RemoteName, reposName)
|
assert.Check(t, is.Equal(reference.Path(repoInfo.Name), expectedRepoInfo.RemoteName), reposName)
|
||||||
checkEqual(t, reference.FamiliarName(repoInfo.Name), expectedRepoInfo.LocalName, reposName)
|
assert.Check(t, is.Equal(reference.FamiliarName(repoInfo.Name), expectedRepoInfo.LocalName), reposName)
|
||||||
checkEqual(t, repoInfo.Name.Name(), expectedRepoInfo.CanonicalName, reposName)
|
assert.Check(t, is.Equal(repoInfo.Name.Name(), expectedRepoInfo.CanonicalName), reposName)
|
||||||
checkEqual(t, repoInfo.Index.Official, expectedRepoInfo.Index.Official, reposName)
|
assert.Check(t, is.Equal(repoInfo.Index.Official, expectedRepoInfo.Index.Official), reposName)
|
||||||
checkEqual(t, repoInfo.Official, expectedRepoInfo.Official, reposName)
|
assert.Check(t, is.Equal(repoInfo.Official, expectedRepoInfo.Official), reposName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -376,10 +375,10 @@ func TestNewIndexInfo(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
} else {
|
} else {
|
||||||
checkEqual(t, index.Name, expectedIndexInfo.Name, indexName+" name")
|
assert.Check(t, is.Equal(index.Name, expectedIndexInfo.Name), indexName+" name")
|
||||||
checkEqual(t, index.Official, expectedIndexInfo.Official, indexName+" is official")
|
assert.Check(t, is.Equal(index.Official, expectedIndexInfo.Official), indexName+" is official")
|
||||||
checkEqual(t, index.Secure, expectedIndexInfo.Secure, indexName+" is secure")
|
assert.Check(t, is.Equal(index.Secure, expectedIndexInfo.Secure), indexName+" is secure")
|
||||||
checkEqual(t, len(index.Mirrors), len(expectedIndexInfo.Mirrors), indexName+" mirrors")
|
assert.Check(t, is.Equal(len(index.Mirrors), len(expectedIndexInfo.Mirrors)), indexName+" mirrors")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -552,9 +551,9 @@ func TestSearchRepositories(t *testing.T) {
|
||||||
if results == nil {
|
if results == nil {
|
||||||
t.Fatal("Expected non-nil SearchResults object")
|
t.Fatal("Expected non-nil SearchResults object")
|
||||||
}
|
}
|
||||||
assertEqual(t, results.NumResults, 1, "Expected 1 search results")
|
assert.Equal(t, results.NumResults, 1, "Expected 1 search results")
|
||||||
assertEqual(t, results.Query, "fakequery", "Expected 'fakequery' as query")
|
assert.Equal(t, results.Query, "fakequery", "Expected 'fakequery' as query")
|
||||||
assertEqual(t, results.Results[0].StarCount, 42, "Expected 'fakeimage' to have 42 stars")
|
assert.Equal(t, results.Results[0].StarCount, 42, "Expected 'fakeimage' to have 42 stars")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTrustedLocation(t *testing.T) {
|
func TestTrustedLocation(t *testing.T) {
|
||||||
|
@ -580,7 +579,7 @@ func TestAddRequiredHeadersToRedirectedRequests(t *testing.T) {
|
||||||
reqFrom.Header.Add("Authorization", "super_secret")
|
reqFrom.Header.Add("Authorization", "super_secret")
|
||||||
reqTo, _ := http.NewRequest(http.MethodGet, urls[1], nil)
|
reqTo, _ := http.NewRequest(http.MethodGet, urls[1], nil)
|
||||||
|
|
||||||
addRequiredHeadersToRedirectedRequests(reqTo, []*http.Request{reqFrom})
|
_ = addRequiredHeadersToRedirectedRequests(reqTo, []*http.Request{reqFrom})
|
||||||
|
|
||||||
if len(reqTo.Header) != 1 {
|
if len(reqTo.Header) != 1 {
|
||||||
t.Fatalf("Expected 1 headers, got %d", len(reqTo.Header))
|
t.Fatalf("Expected 1 headers, got %d", len(reqTo.Header))
|
||||||
|
@ -604,7 +603,7 @@ func TestAddRequiredHeadersToRedirectedRequests(t *testing.T) {
|
||||||
reqFrom.Header.Add("Authorization", "super_secret")
|
reqFrom.Header.Add("Authorization", "super_secret")
|
||||||
reqTo, _ := http.NewRequest(http.MethodGet, urls[1], nil)
|
reqTo, _ := http.NewRequest(http.MethodGet, urls[1], nil)
|
||||||
|
|
||||||
addRequiredHeadersToRedirectedRequests(reqTo, []*http.Request{reqFrom})
|
_ = addRequiredHeadersToRedirectedRequests(reqTo, []*http.Request{reqFrom})
|
||||||
|
|
||||||
if len(reqTo.Header) != 2 {
|
if len(reqTo.Header) != 2 {
|
||||||
t.Fatalf("Expected 2 headers, got %d", len(reqTo.Header))
|
t.Fatalf("Expected 2 headers, got %d", len(reqTo.Header))
|
||||||
|
|
Loading…
Add table
Reference in a new issue