Updating api/validation/schema_test to test testapi.Version rather than just v1beta1

This commit is contained in:
nikhiljindal
2015-04-14 11:31:12 -07:00
parent 86d3072492
commit 40842e4471
14 changed files with 303 additions and 2238 deletions

View File

@@ -73,7 +73,10 @@ func (s *SwaggerSchema) ValidateBytes(data []byte) error {
if err := json.Unmarshal(data, &obj); err != nil {
return err
}
fields := obj.(map[string]interface{})
fields, ok := obj.(map[string]interface{})
if !ok {
return fmt.Errorf("error in unmarshaling data %s", string(data))
}
apiVersion := fields["apiVersion"].(string)
kind := fields["kind"].(string)
return s.ValidateObject(obj, apiVersion, "", apiVersion+"."+kind)
@@ -84,25 +87,37 @@ func (s *SwaggerSchema) ValidateObject(obj interface{}, apiVersion, fieldName, t
// TODO: handle required fields here too.
model, ok := models[typeName]
if !ok {
glog.V(2).Infof("couldn't find type: %s, skipping validation", typeName)
return nil
return fmt.Errorf("couldn't find type: %s", typeName)
}
properties := model.Properties
fields := obj.(map[string]interface{})
if len(properties) == 0 {
// The object does not have any sub-fields.
return nil
}
fields, ok := obj.(map[string]interface{})
if !ok {
return fmt.Errorf("expected object of type map[string]interface{} as value of %s field", fieldName)
}
if len(fieldName) > 0 {
fieldName = fieldName + "."
}
for key, value := range fields {
details, ok := properties[key]
if !ok {
glog.V(2).Infof("couldn't find properties for %s, skipping", key)
// Some properties can be missing because of
// https://github.com/GoogleCloudPlatform/kubernetes/issues/6842.
glog.V(2).Infof("couldn't find properties for %s", key)
continue
}
if details.Type == nil {
glog.V(2).Infof("nil details for %s, skipping", key)
continue
if details.Type == nil && details.Ref == nil {
return fmt.Errorf("could not find the type of %s from object: %v", key, details)
}
var fieldType string
if details.Type != nil {
fieldType = *details.Type
} else {
fieldType = *details.Ref
}
fieldType := *details.Type
if value == nil {
glog.V(2).Infof("Skipping nil field: %s", key)
continue
@@ -134,7 +149,15 @@ func (s *SwaggerSchema) validateField(value interface{}, apiVersion, fieldName,
if !ok {
return NewInvalidTypeError(reflect.Array, reflect.TypeOf(value).Kind(), fieldName)
}
arrType := *fieldDetails.Items.Ref
var arrType string
if fieldDetails.Items.Ref == nil && fieldDetails.Items.Type == nil {
return NewInvalidTypeError(reflect.Array, reflect.TypeOf(value).Kind(), fieldName)
}
if fieldDetails.Items.Ref != nil {
arrType = *fieldDetails.Items.Ref
} else {
arrType = *fieldDetails.Items.Type
}
for ix := range arr {
err := s.validateField(arr[ix], apiVersion, fmt.Sprintf("%s[%d]", fieldName, ix), arrType, nil)
if err != nil {
@@ -156,6 +179,7 @@ func (s *SwaggerSchema) validateField(value interface{}, apiVersion, fieldName,
if _, ok := value.(bool); !ok {
return NewInvalidTypeError(reflect.Bool, reflect.TypeOf(value).Kind(), fieldName)
}
case "any":
default:
return fmt.Errorf("unexpected type: %v", fieldType)
}