2013-05-15 01:41:39 +00:00
|
|
|
package registry
|
|
|
|
|
2013-07-31 17:07:31 +00:00
|
|
|
import (
|
2014-04-14 18:32:47 +00:00
|
|
|
"fmt"
|
2014-06-05 18:37:37 +00:00
|
|
|
"net/http"
|
2014-04-14 18:32:47 +00:00
|
|
|
"net/url"
|
2013-07-31 17:07:31 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2014-06-05 18:37:37 +00:00
|
|
|
|
2014-07-24 22:19:50 +00:00
|
|
|
"github.com/docker/docker/utils"
|
2013-07-31 17:07:31 +00:00
|
|
|
)
|
2013-07-31 17:12:40 +00:00
|
|
|
|
2013-07-31 17:07:31 +00:00
|
|
|
var (
|
|
|
|
IMAGE_ID = "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d"
|
2013-07-31 17:12:40 +00:00
|
|
|
TOKEN = []string{"fake-token"}
|
|
|
|
REPO = "foo42/bar"
|
2013-07-31 17:07:31 +00:00
|
|
|
)
|
|
|
|
|
2014-08-07 14:43:06 +00:00
|
|
|
func spawnTestRegistrySession(t *testing.T) *Session {
|
2014-03-11 00:16:58 +00:00
|
|
|
authConfig := &AuthConfig{}
|
2014-10-11 03:22:12 +00:00
|
|
|
endpoint, err := NewEndpoint(makeURL("/v1/"), false)
|
2014-08-26 23:21:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
r, err := NewSession(authConfig, utils.NewHTTPRequestFactory(), endpoint, true)
|
2013-07-31 17:07:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPingRegistryEndpoint(t *testing.T) {
|
2014-10-11 03:22:12 +00:00
|
|
|
ep, err := NewEndpoint(makeURL("/v1/"), false)
|
2014-08-26 23:21:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
regInfo, err := ep.Ping()
|
2013-07-31 17:07:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-26 00:01:25 +00:00
|
|
|
assertEqual(t, regInfo.Standalone, true, "Expected standalone to be true (default)")
|
2013-07-31 17:07:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRemoteHistory(t *testing.T) {
|
2014-08-07 14:43:06 +00:00
|
|
|
r := spawnTestRegistrySession(t)
|
2013-07-31 17:07:31 +00:00
|
|
|
hist, err := r.GetRemoteHistory(IMAGE_ID, makeURL("/v1/"), TOKEN)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertEqual(t, len(hist), 2, "Expected 2 images in history")
|
2013-07-31 17:12:40 +00:00
|
|
|
assertEqual(t, hist[0], IMAGE_ID, "Expected "+IMAGE_ID+"as first ancestry")
|
2013-07-31 17:07:31 +00:00
|
|
|
assertEqual(t, hist[1], "77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
|
|
|
|
"Unexpected second ancestry")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLookupRemoteImage(t *testing.T) {
|
2014-08-07 14:43:06 +00:00
|
|
|
r := spawnTestRegistrySession(t)
|
2013-07-31 17:07:31 +00:00
|
|
|
found := r.LookupRemoteImage(IMAGE_ID, makeURL("/v1/"), TOKEN)
|
|
|
|
assertEqual(t, found, true, "Expected remote lookup to succeed")
|
|
|
|
found = r.LookupRemoteImage("abcdef", makeURL("/v1/"), TOKEN)
|
|
|
|
assertEqual(t, found, false, "Expected remote lookup to fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRemoteImageJSON(t *testing.T) {
|
2014-08-07 14:43:06 +00:00
|
|
|
r := spawnTestRegistrySession(t)
|
2013-07-31 17:07:31 +00:00
|
|
|
json, size, err := r.GetRemoteImageJSON(IMAGE_ID, makeURL("/v1/"), TOKEN)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertEqual(t, size, 154, "Expected size 154")
|
|
|
|
if len(json) <= 0 {
|
|
|
|
t.Fatal("Expected non-empty json")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err = r.GetRemoteImageJSON("abcdef", makeURL("/v1/"), TOKEN)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected image not found error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRemoteImageLayer(t *testing.T) {
|
2014-08-07 14:43:06 +00:00
|
|
|
r := spawnTestRegistrySession(t)
|
2014-03-26 00:33:17 +00:00
|
|
|
data, err := r.GetRemoteImageLayer(IMAGE_ID, makeURL("/v1/"), TOKEN, 0)
|
2013-07-31 17:07:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if data == nil {
|
|
|
|
t.Fatal("Expected non-nil data result")
|
|
|
|
}
|
|
|
|
|
2014-03-26 00:33:17 +00:00
|
|
|
_, err = r.GetRemoteImageLayer("abcdef", makeURL("/v1/"), TOKEN, 0)
|
2013-07-31 17:07:31 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected image not found error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRemoteTags(t *testing.T) {
|
2014-08-07 14:43:06 +00:00
|
|
|
r := spawnTestRegistrySession(t)
|
2013-07-31 17:07:31 +00:00
|
|
|
tags, err := r.GetRemoteTags([]string{makeURL("/v1/")}, REPO, TOKEN)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertEqual(t, len(tags), 1, "Expected one tag")
|
2013-07-31 17:12:40 +00:00
|
|
|
assertEqual(t, tags["latest"], IMAGE_ID, "Expected tag latest to map to "+IMAGE_ID)
|
2013-07-31 17:07:31 +00:00
|
|
|
|
|
|
|
_, err = r.GetRemoteTags([]string{makeURL("/v1/")}, "foo42/baz", TOKEN)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected error when fetching tags for bogus repo")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRepositoryData(t *testing.T) {
|
2014-08-07 14:43:06 +00:00
|
|
|
r := spawnTestRegistrySession(t)
|
2014-04-14 18:32:47 +00:00
|
|
|
parsedUrl, err := url.Parse(makeURL("/v1/"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
host := "http://" + parsedUrl.Host + "/v1/"
|
2013-10-22 18:49:13 +00:00
|
|
|
data, err := r.GetRepositoryData("foo42/bar")
|
2013-07-31 17:07:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertEqual(t, len(data.ImgList), 2, "Expected 2 images in ImgList")
|
2014-04-14 18:32:47 +00: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 17:07:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushImageJSONRegistry(t *testing.T) {
|
2014-08-07 14:43:06 +00:00
|
|
|
r := spawnTestRegistrySession(t)
|
2013-07-31 17:07:31 +00:00
|
|
|
imgData := &ImgData{
|
2013-07-31 17:12:40 +00:00
|
|
|
ID: "77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
|
2013-07-31 17:07:31 +00:00
|
|
|
Checksum: "sha256:1ac330d56e05eef6d438586545ceff7550d3bdcb6b19961f12c5ba714ee1bb37",
|
|
|
|
}
|
|
|
|
|
2013-07-31 17:12:40 +00:00
|
|
|
err := r.PushImageJSONRegistry(imgData, []byte{0x42, 0xdf, 0x0}, makeURL("/v1/"), TOKEN)
|
2013-07-31 17:07:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushImageLayerRegistry(t *testing.T) {
|
2014-08-07 14:43:06 +00:00
|
|
|
r := spawnTestRegistrySession(t)
|
2013-08-05 18:28:05 +00:00
|
|
|
layer := strings.NewReader("")
|
2014-02-24 17:04:27 +00:00
|
|
|
_, _, err := r.PushImageLayerRegistry(IMAGE_ID, layer, makeURL("/v1/"), TOKEN, []byte{})
|
2013-08-05 18:28:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-07-31 17:07:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestResolveRepositoryName(t *testing.T) {
|
2014-07-24 22:19:50 +00:00
|
|
|
_, _, err := ResolveRepositoryName("https://github.com/docker/docker")
|
2013-07-31 17:07:31 +00:00
|
|
|
assertEqual(t, err, ErrInvalidRepositoryName, "Expected error invalid repo name")
|
|
|
|
ep, repo, err := ResolveRepositoryName("fooo/bar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-03-11 00:16:58 +00:00
|
|
|
assertEqual(t, ep, IndexServerAddress(), "Expected endpoint to be index server address")
|
2013-07-31 17:07:31 +00:00
|
|
|
assertEqual(t, repo, "fooo/bar", "Expected resolved repo to be foo/bar")
|
|
|
|
|
|
|
|
u := makeURL("")[7:]
|
|
|
|
ep, repo, err = ResolveRepositoryName(u + "/private/moonbase")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-02-20 22:57:58 +00:00
|
|
|
assertEqual(t, ep, u, "Expected endpoint to be "+u)
|
2013-07-31 17:07:31 +00:00
|
|
|
assertEqual(t, repo, "private/moonbase", "Expected endpoint to be private/moonbase")
|
2014-04-14 23:15:38 +00:00
|
|
|
|
|
|
|
ep, repo, err = ResolveRepositoryName("ubuntu-12.04-base")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertEqual(t, ep, IndexServerAddress(), "Expected endpoint to be "+IndexServerAddress())
|
|
|
|
assertEqual(t, repo, "ubuntu-12.04-base", "Expected endpoint to be ubuntu-12.04-base")
|
2013-07-31 17:07:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushRegistryTag(t *testing.T) {
|
2014-08-07 14:43:06 +00:00
|
|
|
r := spawnTestRegistrySession(t)
|
2013-07-31 17:07:31 +00:00
|
|
|
err := r.PushRegistryTag("foo42/bar", IMAGE_ID, "stable", makeURL("/v1/"), TOKEN)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushImageJSONIndex(t *testing.T) {
|
2014-08-07 14:43:06 +00:00
|
|
|
r := spawnTestRegistrySession(t)
|
2013-07-31 17:07:31 +00:00
|
|
|
imgData := []*ImgData{
|
2013-09-20 03:25:00 +00:00
|
|
|
{
|
2013-07-31 17:12:40 +00:00
|
|
|
ID: "77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
|
2013-07-31 17:07:31 +00:00
|
|
|
Checksum: "sha256:1ac330d56e05eef6d438586545ceff7550d3bdcb6b19961f12c5ba714ee1bb37",
|
|
|
|
},
|
2013-09-20 03:25:00 +00:00
|
|
|
{
|
2013-07-31 17:12:40 +00:00
|
|
|
ID: "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d",
|
2013-07-31 17:07:31 +00:00
|
|
|
Checksum: "sha256:bea7bf2e4bacd479344b737328db47b18880d09096e6674165533aa994f5e9f2",
|
|
|
|
},
|
|
|
|
}
|
2013-10-22 18:49:13 +00:00
|
|
|
repoData, err := r.PushImageJSONIndex("foo42/bar", imgData, false, nil)
|
2013-07-31 17:07:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if repoData == nil {
|
|
|
|
t.Fatal("Expected RepositoryData object")
|
|
|
|
}
|
2014-08-26 23:21:04 +00:00
|
|
|
repoData, err = r.PushImageJSONIndex("foo42/bar", imgData, true, []string{r.indexEndpoint.String()})
|
2013-07-31 17:07:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if repoData == nil {
|
|
|
|
t.Fatal("Expected RepositoryData object")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSearchRepositories(t *testing.T) {
|
2014-08-07 14:43:06 +00:00
|
|
|
r := spawnTestRegistrySession(t)
|
2014-03-13 17:40:34 +00:00
|
|
|
results, err := r.SearchRepositories("fakequery")
|
2013-07-31 17:07:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if results == nil {
|
|
|
|
t.Fatal("Expected non-nil SearchResults object")
|
|
|
|
}
|
2014-03-13 17:40:34 +00:00
|
|
|
assertEqual(t, results.NumResults, 1, "Expected 1 search results")
|
|
|
|
assertEqual(t, results.Query, "fakequery", "Expected 'fakequery' as query")
|
|
|
|
assertEqual(t, results.Results[0].StarCount, 42, "Expected 'fakeimage' a ot hae 42 stars")
|
2013-07-31 17:12:40 +00:00
|
|
|
}
|
2013-09-20 03:25:00 +00:00
|
|
|
|
|
|
|
func TestValidRepositoryName(t *testing.T) {
|
|
|
|
if err := validateRepositoryName("docker/docker"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-08-18 00:50:15 +00:00
|
|
|
// Support 64-byte non-hexadecimal names (hexadecimal names are forbidden)
|
|
|
|
if err := validateRepositoryName("thisisthesongthatneverendsitgoesonandonandonthisisthesongthatnev"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-09-20 03:25:00 +00:00
|
|
|
if err := validateRepositoryName("docker/Docker"); err == nil {
|
|
|
|
t.Log("Repository name should be invalid")
|
|
|
|
t.Fail()
|
|
|
|
}
|
2014-03-20 23:40:58 +00:00
|
|
|
if err := validateRepositoryName("docker///docker"); err == nil {
|
|
|
|
t.Log("Repository name should be invalid")
|
|
|
|
t.Fail()
|
|
|
|
}
|
2014-08-18 00:50:15 +00:00
|
|
|
if err := validateRepositoryName("1a3f5e7d9c1b3a5f7e9d1c3b5a7f9e1d3c5b7a9f1e3d5d7c9b1a3f5e7d9c1b3a"); err == nil {
|
|
|
|
t.Log("Repository name should be invalid, 64-byte hexadecimal names forbidden")
|
|
|
|
t.Fail()
|
|
|
|
}
|
2013-09-20 03:25:00 +00:00
|
|
|
}
|
2014-06-05 18:37:37 +00:00
|
|
|
|
|
|
|
func TestTrustedLocation(t *testing.T) {
|
2014-09-23 23:18:09 +00: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 18:37:37 +00: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-23 23:18:09 +00:00
|
|
|
for _, url := range []string{"https://docker.io", "https://test.docker.com:80"} {
|
2014-06-05 18:37:37 +00: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)
|
|
|
|
|
|
|
|
AddRequiredHeadersToRedirectedRequests(reqTo, []*http.Request{reqFrom})
|
|
|
|
|
|
|
|
if len(reqTo.Header) != 1 {
|
2014-06-12 05:15:53 +00:00
|
|
|
t.Fatalf("Expected 1 headers, got %d", len(reqTo.Header))
|
2014-06-05 18:37:37 +00: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)
|
|
|
|
|
|
|
|
AddRequiredHeadersToRedirectedRequests(reqTo, []*http.Request{reqFrom})
|
|
|
|
|
|
|
|
if len(reqTo.Header) != 2 {
|
2014-06-12 05:15:53 +00:00
|
|
|
t.Fatalf("Expected 2 headers, got %d", len(reqTo.Header))
|
2014-06-05 18:37:37 +00: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 22:37:44 +00:00
|
|
|
|
|
|
|
func TestIsSecure(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
addr string
|
|
|
|
insecureRegistries []string
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{"example.com", []string{}, true},
|
|
|
|
{"example.com", []string{"example.com"}, false},
|
|
|
|
{"localhost", []string{"localhost:5000"}, false},
|
|
|
|
{"localhost:5000", []string{"localhost:5000"}, false},
|
|
|
|
{"localhost", []string{"example.com"}, false},
|
|
|
|
{"127.0.0.1:5000", []string{"127.0.0.1:5000"}, false},
|
|
|
|
{"localhost", []string{}, false},
|
|
|
|
{"localhost:5000", []string{}, false},
|
|
|
|
{"127.0.0.1", []string{}, false},
|
|
|
|
{"localhost", []string{"example.com"}, false},
|
|
|
|
{"127.0.0.1", []string{"example.com"}, false},
|
|
|
|
{"example.com", []string{}, true},
|
|
|
|
{"example.com", []string{"example.com"}, false},
|
|
|
|
{"127.0.0.1", []string{"example.com"}, false},
|
|
|
|
{"127.0.0.1:5000", []string{"example.com"}, false},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
if sec := isSecure(tt.addr, tt.insecureRegistries); sec != tt.expected {
|
|
|
|
t.Errorf("isSecure failed for %q %v, expected %v got %v", tt.addr, tt.insecureRegistries, tt.expected, sec)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|