client-go/openapi/openapitest/fileclient_test.go
Antoine Pelisse 12beb34ceb 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
2023-03-28 13:40:28 -07:00

112 lines
2.7 KiB
Go

/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package openapitest_test
import (
"testing"
"k8s.io/client-go/openapi/openapitest"
"k8s.io/kube-openapi/pkg/spec3"
kjson "sigs.k8s.io/json"
)
func TestOpenAPIEmbeddedTest(t *testing.T) {
client := openapitest.NewEmbeddedFileClient()
// make sure we get paths
paths, err := client.Paths()
if err != nil {
t.Fatalf("error fetching paths: %v", err)
}
if len(paths) == 0 {
t.Error("empty paths")
}
// spot check specific paths
expectedPaths := []string{
"api/v1",
"apis/apps/v1",
"apis/batch/v1",
"apis/networking.k8s.io/v1alpha1",
"apis/discovery.k8s.io/v1",
}
for _, p := range expectedPaths {
if _, ok := paths[p]; !ok {
t.Fatalf("expected %s", p)
}
}
// make sure all paths can load
for path, gv := range paths {
data, err := gv.Schema("application/json")
if err != nil {
t.Fatalf("error reading schema for %v: %v", path, err)
}
o := &spec3.OpenAPI{}
stricterrs, err := kjson.UnmarshalStrict(data, o)
if err != nil {
t.Fatalf("error unmarshaling schema for %v: %v", path, err)
}
if len(stricterrs) > 0 {
t.Fatalf("strict errors unmarshaling schema for %v: %v", path, stricterrs)
}
}
}
func TestOpenAPITest(t *testing.T) {
client := openapitest.NewFileClient("testdata")
// make sure we get paths
paths, err := client.Paths()
if err != nil {
t.Fatalf("error fetching paths: %v", err)
}
if len(paths) == 0 {
t.Error("empty paths")
}
// spot check specific paths
expectedPaths := []string{
"api/v1",
"apis/apps/v1",
"apis/batch/v1",
"apis/networking.k8s.io/v1alpha1",
"apis/discovery.k8s.io/v1",
}
for _, p := range expectedPaths {
if _, ok := paths[p]; !ok {
t.Fatalf("expected %s", p)
}
}
// make sure all paths can load
for path, gv := range paths {
data, err := gv.Schema("application/json")
if err != nil {
t.Fatalf("error reading schema for %v: %v", path, err)
}
o := &spec3.OpenAPI{}
stricterrs, err := kjson.UnmarshalStrict(data, o)
if err != nil {
t.Fatalf("error unmarshaling schema for %v: %v", path, err)
}
if len(stricterrs) > 0 {
t.Fatalf("strict errors unmarshaling schema for %v: %v", path, stricterrs)
}
}
}