mirror of
https://github.com/kubernetes/client-go.git
synced 2025-07-19 01:27:10 +00:00
New openapi v3 testing file client
Kubernetes-commit: 195a55c84c1a0ba9dcc278cc1cccee320334d5e1
This commit is contained in:
parent
d7a20b15ab
commit
1369f160e9
11
go.mod
11
go.mod
@ -24,11 +24,12 @@ require (
|
||||
golang.org/x/term v0.5.0
|
||||
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8
|
||||
google.golang.org/protobuf v1.28.1
|
||||
k8s.io/api v0.0.0-20230216210204-f6996b4b8b8b
|
||||
k8s.io/apimachinery v0.0.0-20230216205949-e90c788139cd
|
||||
k8s.io/api v0.0.0
|
||||
k8s.io/apimachinery v0.0.0
|
||||
k8s.io/klog/v2 v2.80.1
|
||||
k8s.io/kube-openapi v0.0.0-20230123231816-1cb3ae25d79a
|
||||
k8s.io/utils v0.0.0-20230209194617-a36077c30491
|
||||
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2
|
||||
sigs.k8s.io/structured-merge-diff/v4 v4.2.3
|
||||
sigs.k8s.io/yaml v1.3.0
|
||||
)
|
||||
@ -55,10 +56,10 @@ require (
|
||||
gopkg.in/inf.v0 v0.9.1 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
|
||||
)
|
||||
|
||||
replace (
|
||||
k8s.io/api => k8s.io/api v0.0.0-20230216210204-f6996b4b8b8b
|
||||
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20230216205949-e90c788139cd
|
||||
k8s.io/api => ../api
|
||||
k8s.io/apimachinery => ../apimachinery
|
||||
k8s.io/client-go => ../client-go
|
||||
)
|
||||
|
4
go.sum
4
go.sum
@ -473,10 +473,6 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
|
||||
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
|
||||
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
|
||||
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
|
||||
k8s.io/api v0.0.0-20230216210204-f6996b4b8b8b h1:GQnbELm7+2DGZ59+oWI8+kCSj5cd8UTznkhnhKugWqY=
|
||||
k8s.io/api v0.0.0-20230216210204-f6996b4b8b8b/go.mod h1:Y4swnoZB6W7XZB1AvPG7IixVSHZv7YB3uxyveramlQY=
|
||||
k8s.io/apimachinery v0.0.0-20230216205949-e90c788139cd h1:vkr9Hnk9T+9xhtrRXH2+9NndCHR3F1wgplgbQX5xpsk=
|
||||
k8s.io/apimachinery v0.0.0-20230216205949-e90c788139cd/go.mod h1:X4Ff26Y8AJHTAxjPrGbVLY8N3ADYqSjUy2Zn/uTUFyM=
|
||||
k8s.io/klog/v2 v2.80.1 h1:atnLQ121W371wYYFawwYx1aEY2eUfs4l3J72wtgAwV4=
|
||||
k8s.io/klog/v2 v2.80.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
|
||||
k8s.io/kube-openapi v0.0.0-20230123231816-1cb3ae25d79a h1:s6zvHjyDQX1NtVT88pvw2tddqhqY0Bz0Gbnn+yctsFU=
|
||||
|
107
openapi/openapitest/fileclient.go
Normal file
107
openapi/openapitest/fileclient.go
Normal file
@ -0,0 +1,107 @@
|
||||
/*
|
||||
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
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"k8s.io/client-go/openapi"
|
||||
)
|
||||
|
||||
//go:embed testdata/*_openapi.json
|
||||
var f 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")
|
||||
}
|
||||
return &fileClient{t: t}
|
||||
}
|
||||
|
||||
type fileClient struct {
|
||||
t *testing.T
|
||||
init sync.Once
|
||||
paths map[string]openapi.GroupVersion
|
||||
err error
|
||||
}
|
||||
|
||||
// fileClient implements the openapi.Client interface.
|
||||
var _ openapi.Client = &fileClient{}
|
||||
|
||||
// Paths returns a map of api path string to openapi.GroupVersion or
|
||||
// an error. The OpenAPI V3 GroupVersion specifications are hard-coded
|
||||
// in the "testdata" subdirectory. The api path is derived from the
|
||||
// spec filename. Example:
|
||||
//
|
||||
// apis__apps__v1_openapi.json -> apis/apps/v1
|
||||
//
|
||||
// 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
|
||||
}
|
||||
|
||||
type fileGroupVersion struct {
|
||||
t *testing.T
|
||||
init sync.Once
|
||||
filename string
|
||||
data []byte
|
||||
err error
|
||||
}
|
||||
|
||||
// fileGroupVersion implements the openapi.GroupVersion interface.
|
||||
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) {
|
||||
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
|
||||
}
|
66
openapi/openapitest/fileclient_test.go
Normal file
66
openapi/openapitest/fileclient_test.go
Normal file
@ -0,0 +1,66 @@
|
||||
/*
|
||||
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
|
||||
|
||||
import (
|
||||
"k8s.io/kube-openapi/pkg/spec3"
|
||||
kjson "sigs.k8s.io/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOpenAPITest(t *testing.T) {
|
||||
client := NewFileClient(t)
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
}
|
34853
openapi/openapitest/testdata/api__v1_openapi.json
vendored
Normal file
34853
openapi/openapitest/testdata/api__v1_openapi.json
vendored
Normal file
File diff suppressed because it is too large
Load Diff
15296
openapi/openapitest/testdata/apis__apps__v1_openapi.json
vendored
Normal file
15296
openapi/openapitest/testdata/apis__apps__v1_openapi.json
vendored
Normal file
File diff suppressed because it is too large
Load Diff
8811
openapi/openapitest/testdata/apis__batch__v1_openapi.json
vendored
Normal file
8811
openapi/openapitest/testdata/apis__batch__v1_openapi.json
vendored
Normal file
File diff suppressed because it is too large
Load Diff
2859
openapi/openapitest/testdata/apis__discovery.k8s.io__v1_openapi.json
vendored
Normal file
2859
openapi/openapitest/testdata/apis__discovery.k8s.io__v1_openapi.json
vendored
Normal file
File diff suppressed because it is too large
Load Diff
2450
openapi/openapitest/testdata/apis__networking.k8s.io__v1alpha1_openapi.json
vendored
Normal file
2450
openapi/openapitest/testdata/apis__networking.k8s.io__v1alpha1_openapi.json
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user