Migrate webapp to use exported urls.URLBuilder package

This commit is contained in:
Stephen J Day 2014-12-11 21:59:59 -08:00
parent 9b872ca150
commit 83f882b427
4 changed files with 27 additions and 18 deletions

View File

@ -14,6 +14,7 @@ import (
"testing" "testing"
"github.com/docker/docker-registry/api/errors" "github.com/docker/docker-registry/api/errors"
"github.com/docker/docker-registry/api/urls"
"github.com/docker/docker-registry/common/testutil" "github.com/docker/docker-registry/common/testutil"
"github.com/docker/docker-registry/configuration" "github.com/docker/docker-registry/configuration"
"github.com/docker/docker-registry/digest" "github.com/docker/docker-registry/digest"
@ -34,13 +35,13 @@ func TestCheckAPI(t *testing.T) {
app := NewApp(config) app := NewApp(config)
server := httptest.NewServer(handlers.CombinedLoggingHandler(os.Stderr, app)) server := httptest.NewServer(handlers.CombinedLoggingHandler(os.Stderr, app))
builder, err := newURLBuilderFromString(server.URL) builder, err := urls.NewURLBuilderFromString(server.URL)
if err != nil { if err != nil {
t.Fatalf("error creating url builder: %v", err) t.Fatalf("error creating url builder: %v", err)
} }
baseURL, err := builder.buildBaseURL() baseURL, err := builder.BuildBaseURL()
if err != nil { if err != nil {
t.Fatalf("unexpected error building base url: %v", err) t.Fatalf("unexpected error building base url: %v", err)
} }
@ -81,7 +82,7 @@ func TestLayerAPI(t *testing.T) {
app := NewApp(config) app := NewApp(config)
server := httptest.NewServer(handlers.CombinedLoggingHandler(os.Stderr, app)) server := httptest.NewServer(handlers.CombinedLoggingHandler(os.Stderr, app))
builder, err := newURLBuilderFromString(server.URL) builder, err := urls.NewURLBuilderFromString(server.URL)
if err != nil { if err != nil {
t.Fatalf("error creating url builder: %v", err) t.Fatalf("error creating url builder: %v", err)
@ -98,7 +99,7 @@ func TestLayerAPI(t *testing.T) {
// ----------------------------------- // -----------------------------------
// Test fetch for non-existent content // Test fetch for non-existent content
layerURL, err := builder.buildLayerURL(imageName, layerDigest) layerURL, err := builder.BuildBlobURL(imageName, layerDigest)
if err != nil { if err != nil {
t.Fatalf("error building url: %v", err) t.Fatalf("error building url: %v", err)
} }
@ -121,7 +122,7 @@ func TestLayerAPI(t *testing.T) {
// ------------------------------------------ // ------------------------------------------
// Upload a layer // Upload a layer
layerUploadURL, err := builder.buildLayerUploadURL(imageName) layerUploadURL, err := builder.BuildBlobUploadURL(imageName)
if err != nil { if err != nil {
t.Fatalf("error building upload url: %v", err) t.Fatalf("error building upload url: %v", err)
} }
@ -196,7 +197,7 @@ func TestManifestAPI(t *testing.T) {
app := NewApp(config) app := NewApp(config)
server := httptest.NewServer(handlers.CombinedLoggingHandler(os.Stderr, app)) server := httptest.NewServer(handlers.CombinedLoggingHandler(os.Stderr, app))
builder, err := newURLBuilderFromString(server.URL) builder, err := urls.NewURLBuilderFromString(server.URL)
if err != nil { if err != nil {
t.Fatalf("unexpected error creating url builder: %v", err) t.Fatalf("unexpected error creating url builder: %v", err)
} }
@ -204,7 +205,7 @@ func TestManifestAPI(t *testing.T) {
imageName := "foo/bar" imageName := "foo/bar"
tag := "thetag" tag := "thetag"
manifestURL, err := builder.buildManifestURL(imageName, tag) manifestURL, err := builder.BuildManifestURL(imageName, tag)
if err != nil { if err != nil {
t.Fatalf("unexpected error getting manifest url: %v", err) t.Fatalf("unexpected error getting manifest url: %v", err)
} }
@ -240,7 +241,7 @@ func TestManifestAPI(t *testing.T) {
t.Fatalf("expected manifest unknown error: got %v", respErrs) t.Fatalf("expected manifest unknown error: got %v", respErrs)
} }
tagsURL, err := builder.buildTagsURL(imageName) tagsURL, err := builder.BuildTagsURL(imageName)
if err != nil { if err != nil {
t.Fatalf("unexpected error building tags url: %v", err) t.Fatalf("unexpected error building tags url: %v", err)
} }
@ -427,8 +428,8 @@ func putManifest(t *testing.T, msg, url string, v interface{}) *http.Response {
return resp return resp
} }
func startPushLayer(t *testing.T, ub *urlBuilder, name string) string { func startPushLayer(t *testing.T, ub *urls.URLBuilder, name string) string {
layerUploadURL, err := ub.buildLayerUploadURL(name) layerUploadURL, err := ub.BuildBlobUploadURL(name)
if err != nil { if err != nil {
t.Fatalf("unexpected error building layer upload url: %v", err) t.Fatalf("unexpected error building layer upload url: %v", err)
} }
@ -449,14 +450,21 @@ func startPushLayer(t *testing.T, ub *urlBuilder, name string) string {
} }
// pushLayer pushes the layer content returning the url on success. // pushLayer pushes the layer content returning the url on success.
func pushLayer(t *testing.T, ub *urlBuilder, name string, dgst digest.Digest, uploadURLBase string, rs io.ReadSeeker) string { func pushLayer(t *testing.T, ub *urls.URLBuilder, name string, dgst digest.Digest, uploadURLBase string, rs io.ReadSeeker) string {
rsLength, _ := rs.Seek(0, os.SEEK_END) rsLength, _ := rs.Seek(0, os.SEEK_END)
rs.Seek(0, os.SEEK_SET) rs.Seek(0, os.SEEK_SET)
uploadURL := appendValues(uploadURLBase, url.Values{ u, err := url.Parse(uploadURLBase)
if err != nil {
t.Fatalf("unexpected error parsing pushLayer url: %v", err)
}
u.RawQuery = url.Values{
"digest": []string{dgst.String()}, "digest": []string{dgst.String()},
"size": []string{fmt.Sprint(rsLength)}, "size": []string{fmt.Sprint(rsLength)},
}) }.Encode()
uploadURL := u.String()
// Just do a monolithic upload // Just do a monolithic upload
req, err := http.NewRequest("PUT", uploadURL, rs) req, err := http.NewRequest("PUT", uploadURL, rs)
@ -472,7 +480,7 @@ func pushLayer(t *testing.T, ub *urlBuilder, name string, dgst digest.Digest, up
checkResponse(t, "putting monolithic chunk", resp, http.StatusCreated) checkResponse(t, "putting monolithic chunk", resp, http.StatusCreated)
expectedLayerURL, err := ub.buildLayerURL(name, dgst) expectedLayerURL, err := ub.BuildBlobURL(name, dgst)
if err != nil { if err != nil {
t.Fatalf("error building expected layer url: %v", err) t.Fatalf("error building expected layer url: %v", err)
} }

2
app.go
View File

@ -115,7 +115,7 @@ func (app *App) dispatcher(dispatch dispatchFunc) http.Handler {
context := &Context{ context := &Context{
App: app, App: app,
Name: vars["name"], Name: vars["name"],
urlBuilder: newURLBuilderFromRequest(r), urlBuilder: urls.NewURLBuilderFromRequest(r),
} }
// Store vars for underlying handlers. // Store vars for underlying handlers.

View File

@ -3,6 +3,7 @@ package registry
import ( import (
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/docker/docker-registry/api/errors" "github.com/docker/docker-registry/api/errors"
"github.com/docker/docker-registry/api/urls"
) )
// Context should contain the request specific context for use in across // Context should contain the request specific context for use in across
@ -28,5 +29,5 @@ type Context struct {
// log provides a context specific logger. // log provides a context specific logger.
log *logrus.Entry log *logrus.Entry
urlBuilder *urlBuilder urlBuilder *urls.URLBuilder
} }

View File

@ -151,7 +151,7 @@ func (luh *layerUploadHandler) CancelLayerUpload(w http.ResponseWriter, r *http.
// chunk responses. This sets the correct headers but the response status is // chunk responses. This sets the correct headers but the response status is
// left to the caller. // left to the caller.
func (luh *layerUploadHandler) layerUploadResponse(w http.ResponseWriter, r *http.Request) error { func (luh *layerUploadHandler) layerUploadResponse(w http.ResponseWriter, r *http.Request) error {
uploadURL, err := luh.urlBuilder.forLayerUpload(luh.Upload) uploadURL, err := luh.urlBuilder.BuildBlobUploadChunkURL(luh.Upload.Name(), luh.Upload.UUID())
if err != nil { if err != nil {
logrus.Infof("error building upload url: %s", err) logrus.Infof("error building upload url: %s", err)
return err return err
@ -200,7 +200,7 @@ func (luh *layerUploadHandler) completeUpload(w http.ResponseWriter, r *http.Req
return return
} }
layerURL, err := luh.urlBuilder.forLayer(layer) layerURL, err := luh.urlBuilder.BuildBlobURL(layer.Name(), layer.Digest())
if err != nil { if err != nil {
luh.Errors.Push(errors.ErrorCodeUnknown, err) luh.Errors.Push(errors.ErrorCodeUnknown, err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)