registry/registry_test.go
9bb3dc98
 package registry
fc1d1d87
 
97b7b173
 import (
720f3447
 	"fmt"
a1245318
 	"net/http"
927d13bc
 	"net/http/httputil"
720f3447
 	"net/url"
97b7b173
 	"strings"
 	"testing"
a1245318
 
276c640b
 	"github.com/docker/distribution/registry/client/transport"
2655954c
 	"github.com/docker/docker/reference"
907407d0
 	"github.com/docker/engine-api/types"
 	registrytypes "github.com/docker/engine-api/types/registry"
97b7b173
 )
484ba4a8
 
97b7b173
 var (
ae3b59c1
 	token = []string{"fake-token"}
 )
 
 const (
 	imageID = "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d"
 	REPO    = "foo42/bar"
97b7b173
 )
 
752dd707
 func spawnTestRegistrySession(t *testing.T) *Session {
5b321e32
 	authConfig := &types.AuthConfig{}
f2d481a2
 	endpoint, err := NewV1Endpoint(makeIndex("/v1/"), "", nil)
61c6f206
 	if err != nil {
 		t.Fatal(err)
 	}
61a49bb6
 	userAgent := "docker test client"
19515a7a
 	var tr http.RoundTripper = debugTransport{NewTransport(nil), t.Log}
61a49bb6
 	tr = transport.NewTransport(AuthTransport(tr, authConfig, false), DockerHeaders(userAgent, nil)...)
a01cc3ca
 	client := HTTPClient(tr)
 	r, err := NewSession(client, authConfig, endpoint)
97b7b173
 	if err != nil {
 		t.Fatal(err)
 	}
a01cc3ca
 	// 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
97b7b173
 	return r
 }
 
 func TestPingRegistryEndpoint(t *testing.T) {
96c10098
 	testPing := func(index *registrytypes.IndexInfo, expectedStandalone bool, assertMessage string) {
f2d481a2
 		ep, err := NewV1Endpoint(index, "", nil)
568f86eb
 		if err != nil {
 			t.Fatal(err)
 		}
 		regInfo, err := ep.Ping()
 		if err != nil {
 			t.Fatal(err)
 		}
 
 		assertEqual(t, regInfo.Standalone, expectedStandalone, assertMessage)
61c6f206
 	}
568f86eb
 
 	testPing(makeIndex("/v1/"), true, "Expected standalone to be true (default)")
4fcb9ac4
 	testPing(makeHTTPSIndex("/v1/"), true, "Expected standalone to be true (default)")
568f86eb
 	testPing(makePublicIndex(), false, "Expected standalone to be false for public index")
 }
 
 func TestEndpoint(t *testing.T) {
 	// Simple wrapper to fail test if err != nil
f2d481a2
 	expandEndpoint := func(index *registrytypes.IndexInfo) *V1Endpoint {
 		endpoint, err := NewV1Endpoint(index, "", nil)
568f86eb
 		if err != nil {
 			t.Fatal(err)
 		}
 		return endpoint
 	}
 
96c10098
 	assertInsecureIndex := func(index *registrytypes.IndexInfo) {
568f86eb
 		index.Secure = true
f2d481a2
 		_, err := NewV1Endpoint(index, "", nil)
568f86eb
 		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
 	}
 
96c10098
 	assertSecureIndex := func(index *registrytypes.IndexInfo) {
568f86eb
 		index.Secure = true
f2d481a2
 		_, err := NewV1Endpoint(index, "", nil)
568f86eb
 		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
 	}
 
96c10098
 	index := &registrytypes.IndexInfo{}
568f86eb
 	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)
 
4fcb9ac4
 	index.Name = makeHTTPSURL("/v1/")
568f86eb
 	endpoint = expandEndpoint(index)
 	assertEqual(t, endpoint.String(), index.Name, "Expected endpoint to be "+index.Name)
 	assertSecureIndex(index)
 
4fcb9ac4
 	index.Name = makeHTTPSURL("")
568f86eb
 	endpoint = expandEndpoint(index)
 	assertEqual(t, endpoint.String(), index.Name+"/v1/", index.Name+": Expected endpoint to be "+index.Name+"/v1/")
 	assertSecureIndex(index)
 
4fcb9ac4
 	httpsURL := makeHTTPSURL("")
