k8s.io/client-go@v0.31.1/openapi/openapitest/fileclient_test.go (about) 1 /* 2 Copyright 2023 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 openapitest_test 18 19 import ( 20 "testing" 21 22 "k8s.io/client-go/openapi/openapitest" 23 "k8s.io/kube-openapi/pkg/spec3" 24 kjson "sigs.k8s.io/json" 25 ) 26 27 func TestOpenAPIEmbeddedTest(t *testing.T) { 28 client := openapitest.NewEmbeddedFileClient() 29 30 // make sure we get paths 31 paths, err := client.Paths() 32 if err != nil { 33 t.Fatalf("error fetching paths: %v", err) 34 } 35 if len(paths) == 0 { 36 t.Error("empty paths") 37 } 38 39 // spot check specific paths 40 expectedPaths := []string{ 41 "api/v1", 42 "apis/apps/v1", 43 "apis/batch/v1", 44 "apis/networking.k8s.io/v1alpha1", 45 "apis/discovery.k8s.io/v1", 46 } 47 for _, p := range expectedPaths { 48 if _, ok := paths[p]; !ok { 49 t.Fatalf("expected %s", p) 50 } 51 } 52 53 // make sure all paths can load 54 for path, gv := range paths { 55 data, err := gv.Schema("application/json") 56 if err != nil { 57 t.Fatalf("error reading schema for %v: %v", path, err) 58 } 59 o := &spec3.OpenAPI{} 60 stricterrs, err := kjson.UnmarshalStrict(data, o) 61 if err != nil { 62 t.Fatalf("error unmarshaling schema for %v: %v", path, err) 63 } 64 if len(stricterrs) > 0 { 65 t.Fatalf("strict errors unmarshaling schema for %v: %v", path, stricterrs) 66 } 67 } 68 } 69 70 func TestOpenAPITest(t *testing.T) { 71 client := openapitest.NewFileClient("testdata") 72 73 // make sure we get paths 74 paths, err := client.Paths() 75 if err != nil { 76 t.Fatalf("error fetching paths: %v", err) 77 } 78 if len(paths) == 0 { 79 t.Error("empty paths") 80 } 81 82 // spot check specific paths 83 expectedPaths := []string{ 84 "api/v1", 85 "apis/apps/v1", 86 "apis/batch/v1", 87 "apis/networking.k8s.io/v1alpha1", 88 "apis/discovery.k8s.io/v1", 89 } 90 for _, p := range expectedPaths { 91 if _, ok := paths[p]; !ok { 92 t.Fatalf("expected %s", p) 93 } 94 } 95 96 // make sure all paths can load 97 for path, gv := range paths { 98 data, err := gv.Schema("application/json") 99 if err != nil { 100 t.Fatalf("error reading schema for %v: %v", path, err) 101 } 102 o := &spec3.OpenAPI{} 103 stricterrs, err := kjson.UnmarshalStrict(data, o) 104 if err != nil { 105 t.Fatalf("error unmarshaling schema for %v: %v", path, err) 106 } 107 if len(stricterrs) > 0 { 108 t.Fatalf("strict errors unmarshaling schema for %v: %v", path, stricterrs) 109 } 110 } 111 }