Skip to content

Commit 12beb34

Browse files
apelissek8s-publishing-bot
authored andcommitted
openapi: Make file client more easy to re-use
A few notes about the change: 1. I need to initialize the fileclient once, in an init function, so I don't have access to `testing.T` yet. 2. I want to be able to configure the openapi files that I use 3. We already have a "cache" client that wraps another client, we don't need to re-implement caching here, one can just do: `cache.NewClient(openapitest.NewFileClient("some/path"))` to do a cached client. Or initialize it in an init/global var. Since there is still some value to use the embedded file, make an alternative constructor while using fs.FS interface to be able to manipulate both virtual and disk-based filesystems. Kubernetes-commit: 29503fd8d45bc2c9438e92936bf4111162529b40
1 parent 861f50a commit 12beb34

File tree

3 files changed

+83
-50
lines changed

3 files changed

+83
-50
lines changed

openapi/openapitest/fileclient.go

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,35 @@ package openapitest
1919
import (
2020
"embed"
2121
"errors"
22-
"path/filepath"
22+
"io/fs"
23+
"os"
2324
"strings"
24-
"sync"
25-
"testing"
2625

2726
"k8s.io/client-go/openapi"
2827
)
2928

3029
//go:embed testdata/*_openapi.json
31-
var f embed.FS
30+
var embedded embed.FS
3231

3332
// NewFileClient returns a test client implementing the openapi.Client
34-
// interface, which serves a subset of hard-coded GroupVersion
35-
// Open API V3 specifications files. The subset of specifications is
36-
// located in the "testdata" subdirectory.
37-
func NewFileClient(t *testing.T) openapi.Client {
38-
if t == nil {
39-
panic("non-nil testing.T required; this package is only for use in tests")
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)
4045
}
41-
return &fileClient{t: t}
46+
return &fileClient{f: f}
4247
}
4348

4449
type fileClient struct {
45-
t *testing.T
46-
init sync.Once
47-
paths map[string]openapi.GroupVersion
48-
err error
50+
f fs.FS
4951
}
5052

5153
// fileClient implements the openapi.Client interface.
@@ -60,29 +62,23 @@ var _ openapi.Client = &fileClient{}
6062
//
6163
// The file contents are read only once. All files must parse correctly
6264
// into an api path, or an error is returned.
63-
func (t *fileClient) Paths() (map[string]openapi.GroupVersion, error) {
64-
t.init.Do(func() {
65-
t.paths = map[string]openapi.GroupVersion{}
66-
entries, err := f.ReadDir("testdata")
67-
if err != nil {
68-
t.err = err
69-
t.t.Error(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-
t.paths[path] = &fileGroupVersion{t: t.t, filename: filepath.Join("testdata", e.Name())}
75-
}
76-
})
77-
return t.paths, t.err
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
7877
}
7978

8079
type fileGroupVersion struct {
81-
t *testing.T
82-
init sync.Once
80+
f fs.FS
8381
filename string
84-
data []byte
85-
err error
8682
}
8783

8884
// fileGroupVersion implements the openapi.GroupVersion interface.
@@ -91,17 +87,10 @@ var _ openapi.GroupVersion = &fileGroupVersion{}
9187
// Schema returns the OpenAPI V3 specification for the GroupVersion as
9288
// unstructured bytes, or an error if the contentType is not
9389
// "application/json" or there is an error reading the spec file. The
94-
// file is read only once. The embedded file is located in the "testdata"
95-
// subdirectory.
96-
func (t *fileGroupVersion) Schema(contentType string) ([]byte, error) {
90+
// file is read only once.
91+
func (f *fileGroupVersion) Schema(contentType string) ([]byte, error) {
9792
if contentType != "application/json" {
9893
return nil, errors.New("openapitest only supports 'application/json' contentType")
9994
}
100-
t.init.Do(func() {
101-
t.data, t.err = f.ReadFile(t.filename)
102-
if t.err != nil {
103-
t.t.Error(t.err)
104-
}
105-
})
106-
return t.data, t.err
95+
return fs.ReadFile(f.f, f.filename)
10796
}

openapi/openapitest/fileclient_test.go

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,61 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package openapitest
17+
package openapitest_test
1818

1919
import (
20+
"testing"
21+
22+
"k8s.io/client-go/openapi/openapitest"
2023
"k8s.io/kube-openapi/pkg/spec3"
2124
kjson "sigs.k8s.io/json"
22-
"testing"
2325
)
2426

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+
2570
func TestOpenAPITest(t *testing.T) {
26-
client := NewFileClient(t)
71+
client := openapitest.NewFileClient("testdata")
2772

2873
// make sure we get paths
2974
paths, err := client.Paths()

openapi3/root_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func TestOpenAPIV3Root_GVSpec(t *testing.T) {
150150

151151
for _, test := range tests {
152152
t.Run(test.name, func(t *testing.T) {
153-
client := openapitest.NewFileClient(t)
153+
client := openapitest.NewEmbeddedFileClient()
154154
root := NewRoot(client)
155155
gvSpec, err := root.GVSpec(test.gv)
156156
if test.err != nil {
@@ -209,8 +209,7 @@ func TestOpenAPIV3Root_GVSpecAsMap(t *testing.T) {
209209

210210
for _, test := range tests {
211211
t.Run(test.name, func(t *testing.T) {
212-
client := openapitest.NewFileClient(t)
213-
root := NewRoot(client)
212+
root := NewRoot(openapitest.NewEmbeddedFileClient())
214213
gvSpecAsMap, err := root.GVSpecAsMap(test.gv)
215214
if test.err != nil {
216215
assert.True(t, reflect.DeepEqual(test.err, err))

0 commit comments

Comments
 (0)