568f86eb
 	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
f2d481a2
 		_, err := NewV1Endpoint(index, "", nil)
568f86eb
 		checkNotEqual(t, err, nil, "Expected error while expanding bad endpoint")
97b7b173
 	}
 }
 
 func TestGetRemoteHistory(t *testing.T) {
752dd707
 	r := spawnTestRegistrySession(t)
a01cc3ca
 	hist, err := r.GetRemoteHistory(imageID, makeURL("/v1/"))
97b7b173
 	if err != nil {
 		t.Fatal(err)
 	}
 	assertEqual(t, len(hist), 2, "Expected 2 images in history")
ae3b59c1
 	assertEqual(t, hist[0], imageID, "Expected "+imageID+"as first ancestry")
97b7b173
 	assertEqual(t, hist[1], "77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
 		"Unexpected second ancestry")
 }
 
 func TestLookupRemoteImage(t *testing.T) {
752dd707
 	r := spawnTestRegistrySession(t)
a01cc3ca
 	err := r.LookupRemoteImage(imageID, makeURL("/v1/"))
8123c1e9
 	assertEqual(t, err, nil, "Expected error of remote lookup to nil")
a01cc3ca
 	if err := r.LookupRemoteImage("abcdef", makeURL("/v1/")); err == nil {
8123c1e9
 		t.Fatal("Expected error of remote lookup to not nil")
 	}
97b7b173
 }
 
 func TestGetRemoteImageJSON(t *testing.T) {
752dd707
 	r := spawnTestRegistrySession(t)
a01cc3ca
 	json, size, err := r.GetRemoteImageJSON(imageID, makeURL("/v1/"))
97b7b173
 	if err != nil {
 		t.Fatal(err)
 	}
1f61084d
 	assertEqual(t, size, int64(154), "Expected size 154")
bb05c188
 	if len(json) == 0 {
97b7b173
 		t.Fatal("Expected non-empty json")
 	}
 
a01cc3ca
 	_, _, err = r.GetRemoteImageJSON("abcdef", makeURL("/v1/"))
97b7b173
 	if err == nil {
 		t.Fatal("Expected image not found error")
 	}
 }
 
 func TestGetRemoteImageLayer(t *testing.T) {
752dd707
 	r := spawnTestRegistrySession(t)
a01cc3ca
 	data, err := r.GetRemoteImageLayer(imageID, makeURL("/v1/"), 0)
97b7b173
 	if err != nil {
 		t.Fatal(err)
 	}
 	if data == nil {
 		t.Fatal("Expected non-nil data result")
 	}
 
a01cc3ca
 	_, err = r.GetRemoteImageLayer("abcdef", makeURL("/v1/"), 0)
97b7b173
 	if err == nil {
 		t.Fatal("Expected image not found error")
 	}
 }
 
b349a74c
 func TestGetRemoteTag(t *testing.T) {
 	r := spawnTestRegistrySession(t)
4352da78
 	repoRef, err := reference.ParseNamed(REPO)
 	if err != nil {
 		t.Fatal(err)
 	}
 	tag, err := r.GetRemoteTag([]string{makeURL("/v1/")}, repoRef, "test")
b349a74c
 	if err != nil {
 		t.Fatal(err)
 	}
 	assertEqual(t, tag, imageID, "Expected tag test to map to "+imageID)
 
4352da78
 	bazRef, err := reference.ParseNamed("foo42/baz")
 	if err != nil {
 		t.Fatal(err)
 	}
 	_, err = r.GetRemoteTag([]string{makeURL("/v1/")}, bazRef, "foo")
b349a74c
 	if err != ErrRepoNotFound {
 		t.Fatal("Expected ErrRepoNotFound error when fetching tag for bogus repo")
 	}
 }
 
