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"
"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/configuration"
"github.com/docker/docker-registry/digest"
@ -34,13 +35,13 @@ func TestCheckAPI(t *testing.T) {
app := NewApp(config)
server := httptest.NewServer(handlers.CombinedLoggingHandler(os.Stderr, app))
builder, err := newURLBuilderFromString(server.URL)
builder, err := urls.NewURLBuilderFromString(server.URL)
if err != nil {
t.Fatalf("error creating url builder: %v", err)
}
baseURL, err := builder.buildBaseURL()
baseURL, err := builder.BuildBaseURL()
if err != nil {
t.Fatalf("unexpected error building base url: %v", err)
}
@ -81,7 +82,7 @@ func TestLayerAPI(t *testing.T) {
app := NewApp(config)
server := httptest.NewServer(handlers.CombinedLoggingHandler(os.Stderr, app))
builder, err := newURLBuilderFromString(server.URL)
builder, err := urls.NewURLBuilderFromString(server.URL)
if err != nil {
t.Fatalf("error creating url builder: %v", err)
@ -98,7 +99,7 @@ func TestLayerAPI(t *testing.T) {
// -----------------------------------
// Test fetch for non-existent content
layerURL, err := builder.buildLayerURL(imageName, layerDigest)
layerURL, err := builder.BuildBlobURL(imageName, layerDigest)
if err != nil {
t.Fatalf("error building url: %v", err)
}
@ -121,7 +122,7 @@ func TestLayerAPI(t *testing.T) {
// ------------------------------------------
// Upload a layer
layerUploadURL, err := builder.buildLayerUploadURL(imageName)
layerUploadURL, err := builder.BuildBlobUploadURL(imageName)
if err != nil {
t.Fatalf("error building upload url: %v", err)
}
@ -196,7 +197,7 @@ func TestManifestAPI(t *testing.T) {
app := NewApp(config)
server := httptest.NewServer(handlers.CombinedLoggingHandler(os.Stderr, app))
builder, err := newURLBuilderFromString(server.URL)
builder, err := urls.NewURLBuilderFromString(server.URL)
if err != nil {
t.Fatalf("unexpected error creating url builder: %v", err)
}
@ -204,7 +205,7 @@ func TestManifestAPI(t *testing.T) {
imageName := "foo/bar"
tag := "thetag"
manifestURL, err := builder.buildManifestURL(imageName, tag)
manifestURL, err := builder.BuildManifestURL(imageName, tag)
if err != nil {
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)
}
tagsURL, err := builder.buildTagsURL(imageName)
tagsURL, err := builder.BuildTagsURL(imageName)
if err != nil {
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
}
func startPushLayer(t *testing.T, ub *urlBuilder, name string) string {
layerUploadURL, err := ub.buildLayerUploadURL(name)
func startPushLayer(t *testing.T, ub *urls.URLBuilder, name string) string {
layerUploadURL, err := ub.BuildBlobUploadURL(name)
if err != nil {
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.
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)
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()},
"size": []string{fmt.Sprint(rsLength)},
})
}.Encode()
uploadURL := u.String()
// Just do a monolithic upload
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)
expectedLayerURL, err := ub.buildLayerURL(name, dgst)
expectedLayerURL, err := ub.BuildBlobURL(name, dgst)
if err != nil {
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{
App: app,
Name: vars["name"],
urlBuilder: newURLBuilderFromRequest(r),
urlBuilder: urls.NewURLBuilderFromRequest(r),
}
// Store vars for underlying handlers.

View file

@ -3,6 +3,7 @@ package registry
import (
"github.com/Sirupsen/logrus"
"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
@ -28,5 +29,5 @@ type Context struct {
// log provides a context specific logger.
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
// left to the caller.
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 {
logrus.Infof("error building upload url: %s", err)
return err
@ -200,7 +200,7 @@ func (luh *layerUploadHandler) completeUpload(w http.ResponseWriter, r *http.Req
return
}
layerURL, err := luh.urlBuilder.forLayer(layer)
layerURL, err := luh.urlBuilder.BuildBlobURL(layer.Name(), layer.Digest())
if err != nil {
luh.Errors.Push(errors.ErrorCodeUnknown, err)
w.WriteHeader(http.StatusInternalServerError)