| 1 |
/* |
| 2 |
Copyright 2018 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package openapi |
| 18 |
|
| 19 |
import ( |
| 20 |
"reflect" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"k8s.io/kube-openapi/pkg/util/proto" |
| 24 |
"k8s.io/kube-openapi/pkg/validation/spec" |
| 25 |
) |
| 26 |
|
| 27 |
// TestOpenAPIDefinitionsToProtoSchema tests the openapi parser |
| 28 |
func TestOpenAPIDefinitionsToProtoModels(t *testing.T) { |
| 29 |
openAPISpec := &spec.Swagger{ |
| 30 |
SwaggerProps: spec.SwaggerProps{ |
| 31 |
Swagger: "2.0", |
| 32 |
Info: &spec.Info{ |
| 33 |
InfoProps: spec.InfoProps{ |
| 34 |
Title: "Kubernetes", |
| 35 |
Version: "0.0.0", |
| 36 |
}, |
| 37 |
}, |
| 38 |
Definitions: spec.Definitions{ |
| 39 |
"io.k8s.api.testgroup.v1.Foo": spec.Schema{ |
| 40 |
SchemaProps: spec.SchemaProps{ |
| 41 |
Description: "Description of Foos", |
| 42 |
Properties: map[string]spec.Schema{}, |
| 43 |
}, |
| 44 |
VendorExtensible: spec.VendorExtensible{ |
| 45 |
Extensions: spec.Extensions{ |
| 46 |
"x-kubernetes-group-version-kind": []interface{}{ |
| 47 |
map[string]interface{}{ |
| 48 |
"group": "testgroup.k8s.io", |
| 49 |
"version": "v1", |
| 50 |
"kind": "Foo", |
| 51 |
}, |
| 52 |
}, |
| 53 |
}, |
| 54 |
}, |
| 55 |
}, |
| 56 |
}, |
| 57 |
}, |
| 58 |
} |
| 59 |
expectedSchema := &proto.Arbitrary{ |
| 60 |
BaseSchema: proto.BaseSchema{ |
| 61 |
Description: "Description of Foos", |
| 62 |
Extensions: map[string]interface{}{ |
| 63 |
"x-kubernetes-group-version-kind": []interface{}{ |
| 64 |
map[interface{}]interface{}{ |
| 65 |
"group": "testgroup.k8s.io", |
| 66 |
"version": "v1", |
| 67 |
"kind": "Foo", |
| 68 |
}, |
| 69 |
}, |
| 70 |
}, |
| 71 |
Path: proto.NewPath("io.k8s.api.testgroup.v1.Foo"), |
| 72 |
}, |
| 73 |
} |
| 74 |
protoModels, err := ToProtoModels(openAPISpec) |
| 75 |
if err != nil { |
| 76 |
t.Fatalf("expected ToProtoModels not to return an error") |
| 77 |
} |
| 78 |
actualSchema := protoModels.LookupModel("io.k8s.api.testgroup.v1.Foo") |
| 79 |
if !reflect.DeepEqual(expectedSchema, actualSchema) { |
| 80 |
t.Fatalf("expected schema:\n%v\nbut got:\n%v", expectedSchema, actualSchema) |
| 81 |
} |
| 82 |
} |