97b7b173
 func TestGetRemoteTags(t *testing.T) {
752dd707
 	r := spawnTestRegistrySession(t)
4352da78
 	repoRef, err := reference.ParseNamed(REPO)
 	if err != nil {
 		t.Fatal(err)
 	}
 	tags, err := r.GetRemoteTags([]string{makeURL("/v1/")}, repoRef)
97b7b173
 	if err != nil {
 		t.Fatal(err)
 	}
b349a74c
 	assertEqual(t, len(tags), 2, "Expected two tags")
ae3b59c1
 	assertEqual(t, tags["latest"], imageID, "Expected tag latest to map to "+imageID)
b349a74c
 	assertEqual(t, tags["test"], imageID, "Expected tag test to map to "+imageID)
97b7b173
 
4352da78
 	bazRef, err := reference.ParseNamed("foo42/baz")
 	if err != nil {
 		t.Fatal(err)
 	}
 	_, err = r.GetRemoteTags([]string{makeURL("/v1/")}, bazRef)
b349a74c
 	if err != ErrRepoNotFound {
 		t.Fatal("Expected ErrRepoNotFound error when fetching tags for bogus repo")
97b7b173
 	}
 }
 
 func TestGetRepositoryData(t *testing.T) {
752dd707
 	r := spawnTestRegistrySession(t)
ae3b59c1
 	parsedURL, err := url.Parse(makeURL("/v1/"))
720f3447
 	if err != nil {
 		t.Fatal(err)
 	}
ae3b59c1
 	host := "http://" + parsedURL.Host + "/v1/"
4352da78
 	repoRef, err := reference.ParseNamed(REPO)
 	if err != nil {
 		t.Fatal(err)
 	}
 	data, err := r.GetRepositoryData(repoRef)
97b7b173
 	if err != nil {
 		t.Fatal(err)
 	}
 	assertEqual(t, len(data.ImgList), 2, "Expected 2 images in ImgList")
720f3447
 	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]))
 
97b7b173
 }
 
 func TestPushImageJSONRegistry(t *testing.T) {
752dd707
 	r := spawnTestRegistrySession(t)
97b7b173
 	imgData := &ImgData{
484ba4a8
 		ID:       "77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
97b7b173
 		Checksum: "sha256:1ac330d56e05eef6d438586545ceff7550d3bdcb6b19961f12c5ba714ee1bb37",
 	}
 
a01cc3ca
 	err := r.PushImageJSONRegistry(imgData, []byte{0x42, 0xdf, 0x0}, makeURL("/v1/"))
97b7b173
 	if err != nil {
 		t.Fatal(err)
 	}
 }
 
 func TestPushImageLayerRegistry(t *testing.T) {
752dd707
 	r := spawnTestRegistrySession(t)
8aa9985a
 	layer := strings.NewReader("")
a01cc3ca
 	_, _, err := r.PushImageLayerRegistry(imageID, layer, makeURL("/v1/"), []byte{})
8aa9985a
 	if err != nil {
 		t.Fatal(err)
 	}
97b7b173
 }
 
