2015-02-11 01:25:40 +00:00
|
|
|
package handlers
|
2014-11-11 02:57:38 +00:00
|
|
|
|
|
|
|
import (
|
2014-12-18 20:30:19 +00:00
|
|
|
"encoding/json"
|
2014-11-11 02:57:38 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
2015-02-11 02:14:23 +00:00
|
|
|
"github.com/docker/distribution/configuration"
|
2015-02-11 01:32:22 +00:00
|
|
|
"github.com/docker/distribution/registry/api/v2"
|
2015-02-11 01:34:04 +00:00
|
|
|
_ "github.com/docker/distribution/registry/auth/silly"
|
2015-02-11 01:41:09 +00:00
|
|
|
"github.com/docker/distribution/registry/storage"
|
2015-02-11 02:14:23 +00:00
|
|
|
"github.com/docker/distribution/registry/storage/driver/inmemory"
|
2015-02-07 00:19:19 +00:00
|
|
|
"golang.org/x/net/context"
|
2014-11-11 02:57:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestAppDispatcher builds an application with a test dispatcher and ensures
|
|
|
|
// that requests are properly dispatched and the handlers are constructed.
|
|
|
|
// This only tests the dispatch mechanism. The underlying dispatchers must be
|
|
|
|
// tested individually.
|
|
|
|
func TestAppDispatcher(t *testing.T) {
|
2015-01-17 02:32:27 +00:00
|
|
|
driver := inmemory.New()
|
2014-11-11 02:57:38 +00:00
|
|
|
app := &App{
|
2015-01-17 02:32:27 +00:00
|
|
|
Config: configuration.Configuration{},
|
2015-02-07 00:19:19 +00:00
|
|
|
Context: context.Background(),
|
2015-01-17 02:32:27 +00:00
|
|
|
router: v2.Router(),
|
|
|
|
driver: driver,
|
|
|
|
registry: storage.NewRegistryWithDriver(driver),
|
2014-11-11 02:57:38 +00:00
|
|
|
}
|
|
|
|
server := httptest.NewServer(app)
|
2014-12-12 06:24:25 +00:00
|
|
|
router := v2.Router()
|
2014-11-11 02:57:38 +00:00
|
|
|
|
|
|
|
serverURL, err := url.Parse(server.URL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error parsing server url: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
varCheckingDispatcher := func(expectedVars map[string]string) dispatchFunc {
|
|
|
|
return func(ctx *Context, r *http.Request) http.Handler {
|
|
|
|
// Always checks the same name context
|
2015-02-07 00:19:19 +00:00
|
|
|
if ctx.Repository.Name() != getName(ctx) {
|
2015-01-17 02:32:27 +00:00
|
|
|
t.Fatalf("unexpected name: %q != %q", ctx.Repository.Name(), "foo/bar")
|
2014-11-11 02:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we have all that is expected
|
|
|
|
for expectedK, expectedV := range expectedVars {
|
2015-02-07 00:19:19 +00:00
|
|
|
if ctx.Value(expectedK) != expectedV {
|
|
|
|
t.Fatalf("unexpected %s in context vars: %q != %q", expectedK, ctx.Value(expectedK), expectedV)
|
2014-11-11 02:57:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we only have variables that are expected
|
2015-02-07 00:19:19 +00:00
|
|
|
for k, v := range ctx.Value("vars").(map[string]string) {
|
2014-11-11 02:57:38 +00:00
|
|
|
_, ok := expectedVars[k]
|
|
|
|
|
|
|
|
if !ok { // name is checked on context
|
|
|
|
// We have an unexpected key, fail
|
|
|
|
t.Fatalf("unexpected key %q in vars with value %q", k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// unflatten a list of variables, suitable for gorilla/mux, to a map[string]string
|
|
|
|
unflatten := func(vars []string) map[string]string {
|
|
|
|
m := make(map[string]string)
|
|
|
|
for i := 0; i < len(vars)-1; i = i + 2 {
|
|
|
|
m[vars[i]] = vars[i+1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range []struct {
|
|
|
|
endpoint string
|
|
|
|
vars []string
|
|
|
|
}{
|
|
|
|
{
|
2014-12-12 06:24:25 +00:00
|
|
|
endpoint: v2.RouteNameManifest,
|
2014-11-11 02:57:38 +00:00
|
|
|
vars: []string{
|
|
|
|
"name", "foo/bar",
|
|
|
|
"tag", "sometag",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-12-12 06:24:25 +00:00
|
|
|
endpoint: v2.RouteNameTags,
|
2014-11-11 02:57:38 +00:00
|
|
|
vars: []string{
|
|
|
|
"name", "foo/bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-12-12 06:24:25 +00:00
|
|
|
endpoint: v2.RouteNameBlob,
|
2014-11-11 02:57:38 +00:00
|
|
|
vars: []string{
|
|
|
|
"name", "foo/bar",
|
2014-11-19 03:38:14 +00:00
|
|
|
"digest", "tarsum.v1+bogus:abcdef0123456789",
|
2014-11-11 02:57:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-12-12 06:24:25 +00:00
|
|
|
endpoint: v2.RouteNameBlobUpload,
|
2014-11-11 02:57:38 +00:00
|
|
|
vars: []string{
|
|
|
|
"name", "foo/bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-12-12 06:24:25 +00:00
|
|
|
endpoint: v2.RouteNameBlobUploadChunk,
|
2014-11-11 02:57:38 +00:00
|
|
|
vars: []string{
|
|
|
|
"name", "foo/bar",
|
|
|
|
"uuid", "theuuid",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
app.register(testcase.endpoint, varCheckingDispatcher(unflatten(testcase.vars)))
|
|
|
|
route := router.GetRoute(testcase.endpoint).Host(serverURL.Host)
|
|
|
|
u, err := route.URL(testcase.vars...)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.Get(u.String())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
t.Fatalf("unexpected status code: %v != %v", resp.StatusCode, http.StatusOK)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-18 20:30:19 +00:00
|
|
|
|
|
|
|
// TestNewApp covers the creation of an application via NewApp with a
|
|
|
|
// configuration.
|
|
|
|
func TestNewApp(t *testing.T) {
|
2015-02-07 00:19:19 +00:00
|
|
|
ctx := context.Background()
|
2014-12-18 20:30:19 +00:00
|
|
|
config := configuration.Configuration{
|
|
|
|
Storage: configuration.Storage{
|
|
|
|
"inmemory": nil,
|
|
|
|
},
|
|
|
|
Auth: configuration.Auth{
|
|
|
|
// For now, we simply test that new auth results in a viable
|
|
|
|
// application.
|
|
|
|
"silly": {
|
|
|
|
"realm": "realm-test",
|
|
|
|
"service": "service-test",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mostly, with this test, given a sane configuration, we are simply
|
|
|
|
// ensuring that NewApp doesn't panic. We might want to tweak this
|
|
|
|
// behavior.
|
2015-02-07 00:19:19 +00:00
|
|
|
app := NewApp(ctx, config)
|
2014-12-18 20:30:19 +00:00
|
|
|
|
|
|
|
server := httptest.NewServer(app)
|
|
|
|
builder, err := v2.NewURLBuilderFromString(server.URL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating urlbuilder: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
baseURL, err := builder.BuildBaseURL()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating baseURL: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(stevvooe): The rest of this test might belong in the API tests.
|
|
|
|
|
|
|
|
// Just hit the app and make sure we get a 401 Unauthorized error.
|
|
|
|
req, err := http.Get(baseURL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error during GET: %v", err)
|
|
|
|
}
|
|
|
|
defer req.Body.Close()
|
|
|
|
|
|
|
|
if req.StatusCode != http.StatusUnauthorized {
|
|
|
|
t.Fatalf("unexpected status code during request: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-12-31 04:09:45 +00:00
|
|
|
if req.Header.Get("Content-Type") != "application/json; charset=utf-8" {
|
|
|
|
t.Fatalf("unexpected content-type: %v != %v", req.Header.Get("Content-Type"), "application/json; charset=utf-8")
|
2014-12-18 20:30:19 +00:00
|
|
|
}
|
|
|
|
|
2014-12-19 01:20:35 +00:00
|
|
|
expectedAuthHeader := "Bearer realm=\"realm-test\",service=\"service-test\""
|
|
|
|
if req.Header.Get("Authorization") != expectedAuthHeader {
|
|
|
|
t.Fatalf("unexpected authorization header: %q != %q", req.Header.Get("Authorization"), expectedAuthHeader)
|
|
|
|
}
|
|
|
|
|
2014-12-18 20:30:19 +00:00
|
|
|
var errs v2.Errors
|
|
|
|
dec := json.NewDecoder(req.Body)
|
|
|
|
if err := dec.Decode(&errs); err != nil {
|
|
|
|
t.Fatalf("error decoding error response: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if errs.Errors[0].Code != v2.ErrorCodeUnauthorized {
|
|
|
|
t.Fatalf("unexpected error code: %v != %v", errs.Errors[0].Code, v2.ErrorCodeUnauthorized)
|
|
|
|
}
|
|
|
|
}
|