2015-02-11 02:25:40 +01:00
|
|
|
package handlers
|
2014-11-11 03:57:38 +01:00
|
|
|
|
|
|
|
import (
|
2014-12-18 21:30:19 +01:00
|
|
|
"encoding/json"
|
2014-11-11 03:57:38 +01:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2015-03-10 00:23:27 +01:00
|
|
|
"reflect"
|
2014-11-11 03:57:38 +01:00
|
|
|
"testing"
|
|
|
|
|
2020-08-24 13:18:39 +02:00
|
|
|
"github.com/distribution/distribution/v3/configuration"
|
|
|
|
"github.com/distribution/distribution/v3/context"
|
|
|
|
"github.com/distribution/distribution/v3/registry/api/errcode"
|
|
|
|
v2 "github.com/distribution/distribution/v3/registry/api/v2"
|
|
|
|
"github.com/distribution/distribution/v3/registry/auth"
|
|
|
|
_ "github.com/distribution/distribution/v3/registry/auth/silly"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage"
|
|
|
|
memorycache "github.com/distribution/distribution/v3/registry/storage/cache/memory"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage/driver/testdriver"
|
2014-11-11 03:57:38 +01: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) {
|
2016-05-06 11:46:37 +02:00
|
|
|
driver := testdriver.New()
|
2015-04-28 00:58:58 +02:00
|
|
|
ctx := context.Background()
|
2015-08-18 19:56:27 +02:00
|
|
|
registry, err := storage.NewRegistry(ctx, driver, storage.BlobDescriptorCacheProvider(memorycache.NewInMemoryBlobDescriptorCacheProvider()), storage.EnableDelete, storage.EnableRedirect)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating registry: %v", err)
|
|
|
|
}
|
2014-11-11 03:57:38 +01:00
|
|
|
app := &App{
|
2015-08-20 22:56:36 +02:00
|
|
|
Config: &configuration.Configuration{},
|
2015-04-28 00:58:58 +02:00
|
|
|
Context: ctx,
|
2015-01-17 03:32:27 +01:00
|
|
|
router: v2.Router(),
|
|
|
|
driver: driver,
|
2015-08-18 19:56:27 +02:00
|
|
|
registry: registry,
|
2014-11-11 03:57:38 +01:00
|
|
|
}
|
|
|
|
server := httptest.NewServer(app)
|
2016-08-27 01:20:54 +02:00
|
|
|
defer server.Close()
|
2014-12-12 07:24:25 +01:00
|
|
|
router := v2.Router()
|
2014-11-11 03:57:38 +01: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
|
2016-02-01 22:47:34 +01:00
|
|
|
if ctx.Repository.Named().Name() != getName(ctx) {
|
|
|
|
t.Fatalf("unexpected name: %q != %q", ctx.Repository.Named().Name(), "foo/bar")
|
2014-11-11 03:57:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we have all that is expected
|
|
|
|
for expectedK, expectedV := range expectedVars {
|
2015-02-07 01:19:19 +01:00
|
|
|
if ctx.Value(expectedK) != expectedV {
|
|
|
|
t.Fatalf("unexpected %s in context vars: %q != %q", expectedK, ctx.Value(expectedK), expectedV)
|
2014-11-11 03:57:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we only have variables that are expected
|
2015-02-07 01:19:19 +01:00
|
|
|
for k, v := range ctx.Value("vars").(map[string]string) {
|
2014-11-11 03:57:38 +01: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 07:24:25 +01:00
|
|
|
endpoint: v2.RouteNameManifest,
|
2014-11-11 03:57:38 +01:00
|
|
|
vars: []string{
|
|
|
|
"name", "foo/bar",
|
2015-02-27 00:47:04 +01:00
|
|
|
"reference", "sometag",
|
2014-11-11 03:57:38 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-12-12 07:24:25 +01:00
|
|
|
endpoint: v2.RouteNameTags,
|
2014-11-11 03:57:38 +01:00
|
|
|
vars: []string{
|
|
|
|
"name", "foo/bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-12-12 07:24:25 +01:00
|
|
|
endpoint: v2.RouteNameBlobUpload,
|
2014-11-11 03:57:38 +01:00
|
|
|
vars: []string{
|
|
|
|
"name", "foo/bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-12-12 07:24:25 +01:00
|
|
|
endpoint: v2.RouteNameBlobUploadChunk,
|
2014-11-11 03:57:38 +01: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 21:30:19 +01:00
|
|
|
|
|
|
|
// TestNewApp covers the creation of an application via NewApp with a
|
|
|
|
// configuration.
|
|
|
|
func TestNewApp(t *testing.T) {
|
2015-02-07 01:19:19 +01:00
|
|
|
ctx := context.Background()
|
2014-12-18 21:30:19 +01:00
|
|
|
config := configuration.Configuration{
|
|
|
|
Storage: configuration.Storage{
|
2016-05-06 11:46:37 +02:00
|
|
|
"testdriver": nil,
|
2016-08-27 01:10:11 +02:00
|
|
|
"maintenance": configuration.Parameters{"uploadpurging": map[interface{}]interface{}{
|
|
|
|
"enabled": false,
|
|
|
|
}},
|
2014-12-18 21:30:19 +01:00
|
|
|
},
|
|
|
|
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-08-20 22:56:36 +02:00
|
|
|
app := NewApp(ctx, &config)
|
2014-12-18 21:30:19 +01:00
|
|
|
|
|
|
|
server := httptest.NewServer(app)
|
2016-08-27 01:20:54 +02:00
|
|
|
defer server.Close()
|
2016-02-23 02:49:23 +01:00
|
|
|
builder, err := v2.NewURLBuilderFromString(server.URL, false)
|
2014-12-18 21:30:19 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-01-14 09:53:03 +01:00
|
|
|
if req.Header.Get("Content-Type") != "application/json" {
|
|
|
|
t.Fatalf("unexpected content-type: %v != %v", req.Header.Get("Content-Type"), "application/json")
|
2015-05-27 03:16:45 +02:00
|
|
|
}
|
2014-12-18 21:30:19 +01:00
|
|
|
|
2014-12-19 02:20:35 +01:00
|
|
|
expectedAuthHeader := "Bearer realm=\"realm-test\",service=\"service-test\""
|
2015-03-11 20:10:49 +01:00
|
|
|
if e, a := expectedAuthHeader, req.Header.Get("WWW-Authenticate"); e != a {
|
|
|
|
t.Fatalf("unexpected WWW-Authenticate header: %q != %q", e, a)
|
2014-12-19 02:20:35 +01:00
|
|
|
}
|
|
|
|
|
2015-05-15 03:21:39 +02:00
|
|
|
var errs errcode.Errors
|
2014-12-18 21:30:19 +01:00
|
|
|
dec := json.NewDecoder(req.Body)
|
|
|
|
if err := dec.Decode(&errs); err != nil {
|
|
|
|
t.Fatalf("error decoding error response: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-06-03 15:52:39 +02:00
|
|
|
err2, ok := errs[0].(errcode.ErrorCoder)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("not an ErrorCoder: %#v", errs[0])
|
|
|
|
}
|
2015-08-07 01:25:08 +02:00
|
|
|
if err2.ErrorCode() != errcode.ErrorCodeUnauthorized {
|
|
|
|
t.Fatalf("unexpected error code: %v != %v", err2.ErrorCode(), errcode.ErrorCodeUnauthorized)
|
2014-12-18 21:30:19 +01:00
|
|
|
}
|
|
|
|
}
|
2015-03-10 00:23:27 +01:00
|
|
|
|
|
|
|
// Test the access record accumulator
|
|
|
|
func TestAppendAccessRecords(t *testing.T) {
|
|
|
|
repo := "testRepo"
|
|
|
|
|
|
|
|
expectedResource := auth.Resource{
|
|
|
|
Type: "repository",
|
|
|
|
Name: repo,
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedPullRecord := auth.Access{
|
|
|
|
Resource: expectedResource,
|
|
|
|
Action: "pull",
|
|
|
|
}
|
|
|
|
expectedPushRecord := auth.Access{
|
|
|
|
Resource: expectedResource,
|
|
|
|
Action: "push",
|
|
|
|
}
|
2017-01-03 21:27:12 +01:00
|
|
|
expectedDeleteRecord := auth.Access{
|
2015-03-10 00:23:27 +01:00
|
|
|
Resource: expectedResource,
|
2017-01-03 21:27:12 +01:00
|
|
|
Action: "delete",
|
2015-03-10 00:23:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
records := []auth.Access{}
|
|
|
|
result := appendAccessRecords(records, "GET", repo)
|
|
|
|
expectedResult := []auth.Access{expectedPullRecord}
|
|
|
|
if ok := reflect.DeepEqual(result, expectedResult); !ok {
|
|
|
|
t.Fatalf("Actual access record differs from expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
records = []auth.Access{}
|
|
|
|
result = appendAccessRecords(records, "HEAD", repo)
|
|
|
|
expectedResult = []auth.Access{expectedPullRecord}
|
|
|
|
if ok := reflect.DeepEqual(result, expectedResult); !ok {
|
|
|
|
t.Fatalf("Actual access record differs from expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
records = []auth.Access{}
|
|
|
|
result = appendAccessRecords(records, "POST", repo)
|
|
|
|
expectedResult = []auth.Access{expectedPullRecord, expectedPushRecord}
|
|
|
|
if ok := reflect.DeepEqual(result, expectedResult); !ok {
|
|
|
|
t.Fatalf("Actual access record differs from expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
records = []auth.Access{}
|
|
|
|
result = appendAccessRecords(records, "PUT", repo)
|
|
|
|
expectedResult = []auth.Access{expectedPullRecord, expectedPushRecord}
|
|
|
|
if ok := reflect.DeepEqual(result, expectedResult); !ok {
|
|
|
|
t.Fatalf("Actual access record differs from expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
records = []auth.Access{}
|
|
|
|
result = appendAccessRecords(records, "PATCH", repo)
|
|
|
|
expectedResult = []auth.Access{expectedPullRecord, expectedPushRecord}
|
|
|
|
if ok := reflect.DeepEqual(result, expectedResult); !ok {
|
|
|
|
t.Fatalf("Actual access record differs from expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
records = []auth.Access{}
|
|
|
|
result = appendAccessRecords(records, "DELETE", repo)
|
2017-01-03 21:27:12 +01:00
|
|
|
expectedResult = []auth.Access{expectedDeleteRecord}
|
2015-03-10 00:23:27 +01:00
|
|
|
if ok := reflect.DeepEqual(result, expectedResult); !ok {
|
|
|
|
t.Fatalf("Actual access record differs from expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|