2013-05-15 03:41:39 +02:00
|
|
|
package registry
|
|
|
|
|
2013-07-31 19:07:31 +02:00
|
|
|
import (
|
2014-04-14 20:32:47 +02:00
|
|
|
"fmt"
|
2014-06-05 20:37:37 +02:00
|
|
|
"net/http"
|
2015-06-20 14:28:18 +02:00
|
|
|
"net/http/httputil"
|
2014-04-14 20:32:47 +02:00
|
|
|
"net/url"
|
2013-07-31 19:07:31 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2014-06-05 20:37:37 +02:00
|
|
|
|
2015-05-17 11:07:48 +02:00
|
|
|
"github.com/docker/distribution/registry/client/transport"
|
2015-12-04 22:55:15 +01:00
|
|
|
"github.com/docker/docker/reference"
|
2016-01-05 01:05:26 +01:00
|
|
|
"github.com/docker/engine-api/types"
|
|
|
|
registrytypes "github.com/docker/engine-api/types/registry"
|
2013-07-31 19:07:31 +02:00
|
|
|
)
|
2013-07-31 19:12:40 +02:00
|
|
|
|
2013-07-31 19:07:31 +02:00
|
|
|
var (
|
2014-10-06 21:34:39 +02:00
|
|
|
token = []string{"fake-token"}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
imageID = "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d"
|
|
|
|
REPO = "foo42/bar"
|
2013-07-31 19:07:31 +02:00
|
|
|
)
|
|
|
|
|
2014-08-07 16:43:06 +02:00
|
|
|
func spawnTestRegistrySession(t *testing.T) *Session {
|
2015-12-12 05:11:42 +01:00
|
|
|
authConfig := &types.AuthConfig{}
|
2016-03-01 08:07:41 +01:00
|
|
|
endpoint, err := NewV1Endpoint(makeIndex("/v1/"), "", nil)
|
2014-08-27 01:21:04 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-01-04 19:36:01 +01:00
|
|
|
userAgent := "docker test client"
|
2015-02-12 19:23:22 +01:00
|
|
|
var tr http.RoundTripper = debugTransport{NewTransport(nil), t.Log}
|
2016-01-04 19:36:01 +01:00
|
|
|
tr = transport.NewTransport(AuthTransport(tr, authConfig, false), DockerHeaders(userAgent, nil)...)
|
2015-05-14 16:12:54 +02:00
|
|
|
client := HTTPClient(tr)
|
|
|
|
r, err := NewSession(client, authConfig, endpoint)
|
2013-07-31 19:07:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-14 16:12:54 +02:00
|
|
|
// In a normal scenario for the v1 registry, the client should send a `X-Docker-Token: true`
|
|
|
|
// header while authenticating, in order to retrieve a token that can be later used to
|
|
|
|
// perform authenticated actions.
|
|
|
|
//
|
|
|
|
// The mock v1 registry does not support that, (TODO(tiborvass): support it), instead,
|
|
|
|
// it will consider authenticated any request with the header `X-Docker-Token: fake-token`.
|
|
|
|
//
|
|
|
|
// Because we know that the client's transport is an `*authTransport` we simply cast it,
|
|
|
|
// in order to set the internal cached token to the fake token, and thus send that fake token
|
|
|
|
// upon every subsequent requests.
|
|
|
|
r.client.Transport.(*authTransport).token = token
|
2013-07-31 19:07:31 +02:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPingRegistryEndpoint(t *testing.T) {
|
2015-12-12 03:14:52 +01:00
|
|
|
testPing := func(index *registrytypes.IndexInfo, expectedStandalone bool, assertMessage string) {
|
2016-03-01 08:07:41 +01:00
|
|
|
ep, err := NewV1Endpoint(index, "", nil)
|
2014-10-07 03:54:52 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
regInfo, err := ep.Ping()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEqual(t, regInfo.Standalone, expectedStandalone, assertMessage)
|
2014-08-27 01:21:04 +02:00
|
|
|
}
|
2014-10-07 03:54:52 +02:00
|
|
|
|
|
|
|
testPing(makeIndex("/v1/"), true, "Expected standalone to be true (default)")
|
2015-07-21 21:40:36 +02:00
|
|
|
testPing(makeHTTPSIndex("/v1/"), true, "Expected standalone to be true (default)")
|
2014-10-07 03:54:52 +02:00
|
|
|
testPing(makePublicIndex(), false, "Expected standalone to be false for public index")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEndpoint(t *testing.T) {
|
|
|
|
// Simple wrapper to fail test if err != nil
|
2016-03-01 08:07:41 +01:00
|
|
|
expandEndpoint := func(index *registrytypes.IndexInfo) *V1Endpoint {
|
|
|
|
endpoint, err := NewV1Endpoint(index, "", nil)
|
2014-10-07 03:54:52 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return endpoint
|
|
|
|
}
|
|
|
|
|
2015-12-12 03:14:52 +01:00
|
|
|
assertInsecureIndex := func(index *registrytypes.IndexInfo) {
|
2014-10-07 03:54:52 +02:00
|
|
|
index.Secure = true
|
2016-03-01 08:07:41 +01:00
|
|
|
_, err := NewV1Endpoint(index, "", nil)
|
2014-10-07 03:54:52 +02:00
|
|
|
assertNotEqual(t, err, nil, index.Name+": Expected 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
|
|
|
|
}
|
|
|
|
|
2015-12-12 03:14:52 +01:00
|
|
|
assertSecureIndex := func(index *registrytypes.IndexInfo) {
|
2014-10-07 03:54:52 +02:00
|
|
|
index.Secure = true
|
2016-03-01 08:07:41 +01:00
|
|
|
_, err := NewV1Endpoint(index, "", nil)
|
2014-10-07 03:54:52 +02:00
|
|
|
assertNotEqual(t, err, nil, 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
|
|
|
|
}
|
|
|
|
|
2015-12-12 03:14:52 +01:00
|
|
|
index := ®istrytypes.IndexInfo{}
|
2014-10-07 03:54:52 +02:00
|
|
|
index.Name = makeURL("/v1/")
|
|
|
|
endpoint := expandEndpoint(index)
|
|
|
|
assertEqual(t, endpoint.String(), index.Name, "Expected endpoint to be "+index.Name)
|
|
|
|
assertInsecureIndex(index)
|
|
|
|
|
|
|
|
index.Name = makeURL("")
|
|
|
|
endpoint = expandEndpoint(index)
|
|
|
|
assertEqual(t, endpoint.String(), index.Name+"/v1/", index.Name+": Expected endpoint to be "+index.Name+"/v1/")
|
|
|
|
assertInsecureIndex(index)
|
|
|
|
|
|
|
|
httpURL := makeURL("")
|
|
|
|
index.Name = strings.SplitN(httpURL, "://", 2)[1]
|
|
|
|
endpoint = expandEndpoint(index)
|
|
|
|
assertEqual(t, endpoint.String(), httpURL+"/v1/", index.Name+": Expected endpoint to be "+httpURL+"/v1/")
|
|
|
|
assertInsecureIndex(index)
|
|
|
|
|
2015-07-21 21:40:36 +02:00
|
|
|
index.Name = makeHTTPSURL("/v1/")
|
2014-10-07 03:54:52 +02:00
|
|
|
endpoint = expandEndpoint(index)
|
|
|
|
assertEqual(t, endpoint.String(), index.Name, "Expected endpoint to be "+index.Name)
|
|
|
|
assertSecureIndex(index)
|
|
|
|
|
2015-07-21 21:40:36 +02:00
|
|
|
index.Name = makeHTTPSURL("")
|
2014-10-07 03:54:52 +02:00
|
|
|
endpoint = expandEndpoint(index)
|
|
|
|
assertEqual(t, endpoint.String(), index.Name+"/v1/", index.Name+": Expected endpoint to be "+index.Name+"/v1/")
|
|
|
|
assertSecureIndex(index)
|
|
|
|
|
2015-07-21 21:40:36 +02:00
|
|
|
httpsURL := makeHTTPSURL("")
|
2014-10-07 03:54:52 +02:00
|
|
|
index.Name = strings.SplitN(httpsURL, "://", 2)[1]
|
|
|
|
endpoint = expandEndpoint(index)
|
|
|
|
assertEqual(t, endpoint.String(), httpsURL+"/v1/", index.Name+": Expected endpoint to be "+httpsURL+"/v1/")
|
|
|
|
assertSecureIndex(index)
|
|
|
|
|
|
|
|
badEndpoints := []string{
|
|
|
|
"http://127.0.0.1/v1/",
|
|
|
|
"https://127.0.0.1/v1/",
|
|
|
|
"http://127.0.0.1",
|
|
|
|
"https://127.0.0.1",
|
|
|
|
"127.0.0.1",
|
|
|
|
}
|
|
|
|
for _, address := range badEndpoints {
|
|
|
|
index.Name = address
|
2016-03-01 08:07:41 +01:00
|
|
|
_, err := NewV1Endpoint(index, "", nil)
|
2014-10-07 03:54:52 +02:00
|
|
|
checkNotEqual(t, err, nil, "Expected error while expanding bad endpoint")
|
2013-07-31 19:07:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRemoteHistory(t *testing.T) {
|
2014-08-07 16:43:06 +02:00
|
|
|
r := spawnTestRegistrySession(t)
|
2015-05-14 16:12:54 +02:00
|
|
|
hist, err := r.GetRemoteHistory(imageID, makeURL("/v1/"))
|
2013-07-31 19:07:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertEqual(t, len(hist), 2, "Expected 2 images in history")
|
2014-10-06 21:34:39 +02:00
|
|
|
assertEqual(t, hist[0], imageID, "Expected "+imageID+"as first ancestry")
|
2013-07-31 19:07:31 +02:00
|
|
|
assertEqual(t, hist[1], "77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
|
|
|
|
"Unexpected second ancestry")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLookupRemoteImage(t *testing.T) {
|
2014-08-07 16:43:06 +02:00
|
|
|
r := spawnTestRegistrySession(t)
|
2015-05-14 16:12:54 +02:00
|
|
|
err := r.LookupRemoteImage(imageID, makeURL("/v1/"))
|
2014-11-16 14:25:10 +01:00
|
|
|
assertEqual(t, err, nil, "Expected error of remote lookup to nil")
|
2015-05-14 16:12:54 +02:00
|
|
|
if err := r.LookupRemoteImage("abcdef", makeURL("/v1/")); err == nil {
|
2014-11-16 14:25:10 +01:00
|
|
|
t.Fatal("Expected error of remote lookup to not nil")
|
|
|
|
}
|
2013-07-31 19:07:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRemoteImageJSON(t *testing.T) {
|
2014-08-07 16:43:06 +02:00
|
|
|
r := spawnTestRegistrySession(t)
|
2015-05-14 16:12:54 +02:00
|
|
|
json, size, err := r.GetRemoteImageJSON(imageID, makeURL("/v1/"))
|
2013-07-31 19:07:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-23 23:19:58 +02:00
|
|
|
assertEqual(t, size, int64(154), "Expected size 154")
|
2013-07-31 19:07:31 +02:00
|
|
|
if len(json) <= 0 {
|
|
|
|
t.Fatal("Expected non-empty json")
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:12:54 +02:00
|
|
|
_, _, err = r.GetRemoteImageJSON("abcdef", makeURL("/v1/"))
|
2013-07-31 19:07:31 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected image not found error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRemoteImageLayer(t *testing.T) {
|
2014-08-07 16:43:06 +02:00
|
|
|
r := spawnTestRegistrySession(t)
|
2015-05-14 16:12:54 +02:00
|
|
|
data, err := r.GetRemoteImageLayer(imageID, makeURL("/v1/"), 0)
|
2013-07-31 19:07:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if data == nil {
|
|
|
|
t.Fatal("Expected non-nil data result")
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:12:54 +02:00
|
|
|
_, err = r.GetRemoteImageLayer("abcdef", makeURL("/v1/"), 0)
|
2013-07-31 19:07:31 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected image not found error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-11 00:18:15 +02:00
|
|
|
func TestGetRemoteTag(t *testing.T) {
|
|
|
|
r := spawnTestRegistrySession(t)
|
2015-11-18 23:20:54 +01:00
|
|
|
repoRef, err := reference.ParseNamed(REPO)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
tag, err := r.GetRemoteTag([]string{makeURL("/v1/")}, repoRef, "test")
|
2015-06-11 00:18:15 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertEqual(t, tag, imageID, "Expected tag test to map to "+imageID)
|
|
|
|
|
2015-11-18 23:20:54 +01:00
|
|
|
bazRef, err := reference.ParseNamed("foo42/baz")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = r.GetRemoteTag([]string{makeURL("/v1/")}, bazRef, "foo")
|
2015-06-11 00:18:15 +02:00
|
|
|
if err != ErrRepoNotFound {
|
|
|
|
t.Fatal("Expected ErrRepoNotFound error when fetching tag for bogus repo")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-31 19:07:31 +02:00
|
|
|
func TestGetRemoteTags(t *testing.T) {
|
2014-08-07 16:43:06 +02:00
|
|
|
r := spawnTestRegistrySession(t)
|
2015-11-18 23:20:54 +01:00
|
|
|
repoRef, err := reference.ParseNamed(REPO)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
tags, err := r.GetRemoteTags([]string{makeURL("/v1/")}, repoRef)
|
2013-07-31 19:07:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-11 00:18:15 +02:00
|
|
|
assertEqual(t, len(tags), 2, "Expected two tags")
|
2014-10-06 21:34:39 +02:00
|
|
|
assertEqual(t, tags["latest"], imageID, "Expected tag latest to map to "+imageID)
|
2015-06-11 00:18:15 +02:00
|
|
|
assertEqual(t, tags["test"], imageID, "Expected tag test to map to "+imageID)
|
2013-07-31 19:07:31 +02:00
|
|
|
|
2015-11-18 23:20:54 +01:00
|
|
|
bazRef, err := reference.ParseNamed("foo42/baz")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = r.GetRemoteTags([]string{makeURL("/v1/")}, bazRef)
|
2015-06-11 00:18:15 +02:00
|
|
|
if err != ErrRepoNotFound {
|
|
|
|
t.Fatal("Expected ErrRepoNotFound error when fetching tags for bogus repo")
|
2013-07-31 19:07:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRepositoryData(t *testing.T) {
|
2014-08-07 16:43:06 +02:00
|
|
|
r := spawnTestRegistrySession(t)
|
2014-10-06 21:34:39 +02:00
|
|
|
parsedURL, err := url.Parse(makeURL("/v1/"))
|
2014-04-14 20:32:47 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-10-06 21:34:39 +02:00
|
|
|
host := "http://" + parsedURL.Host + "/v1/"
|
2015-11-18 23:20:54 +01:00
|
|
|
repoRef, err := reference.ParseNamed(REPO)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
data, err := r.GetRepositoryData(repoRef)
|
2013-07-31 19:07:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertEqual(t, len(data.ImgList), 2, "Expected 2 images in ImgList")
|
2014-04-14 20:32:47 +02:00
|
|
|
assertEqual(t, len(data.Endpoints), 2,
|
|
|
|
fmt.Sprintf("Expected 2 endpoints in Endpoints, found %d instead", len(data.Endpoints)))
|
|
|
|
assertEqual(t, data.Endpoints[0], host,
|
|
|
|
fmt.Sprintf("Expected first endpoint to be %s but found %s instead", host, data.Endpoints[0]))
|
|
|
|
assertEqual(t, data.Endpoints[1], "http://test.example.com/v1/",
|
|
|
|
fmt.Sprintf("Expected first endpoint to be http://test.example.com/v1/ but found %s instead", data.Endpoints[1]))
|
|
|
|
|
2013-07-31 19:07:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushImageJSONRegistry(t *testing.T) {
|
2014-08-07 16:43:06 +02:00
|
|
|
r := spawnTestRegistrySession(t)
|
2013-07-31 19:07:31 +02:00
|
|
|
imgData := &ImgData{
|
2013-07-31 19:12:40 +02:00
|
|
|
ID: "77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
|
2013-07-31 19:07:31 +02:00
|
|
|
Checksum: "sha256:1ac330d56e05eef6d438586545ceff7550d3bdcb6b19961f12c5ba714ee1bb37",
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:12:54 +02:00
|
|
|
err := r.PushImageJSONRegistry(imgData, []byte{0x42, 0xdf, 0x0}, makeURL("/v1/"))
|
2013-07-31 19:07:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushImageLayerRegistry(t *testing.T) {
|
2014-08-07 16:43:06 +02:00
|
|
|
r := spawnTestRegistrySession(t)
|
2013-08-05 20:28:05 +02:00
|
|
|
layer := strings.NewReader("")
|
2015-05-14 16:12:54 +02:00
|
|
|
_, _, err := r.PushImageLayerRegistry(imageID, layer, makeURL("/v1/"), []byte{})
|
2013-08-05 20:28:05 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-07-31 19:07:31 +02:00
|
|
|
}
|
|
|
|
|
2014-10-07 03:54:52 +02:00
|
|
|
func TestParseRepositoryInfo(t *testing.T) {
|
2015-12-11 20:00:13 +01:00
|
|
|
type staticRepositoryInfo struct {
|
|
|
|
Index *registrytypes.IndexInfo
|
|
|
|
RemoteName string
|
|
|
|
CanonicalName string
|
|
|
|
LocalName string
|
|
|
|
Official bool
|
2015-11-18 23:20:54 +01:00
|
|
|
}
|
|
|
|
|
2015-12-11 20:00:13 +01:00
|
|
|
expectedRepoInfos := map[string]staticRepositoryInfo{
|
2014-10-07 03:54:52 +02:00
|
|
|
"fooo/bar": {
|
2015-12-12 03:14:52 +01:00
|
|
|
Index: ®istrytypes.IndexInfo{
|
2015-07-21 21:40:36 +02:00
|
|
|
Name: IndexName,
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: true,
|
|
|
|
},
|
2015-12-11 20:00:13 +01:00
|
|
|
RemoteName: "fooo/bar",
|
|
|
|
LocalName: "fooo/bar",
|
|
|
|
CanonicalName: "docker.io/fooo/bar",
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: false,
|
|
|
|
},
|
|
|
|
"library/ubuntu": {
|
2015-12-12 03:14:52 +01:00
|
|
|
Index: ®istrytypes.IndexInfo{
|
2015-07-21 21:40:36 +02:00
|
|
|
Name: IndexName,
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: true,
|
|
|
|
},
|
2015-12-11 20:00:13 +01:00
|
|
|
RemoteName: "library/ubuntu",
|
|
|
|
LocalName: "ubuntu",
|
|
|
|
CanonicalName: "docker.io/library/ubuntu",
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: true,
|
|
|
|
},
|
|
|
|
"nonlibrary/ubuntu": {
|
2015-12-12 03:14:52 +01:00
|
|
|
Index: ®istrytypes.IndexInfo{
|
2015-07-21 21:40:36 +02:00
|
|
|
Name: IndexName,
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: true,
|
|
|
|
},
|
2015-12-11 20:00:13 +01:00
|
|
|
RemoteName: "nonlibrary/ubuntu",
|
|
|
|
LocalName: "nonlibrary/ubuntu",
|
|
|
|
CanonicalName: "docker.io/nonlibrary/ubuntu",
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: false,
|
|
|
|
},
|
|
|
|
"ubuntu": {
|
2015-12-12 03:14:52 +01:00
|
|
|
Index: ®istrytypes.IndexInfo{
|
2015-07-21 21:40:36 +02:00
|
|
|
Name: IndexName,
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: true,
|
|
|
|
},
|
2015-12-11 20:00:13 +01:00
|
|
|
RemoteName: "library/ubuntu",
|
|
|
|
LocalName: "ubuntu",
|
|
|
|
CanonicalName: "docker.io/library/ubuntu",
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: true,
|
|
|
|
},
|
|
|
|
"other/library": {
|
2015-12-12 03:14:52 +01:00
|
|
|
Index: ®istrytypes.IndexInfo{
|
2015-07-21 21:40:36 +02:00
|
|
|
Name: IndexName,
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: true,
|
|
|
|
},
|
2015-12-11 20:00:13 +01:00
|
|
|
RemoteName: "other/library",
|
|
|
|
LocalName: "other/library",
|
|
|
|
CanonicalName: "docker.io/other/library",
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: false,
|
|
|
|
},
|
|
|
|
"127.0.0.1:8000/private/moonbase": {
|
2015-12-12 03:14:52 +01:00
|
|
|
Index: ®istrytypes.IndexInfo{
|
2014-10-07 03:54:52 +02:00
|
|
|
Name: "127.0.0.1:8000",
|
|
|
|
Official: false,
|
|
|
|
},
|
2015-12-11 20:00:13 +01:00
|
|
|
RemoteName: "private/moonbase",
|
|
|
|
LocalName: "127.0.0.1:8000/private/moonbase",
|
|
|
|
CanonicalName: "127.0.0.1:8000/private/moonbase",
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: false,
|
|
|
|
},
|
|
|
|
"127.0.0.1:8000/privatebase": {
|
2015-12-12 03:14:52 +01:00
|
|
|
Index: ®istrytypes.IndexInfo{
|
2014-10-07 03:54:52 +02:00
|
|
|
Name: "127.0.0.1:8000",
|
|
|
|
Official: false,
|
|
|
|
},
|
2015-12-11 20:00:13 +01:00
|
|
|
RemoteName: "privatebase",
|
|
|
|
LocalName: "127.0.0.1:8000/privatebase",
|
|
|
|
CanonicalName: "127.0.0.1:8000/privatebase",
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: false,
|
|
|
|
},
|
|
|
|
"localhost:8000/private/moonbase": {
|
2015-12-12 03:14:52 +01:00
|
|
|
Index: ®istrytypes.IndexInfo{
|
2014-10-07 03:54:52 +02:00
|
|
|
Name: "localhost:8000",
|
|
|
|
Official: false,
|
|
|
|
},
|
2015-12-11 20:00:13 +01:00
|
|
|
RemoteName: "private/moonbase",
|
|
|
|
LocalName: "localhost:8000/private/moonbase",
|
|
|
|
CanonicalName: "localhost:8000/private/moonbase",
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: false,
|
|
|
|
},
|
|
|
|
"localhost:8000/privatebase": {
|
2015-12-12 03:14:52 +01:00
|
|
|
Index: ®istrytypes.IndexInfo{
|
2014-10-07 03:54:52 +02:00
|
|
|
Name: "localhost:8000",
|
|
|
|
Official: false,
|
|
|
|
},
|
2015-12-11 20:00:13 +01:00
|
|
|
RemoteName: "privatebase",
|
|
|
|
LocalName: "localhost:8000/privatebase",
|
|
|
|
CanonicalName: "localhost:8000/privatebase",
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: false,
|
|
|
|
},
|
|
|
|
"example.com/private/moonbase": {
|
2015-12-12 03:14:52 +01:00
|
|
|
Index: ®istrytypes.IndexInfo{
|
2014-10-07 03:54:52 +02:00
|
|
|
Name: "example.com",
|
|
|
|
Official: false,
|
|
|
|
},
|
2015-12-11 20:00:13 +01:00
|
|
|
RemoteName: "private/moonbase",
|
|
|
|
LocalName: "example.com/private/moonbase",
|
|
|
|
CanonicalName: "example.com/private/moonbase",
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: false,
|
|
|
|
},
|
|
|
|
"example.com/privatebase": {
|
2015-12-12 03:14:52 +01:00
|
|
|
Index: ®istrytypes.IndexInfo{
|
2014-10-07 03:54:52 +02:00
|
|
|
Name: "example.com",
|
|
|
|
Official: false,
|
|
|
|
},
|
2015-12-11 20:00:13 +01:00
|
|
|
RemoteName: "privatebase",
|
|
|
|
LocalName: "example.com/privatebase",
|
|
|
|
CanonicalName: "example.com/privatebase",
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: false,
|
|
|
|
},
|
|
|
|
"example.com:8000/private/moonbase": {
|
2015-12-12 03:14:52 +01:00
|
|
|
Index: ®istrytypes.IndexInfo{
|
2014-10-07 03:54:52 +02:00
|
|
|
Name: "example.com:8000",
|
|
|
|
Official: false,
|
|
|
|
},
|
2015-12-11 20:00:13 +01:00
|
|
|
RemoteName: "private/moonbase",
|
|
|
|
LocalName: "example.com:8000/private/moonbase",
|
|
|
|
CanonicalName: "example.com:8000/private/moonbase",
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: false,
|
|
|
|
},
|
|
|
|
"example.com:8000/privatebase": {
|
2015-12-12 03:14:52 +01:00
|
|
|
Index: ®istrytypes.IndexInfo{
|
2014-10-07 03:54:52 +02:00
|
|
|
Name: "example.com:8000",
|
|
|
|
Official: false,
|
|
|
|
},
|
2015-12-11 20:00:13 +01:00
|
|
|
RemoteName: "privatebase",
|
|
|
|
LocalName: "example.com:8000/privatebase",
|
|
|
|
CanonicalName: "example.com:8000/privatebase",
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: false,
|
|
|
|
},
|
|
|
|
"localhost/private/moonbase": {
|
2015-12-12 03:14:52 +01:00
|
|
|
Index: ®istrytypes.IndexInfo{
|
2014-10-07 03:54:52 +02:00
|
|
|
Name: "localhost",
|
|
|
|
Official: false,
|
|
|
|
},
|
2015-12-11 20:00:13 +01:00
|
|
|
RemoteName: "private/moonbase",
|
|
|
|
LocalName: "localhost/private/moonbase",
|
|
|
|
CanonicalName: "localhost/private/moonbase",
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: false,
|
|
|
|
},
|
|
|
|
"localhost/privatebase": {
|
2015-12-12 03:14:52 +01:00
|
|
|
Index: ®istrytypes.IndexInfo{
|
2014-10-07 03:54:52 +02:00
|
|
|
Name: "localhost",
|
|
|
|
Official: false,
|
|
|
|
},
|
2015-12-11 20:00:13 +01:00
|
|
|
RemoteName: "privatebase",
|
|
|
|
LocalName: "localhost/privatebase",
|
|
|
|
CanonicalName: "localhost/privatebase",
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: false,
|
|
|
|
},
|
2015-07-21 21:40:36 +02:00
|
|
|
IndexName + "/public/moonbase": {
|
2015-12-12 03:14:52 +01:00
|
|
|
Index: ®istrytypes.IndexInfo{
|
2015-07-21 21:40:36 +02:00
|
|
|
Name: IndexName,
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: true,
|
|
|
|
},
|
2015-12-11 20:00:13 +01:00
|
|
|
RemoteName: "public/moonbase",
|
|
|
|
LocalName: "public/moonbase",
|
|
|
|
CanonicalName: "docker.io/public/moonbase",
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: false,
|
|
|
|
},
|
2015-07-21 21:40:36 +02:00
|
|
|
"index." + IndexName + "/public/moonbase": {
|
2015-12-12 03:14:52 +01:00
|
|
|
Index: ®istrytypes.IndexInfo{
|
2015-07-21 21:40:36 +02:00
|
|
|
Name: IndexName,
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: true,
|
|
|
|
},
|
2015-12-11 20:00:13 +01:00
|
|
|
RemoteName: "public/moonbase",
|
|
|
|
LocalName: "public/moonbase",
|
|
|
|
CanonicalName: "docker.io/public/moonbase",
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: false,
|
|
|
|
},
|
|
|
|
"ubuntu-12.04-base": {
|
2015-12-12 03:14:52 +01:00
|
|
|
Index: ®istrytypes.IndexInfo{
|
2015-07-21 21:40:36 +02:00
|
|
|
Name: IndexName,
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: true,
|
|
|
|
},
|
2015-12-11 20:00:13 +01:00
|
|
|
RemoteName: "library/ubuntu-12.04-base",
|
|
|
|
LocalName: "ubuntu-12.04-base",
|
|
|
|
CanonicalName: "docker.io/library/ubuntu-12.04-base",
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: true,
|
|
|
|
},
|
2015-07-21 21:40:36 +02:00
|
|
|
IndexName + "/ubuntu-12.04-base": {
|
2015-12-12 03:14:52 +01:00
|
|
|
Index: ®istrytypes.IndexInfo{
|
2015-07-21 21:40:36 +02:00
|
|
|
Name: IndexName,
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: true,
|
|
|
|
},
|
2015-12-11 20:00:13 +01:00
|
|
|
RemoteName: "library/ubuntu-12.04-base",
|
|
|
|
LocalName: "ubuntu-12.04-base",
|
|
|
|
CanonicalName: "docker.io/library/ubuntu-12.04-base",
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: true,
|
|
|
|
},
|
2015-07-21 21:40:36 +02:00
|
|
|
"index." + IndexName + "/ubuntu-12.04-base": {
|
2015-12-12 03:14:52 +01:00
|
|
|
Index: ®istrytypes.IndexInfo{
|
2015-07-21 21:40:36 +02:00
|
|
|
Name: IndexName,
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: true,
|
|
|
|
},
|
2015-12-11 20:00:13 +01:00
|
|
|
RemoteName: "library/ubuntu-12.04-base",
|
|
|
|
LocalName: "ubuntu-12.04-base",
|
|
|
|
CanonicalName: "docker.io/library/ubuntu-12.04-base",
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for reposName, expectedRepoInfo := range expectedRepoInfos {
|
2015-11-18 23:20:54 +01:00
|
|
|
named, err := reference.WithName(reposName)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
repoInfo, err := ParseRepositoryInfo(named)
|
2014-10-07 03:54:52 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
checkEqual(t, repoInfo.Index.Name, expectedRepoInfo.Index.Name, reposName)
|
2015-12-11 20:00:13 +01:00
|
|
|
checkEqual(t, repoInfo.RemoteName(), expectedRepoInfo.RemoteName, reposName)
|
|
|
|
checkEqual(t, repoInfo.Name(), expectedRepoInfo.LocalName, reposName)
|
|
|
|
checkEqual(t, repoInfo.FullName(), expectedRepoInfo.CanonicalName, reposName)
|
2014-10-07 03:54:52 +02:00
|
|
|
checkEqual(t, repoInfo.Index.Official, expectedRepoInfo.Index.Official, reposName)
|
|
|
|
checkEqual(t, repoInfo.Official, expectedRepoInfo.Official, reposName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewIndexInfo(t *testing.T) {
|
2016-03-08 22:03:37 +01:00
|
|
|
testIndexInfo := func(config *serviceConfig, expectedIndexInfos map[string]*registrytypes.IndexInfo) {
|
2014-10-07 03:54:52 +02:00
|
|
|
for indexName, expectedIndexInfo := range expectedIndexInfos {
|
2015-12-12 03:14:52 +01:00
|
|
|
index, err := newIndexInfo(config, indexName)
|
2014-10-07 03:54:52 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else {
|
|
|
|
checkEqual(t, index.Name, expectedIndexInfo.Name, indexName+" name")
|
|
|
|
checkEqual(t, index.Official, expectedIndexInfo.Official, indexName+" is official")
|
|
|
|
checkEqual(t, index.Secure, expectedIndexInfo.Secure, indexName+" is secure")
|
|
|
|
checkEqual(t, len(index.Mirrors), len(expectedIndexInfo.Mirrors), indexName+" mirrors")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-08 22:03:37 +01:00
|
|
|
config := newServiceConfig(ServiceOptions{})
|
2015-07-21 21:40:36 +02:00
|
|
|
noMirrors := []string{}
|
2015-12-12 03:14:52 +01:00
|
|
|
expectedIndexInfos := map[string]*registrytypes.IndexInfo{
|
2015-07-21 21:40:36 +02:00
|
|
|
IndexName: {
|
|
|
|
Name: IndexName,
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: true,
|
|
|
|
Secure: true,
|
|
|
|
Mirrors: noMirrors,
|
|
|
|
},
|
2015-07-21 21:40:36 +02:00
|
|
|
"index." + IndexName: {
|
|
|
|
Name: IndexName,
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: true,
|
|
|
|
Secure: true,
|
|
|
|
Mirrors: noMirrors,
|
|
|
|
},
|
|
|
|
"example.com": {
|
|
|
|
Name: "example.com",
|
|
|
|
Official: false,
|
|
|
|
Secure: true,
|
|
|
|
Mirrors: noMirrors,
|
|
|
|
},
|
|
|
|
"127.0.0.1:5000": {
|
|
|
|
Name: "127.0.0.1:5000",
|
|
|
|
Official: false,
|
|
|
|
Secure: false,
|
|
|
|
Mirrors: noMirrors,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testIndexInfo(config, expectedIndexInfos)
|
|
|
|
|
|
|
|
publicMirrors := []string{"http://mirror1.local", "http://mirror2.local"}
|
|
|
|
config = makeServiceConfig(publicMirrors, []string{"example.com"})
|
|
|
|
|
2015-12-12 03:14:52 +01:00
|
|
|
expectedIndexInfos = map[string]*registrytypes.IndexInfo{
|
2015-07-21 21:40:36 +02:00
|
|
|
IndexName: {
|
|
|
|
Name: IndexName,
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: true,
|
|
|
|
Secure: true,
|
|
|
|
Mirrors: publicMirrors,
|
|
|
|
},
|
2015-07-21 21:40:36 +02:00
|
|
|
"index." + IndexName: {
|
|
|
|
Name: IndexName,
|
2014-10-07 03:54:52 +02:00
|
|
|
Official: true,
|
|
|
|
Secure: true,
|
|
|
|
Mirrors: publicMirrors,
|
|
|
|
},
|
|
|
|
"example.com": {
|
|
|
|
Name: "example.com",
|
|
|
|
Official: false,
|
|
|
|
Secure: false,
|
|
|
|
Mirrors: noMirrors,
|
|
|
|
},
|
|
|
|
"example.com:5000": {
|
|
|
|
Name: "example.com:5000",
|
|
|
|
Official: false,
|
|
|
|
Secure: true,
|
|
|
|
Mirrors: noMirrors,
|
|
|
|
},
|
|
|
|
"127.0.0.1": {
|
|
|
|
Name: "127.0.0.1",
|
|
|
|
Official: false,
|
|
|
|
Secure: false,
|
|
|
|
Mirrors: noMirrors,
|
|
|
|
},
|
|
|
|
"127.0.0.1:5000": {
|
|
|
|
Name: "127.0.0.1:5000",
|
|
|
|
Official: false,
|
|
|
|
Secure: false,
|
|
|
|
Mirrors: noMirrors,
|
|
|
|
},
|
|
|
|
"other.com": {
|
|
|
|
Name: "other.com",
|
|
|
|
Official: false,
|
|
|
|
Secure: true,
|
|
|
|
Mirrors: noMirrors,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testIndexInfo(config, expectedIndexInfos)
|
|
|
|
|
|
|
|
config = makeServiceConfig(nil, []string{"42.42.0.0/16"})
|
2015-12-12 03:14:52 +01:00
|
|
|
expectedIndexInfos = map[string]*registrytypes.IndexInfo{
|
2014-10-07 03:54:52 +02:00
|
|
|
"example.com": {
|
|
|
|
Name: "example.com",
|
|
|
|
Official: false,
|
|
|
|
Secure: false,
|
|
|
|
Mirrors: noMirrors,
|
|
|
|
},
|
|
|
|
"example.com:5000": {
|
|
|
|
Name: "example.com:5000",
|
|
|
|
Official: false,
|
|
|
|
Secure: false,
|
|
|
|
Mirrors: noMirrors,
|
|
|
|
},
|
|
|
|
"127.0.0.1": {
|
|
|
|
Name: "127.0.0.1",
|
|
|
|
Official: false,
|
|
|
|
Secure: false,
|
|
|
|
Mirrors: noMirrors,
|
|
|
|
},
|
|
|
|
"127.0.0.1:5000": {
|
|
|
|
Name: "127.0.0.1:5000",
|
|
|
|
Official: false,
|
|
|
|
Secure: false,
|
|
|
|
Mirrors: noMirrors,
|
|
|
|
},
|
|
|
|
"other.com": {
|
|
|
|
Name: "other.com",
|
|
|
|
Official: false,
|
|
|
|
Secure: true,
|
|
|
|
Mirrors: noMirrors,
|
|
|
|
},
|
2014-04-15 01:15:38 +02:00
|
|
|
}
|
2014-10-07 03:54:52 +02:00
|
|
|
testIndexInfo(config, expectedIndexInfos)
|
2013-07-31 19:07:31 +02:00
|
|
|
}
|
|
|
|
|
2015-08-07 03:21:02 +02:00
|
|
|
func TestMirrorEndpointLookup(t *testing.T) {
|
|
|
|
containsMirror := func(endpoints []APIEndpoint) bool {
|
|
|
|
for _, pe := range endpoints {
|
2016-02-18 01:53:25 +01:00
|
|
|
if pe.URL.Host == "my.mirror" {
|
2015-08-07 03:21:02 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2016-03-08 22:03:37 +01:00
|
|
|
s := Service{config: makeServiceConfig([]string{"my.mirror"}, nil)}
|
2015-08-07 03:21:02 +02:00
|
|
|
|
2015-11-18 23:20:54 +01:00
|
|
|
imageName, err := reference.WithName(IndexName + "/test/image")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2016-03-01 08:07:41 +01:00
|
|
|
pushAPIEndpoints, err := s.LookupPushEndpoints(imageName.Hostname())
|
2015-08-07 03:21:02 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if containsMirror(pushAPIEndpoints) {
|
|
|
|
t.Fatal("Push endpoint should not contain mirror")
|
|
|
|
}
|
|
|
|
|
2016-03-01 08:07:41 +01:00
|
|
|
pullAPIEndpoints, err := s.LookupPullEndpoints(imageName.Hostname())
|
2015-08-07 03:21:02 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !containsMirror(pullAPIEndpoints) {
|
|
|
|
t.Fatal("Pull endpoint should contain mirror")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-31 19:07:31 +02:00
|
|
|
func TestPushRegistryTag(t *testing.T) {
|
2014-08-07 16:43:06 +02:00
|
|
|
r := spawnTestRegistrySession(t)
|
2015-11-18 23:20:54 +01:00
|
|
|
repoRef, err := reference.ParseNamed(REPO)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = r.PushRegistryTag(repoRef, imageID, "stable", makeURL("/v1/"))
|
2013-07-31 19:07:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushImageJSONIndex(t *testing.T) {
|
2014-08-07 16:43:06 +02:00
|
|
|
r := spawnTestRegistrySession(t)
|
2013-07-31 19:07:31 +02:00
|
|
|
imgData := []*ImgData{
|
2013-09-20 05:25:00 +02:00
|
|
|
{
|
2013-07-31 19:12:40 +02:00
|
|
|
ID: "77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
|
2013-07-31 19:07:31 +02:00
|
|
|
Checksum: "sha256:1ac330d56e05eef6d438586545ceff7550d3bdcb6b19961f12c5ba714ee1bb37",
|
|
|
|
},
|
2013-09-20 05:25:00 +02:00
|
|
|
{
|
2013-07-31 19:12:40 +02:00
|
|
|
ID: "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d",
|
2013-07-31 19:07:31 +02:00
|
|
|
Checksum: "sha256:bea7bf2e4bacd479344b737328db47b18880d09096e6674165533aa994f5e9f2",
|
|
|
|
},
|
|
|
|
}
|
2015-11-18 23:20:54 +01:00
|
|
|
repoRef, err := reference.ParseNamed(REPO)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
repoData, err := r.PushImageJSONIndex(repoRef, imgData, false, nil)
|
2013-07-31 19:07:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if repoData == nil {
|
|
|
|
t.Fatal("Expected RepositoryData object")
|
|
|
|
}
|
2015-11-18 23:20:54 +01:00
|
|
|
repoData, err = r.PushImageJSONIndex(repoRef, imgData, true, []string{r.indexEndpoint.String()})
|
2013-07-31 19:07:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if repoData == nil {
|
|
|
|
t.Fatal("Expected RepositoryData object")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSearchRepositories(t *testing.T) {
|
2014-08-07 16:43:06 +02:00
|
|
|
r := spawnTestRegistrySession(t)
|
2014-03-13 18:40:34 +01:00
|
|
|
results, err := r.SearchRepositories("fakequery")
|
2013-07-31 19:07:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if results == nil {
|
|
|
|
t.Fatal("Expected non-nil SearchResults object")
|
|
|
|
}
|
2014-03-13 18:40:34 +01:00
|
|
|
assertEqual(t, results.NumResults, 1, "Expected 1 search results")
|
|
|
|
assertEqual(t, results.Query, "fakequery", "Expected 'fakequery' as query")
|
2015-04-27 22:33:30 +02:00
|
|
|
assertEqual(t, results.Results[0].StarCount, 42, "Expected 'fakeimage' to have 42 stars")
|
2013-07-31 19:12:40 +02:00
|
|
|
}
|
2013-09-20 05:25:00 +02:00
|
|
|
|
2014-06-05 20:37:37 +02:00
|
|
|
func TestTrustedLocation(t *testing.T) {
|
2014-09-24 01:18:09 +02:00
|
|
|
for _, url := range []string{"http://example.com", "https://example.com:7777", "http://docker.io", "http://test.docker.com", "https://fakedocker.com"} {
|
2014-06-05 20:37:37 +02:00
|
|
|
req, _ := http.NewRequest("GET", url, nil)
|
|
|
|
if trustedLocation(req) == true {
|
|
|
|
t.Fatalf("'%s' shouldn't be detected as a trusted location", url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-24 01:18:09 +02:00
|
|
|
for _, url := range []string{"https://docker.io", "https://test.docker.com:80"} {
|
2014-06-05 20:37:37 +02:00
|
|
|
req, _ := http.NewRequest("GET", url, nil)
|
|
|
|
if trustedLocation(req) == false {
|
|
|
|
t.Fatalf("'%s' should be detected as a trusted location", url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddRequiredHeadersToRedirectedRequests(t *testing.T) {
|
|
|
|
for _, urls := range [][]string{
|
|
|
|
{"http://docker.io", "https://docker.com"},
|
|
|
|
{"https://foo.docker.io:7777", "http://bar.docker.com"},
|
|
|
|
{"https://foo.docker.io", "https://example.com"},
|
|
|
|
} {
|
|
|
|
reqFrom, _ := http.NewRequest("GET", urls[0], nil)
|
|
|
|
reqFrom.Header.Add("Content-Type", "application/json")
|
|
|
|
reqFrom.Header.Add("Authorization", "super_secret")
|
|
|
|
reqTo, _ := http.NewRequest("GET", urls[1], nil)
|
|
|
|
|
2015-07-21 21:40:36 +02:00
|
|
|
addRequiredHeadersToRedirectedRequests(reqTo, []*http.Request{reqFrom})
|
2014-06-05 20:37:37 +02:00
|
|
|
|
|
|
|
if len(reqTo.Header) != 1 {
|
2014-06-12 07:15:53 +02:00
|
|
|
t.Fatalf("Expected 1 headers, got %d", len(reqTo.Header))
|
2014-06-05 20:37:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if reqTo.Header.Get("Content-Type") != "application/json" {
|
|
|
|
t.Fatal("'Content-Type' should be 'application/json'")
|
|
|
|
}
|
|
|
|
|
|
|
|
if reqTo.Header.Get("Authorization") != "" {
|
|
|
|
t.Fatal("'Authorization' should be empty")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, urls := range [][]string{
|
|
|
|
{"https://docker.io", "https://docker.com"},
|
|
|
|
{"https://foo.docker.io:7777", "https://bar.docker.com"},
|
|
|
|
} {
|
|
|
|
reqFrom, _ := http.NewRequest("GET", urls[0], nil)
|
|
|
|
reqFrom.Header.Add("Content-Type", "application/json")
|
|
|
|
reqFrom.Header.Add("Authorization", "super_secret")
|
|
|
|
reqTo, _ := http.NewRequest("GET", urls[1], nil)
|
|
|
|
|
2015-07-21 21:40:36 +02:00
|
|
|
addRequiredHeadersToRedirectedRequests(reqTo, []*http.Request{reqFrom})
|
2014-06-05 20:37:37 +02:00
|
|
|
|
|
|
|
if len(reqTo.Header) != 2 {
|
2014-06-12 07:15:53 +02:00
|
|
|
t.Fatalf("Expected 2 headers, got %d", len(reqTo.Header))
|
2014-06-05 20:37:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if reqTo.Header.Get("Content-Type") != "application/json" {
|
|
|
|
t.Fatal("'Content-Type' should be 'application/json'")
|
|
|
|
}
|
|
|
|
|
|
|
|
if reqTo.Header.Get("Authorization") != "super_secret" {
|
|
|
|
t.Fatal("'Authorization' should be 'super_secret'")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-11 20:01:49 +01:00
|
|
|
|
2014-10-07 03:54:52 +02:00
|
|
|
func TestIsSecureIndex(t *testing.T) {
|
2014-11-11 20:01:49 +01:00
|
|
|
tests := []struct {
|
|
|
|
addr string
|
|
|
|
insecureRegistries []string
|
|
|
|
expected bool
|
|
|
|
}{
|
2015-07-21 21:40:36 +02:00
|
|
|
{IndexName, nil, true},
|
2014-11-11 20:01:49 +01:00
|
|
|
{"example.com", []string{}, true},
|
|
|
|
{"example.com", []string{"example.com"}, false},
|
2014-11-12 18:08:45 +01:00
|
|
|
{"localhost", []string{"localhost:5000"}, false},
|
2014-11-11 20:01:49 +01:00
|
|
|
{"localhost:5000", []string{"localhost:5000"}, false},
|
2014-11-12 18:08:45 +01:00
|
|
|
{"localhost", []string{"example.com"}, false},
|
2014-11-11 20:01:49 +01:00
|
|
|
{"127.0.0.1:5000", []string{"127.0.0.1:5000"}, false},
|
2014-11-11 22:31:15 +01:00
|
|
|
{"localhost", nil, false},
|
|
|
|
{"localhost:5000", nil, false},
|
|
|
|
{"127.0.0.1", nil, false},
|
2014-11-12 18:08:45 +01:00
|
|
|
{"localhost", []string{"example.com"}, false},
|
|
|
|
{"127.0.0.1", []string{"example.com"}, false},
|
2014-11-11 22:31:15 +01:00
|
|
|
{"example.com", nil, true},
|
2014-10-31 21:00:49 +01:00
|
|
|
{"example.com", []string{"example.com"}, false},
|
2014-11-12 18:08:45 +01:00
|
|
|
{"127.0.0.1", []string{"example.com"}, false},
|
|
|
|
{"127.0.0.1:5000", []string{"example.com"}, false},
|
2014-11-11 22:31:15 +01:00
|
|
|
{"example.com:5000", []string{"42.42.0.0/16"}, false},
|
|
|
|
{"example.com", []string{"42.42.0.0/16"}, false},
|
|
|
|
{"example.com:5000", []string{"42.42.42.42/8"}, false},
|
|
|
|
{"127.0.0.1:5000", []string{"127.0.0.0/8"}, false},
|
|
|
|
{"42.42.42.42:5000", []string{"42.1.1.1/8"}, false},
|
2014-12-19 01:13:02 +01:00
|
|
|
{"invalid.domain.com", []string{"42.42.0.0/16"}, true},
|
|
|
|
{"invalid.domain.com", []string{"invalid.domain.com"}, false},
|
2014-12-19 22:40:28 +01:00
|
|
|
{"invalid.domain.com:5000", []string{"invalid.domain.com"}, true},
|
|
|
|
{"invalid.domain.com:5000", []string{"invalid.domain.com:5000"}, false},
|
2014-10-31 21:00:49 +01:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2014-10-07 03:54:52 +02:00
|
|
|
config := makeServiceConfig(nil, tt.insecureRegistries)
|
2015-12-12 03:14:52 +01:00
|
|
|
if sec := isSecureIndex(config, tt.addr); sec != tt.expected {
|
2014-10-07 03:54:52 +02:00
|
|
|
t.Errorf("isSecureIndex failed for %q %v, expected %v got %v", tt.addr, tt.insecureRegistries, tt.expected, sec)
|
2014-10-31 21:00:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-20 14:28:18 +02:00
|
|
|
|
|
|
|
type debugTransport struct {
|
|
|
|
http.RoundTripper
|
|
|
|
log func(...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tr debugTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
dump, err := httputil.DumpRequestOut(req, false)
|
|
|
|
if err != nil {
|
|
|
|
tr.log("could not dump request")
|
|
|
|
}
|
|
|
|
tr.log(string(dump))
|
|
|
|
resp, err := tr.RoundTripper.RoundTrip(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dump, err = httputil.DumpResponse(resp, false)
|
|
|
|
if err != nil {
|
|
|
|
tr.log("could not dump response")
|
|
|
|
}
|
|
|
|
tr.log(string(dump))
|
|
|
|
return resp, err
|
|
|
|
}
|