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
This commit is contained in:
Antoine Pelisse
2023-03-28 13:40:28 -07:00
committed by Kubernetes Publisher
parent 861f50a667
commit 12beb34ceb
3 changed files with 84 additions and 51 deletions

View File

@@ -19,33 +19,35 @@ package openapitest
import (
"embed"
"errors"
"path/filepath"
"io/fs"
"os"
"strings"
"sync"
"testing"
"k8s.io/client-go/openapi"
)
//go:embed testdata/*_openapi.json
var f embed.FS
var embedded embed.FS
// NewFileClient returns a test client implementing the openapi.Client
// interface, which serves a subset of hard-coded GroupVersion
// Open API V3 specifications files. The subset of specifications is
// located in the "testdata" subdirectory.
func NewFileClient(t *testing.T) openapi.Client {
if t == nil {
panic("non-nil testing.T required; this package is only for use in tests")
// interface, which serves Open API V3 specifications files from the
// given path, as prepared in `api/openapi-spec/v3`.
func NewFileClient(path string) openapi.Client {
return &fileClient{f: os.DirFS(path)}
}
// NewEmbeddedFileClient returns a test client that uses the embedded
// `testdata` openapi files.
func NewEmbeddedFileClient() openapi.Client {
f, err := fs.Sub(embedded, "testdata")
if err != nil {
panic(err)
}
return &fileClient{t: t}
return &fileClient{f: f}
}
type fileClient struct {
t *testing.T
init sync.Once
paths map[string]openapi.GroupVersion
err error
f fs.FS
}
// fileClient implements the openapi.Client interface.
@@ -60,29 +62,23 @@ var _ openapi.Client = &fileClient{}
//
// The file contents are read only once. All files must parse correctly
// into an api path, or an error is returned.
func (t *fileClient) Paths() (map[string]openapi.GroupVersion, error) {
t.init.Do(func() {
t.paths = map[string]openapi.GroupVersion{}
entries, err := f.ReadDir("testdata")
if err != nil {
t.err = err
t.t.Error(err)
}
for _, e := range entries {
// this reverses the transformation done in hack/update-openapi-spec.sh
path := strings.ReplaceAll(strings.TrimSuffix(e.Name(), "_openapi.json"), "__", "/")
t.paths[path] = &fileGroupVersion{t: t.t, filename: filepath.Join("testdata", e.Name())}
}
})
return t.paths, t.err
func (f *fileClient) Paths() (map[string]openapi.GroupVersion, error) {
paths := map[string]openapi.GroupVersion{}
entries, err := fs.ReadDir(f.f, ".")
if err != nil {
return nil, err
}
for _, e := range entries {
// this reverses the transformation done in hack/update-openapi-spec.sh
path := strings.ReplaceAll(strings.TrimSuffix(e.Name(), "_openapi.json"), "__", "/")
paths[path] = &fileGroupVersion{f: f.f, filename: e.Name()}
}
return paths, nil
}
type fileGroupVersion struct {
t *testing.T
init sync.Once
f fs.FS
filename string
data []byte
err error
}
// fileGroupVersion implements the openapi.GroupVersion interface.
@@ -91,17 +87,10 @@ var _ openapi.GroupVersion = &fileGroupVersion{}
// Schema returns the OpenAPI V3 specification for the GroupVersion as
// unstructured bytes, or an error if the contentType is not
// "application/json" or there is an error reading the spec file. The
// file is read only once. The embedded file is located in the "testdata"
// subdirectory.
func (t *fileGroupVersion) Schema(contentType string) ([]byte, error) {
// file is read only once.
func (f *fileGroupVersion) Schema(contentType string) ([]byte, error) {
if contentType != "application/json" {
return nil, errors.New("openapitest only supports 'application/json' contentType")
}
t.init.Do(func() {
t.data, t.err = f.ReadFile(t.filename)
if t.err != nil {
t.t.Error(t.err)
}
})
return t.data, t.err
return fs.ReadFile(f.f, f.filename)
}