k8s.io/kubernetes@v1.29.3/pkg/generated/openapi/openapi_test.go (about) 1 /* 2 Copyright 2019 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 "encoding/json" 21 "reflect" 22 "testing" 23 24 "github.com/google/go-cmp/cmp" 25 "k8s.io/kube-openapi/pkg/common" 26 "k8s.io/kube-openapi/pkg/handler" 27 "k8s.io/kube-openapi/pkg/validation/spec" 28 ) 29 30 func TestOpenAPIRoundtrip(t *testing.T) { 31 dummyRef := func(name string) spec.Ref { return spec.MustCreateRef("#/definitions/dummy") } 32 for name, value := range GetOpenAPIDefinitions(dummyRef) { 33 t.Run(name, func(t *testing.T) { 34 // TODO(kubernetes/gengo#193): We currently round-trip ints to floats. 35 value.Schema = *handler.PruneDefaultsSchema(&value.Schema) 36 data, err := json.Marshal(value.Schema) 37 if err != nil { 38 t.Error(err) 39 return 40 } 41 42 roundTripped := spec.Schema{} 43 if err := json.Unmarshal(data, &roundTripped); err != nil { 44 t.Error(err) 45 return 46 } 47 48 // Remove the embedded v2 schema if it presents. 49 // The v2 schema either become the schema (when serving v2) or get pruned (v3) 50 // and it is never round-tripped. 51 delete(roundTripped.Extensions, common.ExtensionV2Schema) 52 delete(value.Schema.Extensions, common.ExtensionV2Schema) 53 54 if !reflect.DeepEqual(value.Schema, roundTripped) { 55 t.Errorf("unexpected diff (a=expected,b=roundtripped):\n%s", cmp.Diff(value.Schema, roundTripped)) 56 return 57 } 58 }) 59 } 60 }