2013-05-14 21:41:39 -04:00
|
|
|
package registry
|
2013-05-08 13:21:21 -04:00
|
|
|
|
2013-07-31 13:07:31 -04:00
|
|
|
import (
|
|
|
|
"github.com/dotcloud/docker/auth"
|
2013-08-05 14:28:05 -04:00
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-07-31 13:07:31 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
2013-07-31 13:12:40 -04:00
|
|
|
|
2013-07-31 13:07:31 -04:00
|
|
|
var (
|
|
|
|
IMAGE_ID = "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d"
|
2013-07-31 13:12:40 -04:00
|
|
|
TOKEN = []string{"fake-token"}
|
|
|
|
REPO = "foo42/bar"
|
2013-07-31 13:07:31 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func spawnTestRegistry(t *testing.T) *Registry {
|
|
|
|
authConfig := &auth.AuthConfig{}
|
2013-10-22 14:49:13 -04:00
|
|
|
r, err := NewRegistry(authConfig, utils.NewHTTPRequestFactory(), makeURL("/v1/"))
|
2013-07-31 13:07:31 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPingRegistryEndpoint(t *testing.T) {
|
2014-01-06 15:04:44 -05:00
|
|
|
standalone, err := pingRegistryEndpoint(makeURL("/v1/"))
|
2013-07-31 13:07:31 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-01-06 15:04:44 -05:00
|
|
|
assertEqual(t, standalone, true, "Expected standalone to be true (default)")
|
2013-07-31 13:07:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRemoteHistory(t *testing.T) {
|
|
|
|
r := spawnTestRegistry(t)
|
|
|
|
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 13:12:40 -04:00
|
|
|
assertEqual(t, hist[0], IMAGE_ID, "Expected "+IMAGE_ID+"as first ancestry")
|
2013-07-31 13:07:31 -04:00
|
|
|
assertEqual(t, hist[1], "77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
|
|
|
|
"Unexpected second ancestry")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLookupRemoteImage(t *testing.T) {
|
|
|
|
r := spawnTestRegistry(t)
|
|
|
|
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) {
|
|
|
|
r := spawnTestRegistry(t)
|
|
|
|
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) {
|
|
|
|
r := spawnTestRegistry(t)
|
|
|
|
data, err := r.GetRemoteImageLayer(IMAGE_ID, makeURL("/v1/"), TOKEN)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if data == nil {
|
|
|
|
t.Fatal("Expected non-nil data result")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = r.GetRemoteImageLayer("abcdef", makeURL("/v1/"), TOKEN)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected image not found error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRemoteTags(t *testing.T) {
|
|
|
|
r := spawnTestRegistry(t)
|
|
|
|
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 13:12:40 -04:00
|
|
|
assertEqual(t, tags["latest"], IMAGE_ID, "Expected tag latest to map to "+IMAGE_ID)
|
2013-07-31 13:07:31 -04: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) {
|
|
|
|
r := spawnTestRegistry(t)
|
2013-10-22 14:49:13 -04:00
|
|
|
data, err := r.GetRepositoryData("foo42/bar")
|
2013-07-31 13:07:31 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertEqual(t, len(data.ImgList), 2, "Expected 2 images in ImgList")
|
|
|
|
assertEqual(t, len(data.Endpoints), 1, "Expected one endpoint in Endpoints")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushImageJSONRegistry(t *testing.T) {
|
|
|
|
r := spawnTestRegistry(t)
|
|
|
|
imgData := &ImgData{
|
2013-07-31 13:12:40 -04:00
|
|
|
ID: "77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
|
2013-07-31 13:07:31 -04:00
|
|
|
Checksum: "sha256:1ac330d56e05eef6d438586545ceff7550d3bdcb6b19961f12c5ba714ee1bb37",
|
|
|
|
}
|
|
|
|
|
2013-07-31 13:12:40 -04:00
|
|
|
err := r.PushImageJSONRegistry(imgData, []byte{0x42, 0xdf, 0x0}, makeURL("/v1/"), TOKEN)
|
2013-07-31 13:07:31 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushImageLayerRegistry(t *testing.T) {
|
|
|
|
r := spawnTestRegistry(t)
|
2013-08-05 14:28:05 -04:00
|
|
|
layer := strings.NewReader("")
|
|
|
|
_, err := r.PushImageLayerRegistry(IMAGE_ID, layer, makeURL("/v1/"), TOKEN, []byte{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-07-31 13:07:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestResolveRepositoryName(t *testing.T) {
|
|
|
|
_, _, err := ResolveRepositoryName("https://github.com/dotcloud/docker")
|
|
|
|
assertEqual(t, err, ErrInvalidRepositoryName, "Expected error invalid repo name")
|
|
|
|
ep, repo, err := ResolveRepositoryName("fooo/bar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertEqual(t, ep, auth.IndexServerAddress(), "Expected endpoint to be index server address")
|
|
|
|
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)
|
|
|
|
}
|
2013-07-31 13:12:40 -04:00
|
|
|
assertEqual(t, ep, "http://"+u+"/v1/", "Expected endpoint to be "+u)
|
2013-07-31 13:07:31 -04:00
|
|
|
assertEqual(t, repo, "private/moonbase", "Expected endpoint to be private/moonbase")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushRegistryTag(t *testing.T) {
|
|
|
|
r := spawnTestRegistry(t)
|
|
|
|
err := r.PushRegistryTag("foo42/bar", IMAGE_ID, "stable", makeURL("/v1/"), TOKEN)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushImageJSONIndex(t *testing.T) {
|
|
|
|
r := spawnTestRegistry(t)
|
|
|
|
imgData := []*ImgData{
|
2013-09-19 23:25:00 -04:00
|
|
|
{
|
2013-07-31 13:12:40 -04:00
|
|
|
ID: "77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
|
2013-07-31 13:07:31 -04:00
|
|
|
Checksum: "sha256:1ac330d56e05eef6d438586545ceff7550d3bdcb6b19961f12c5ba714ee1bb37",
|
|
|
|
},
|
2013-09-19 23:25:00 -04:00
|
|
|
{
|
2013-07-31 13:12:40 -04:00
|
|
|
ID: "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d",
|
2013-07-31 13:07:31 -04:00
|
|
|
Checksum: "sha256:bea7bf2e4bacd479344b737328db47b18880d09096e6674165533aa994f5e9f2",
|
|
|
|
},
|
|
|
|
}
|
2013-10-22 14:49:13 -04:00
|
|
|
repoData, err := r.PushImageJSONIndex("foo42/bar", imgData, false, nil)
|
2013-07-31 13:07:31 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if repoData == nil {
|
|
|
|
t.Fatal("Expected RepositoryData object")
|
|
|
|
}
|
2013-10-22 14:49:13 -04:00
|
|
|
repoData, err = r.PushImageJSONIndex("foo42/bar", imgData, true, []string{r.indexEndpoint})
|
2013-07-31 13:07:31 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if repoData == nil {
|
|
|
|
t.Fatal("Expected RepositoryData object")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSearchRepositories(t *testing.T) {
|
|
|
|
r := spawnTestRegistry(t)
|
|
|
|
results, err := r.SearchRepositories("supercalifragilisticepsialidocious")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if results == nil {
|
|
|
|
t.Fatal("Expected non-nil SearchResults object")
|
|
|
|
}
|
|
|
|
assertEqual(t, results.NumResults, 0, "Expected 0 search results")
|
2013-07-31 13:12:40 -04:00
|
|
|
}
|
2013-09-19 23:25:00 -04:00
|
|
|
|
|
|
|
func TestValidRepositoryName(t *testing.T) {
|
|
|
|
if err := validateRepositoryName("docker/docker"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := validateRepositoryName("docker/Docker"); err == nil {
|
|
|
|
t.Log("Repository name should be invalid")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|