568f86eb
 func TestParseRepositoryInfo(t *testing.T) {
ffded61d
 	type staticRepositoryInfo struct {
 		Index         *registrytypes.IndexInfo
 		RemoteName    string
 		CanonicalName string
 		LocalName     string
 		Official      bool
4352da78
 	}
 
ffded61d
 	expectedRepoInfos := map[string]staticRepositoryInfo{
568f86eb
 		"fooo/bar": {
96c10098
 			Index: &registrytypes.IndexInfo{
4fcb9ac4
 				Name:     IndexName,
568f86eb
 				Official: true,
 			},
ffded61d
 			RemoteName:    "fooo/bar",
 			LocalName:     "fooo/bar",
 			CanonicalName: "docker.io/fooo/bar",
568f86eb
 			Official:      false,
 		},
 		"library/ubuntu": {
96c10098
 			Index: &registrytypes.IndexInfo{
4fcb9ac4
 				Name:     IndexName,
568f86eb
 				Official: true,
 			},
ffded61d
 			RemoteName:    "library/ubuntu",
 			LocalName:     "ubuntu",
 			CanonicalName: "docker.io/library/ubuntu",
568f86eb
 			Official:      true,
 		},
 		"nonlibrary/ubuntu": {
96c10098
 			Index: &registrytypes.IndexInfo{
4fcb9ac4
 				Name:     IndexName,
568f86eb
 				Official: true,
 			},
ffded61d
 			RemoteName:    "nonlibrary/ubuntu",
 			LocalName:     "nonlibrary/ubuntu",
 			CanonicalName: "docker.io/nonlibrary/ubuntu",
568f86eb
 			Official:      false,
 		},
 		"ubuntu": {
96c10098
 			Index: &registrytypes.IndexInfo{
4fcb9ac4
 				Name:     IndexName,
568f86eb
 				Official: true,
 			},
ffded61d
 			RemoteName:    "library/ubuntu",
 			LocalName:     "ubuntu",
 			CanonicalName: "docker.io/library/ubuntu",
568f86eb
 			Official:      true,
 		},
 		"other/library": {
96c10098
 			Index: &registrytypes.IndexInfo{
4fcb9ac4
 				Name:     IndexName,
568f86eb
 				Official: true,
 			},
ffded61d
 			RemoteName:    "other/library",
 			LocalName:     "other/library",
 			CanonicalName: "docker.io/other/library",
568f86eb
 			Official:      false,
 		},
 		"127.0.0.1:8000/private/moonbase": {
96c10098
 			Index: &registrytypes.IndexInfo{
568f86eb
 				Name:     "127.0.0.1:8000",
 				Official: false,
 			},
ffded61d
 			RemoteName:    "private/moonbase",
 			LocalName:     "127.0.0.1:8000/private/moonbase",
 			CanonicalName: "127.0.0.1:8000/private/moonbase",
568f86eb
 			Official:      false,
 		},
 		"127.0.0.1:8000/privatebase": {
96c10098
 			Index: &registrytypes.IndexInfo{
568f86eb
 				Name:     "127.0.0.1:8000",
 				Official: false,
 			},
ffded61d
 			RemoteName:    "privatebase",
 			LocalName:     "127.0.0.1:8000/privatebase",
 			CanonicalName: "127.0.0.1:8000/privatebase",
568f86eb
 			Official:      false,
 		},
 		"localhost:8000/private/moonbase": {
96c10098
 			Index: &registrytypes.IndexInfo{
568f86eb
 				Name:     "localhost:8000",
 				Official: false,
 			},
ffded61d
 			RemoteName:    "private/moonbase",
 			LocalName:     "localhost:8000/private/moonbase",
 			CanonicalName: "localhost:8000/private/moonbase",
568f86eb
 			Official:      false,
 		},
 		"localhost:8000/privatebase": {
96c10098
 			Index: &registrytypes.IndexInfo{
568f86eb
 				Name:     "localhost:8000",
 				Official: false,
 			},
ffded61d
 			RemoteName:    "privatebase",
 			LocalName:     "localhost:8000/privatebase",
 			CanonicalName: "localhost:8000/privatebase",
568f86eb
 			Official:      false,
 		},
 		"example.com/private/moonbase": {
96c10098
 			Index: &registrytypes.IndexInfo{
568f86eb
 				Name:     "example.com",
 				Official: false,
 			},
ffded61d
 			RemoteName:    "private/moonbase",
 			LocalName:     "example.com/private/moonbase",
 			CanonicalName: "example.com/private/moonbase",
568f86eb
 			Official:      false,
 		},
 		"example.com/privatebase": {
96c10098
 			Index: &registrytypes.IndexInfo{
568f86eb
 				Name:     "example.com",
 				Official: false,
 			},
ffded61d
 			RemoteName:    "privatebase",
 			LocalName:     "example.com/privatebase",
 			CanonicalName: "example.com/privatebase",
568f86eb
 			Official:      false,
 		},
 		"example.com:8000/private/moonbase": {
96c10098
 			Index: &registrytypes.IndexInfo{
568f86eb
 				Name:     "example.com:8000",
 				Official: false,
 			},
ffded61d
 			RemoteName:    "private/moonbase",
 			LocalName:     "example.com:8000/private/moonbase",
 			CanonicalName: "example.com:8000/private/moonbase",
568f86eb
 			Official:      false,
 		},
 		"example.com:8000/privatebase": {
96c10098
 			Index: &registrytypes.IndexInfo{
568f86eb
 				Name:     "example.com:8000",
 				Official: false,
 			},
ffded61d
 			RemoteName:    "privatebase",
 			LocalName:     "example.com:8000/privatebase",
 			CanonicalName: "example.com:8000/privatebase",
568f86eb
 			Official:      false,
 		},
 		"localhost/private/moonbase": {
96c10098
 			Index: &registrytypes.IndexInfo{
568f86eb
 				Name:     "localhost",
 				Official: false,
 			},
ffded61d
 			RemoteName:    "private/moonbase",
 			LocalName:     "localhost/private/moonbase",
 			CanonicalName: "localhost/private/moonbase",
568f86eb
 			Official:      false,
 		},
 		"localhost/privatebase": {
96c10098
 			Index: &registrytypes.IndexInfo{
568f86eb
 				Name:     "localhost",
 				Official: false,
 			},
ffded61d
 			RemoteName:    "privatebase",
 			LocalName:     "localhost/privatebase",
 			CanonicalName: "localhost/privatebase",
568f86eb
 			Official:      false,
 		},
4fcb9ac4
 		IndexName + "/public/moonbase": {
96c10098
 			Index: &registrytypes.IndexInfo{
4fcb9ac4
 				Name:     IndexName,
568f86eb
 				Official: true,
 			},
ffded61d
 			RemoteName:    "public/moonbase",
 			LocalName:     "public/moonbase",
 			CanonicalName: "docker.io/public/moonbase",
568f86eb
 			Official:      false,
 		},
4fcb9ac4
 		"index." + IndexName + "/public/moonbase": {
96c10098
 			Index: &registrytypes.IndexInfo{
4fcb9ac4
 				Name:     IndexName,
568f86eb
 				Official: true,
 			},
ffded61d
 			RemoteName:    "public/moonbase",
 			LocalName:     "public/moonbase",
 			CanonicalName: "docker.io/public/moonbase",
568f86eb
 			Official:      false,
 		},
 		"ubuntu-12.04-base": {
96c10098
 			Index: &registrytypes.IndexInfo{
4fcb9ac4
 				Name:     IndexName,
568f86eb
 				Official: true,
 			},
ffded61d
 			RemoteName:    "library/ubuntu-12.04-base",
 			LocalName:     "ubuntu-12.04-base",
 			CanonicalName: "docker.io/library/ubuntu-12.04-base",
568f86eb
 			Official:      true,
 		},
4fcb9ac4
 		IndexName + "/ubuntu-12.04-base": {
96c10098
 			Index: &registrytypes.IndexInfo{
4fcb9ac4
 				Name:     IndexName,
568f86eb
 				Official: true,
 			},
ffded61d
 			RemoteName:    "library/ubuntu-12.04-base",
 			LocalName:     "ubuntu-12.04-base",
 			CanonicalName: "docker.io/library/ubuntu-12.04-base",
568f86eb
 			Official:      true,
 		},
4fcb9ac4
 		"index." + IndexName + "/ubuntu-12.04-base": {
96c10098
 			Index: &registrytypes.IndexInfo{
4fcb9ac4
 				Name:     IndexName,
568f86eb
 				Official: true,
 			},
ffded61d
 			RemoteName:    "library/ubuntu-12.04-base",
 			LocalName:     "ubuntu-12.04-base",
 			CanonicalName: "docker.io/library/ubuntu-12.04-base",
568f86eb
 			Official:      true,
 		},
 	}
 
 	for reposName, expectedRepoInfo := range expectedRepoInfos {
4352da78
 		named, err := reference.WithName(reposName)
 		if err != nil {
 			t.Error(err)
 		}
 
 		repoInfo, err := ParseRepositoryInfo(named)
568f86eb
 		if err != nil {
 			t.Error(err)
 		} else {
 			checkEqual(t, repoInfo.Index.Name, expectedRepoInfo.Index.Name, reposName)
ffded61d
 			checkEqual(t, repoInfo.RemoteName(), expectedRepoInfo.RemoteName, reposName)
 			checkEqual(t, repoInfo.Name(), expectedRepoInfo.LocalName, reposName)
 			checkEqual(t, repoInfo.FullName(), expectedRepoInfo.CanonicalName, reposName)
568f86eb
 			checkEqual(t, repoInfo.Index.Official, expectedRepoInfo.Index.Official, reposName)
 			checkEqual(t, repoInfo.Official, expectedRepoInfo.Official, reposName)
 		}
 	}
 }
 
 func TestNewIndexInfo(t *testing.T) {
59586d02
 	testIndexInfo := func(config *serviceConfig, expectedIndexInfos map[string]*registrytypes.IndexInfo) {
568f86eb
 		for indexName, expectedIndexInfo := range expectedIndexInfos {
96c10098
 			index, err := newIndexInfo(config, indexName)
568f86eb
 			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")
 			}
 		}
 	}
 
