2015-05-07 23:11:04 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/distribution"
|
2015-05-15 01:21:39 +00:00
|
|
|
"github.com/docker/distribution/registry/api/errcode"
|
2015-05-07 23:11:04 +00:00
|
|
|
"github.com/docker/distribution/registry/api/v2"
|
|
|
|
"github.com/docker/distribution/testutil"
|
|
|
|
)
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
// Test implements distribution.BlobWriter
|
|
|
|
var _ distribution.BlobWriter = &httpBlobUpload{}
|
2015-05-07 23:11:04 +00:00
|
|
|
|
|
|
|
func TestUploadReadFrom(t *testing.T) {
|
|
|
|
_, b := newRandomBlob(64)
|
|
|
|
repo := "test/upload/readfrom"
|
|
|
|
locationPath := fmt.Sprintf("/v2/%s/uploads/testid", repo)
|
|
|
|
|
|
|
|
m := testutil.RequestResponseMap([]testutil.RequestResponseMapping{
|
|
|
|
{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "GET",
|
|
|
|
Route: "/v2/",
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Docker-Distribution-API-Version": {"registry/2.0"},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Test Valid case
|
|
|
|
{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "PATCH",
|
|
|
|
Route: locationPath,
|
|
|
|
Body: b,
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusAccepted,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Docker-Upload-UUID": {"46603072-7a1b-4b41-98f9-fd8a7da89f9b"},
|
|
|
|
"Location": {locationPath},
|
|
|
|
"Range": {"0-63"},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Test invalid range
|
|
|
|
{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "PATCH",
|
|
|
|
Route: locationPath,
|
|
|
|
Body: b,
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusAccepted,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Docker-Upload-UUID": {"46603072-7a1b-4b41-98f9-fd8a7da89f9b"},
|
|
|
|
"Location": {locationPath},
|
|
|
|
"Range": {""},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Test 404
|
|
|
|
{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "PATCH",
|
|
|
|
Route: locationPath,
|
|
|
|
Body: b,
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Test 400 valid json
|
|
|
|
{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "PATCH",
|
|
|
|
Route: locationPath,
|
|
|
|
Body: b,
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusBadRequest,
|
|
|
|
Body: []byte(`
|
2015-05-27 00:18:32 +00:00
|
|
|
[
|
2015-05-07 23:11:04 +00:00
|
|
|
{
|
|
|
|
"code": "BLOB_UPLOAD_INVALID",
|
|
|
|
"detail": "more detail"
|
|
|
|
}
|
2015-05-27 00:18:32 +00:00
|
|
|
] `),
|
2015-05-07 23:11:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// Test 400 invalid json
|
|
|
|
{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "PATCH",
|
|
|
|
Route: locationPath,
|
|
|
|
Body: b,
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusBadRequest,
|
|
|
|
Body: []byte("something bad happened"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Test 500
|
|
|
|
{
|
|
|
|
Request: testutil.Request{
|
|
|
|
Method: "PATCH",
|
|
|
|
Route: locationPath,
|
|
|
|
Body: b,
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusInternalServerError,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
e, c := testServer(m)
|
|
|
|
defer c()
|
|
|
|
|
2015-05-15 20:29:44 +00:00
|
|
|
blobUpload := &httpBlobUpload{
|
2015-05-14 16:54:23 +00:00
|
|
|
client: &http.Client{},
|
2015-05-07 23:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Valid case
|
2015-05-15 20:29:44 +00:00
|
|
|
blobUpload.location = e + locationPath
|
|
|
|
n, err := blobUpload.ReadFrom(bytes.NewReader(b))
|
2015-05-07 23:11:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error calling ReadFrom: %s", err)
|
|
|
|
}
|
|
|
|
if n != 64 {
|
|
|
|
t.Fatalf("Wrong length returned from ReadFrom: %d, expected 64", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bad range
|
2015-05-15 20:29:44 +00:00
|
|
|
blobUpload.location = e + locationPath
|
|
|
|
_, err = blobUpload.ReadFrom(bytes.NewReader(b))
|
2015-05-07 23:11:04 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error when bad range received")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 404
|
2015-05-15 20:29:44 +00:00
|
|
|
blobUpload.location = e + locationPath
|
|
|
|
_, err = blobUpload.ReadFrom(bytes.NewReader(b))
|
2015-05-07 23:11:04 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error when not found")
|
|
|
|
}
|
2015-05-15 23:34:00 +00:00
|
|
|
if err != distribution.ErrBlobUploadUnknown {
|
2015-05-16 00:37:32 +00:00
|
|
|
t.Fatalf("Wrong error thrown: %s, expected %s", err, distribution.ErrBlobUploadUnknown)
|
2015-05-07 23:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 400 valid json
|
2015-05-15 20:29:44 +00:00
|
|
|
blobUpload.location = e + locationPath
|
|
|
|
_, err = blobUpload.ReadFrom(bytes.NewReader(b))
|
2015-05-07 23:11:04 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error when not found")
|
|
|
|
}
|
2015-05-27 00:18:32 +00:00
|
|
|
if uploadErr, ok := err.(errcode.Errors); !ok {
|
2015-05-07 23:11:04 +00:00
|
|
|
t.Fatalf("Wrong error type %T: %s", err, err)
|
2015-05-27 00:18:32 +00:00
|
|
|
} else if len(uploadErr) != 1 {
|
|
|
|
t.Fatalf("Unexpected number of errors: %d, expected 1", len(uploadErr))
|
2015-05-07 23:11:04 +00:00
|
|
|
} else {
|
2015-06-03 13:52:39 +00:00
|
|
|
v2Err, ok := uploadErr[0].(errcode.Error)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Not an 'Error' type: %#v", uploadErr[0])
|
|
|
|
}
|
2015-05-07 23:11:04 +00:00
|
|
|
if v2Err.Code != v2.ErrorCodeBlobUploadInvalid {
|
2015-05-15 01:21:39 +00:00
|
|
|
t.Fatalf("Unexpected error code: %s, expected %d", v2Err.Code.String(), v2.ErrorCodeBlobUploadInvalid)
|
2015-05-07 23:11:04 +00:00
|
|
|
}
|
2015-05-27 00:18:32 +00:00
|
|
|
if expected := "blob upload invalid"; v2Err.Message() != expected {
|
|
|
|
t.Fatalf("Unexpected error message: %s, expected %s", v2Err.Message(), expected)
|
2015-05-07 23:11:04 +00:00
|
|
|
}
|
|
|
|
if expected := "more detail"; v2Err.Detail.(string) != expected {
|
|
|
|
t.Fatalf("Unexpected error message: %s, expected %s", v2Err.Detail.(string), expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 400 invalid json
|
2015-05-15 20:29:44 +00:00
|
|
|
blobUpload.location = e + locationPath
|
|
|
|
_, err = blobUpload.ReadFrom(bytes.NewReader(b))
|
2015-05-07 23:11:04 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error when not found")
|
|
|
|
}
|
|
|
|
if uploadErr, ok := err.(*UnexpectedHTTPResponseError); !ok {
|
|
|
|
t.Fatalf("Wrong error type %T: %s", err, err)
|
|
|
|
} else {
|
|
|
|
respStr := string(uploadErr.Response)
|
|
|
|
if expected := "something bad happened"; respStr != expected {
|
|
|
|
t.Fatalf("Unexpected response string: %s, expected: %s", respStr, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 500
|
2015-05-15 20:29:44 +00:00
|
|
|
blobUpload.location = e + locationPath
|
|
|
|
_, err = blobUpload.ReadFrom(bytes.NewReader(b))
|
2015-05-07 23:11:04 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error when not found")
|
|
|
|
}
|
|
|
|
if uploadErr, ok := err.(*UnexpectedHTTPStatusError); !ok {
|
|
|
|
t.Fatalf("Wrong error type %T: %s", err, err)
|
|
|
|
} else if expected := "500 " + http.StatusText(http.StatusInternalServerError); uploadErr.Status != expected {
|
|
|
|
t.Fatalf("Unexpected response status: %s, expected %s", uploadErr.Status, expected)
|
|
|
|
}
|
|
|
|
}
|