2015-05-07 23:11:04 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-08-10 17:38:30 +00:00
|
|
|
"context"
|
2015-05-07 23:11:04 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3"
|
|
|
|
"github.com/distribution/distribution/v3/registry/api/errcode"
|
|
|
|
"github.com/distribution/distribution/v3/testutil"
|
2015-05-07 23:11:04 +00:00
|
|
|
)
|
|
|
|
|
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{
|
2022-11-02 22:31:23 +00:00
|
|
|
Method: http.MethodGet,
|
2015-05-07 23:11:04 +00:00
|
|
|
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{
|
2022-11-02 22:31:23 +00:00
|
|
|
Method: http.MethodPatch,
|
2015-05-07 23:11:04 +00:00
|
|
|
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{
|
2022-11-02 22:31:23 +00:00
|
|
|
Method: http.MethodPatch,
|
2015-05-07 23:11:04 +00:00
|
|
|
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{
|
2022-11-02 22:31:23 +00:00
|
|
|
Method: http.MethodPatch,
|
2015-05-07 23:11:04 +00:00
|
|
|
Route: locationPath,
|
|
|
|
Body: b,
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Test 400 valid json
|
|
|
|
{
|
|
|
|
Request: testutil.Request{
|
2022-11-02 22:31:23 +00:00
|
|
|
Method: http.MethodPatch,
|
2015-05-07 23:11:04 +00:00
|
|
|
Route: locationPath,
|
|
|
|
Body: b,
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusBadRequest,
|
2023-08-24 07:08:04 +00:00
|
|
|
Headers: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
|
2015-05-07 23:11:04 +00:00
|
|
|
Body: []byte(`
|
2015-06-19 01:00:26 +00:00
|
|
|
{ "errors":
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"code": "BLOB_UPLOAD_INVALID",
|
2015-06-18 00:39:27 +00:00
|
|
|
"message": "blob upload invalid",
|
2015-06-19 01:00:26 +00:00
|
|
|
"detail": "more detail"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
} `),
|
2015-05-07 23:11:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// Test 400 invalid json
|
|
|
|
{
|
|
|
|
Request: testutil.Request{
|
2022-11-02 22:31:23 +00:00
|
|
|
Method: http.MethodPatch,
|
2015-05-07 23:11:04 +00:00
|
|
|
Route: locationPath,
|
|
|
|
Body: b,
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusBadRequest,
|
2023-08-24 07:08:04 +00:00
|
|
|
Headers: http.Header{"Content-Type": []string{"application/json"}},
|
2015-05-07 23:11:04 +00:00
|
|
|
Body: []byte("something bad happened"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Test 500
|
|
|
|
{
|
|
|
|
Request: testutil.Request{
|
2022-11-02 22:31:23 +00:00
|
|
|
Method: http.MethodPatch,
|
2015-05-07 23:11:04 +00:00
|
|
|
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{
|
2022-08-10 17:38:30 +00:00
|
|
|
ctx: context.Background(),
|
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])
|
|
|
|
}
|
2023-09-04 15:52:12 +00:00
|
|
|
if v2Err.Code != errcode.ErrorCodeBlobUploadInvalid {
|
|
|
|
t.Fatalf("Unexpected error code: %s, expected %d", v2Err.Code.String(), errcode.ErrorCodeBlobUploadInvalid)
|
2015-05-07 23:11:04 +00:00
|
|
|
}
|
2015-06-18 00:39:27 +00:00
|
|
|
if expected := "blob upload invalid"; v2Err.Message != expected {
|
|
|
|
t.Fatalf("Unexpected error message: %q, expected %q", v2Err.Message, expected)
|
2015-05-07 23:11:04 +00:00
|
|
|
}
|
|
|
|
if expected := "more detail"; v2Err.Detail.(string) != expected {
|
2015-06-18 00:39:27 +00:00
|
|
|
t.Fatalf("Unexpected error message: %q, expected %q", v2Err.Detail.(string), expected)
|
2015-05-07 23:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
2019-05-09 12:07:58 +00:00
|
|
|
|
|
|
|
func TestUploadSize(t *testing.T) {
|
|
|
|
_, b := newRandomBlob(64)
|
|
|
|
readFromLocationPath := "/v2/test/upload/readfrom/uploads/testid"
|
|
|
|
writeLocationPath := "/v2/test/upload/readfrom/uploads/testid"
|
|
|
|
|
|
|
|
m := testutil.RequestResponseMap([]testutil.RequestResponseMapping{
|
|
|
|
{
|
|
|
|
Request: testutil.Request{
|
2022-11-02 22:31:23 +00:00
|
|
|
Method: http.MethodGet,
|
2019-05-09 12:07:58 +00:00
|
|
|
Route: "/v2/",
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Docker-Distribution-API-Version": {"registry/2.0"},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Request: testutil.Request{
|
2022-11-02 22:31:23 +00:00
|
|
|
Method: http.MethodPatch,
|
2019-05-09 12:07:58 +00:00
|
|
|
Route: readFromLocationPath,
|
|
|
|
Body: b,
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusAccepted,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Docker-Upload-UUID": {"46603072-7a1b-4b41-98f9-fd8a7da89f9b"},
|
|
|
|
"Location": {readFromLocationPath},
|
|
|
|
"Range": {"0-63"},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Request: testutil.Request{
|
2022-11-02 22:31:23 +00:00
|
|
|
Method: http.MethodPatch,
|
2019-05-09 12:07:58 +00:00
|
|
|
Route: writeLocationPath,
|
|
|
|
Body: b,
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusAccepted,
|
|
|
|
Headers: http.Header(map[string][]string{
|
|
|
|
"Docker-Upload-UUID": {"46603072-7a1b-4b41-98f9-fd8a7da89f9b"},
|
|
|
|
"Location": {writeLocationPath},
|
|
|
|
"Range": {"0-63"},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
e, c := testServer(m)
|
|
|
|
defer c()
|
|
|
|
|
|
|
|
// Writing with ReadFrom
|
|
|
|
blobUpload := &httpBlobUpload{
|
2022-08-10 17:38:30 +00:00
|
|
|
ctx: context.Background(),
|
2019-05-09 12:07:58 +00:00
|
|
|
client: &http.Client{},
|
|
|
|
location: e + readFromLocationPath,
|
|
|
|
}
|
|
|
|
|
|
|
|
if blobUpload.Size() != 0 {
|
|
|
|
t.Fatalf("Wrong size returned from Size: %d, expected 0", blobUpload.Size())
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := blobUpload.ReadFrom(bytes.NewReader(b))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error calling ReadFrom: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if blobUpload.Size() != 64 {
|
|
|
|
t.Fatalf("Wrong size returned from Size: %d, expected 64", blobUpload.Size())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Writing with Write
|
|
|
|
blobUpload = &httpBlobUpload{
|
2022-08-10 17:38:30 +00:00
|
|
|
ctx: context.Background(),
|
2019-05-09 12:07:58 +00:00
|
|
|
client: &http.Client{},
|
|
|
|
location: e + writeLocationPath,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = blobUpload.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error calling Write: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if blobUpload.Size() != 64 {
|
|
|
|
t.Fatalf("Wrong size returned from Size: %d, expected 64", blobUpload.Size())
|
|
|
|
}
|
|
|
|
}
|
2019-05-07 09:16:05 +00:00
|
|
|
|
|
|
|
func TestUploadWrite(t *testing.T) {
|
|
|
|
_, b := newRandomBlob(64)
|
|
|
|
repo := "test/upload/write"
|
|
|
|
locationPath := fmt.Sprintf("/v2/%s/uploads/testid", repo)
|
|
|
|
|
|
|
|
m := testutil.RequestResponseMap([]testutil.RequestResponseMapping{
|
|
|
|
{
|
|
|
|
Request: testutil.Request{
|
2022-11-02 22:31:23 +00:00
|
|
|
Method: http.MethodGet,
|
2019-05-07 09:16:05 +00:00
|
|
|
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{
|
2022-11-02 22:31:23 +00:00
|
|
|
Method: http.MethodPatch,
|
2019-05-07 09:16:05 +00:00
|
|
|
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{
|
2022-11-02 22:31:23 +00:00
|
|
|
Method: http.MethodPatch,
|
2019-05-07 09:16:05 +00:00
|
|
|
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{
|
2022-11-02 22:31:23 +00:00
|
|
|
Method: http.MethodPatch,
|
2019-05-07 09:16:05 +00:00
|
|
|
Route: locationPath,
|
|
|
|
Body: b,
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Test 400 valid json
|
|
|
|
{
|
|
|
|
Request: testutil.Request{
|
2022-11-02 22:31:23 +00:00
|
|
|
Method: http.MethodPatch,
|
2019-05-07 09:16:05 +00:00
|
|
|
Route: locationPath,
|
|
|
|
Body: b,
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusBadRequest,
|
2023-08-24 07:08:04 +00:00
|
|
|
Headers: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
|
2019-05-07 09:16:05 +00:00
|
|
|
Body: []byte(`
|
|
|
|
{ "errors":
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"code": "BLOB_UPLOAD_INVALID",
|
|
|
|
"message": "blob upload invalid",
|
|
|
|
"detail": "more detail"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
} `),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Test 400 invalid json
|
|
|
|
{
|
|
|
|
Request: testutil.Request{
|
2022-11-02 22:31:23 +00:00
|
|
|
Method: http.MethodPatch,
|
2019-05-07 09:16:05 +00:00
|
|
|
Route: locationPath,
|
|
|
|
Body: b,
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusBadRequest,
|
2023-08-24 07:08:04 +00:00
|
|
|
Headers: http.Header{"Content-Type": []string{"application/json"}},
|
2019-05-07 09:16:05 +00:00
|
|
|
Body: []byte("something bad happened"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Test 500
|
|
|
|
{
|
|
|
|
Request: testutil.Request{
|
2022-11-02 22:31:23 +00:00
|
|
|
Method: http.MethodPatch,
|
2019-05-07 09:16:05 +00:00
|
|
|
Route: locationPath,
|
|
|
|
Body: b,
|
|
|
|
},
|
|
|
|
Response: testutil.Response{
|
|
|
|
StatusCode: http.StatusInternalServerError,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
e, c := testServer(m)
|
|
|
|
defer c()
|
|
|
|
|
|
|
|
blobUpload := &httpBlobUpload{
|
2022-08-10 17:38:30 +00:00
|
|
|
ctx: context.Background(),
|
2019-05-07 09:16:05 +00:00
|
|
|
client: &http.Client{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid case
|
|
|
|
blobUpload.location = e + locationPath
|
|
|
|
n, err := blobUpload.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error calling Write: %s", err)
|
|
|
|
}
|
|
|
|
if n != 64 {
|
|
|
|
t.Fatalf("Wrong length returned from Write: %d, expected 64", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bad range
|
|
|
|
blobUpload.location = e + locationPath
|
|
|
|
_, err = blobUpload.Write(b)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error when bad range received")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 404
|
|
|
|
blobUpload.location = e + locationPath
|
|
|
|
_, err = blobUpload.Write(b)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error when not found")
|
|
|
|
}
|
|
|
|
if err != distribution.ErrBlobUploadUnknown {
|
|
|
|
t.Fatalf("Wrong error thrown: %s, expected %s", err, distribution.ErrBlobUploadUnknown)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 400 valid json
|
|
|
|
blobUpload.location = e + locationPath
|
|
|
|
_, err = blobUpload.Write(b)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error when not found")
|
|
|
|
}
|
|
|
|
if uploadErr, ok := err.(errcode.Errors); !ok {
|
|
|
|
t.Fatalf("Wrong error type %T: %s", err, err)
|
|
|
|
} else if len(uploadErr) != 1 {
|
|
|
|
t.Fatalf("Unexpected number of errors: %d, expected 1", len(uploadErr))
|
|
|
|
} else {
|
|
|
|
v2Err, ok := uploadErr[0].(errcode.Error)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Not an 'Error' type: %#v", uploadErr[0])
|
|
|
|
}
|
2023-09-04 15:52:12 +00:00
|
|
|
if v2Err.Code != errcode.ErrorCodeBlobUploadInvalid {
|
|
|
|
t.Fatalf("Unexpected error code: %s, expected %d", v2Err.Code.String(), errcode.ErrorCodeBlobUploadInvalid)
|
2019-05-07 09:16:05 +00:00
|
|
|
}
|
|
|
|
if expected := "blob upload invalid"; v2Err.Message != expected {
|
|
|
|
t.Fatalf("Unexpected error message: %q, expected %q", v2Err.Message, expected)
|
|
|
|
}
|
|
|
|
if expected := "more detail"; v2Err.Detail.(string) != expected {
|
|
|
|
t.Fatalf("Unexpected error message: %q, expected %q", v2Err.Detail.(string), expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 400 invalid json
|
|
|
|
blobUpload.location = e + locationPath
|
|
|
|
_, err = blobUpload.Write(b)
|
|
|
|
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
|
|
|
|
blobUpload.location = e + locationPath
|
|
|
|
_, err = blobUpload.Write(b)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|