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 18 19 import ( 20 "embed" 21 "errors" 22 "io/fs" 23 "os" 24 "strings" 25 26 "k8s.io/client-go/openapi" 27 ) 28 29 //go:embed testdata/*_openapi.json 30 var embedded embed.FS 31 32 // NewFileClient returns a test client implementing the openapi.Client 33 // interface, which serves Open API V3 specifications files from the 34 // given path, as prepared in `api/openapi-spec/v3`. 35 func NewFileClient(path string) openapi.Client { 36 return &fileClient{f: os.DirFS(path)} 37 } 38 39 // NewEmbeddedFileClient returns a test client that uses the embedded 40 // `testdata` openapi files. 41 func NewEmbeddedFileClient() openapi.Client { 42 f, err := fs.Sub(embedded, "testdata") 43 if err != nil { 44 panic(err) 45 } 46 return &fileClient{f: f} 47 } 48 49 type fileClient struct { 50 f fs.FS 51 } 52 53 // fileClient implements the openapi.Client interface. 54 var _ openapi.Client = &fileClient{} 55 56 // Paths returns a map of api path string to openapi.GroupVersion or 57 // an error. The OpenAPI V3 GroupVersion specifications are hard-coded 58 // in the "testdata" subdirectory. The api path is derived from the 59 // spec filename. Example: 60 // 61 // apis__apps__v1_openapi.json -> apis/apps/v1 62 // 63 // The file contents are read only once. All files must parse correctly 64 // into an api path, or an error is returned. 65 func (f *fileClient) Paths() (map[string]openapi.GroupVersion, error) { 66 paths := map[string]openapi.GroupVersion{} 67 entries, err := fs.ReadDir(f.f, ".") 68 if err != nil { 69 return nil, err 70 } 71 for _, e := range entries { 72 // this reverses the transformation done in hack/update-openapi-spec.sh 73 path := strings.ReplaceAll(strings.TrimSuffix(e.Name(), "_openapi.json"), "__", "/") 74 paths[path] = &fileGroupVersion{f: f.f, filename: e.Name()} 75 } 76 return paths, nil 77 } 78 79 type fileGroupVersion struct { 80 f fs.FS 81 filename string 82 } 83 84 // fileGroupVersion implements the openapi.GroupVersion interface. 85 var _ openapi.GroupVersion = &fileGroupVersion{} 86 87 // Schema returns the OpenAPI V3 specification for the GroupVersion as 88 // unstructured bytes, or an error if the contentType is not 89 // "application/json" or there is an error reading the spec file. The 90 // file is read only once. 91 func (f *fileGroupVersion) Schema(contentType string) ([]byte, error) { 92 if contentType != "application/json" { 93 return nil, errors.New("openapitest only supports 'application/json' contentType") 94 } 95 return fs.ReadFile(f.f, f.filename) 96 } 97