2014-11-08 00:08:14 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
2014-11-13 00:59:50 +00:00
|
|
|
type routeTestCase struct {
|
2014-11-08 00:08:14 +00:00
|
|
|
RequestURI string
|
|
|
|
Vars map[string]string
|
2014-11-13 00:59:50 +00:00
|
|
|
RouteName string
|
2014-11-13 22:39:13 +00:00
|
|
|
StatusCode int
|
2014-11-08 00:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestRouter registers a test handler with all the routes and ensures that
|
|
|
|
// each route returns the expected path variables. Not method verification is
|
|
|
|
// present. This not meant to be exhaustive but as check to ensure that the
|
|
|
|
// expected variables are extracted.
|
|
|
|
//
|
|
|
|
// This may go away as the application structure comes together.
|
|
|
|
func TestRouter(t *testing.T) {
|
|
|
|
|
|
|
|
router := v2APIRouter()
|
|
|
|
|
|
|
|
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2014-11-13 00:59:50 +00:00
|
|
|
testCase := routeTestCase{
|
2014-11-08 00:08:14 +00:00
|
|
|
RequestURI: r.RequestURI,
|
|
|
|
Vars: mux.Vars(r),
|
2014-11-13 00:59:50 +00:00
|
|
|
RouteName: mux.CurrentRoute(r).GetName(),
|
2014-11-08 00:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
|
2014-11-13 00:59:50 +00:00
|
|
|
if err := enc.Encode(testCase); err != nil {
|
2014-11-08 00:08:14 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Startup test server
|
|
|
|
server := httptest.NewServer(router)
|
|
|
|
|
2014-11-13 00:59:50 +00:00
|
|
|
for _, testcase := range []routeTestCase{
|
2014-11-08 00:08:14 +00:00
|
|
|
{
|
2014-11-13 00:59:50 +00:00
|
|
|
RouteName: routeNameImageManifest,
|
2014-12-11 06:29:58 +00:00
|
|
|
RequestURI: "/v2/foo/bar/manifests/tag",
|
2014-11-13 00:59:50 +00:00
|
|
|
Vars: map[string]string{
|
|
|
|
"name": "foo/bar",
|
|
|
|
"tag": "tag",
|
2014-11-08 00:08:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-11-13 00:59:50 +00:00
|
|
|
RouteName: routeNameTags,
|
|
|
|
RequestURI: "/v2/foo/bar/tags/list",
|
|
|
|
Vars: map[string]string{
|
|
|
|
"name": "foo/bar",
|
2014-11-08 00:08:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-11-19 03:38:14 +00:00
|
|
|
RouteName: routeNameBlob,
|
2014-12-11 06:29:58 +00:00
|
|
|
RequestURI: "/v2/foo/bar/blobs/tarsum.dev+foo:abcdef0919234",
|
2014-11-13 00:59:50 +00:00
|
|
|
Vars: map[string]string{
|
|
|
|
"name": "foo/bar",
|
2014-11-19 03:38:14 +00:00
|
|
|
"digest": "tarsum.dev+foo:abcdef0919234",
|
2014-11-08 00:08:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-11-19 03:38:14 +00:00
|
|
|
RouteName: routeNameBlob,
|
2014-12-11 06:29:58 +00:00
|
|
|
RequestURI: "/v2/foo/bar/blobs/sha256:abcdef0919234",
|
2014-11-13 00:59:50 +00:00
|
|
|
Vars: map[string]string{
|
|
|
|
"name": "foo/bar",
|
2014-11-19 03:38:14 +00:00
|
|
|
"digest": "sha256:abcdef0919234",
|
2014-11-08 00:08:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-11-19 03:38:14 +00:00
|
|
|
RouteName: routeNameBlobUpload,
|
2014-12-11 06:29:58 +00:00
|
|
|
RequestURI: "/v2/foo/bar/blobs/uploads/",
|
2014-11-13 00:59:50 +00:00
|
|
|
Vars: map[string]string{
|
2014-11-19 03:38:14 +00:00
|
|
|
"name": "foo/bar",
|
2014-11-13 00:59:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-11-19 03:38:14 +00:00
|
|
|
RouteName: routeNameBlobUploadResume,
|
2014-12-11 06:29:58 +00:00
|
|
|
RequestURI: "/v2/foo/bar/blobs/uploads/uuid",
|
2014-11-13 00:59:50 +00:00
|
|
|
Vars: map[string]string{
|
2014-11-19 03:38:14 +00:00
|
|
|
"name": "foo/bar",
|
|
|
|
"uuid": "uuid",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RouteName: routeNameBlobUploadResume,
|
2014-12-11 06:29:58 +00:00
|
|
|
RequestURI: "/v2/foo/bar/blobs/uploads/D95306FA-FAD3-4E36-8D41-CF1C93EF8286",
|
2014-11-19 03:38:14 +00:00
|
|
|
Vars: map[string]string{
|
|
|
|
"name": "foo/bar",
|
|
|
|
"uuid": "D95306FA-FAD3-4E36-8D41-CF1C93EF8286",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RouteName: routeNameBlobUploadResume,
|
2014-12-11 06:29:58 +00:00
|
|
|
RequestURI: "/v2/foo/bar/blobs/uploads/RDk1MzA2RkEtRkFEMy00RTM2LThENDEtQ0YxQzkzRUY4Mjg2IA==",
|
2014-11-19 03:38:14 +00:00
|
|
|
Vars: map[string]string{
|
|
|
|
"name": "foo/bar",
|
|
|
|
"uuid": "RDk1MzA2RkEtRkFEMy00RTM2LThENDEtQ0YxQzkzRUY4Mjg2IA==",
|
2014-11-13 00:59:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Check ambiguity: ensure we can distinguish between tags for
|
|
|
|
// "foo/bar/image/image" and image for "foo/bar/image" with tag
|
|
|
|
// "tags"
|
|
|
|
RouteName: routeNameImageManifest,
|
2014-12-11 06:29:58 +00:00
|
|
|
RequestURI: "/v2/foo/bar/manifests/manifests/tags",
|
2014-11-13 00:59:50 +00:00
|
|
|
Vars: map[string]string{
|
2014-12-11 06:29:58 +00:00
|
|
|
"name": "foo/bar/manifests",
|
2014-11-13 00:59:50 +00:00
|
|
|
"tag": "tags",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// This case presents an ambiguity between foo/bar with tag="tags"
|
2014-11-19 03:38:14 +00:00
|
|
|
// and list tags for "foo/bar/manifest"
|
2014-11-13 00:59:50 +00:00
|
|
|
RouteName: routeNameTags,
|
2014-12-11 06:29:58 +00:00
|
|
|
RequestURI: "/v2/foo/bar/manifests/tags/list",
|
2014-11-13 00:59:50 +00:00
|
|
|
Vars: map[string]string{
|
2014-12-11 06:29:58 +00:00
|
|
|
"name": "foo/bar/manifests",
|
2014-11-08 00:08:14 +00:00
|
|
|
},
|
|
|
|
},
|
2014-11-13 22:39:13 +00:00
|
|
|
{
|
2014-11-19 03:38:14 +00:00
|
|
|
RouteName: routeNameBlobUploadResume,
|
2014-12-11 06:29:58 +00:00
|
|
|
RequestURI: "/v2/foo/../../blob/uploads/D95306FA-FAD3-4E36-8D41-CF1C93EF8286",
|
2014-11-13 22:39:13 +00:00
|
|
|
StatusCode: http.StatusNotFound,
|
|
|
|
},
|
2014-11-08 00:08:14 +00:00
|
|
|
} {
|
|
|
|
// Register the endpoint
|
2014-11-13 00:59:50 +00:00
|
|
|
router.GetRoute(testcase.RouteName).Handler(testHandler)
|
|
|
|
u := server.URL + testcase.RequestURI
|
2014-11-08 00:08:14 +00:00
|
|
|
|
|
|
|
resp, err := http.Get(u)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error issuing get request: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-11-13 22:39:13 +00:00
|
|
|
if testcase.StatusCode == 0 {
|
|
|
|
// Override default, zero-value
|
|
|
|
testcase.StatusCode = http.StatusOK
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != testcase.StatusCode {
|
2014-11-13 00:59:50 +00:00
|
|
|
t.Fatalf("unexpected status for %s: %v %v", u, resp.Status, resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
2014-11-13 22:39:13 +00:00
|
|
|
if testcase.StatusCode != http.StatusOK {
|
|
|
|
// We don't care about json response.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-11-08 00:08:14 +00:00
|
|
|
dec := json.NewDecoder(resp.Body)
|
|
|
|
|
2014-11-13 00:59:50 +00:00
|
|
|
var actualRouteInfo routeTestCase
|
2014-11-08 00:08:14 +00:00
|
|
|
if err := dec.Decode(&actualRouteInfo); err != nil {
|
|
|
|
t.Fatalf("error reading json response: %v", err)
|
|
|
|
}
|
2014-11-13 22:39:13 +00:00
|
|
|
// Needs to be set out of band
|
|
|
|
actualRouteInfo.StatusCode = resp.StatusCode
|
2014-11-08 00:08:14 +00:00
|
|
|
|
2014-11-13 00:59:50 +00:00
|
|
|
if actualRouteInfo.RouteName != testcase.RouteName {
|
|
|
|
t.Fatalf("incorrect route %q matched, expected %q", actualRouteInfo.RouteName, testcase.RouteName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actualRouteInfo, testcase) {
|
|
|
|
t.Fatalf("actual does not equal expected: %#v != %#v", actualRouteInfo, testcase)
|
2014-11-08 00:08:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|