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 )
}
2014-11-21 04:15:09 +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\"}}}]}"
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 )
}
}