| 1 | /* |
| 2 | Copyright 2017 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 testing |
| 18 | |
| 19 | import ( |
| 20 | "os" |
| 21 | "sync" |
| 22 | |
| 23 | openapi_v2 "github.com/google/gnostic-models/openapiv2" |
| 24 | ) |
| 25 | |
| 26 | // Fake opens and returns a openapi swagger from a file Path. It will |
| 27 | // parse only once and then return the same copy everytime. |
| 28 | type Fake struct { |
| 29 | Path string |
| 30 | |
| 31 | once sync.Once |
| 32 | document *openapi_v2.Document |
| 33 | err error |
| 34 | } |
| 35 | |
| 36 | // OpenAPISchema returns the openapi document and a potential error. |
| 37 | func (f *Fake) OpenAPISchema() (*openapi_v2.Document, error) { |
| 38 | f.once.Do(func() { |
| 39 | _, err := os.Stat(f.Path) |
| 40 | if err != nil { |
| 41 | f.err = err |
| 42 | return |
| 43 | } |
| 44 | spec, err := os.ReadFile(f.Path) |
| 45 | if err != nil { |
| 46 | f.err = err |
| 47 | return |
| 48 | } |
| 49 | f.document, f.err = openapi_v2.ParseDocument(spec) |
| 50 | }) |
| 51 | return f.document, f.err |
| 52 | } |
| 53 | |
| 54 | type Empty struct{} |
| 55 | |
| 56 | func (Empty) OpenAPISchema() (*openapi_v2.Document, error) { |
| 57 | return nil, nil |
| 58 | } |