2014-11-11 00:09:10 +01:00
package registry
import (
"encoding/json"
"testing"
)
// TestErrorCodes ensures that error code format, mappings and
// marshaling/unmarshaling. round trips are stable.
func TestErrorCodes ( t * testing . T ) {
2014-11-15 00:44:49 +01:00
for ec := range errorCodeStrings {
2014-11-11 00:09:10 +01:00
if ec . String ( ) != errorCodeStrings [ ec ] {
t . Fatalf ( "error code string incorrect: %q != %q" , ec . String ( ) , errorCodeStrings [ ec ] )
}
if ec . Message ( ) != errorCodesMessages [ ec ] {
2014-11-13 02:19:19 +01:00
t . Fatalf ( "incorrect message for error code %v: %q != %q" , ec , ec . Message ( ) , errorCodesMessages [ ec ] )
2014-11-11 00:09:10 +01:00
}
// Serialize the error code using the json library to ensure that we
// get a string and it works round trip.
p , err := json . Marshal ( ec )
if err != nil {
t . Fatalf ( "error marshaling error code %v: %v" , ec , err )
}
if len ( p ) <= 0 {
2014-11-13 02:19:19 +01:00
t . Fatalf ( "expected content in marshaled before for error code %v" , ec )
2014-11-11 00:09:10 +01:00
}
// First, unmarshal to interface and ensure we have a string.
var ecUnspecified interface { }
if err := json . Unmarshal ( p , & ecUnspecified ) ; err != nil {
t . Fatalf ( "error unmarshaling error code %v: %v" , ec , err )
}
if _ , ok := ecUnspecified . ( string ) ; ! ok {
t . Fatalf ( "expected a string for error code %v on unmarshal got a %T" , ec , ecUnspecified )
}
// Now, unmarshal with the error code type and ensure they are equal
var ecUnmarshaled ErrorCode
if err := json . Unmarshal ( p , & ecUnmarshaled ) ; err != nil {
t . Fatalf ( "error unmarshaling error code %v: %v" , ec , err )
}
if ecUnmarshaled != ec {
t . Fatalf ( "unexpected error code during error code marshal/unmarshal: %v != %v" , ecUnmarshaled , ec )
}
}
}
// TestErrorsManagement does a quick check of the Errors type to ensure that
// members are properly pushed and marshaled.
func TestErrorsManagement ( t * testing . T ) {
var errs Errors
2014-11-21 04:15:09 +01:00
errs . Push ( ErrorCodeInvalidDigest )
2014-11-11 00:09:10 +01:00
var detail DetailUnknownLayer
detail . Unknown . BlobSum = "sometestblobsumdoesntmatter"
errs . Push ( ErrorCodeUnknownLayer , detail )
p , err := json . Marshal ( errs )
if err != nil {
t . Fatalf ( "error marashaling errors: %v" , err )
}
Initial implementation of Manifest HTTP API
Push, pull and delete of manifest files in the registry have been implemented
on top of the storage services. Basic workflows, including reporting of missing
manifests are tested, including various proposed response codes. Common testing
functionality has been collected into shared methods. A test suite may be
emerging but it might better to capture more edge cases (such as resumable
upload, range requests, etc.) before we commit to a full approach.
To support clearer test cases and simpler handler methods, an application aware
urlBuilder has been added. We may want to export the functionality for use in
the client, which could allow us to abstract away from gorilla/mux.
A few error codes have been added to fill in error conditions missing from the
proposal. Some use cases have identified some problems with the approach to
error reporting that requires more work to reconcile. To resolve this, the
mapping of Go errors into error types needs to pulled out of the handlers and
into the application. We also need to move to type-based errors, with rich
information, rather than value-based errors. ErrorHandlers will probably
replace the http.Handlers to make this work correctly.
Unrelated to the above, the "length" parameter has been migrated to "size" for
completing layer uploads. This change should have gone out before but these
diffs ending up being coupled with the parameter name change due to updates to
the layer unit tests.
2014-11-26 21:16:58 +01:00
expectedJSON := "{\"errors\":[{\"code\":\"INVALID_DIGEST\",\"message\":\"provided digest did not match uploaded content\"},{\"code\":\"UNKNOWN_LAYER\",\"message\":\"referenced layer not available\",\"detail\":{\"unknown\":{\"blobSum\":\"sometestblobsumdoesntmatter\"}}}]}"
2014-11-21 04:15:09 +01:00
if string ( p ) != expectedJSON {
t . Fatalf ( "unexpected json: %q != %q" , string ( p ) , expectedJSON )
}
errs . Clear ( )
errs . Push ( ErrorCodeUnknown )
expectedJSON = "{\"errors\":[{\"code\":\"UNKNOWN\",\"message\":\"unknown error\"}]}"
p , err = json . Marshal ( errs )
if err != nil {
t . Fatalf ( "error marashaling errors: %v" , err )
}
2014-11-11 00:09:10 +01:00
if string ( p ) != expectedJSON {
t . Fatalf ( "unexpected json: %q != %q" , string ( p ) , expectedJSON )
}
}