59586d02
 	config := newServiceConfig(ServiceOptions{})
4fcb9ac4
 	noMirrors := []string{}
96c10098
 	expectedIndexInfos := map[string]*registrytypes.IndexInfo{
4fcb9ac4
 		IndexName: {
 			Name:     IndexName,
568f86eb
 			Official: true,
 			Secure:   true,
 			Mirrors:  noMirrors,
 		},
4fcb9ac4
 		"index." + IndexName: {
 			Name:     IndexName,
568f86eb
 			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"})
 
96c10098
 	expectedIndexInfos = map[string]*registrytypes.IndexInfo{
4fcb9ac4
 		IndexName: {
 			Name:     IndexName,
568f86eb
 			Official: true,
 			Secure:   true,
 			Mirrors:  publicMirrors,
 		},
4fcb9ac4
 		"index." + IndexName: {
 			Name:     IndexName,
568f86eb
 			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"})
96c10098
 	expectedIndexInfos = map[string]*registrytypes.IndexInfo{
568f86eb
 		"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,
 		},
d61fce9a
 	}
568f86eb
 	testIndexInfo(config, expectedIndexInfos)
97b7b173
 }
 
c016d2de
 func TestMirrorEndpointLookup(t *testing.T) {
 	containsMirror := func(endpoints []APIEndpoint) bool {
 		for _, pe := range endpoints {
79db131a
 			if pe.URL.Host == "my.mirror" {
c016d2de
 				return true
 			}
 		}
 		return false
 	}
59586d02
 	s := Service{config: makeServiceConfig([]string{"my.mirror"}, nil)}
c016d2de
 
4352da78
 	imageName, err := reference.WithName(IndexName + "/test/image")
 	if err != nil {
 		t.Error(err)
 	}
f2d481a2
 	pushAPIEndpoints, err := s.LookupPushEndpoints(imageName.Hostname())
c016d2de
 	if err != nil {
 		t.Fatal(err)
 	}
 	if containsMirror(pushAPIEndpoints) {
 		t.Fatal("Push endpoint should not contain mirror")
 	}
 
f2d481a2
 	pullAPIEndpoints, err := s.LookupPullEndpoints(imageName.Hostname())
c016d2de
 	if err != nil {
 		t.Fatal(err)
 	}
 	if !containsMirror(pullAPIEndpoints) {
 		t.Fatal("Pull endpoint should contain mirror")
 	}
 }
 
97b7b173
 func TestPushRegistryTag(t *testing.T) {
752dd707
 	r := spawnTestRegistrySession(t)
4352da78
 	repoRef, err := reference.ParseNamed(REPO)
 	if err != nil {
 		t.Fatal(err)
 	}
 	err = r.PushRegistryTag(repoRef, imageID, "stable", makeURL("/v1/"))
97b7b173
 	if err != nil {
 		t.Fatal(err)
 	}
 }
 
 func TestPushImageJSONIndex(t *testing.T) {
752dd707
 	r := spawnTestRegistrySession(t)
97b7b173
 	imgData := []*ImgData{
5867f9e7
 		{
484ba4a8
 			ID:       "77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
97b7b173
 			Checksum: "sha256:1ac330d56e05eef6d438586545ceff7550d3bdcb6b19961f12c5ba714ee1bb37",
 		},
5867f9e7
 		{
484ba4a8
 			ID:       "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d",
97b7b173
 			Checksum: "sha256:bea7bf2e4bacd479344b737328db47b18880d09096e6674165533aa994f5e9f2",
 		},
 	}
4352da78
 	repoRef, err := reference.ParseNamed(REPO)
 	if err != nil {
 		t.Fatal(err)
 	}
 	repoData, err := r.PushImageJSONIndex(repoRef, imgData, false, nil)
97b7b173
 	if err != nil {
 		t.Fatal(err)
 	}
 	if repoData == nil {
 		t.Fatal("Expected RepositoryData object")
 	}
4352da78
 	repoData, err = r.PushImageJSONIndex(repoRef, imgData, true, []string{r.indexEndpoint.String()})
97b7b173
 	if err != nil {
 		t.Fatal(err)
 	}
 	if repoData == nil {
 		t.Fatal("Expected RepositoryData object")
 	}
 }
 
 func TestSearchRepositories(t *testing.T) {
752dd707
 	r := spawnTestRegistrySession(t)
9a0d7fe0
 	results, err := r.SearchRepositories("fakequery")
97b7b173
 	if err != nil {
 		t.Fatal(err)
 	}
 	if results == nil {
 		t.Fatal("Expected non-nil SearchResults object")
 	}
9a0d7fe0
 	assertEqual(t, results.NumResults, 1, "Expected 1 search results")
 	assertEqual(t, results.Query, "fakequery", "Expected 'fakequery' as query")
3941623f
 	assertEqual(t, results.Results[0].StarCount, 42, "Expected 'fakeimage' to have 42 stars")
484ba4a8
 }
5867f9e7
 
a1245318
 func TestTrustedLocation(t *testing.T) {
80825765
 	for _, url := range []string{"http://example.com", "https://example.com:7777", "http://docker.io", "http://test.docker.com", "https://fakedocker.com"} {
a1245318
 		req, _ := http.NewRequest("GET", url, nil)
 		if trustedLocation(req) == true {
 			t.Fatalf("'%s' shouldn't be detected as a trusted location", url)
 		}
 	}
 
80825765
 	for _, url := range []string{"https://docker.io", "https://test.docker.com:80"} {
a1245318
 		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)
 
4fcb9ac4
 		addRequiredHeadersToRedirectedRequests(reqTo, []*http.Request{reqFrom})
a1245318
 
 		if len(reqTo.Header) != 1 {
f08cd445
 			t.Fatalf("Expected 1 headers, got %d", len(reqTo.Header))
a1245318
 		}
 
 		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)
 
4fcb9ac4
 		addRequiredHeadersToRedirectedRequests(reqTo, []*http.Request{reqFrom})
a1245318
 
 		if len(reqTo.Header) != 2 {
f08cd445
 			t.Fatalf("Expected 2 headers, got %d", len(reqTo.Header))
a1245318
 		}
 
 		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'")
 		}
 	}
 }
