New registry unit tests remade from scratch, using the mock registry

This commit is contained in:
shin- 2013-07-31 19:07:31 +02:00
parent 93877a859a
commit 4d9dcc3cba

View File

@ -1,168 +1,211 @@
package registry package registry
// import ( import (
// "crypto/rand" "github.com/dotcloud/docker/auth"
// "encoding/hex" "strings"
// "github.com/dotcloud/docker" "testing"
// "github.com/dotcloud/docker/auth" )
// "io/ioutil" var (
// "os" IMAGE_ID = "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d"
// "path" TOKEN = []string{"fake-token"}
// "testing" REPO = "foo42/bar"
// ) )
// func newTestRuntime() (*Runtime, error) { type simpleVersionInfo struct {
// root, err := ioutil.TempDir("", "docker-test") name string
// if err != nil { version string
// return nil, err }
// }
// if err := os.Remove(root); err != nil {
// return nil, err
// }
// if err := os.MkdirAll(root, 0700); err != nil && !os.IsExist(err) { func (v *simpleVersionInfo) Name() string {
// return nil, err return v.name
// } }
// return runtime, nil func (v *simpleVersionInfo) Version() string {
// } return v.version
}
// func TestPull(t *testing.T) { func spawnTestRegistry(t *testing.T) *Registry {
// os.Setenv("DOCKER_INDEX_URL", "") versionInfo := make([]VersionInfo, 0, 4)
// runtime, err := newTestRuntime() versionInfo = append(versionInfo, &simpleVersionInfo{"docker", "0.0.0test"})
// if err != nil { versionInfo = append(versionInfo, &simpleVersionInfo{"go", "test"})
// t.Fatal(err) versionInfo = append(versionInfo, &simpleVersionInfo{"git-commit", "test"})
// } versionInfo = append(versionInfo, &simpleVersionInfo{"kernel", "test"})
// defer nuke(runtime) authConfig := &auth.AuthConfig{}
r, err := NewRegistry("", authConfig, versionInfo...)
if err != nil {
t.Fatal(err)
}
return r
}
// err = runtime.graph.PullRepository(ioutil.Discard, "busybox", "", runtime.repositories, nil) func TestPingRegistryEndpoint(t *testing.T) {
// if err != nil { err := pingRegistryEndpoint(makeURL("/v1/"))
// t.Fatal(err) if err != nil {
// } t.Fatal(err)
// img, err := runtime.repositories.LookupImage("busybox") }
// if err != nil { }
// t.Fatal(err)
// }
// // Try to run something on this image to make sure the layer's been downloaded properly. func TestGetRemoteHistory(t *testing.T) {
// config, _, err := docker.ParseRun([]string{img.Id, "echo", "Hello World"}, runtime.capabilities) r := spawnTestRegistry(t)
// if err != nil { hist, err := r.GetRemoteHistory(IMAGE_ID, makeURL("/v1/"), TOKEN)
// t.Fatal(err) if err != nil {
// } t.Fatal(err)
}
assertEqual(t, len(hist), 2, "Expected 2 images in history")
assertEqual(t, hist[0], IMAGE_ID, "Expected " + IMAGE_ID + "as first ancestry")
assertEqual(t, hist[1], "77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
"Unexpected second ancestry")
}
// b := NewBuilder(runtime) func TestLookupRemoteImage(t *testing.T) {
// container, err := b.Create(config) r := spawnTestRegistry(t)
// if err != nil { found := r.LookupRemoteImage(IMAGE_ID, makeURL("/v1/"), TOKEN)
// t.Fatal(err) assertEqual(t, found, true, "Expected remote lookup to succeed")
// } found = r.LookupRemoteImage("abcdef", makeURL("/v1/"), TOKEN)
// if err := container.Start(); err != nil { assertEqual(t, found, false, "Expected remote lookup to fail")
// t.Fatal(err) }
// }
// if status := container.Wait(); status != 0 { func TestGetRemoteImageJSON(t *testing.T) {
// t.Fatalf("Expected status code 0, found %d instead", status) 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")
}
// func TestPullTag(t *testing.T) { _, _, err = r.GetRemoteImageJSON("abcdef", makeURL("/v1/"), TOKEN)
// os.Setenv("DOCKER_INDEX_URL", "") if err == nil {
// runtime, err := newTestRuntime() t.Fatal("Expected image not found error")
// if err != nil { }
// t.Fatal(err) }
// }
// defer nuke(runtime)
// err = runtime.graph.PullRepository(ioutil.Discard, "ubuntu", "12.04", runtime.repositories, nil) func TestGetRemoteImageLayer(t *testing.T) {
// if err != nil { r := spawnTestRegistry(t)
// t.Fatal(err) data, err := r.GetRemoteImageLayer(IMAGE_ID, makeURL("/v1/"), TOKEN)
// } if err != nil {
// _, err = runtime.repositories.LookupImage("ubuntu:12.04") t.Fatal(err)
// if err != nil { }
// t.Fatal(err) if data == nil {
// } t.Fatal("Expected non-nil data result")
}
// img2, err := runtime.repositories.LookupImage("ubuntu:12.10") _, err = r.GetRemoteImageLayer("abcdef", makeURL("/v1/"), TOKEN)
// if img2 != nil { if err == nil {
// t.Fatalf("Expected nil image but found %v instead", img2.Id) t.Fatal("Expected image not found error")
// } }
// } }
// func login(runtime *Runtime) error { func TestGetRemoteTags(t *testing.T) {
// authConfig := auth.NewAuthConfig("unittester", "surlautrerivejetattendrai", "noise+unittester@dotcloud.com", runtime.root) r := spawnTestRegistry(t)
// runtime.authConfig = authConfig tags, err := r.GetRemoteTags([]string{makeURL("/v1/")}, REPO, TOKEN)
// _, err := auth.Login(authConfig) if err != nil {
// return err t.Fatal(err)
// } }
assertEqual(t, len(tags), 1, "Expected one tag")
assertEqual(t, tags["latest"], IMAGE_ID, "Expected tag latest to map to " + IMAGE_ID)
// func TestPush(t *testing.T) { _, err = r.GetRemoteTags([]string{makeURL("/v1/")}, "foo42/baz", TOKEN)
// os.Setenv("DOCKER_INDEX_URL", "https://indexstaging-docker.dotcloud.com") if err == nil {
// defer os.Setenv("DOCKER_INDEX_URL", "") t.Fatal("Expected error when fetching tags for bogus repo")
// runtime, err := newTestRuntime() }
// if err != nil { }
// t.Fatal(err)
// }
// defer nuke(runtime)
// err = login(runtime) func TestGetRepositoryData(t *testing.T) {
// if err != nil { r := spawnTestRegistry(t)
// t.Fatal(err) data, err := r.GetRepositoryData(makeURL("/v1/"), "foo42/bar")
// } 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")
}
// err = runtime.graph.PullRepository(ioutil.Discard, "joffrey/busybox", "", runtime.repositories, nil) func TestPushImageJSONRegistry(t *testing.T) {
// if err != nil { r := spawnTestRegistry(t)
// t.Fatal(err) imgData := &ImgData{
// } ID: "77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
// tokenBuffer := make([]byte, 16) Checksum: "sha256:1ac330d56e05eef6d438586545ceff7550d3bdcb6b19961f12c5ba714ee1bb37",
// _, err = rand.Read(tokenBuffer) }
// if err != nil {
// t.Fatal(err)
// }
// token := hex.EncodeToString(tokenBuffer)[:29]
// config, _, err := ParseRun([]string{"joffrey/busybox", "touch", "/" + token}, runtime.capabilities)
// if err != nil {
// t.Fatal(err)
// }
// b := NewBuilder(runtime) err := r.PushImageJSONRegistry(imgData, []byte{ 0x42, 0xdf, 0x0 }, makeURL("/v1/"), TOKEN)
// container, err := b.Create(config) if err != nil {
// if err != nil { t.Fatal(err)
// t.Fatal(err) }
// } }
// if err := container.Start(); err != nil {
// t.Fatal(err)
// }
// if status := container.Wait(); status != 0 { func TestPushImageLayerRegistry(t *testing.T) {
// t.Fatalf("Expected status code 0, found %d instead", status) r := spawnTestRegistry(t)
// } layer := strings.NewReader("FAKELAYER")
r.PushImageLayerRegistry(IMAGE_ID, layer, makeURL("/v1/"), TOKEN)
}
// img, err := b.Commit(container, "unittester/"+token, "", "", "", nil) func TestResolveRepositoryName(t *testing.T) {
// if err != nil { _, _, err := ResolveRepositoryName("https://github.com/dotcloud/docker")
// t.Fatal(err) 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")
// repo := runtime.repositories.Repositories["unittester/"+token] u := makeURL("")[7:]
// err = runtime.graph.PushRepository(ioutil.Discard, "unittester/"+token, repo, runtime.authConfig) ep, repo, err = ResolveRepositoryName(u + "/private/moonbase")
// if err != nil { if err != nil {
// t.Fatal(err) t.Fatal(err)
// } }
assertEqual(t, ep, "http://" + u + "/v1/", "Expected endpoint to be " + u)
assertEqual(t, repo, "private/moonbase", "Expected endpoint to be private/moonbase")
}
// // Remove image so we can pull it again func TestPushRegistryTag(t *testing.T) {
// if err := runtime.graph.Delete(img.Id); err != nil { r := spawnTestRegistry(t)
// t.Fatal(err) err := r.PushRegistryTag("foo42/bar", IMAGE_ID, "stable", makeURL("/v1/"), TOKEN)
// } if err != nil {
t.Fatal(err)
}
}
// err = runtime.graph.PullRepository(ioutil.Discard, "unittester/"+token, "", runtime.repositories, runtime.authConfig) func TestPushImageJSONIndex(t *testing.T) {
// if err != nil { r := spawnTestRegistry(t)
// t.Fatal(err) imgData := []*ImgData{
// } &ImgData{
ID: "77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
Checksum: "sha256:1ac330d56e05eef6d438586545ceff7550d3bdcb6b19961f12c5ba714ee1bb37",
},
&ImgData{
ID: "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d",
Checksum: "sha256:bea7bf2e4bacd479344b737328db47b18880d09096e6674165533aa994f5e9f2",
},
}
ep := makeURL("/v1/")
repoData, err := r.PushImageJSONIndex(ep, "foo42/bar", imgData, false, nil)
if err != nil {
t.Fatal(err)
}
if repoData == nil {
t.Fatal("Expected RepositoryData object")
}
repoData, err = r.PushImageJSONIndex(ep, "foo42/bar", imgData, true, []string{ep})
if err != nil {
t.Fatal(err)
}
if repoData == nil {
t.Fatal("Expected RepositoryData object")
}
}
// layerPath, err := img.layer() func TestSearchRepositories(t *testing.T) {
// if err != nil { r := spawnTestRegistry(t)
// t.Fatal(err) results, err := r.SearchRepositories("supercalifragilisticepsialidocious")
// } if err != nil {
t.Fatal(err)
// if _, err := os.Stat(path.Join(layerPath, token)); err != nil { }
// t.Fatalf("Error while trying to retrieve token file: %v", err) if results == nil {
// } t.Fatal("Expected non-nil SearchResults object")
// } }
assertEqual(t, results.NumResults, 0, "Expected 0 search results")
}