github.com/splunk/dan1-qbec@v0.7.3/internal/remote/k8smeta/schema_test.go (about) 1 /* 2 Copyright 2019 Splunk Inc. 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 k8smeta 18 19 import ( 20 "io/ioutil" 21 "path/filepath" 22 "testing" 23 24 "github.com/golang/protobuf/proto" 25 openapi_v2 "github.com/googleapis/gnostic/OpenAPIv2" 26 "github.com/stretchr/testify/assert" 27 "github.com/stretchr/testify/require" 28 "k8s.io/apimachinery/pkg/runtime/schema" 29 ) 30 31 type sd struct{} 32 33 func (d sd) OpenAPISchema() (*openapi_v2.Document, error) { 34 b, err := ioutil.ReadFile(filepath.Join("testdata", "swagger-2.0.0.pb-v1")) 35 if err != nil { 36 return nil, err 37 } 38 var doc openapi_v2.Document 39 if err := proto.Unmarshal(b, &doc); err != nil { 40 return nil, err 41 } 42 return &doc, nil 43 } 44 45 func TestMetadataValidator(t *testing.T) { 46 a := assert.New(t) 47 ss := NewServerSchema(sd{}) 48 v, err := ss.ValidatorFor(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Namespace"}) 49 require.Nil(t, err) 50 errs := v.Validate(loadObject(t, "ns-good.json").ToUnstructured()) 51 require.Nil(t, errs) 52 53 errs = v.Validate(loadObject(t, "ns-bad.json").ToUnstructured()) 54 require.NotNil(t, errs) 55 a.Equal(1, len(errs)) 56 a.Contains(errs[0].Error(), `unknown field "foo"`) 57 58 _, err = ss.ValidatorFor(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "FooBar"}) 59 require.NotNil(t, err) 60 a.Equal(ErrSchemaNotFound, err) 61 62 }