75e3b35b
 
568f86eb
 func TestIsSecureIndex(t *testing.T) {
75e3b35b
 	tests := []struct {
 		addr               string
 		insecureRegistries []string
 		expected           bool
 	}{
4fcb9ac4
 		{IndexName, nil, true},
75e3b35b
 		{"example.com", []string{}, true},
 		{"example.com", []string{"example.com"}, false},
11380a10
 		{"localhost", []string{"localhost:5000"}, false},
75e3b35b
 		{"localhost:5000", []string{"localhost:5000"}, false},
11380a10
 		{"localhost", []string{"example.com"}, false},
75e3b35b
 		{"127.0.0.1:5000", []string{"127.0.0.1:5000"}, false},
6aba75db
 		{"localhost", nil, false},
 		{"localhost:5000", nil, false},
 		{"127.0.0.1", nil, false},
11380a10
 		{"localhost", []string{"example.com"}, false},
 		{"127.0.0.1", []string{"example.com"}, false},
6aba75db
 		{"example.com", nil, true},
28ee373e
 		{"example.com", []string{"example.com"}, false},
11380a10
 		{"127.0.0.1", []string{"example.com"}, false},
 		{"127.0.0.1:5000", []string{"example.com"}, false},
6aba75db
 		{"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},
a70d7aaf
 		{"invalid.domain.com", []string{"42.42.0.0/16"}, true},
 		{"invalid.domain.com", []string{"invalid.domain.com"}, false},
9a50dd5f
 		{"invalid.domain.com:5000", []string{"invalid.domain.com"}, true},
 		{"invalid.domain.com:5000", []string{"invalid.domain.com:5000"}, false},
28ee373e
 	}
 	for _, tt := range tests {
568f86eb
 		config := makeServiceConfig(nil, tt.insecureRegistries)
96c10098
 		if sec := isSecureIndex(config, tt.addr); sec != tt.expected {
568f86eb
 			t.Errorf("isSecureIndex failed for %q %v, expected %v got %v", tt.addr, tt.insecureRegistries, tt.expected, sec)
28ee373e
 		}
 	}
 }
927d13bc
 
 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
 }