mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 15:25:57 +00:00
openapi: Move Fakes to testing package
This commit is contained in:
parent
4e3d37c076
commit
eb735bfeb0
@ -40,6 +40,7 @@ go_test(
|
|||||||
data = ["//api/openapi-spec:swagger-spec"],
|
data = ["//api/openapi-spec:swagger-spec"],
|
||||||
deps = [
|
deps = [
|
||||||
":go_default_library",
|
":go_default_library",
|
||||||
|
"//pkg/kubectl/cmd/util/openapi/testing:go_default_library",
|
||||||
"//vendor/github.com/googleapis/gnostic/OpenAPIv2:go_default_library",
|
"//vendor/github.com/googleapis/gnostic/OpenAPIv2:go_default_library",
|
||||||
"//vendor/github.com/googleapis/gnostic/compiler:go_default_library",
|
"//vendor/github.com/googleapis/gnostic/compiler:go_default_library",
|
||||||
"//vendor/github.com/onsi/ginkgo:go_default_library",
|
"//vendor/github.com/onsi/ginkgo:go_default_library",
|
||||||
@ -60,7 +61,10 @@ filegroup(
|
|||||||
|
|
||||||
filegroup(
|
filegroup(
|
||||||
name = "all-srcs",
|
name = "all-srcs",
|
||||||
srcs = [":package-srcs"],
|
srcs = [
|
||||||
|
":package-srcs",
|
||||||
|
"//pkg/kubectl/cmd/util/openapi/testing:all-srcs",
|
||||||
|
],
|
||||||
tags = ["automanaged"],
|
tags = ["automanaged"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -23,16 +23,17 @@ import (
|
|||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
|
|
||||||
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
|
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
|
||||||
|
tst "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = Describe("Getting the Resources", func() {
|
var _ = Describe("Getting the Resources", func() {
|
||||||
var client *fakeOpenAPIClient
|
var client *tst.FakeClient
|
||||||
var expectedData openapi.Resources
|
var expectedData openapi.Resources
|
||||||
var instance openapi.Getter
|
var instance openapi.Getter
|
||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
client = &fakeOpenAPIClient{}
|
client = tst.NewFakeClient(&fakeSchema)
|
||||||
d, err := data.OpenAPISchema()
|
d, err := fakeSchema.OpenAPISchema()
|
||||||
Expect(err).To(BeNil())
|
Expect(err).To(BeNil())
|
||||||
|
|
||||||
expectedData, err = openapi.NewOpenAPIData(d)
|
expectedData, err = openapi.NewOpenAPIData(d)
|
||||||
@ -43,34 +44,34 @@ var _ = Describe("Getting the Resources", func() {
|
|||||||
|
|
||||||
Context("when the server returns a successful result", func() {
|
Context("when the server returns a successful result", func() {
|
||||||
It("should return the same data for multiple calls", func() {
|
It("should return the same data for multiple calls", func() {
|
||||||
Expect(client.calls).To(Equal(0))
|
Expect(client.Calls).To(Equal(0))
|
||||||
|
|
||||||
result, err := instance.Get()
|
result, err := instance.Get()
|
||||||
Expect(err).To(BeNil())
|
Expect(err).To(BeNil())
|
||||||
Expect(result).To(Equal(expectedData))
|
Expect(result).To(Equal(expectedData))
|
||||||
Expect(client.calls).To(Equal(1))
|
Expect(client.Calls).To(Equal(1))
|
||||||
|
|
||||||
result, err = instance.Get()
|
result, err = instance.Get()
|
||||||
Expect(err).To(BeNil())
|
Expect(err).To(BeNil())
|
||||||
Expect(result).To(Equal(expectedData))
|
Expect(result).To(Equal(expectedData))
|
||||||
// No additional client calls expected
|
// No additional client calls expected
|
||||||
Expect(client.calls).To(Equal(1))
|
Expect(client.Calls).To(Equal(1))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
Context("when the server returns an unsuccessful result", func() {
|
Context("when the server returns an unsuccessful result", func() {
|
||||||
It("should return the same instance for multiple calls.", func() {
|
It("should return the same instance for multiple calls.", func() {
|
||||||
Expect(client.calls).To(Equal(0))
|
Expect(client.Calls).To(Equal(0))
|
||||||
|
|
||||||
client.err = fmt.Errorf("expected error")
|
client.Err = fmt.Errorf("expected error")
|
||||||
_, err := instance.Get()
|
_, err := instance.Get()
|
||||||
Expect(err).To(Equal(client.err))
|
Expect(err).To(Equal(client.Err))
|
||||||
Expect(client.calls).To(Equal(1))
|
Expect(client.Calls).To(Equal(1))
|
||||||
|
|
||||||
_, err = instance.Get()
|
_, err = instance.Get()
|
||||||
Expect(err).To(Equal(client.err))
|
Expect(err).To(Equal(client.Err))
|
||||||
// No additional client calls expected
|
// No additional client calls expected
|
||||||
Expect(client.calls).To(Equal(1))
|
Expect(client.Calls).To(Equal(1))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -17,17 +17,22 @@ limitations under the License.
|
|||||||
package openapi_test
|
package openapi_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
|
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
|
||||||
|
tst "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var fakeSchema = tst.Fake{Path: filepath.Join("..", "..", "..", "..", "..", "api", "openapi-spec", "swagger.json")}
|
||||||
|
|
||||||
var _ = Describe("Reading apps/v1beta1/Deployment from openAPIData", func() {
|
var _ = Describe("Reading apps/v1beta1/Deployment from openAPIData", func() {
|
||||||
var resources openapi.Resources
|
var resources openapi.Resources
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
s, err := data.OpenAPISchema()
|
s, err := fakeSchema.OpenAPISchema()
|
||||||
Expect(err).To(BeNil())
|
Expect(err).To(BeNil())
|
||||||
resources, err = openapi.NewOpenAPIData(s)
|
resources, err = openapi.NewOpenAPIData(s)
|
||||||
Expect(err).To(BeNil())
|
Expect(err).To(BeNil())
|
||||||
@ -136,7 +141,7 @@ var _ = Describe("Reading apps/v1beta1/Deployment from openAPIData", func() {
|
|||||||
var _ = Describe("Reading authorization.k8s.io/v1/SubjectAccessReview from openAPIData", func() {
|
var _ = Describe("Reading authorization.k8s.io/v1/SubjectAccessReview from openAPIData", func() {
|
||||||
var resources openapi.Resources
|
var resources openapi.Resources
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
s, err := data.OpenAPISchema()
|
s, err := fakeSchema.OpenAPISchema()
|
||||||
Expect(err).To(BeNil())
|
Expect(err).To(BeNil())
|
||||||
resources, err = openapi.NewOpenAPIData(s)
|
resources, err = openapi.NewOpenAPIData(s)
|
||||||
Expect(err).To(BeNil())
|
Expect(err).To(BeNil())
|
||||||
|
32
pkg/kubectl/cmd/util/openapi/testing/BUILD
Normal file
32
pkg/kubectl/cmd/util/openapi/testing/BUILD
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package(default_visibility = ["//visibility:public"])
|
||||||
|
|
||||||
|
licenses(["notice"])
|
||||||
|
|
||||||
|
load(
|
||||||
|
"@io_bazel_rules_go//go:def.bzl",
|
||||||
|
"go_library",
|
||||||
|
)
|
||||||
|
|
||||||
|
go_library(
|
||||||
|
name = "go_default_library",
|
||||||
|
srcs = ["openapi.go"],
|
||||||
|
tags = ["automanaged"],
|
||||||
|
deps = [
|
||||||
|
"//vendor/github.com/googleapis/gnostic/OpenAPIv2:go_default_library",
|
||||||
|
"//vendor/github.com/googleapis/gnostic/compiler:go_default_library",
|
||||||
|
"//vendor/gopkg.in/yaml.v2:go_default_library",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
filegroup(
|
||||||
|
name = "package-srcs",
|
||||||
|
srcs = glob(["**"]),
|
||||||
|
tags = ["automanaged"],
|
||||||
|
visibility = ["//visibility:private"],
|
||||||
|
)
|
||||||
|
|
||||||
|
filegroup(
|
||||||
|
name = "all-srcs",
|
||||||
|
srcs = [":package-srcs"],
|
||||||
|
tags = ["automanaged"],
|
||||||
|
)
|
89
pkg/kubectl/cmd/util/openapi/testing/openapi.go
Normal file
89
pkg/kubectl/cmd/util/openapi/testing/openapi.go
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2017 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 testing
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
yaml "gopkg.in/yaml.v2"
|
||||||
|
|
||||||
|
"github.com/googleapis/gnostic/OpenAPIv2"
|
||||||
|
"github.com/googleapis/gnostic/compiler"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Fake opens and returns a openapi swagger from a file Path. It will
|
||||||
|
// parse only once and then return the same copy everytime.
|
||||||
|
type Fake struct {
|
||||||
|
Path string
|
||||||
|
|
||||||
|
once sync.Once
|
||||||
|
document *openapi_v2.Document
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
// OpenAPISchema returns the openapi document and a potential error.
|
||||||
|
func (f *Fake) OpenAPISchema() (*openapi_v2.Document, error) {
|
||||||
|
f.once.Do(func() {
|
||||||
|
_, err := os.Stat(f.Path)
|
||||||
|
if err != nil {
|
||||||
|
f.err = err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
spec, err := ioutil.ReadFile(f.Path)
|
||||||
|
if err != nil {
|
||||||
|
f.err = err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var info yaml.MapSlice
|
||||||
|
err = yaml.Unmarshal(spec, &info)
|
||||||
|
if err != nil {
|
||||||
|
f.err = err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
f.document, f.err = openapi_v2.NewDocument(info, compiler.NewContext("$root", nil))
|
||||||
|
})
|
||||||
|
return f.document, f.err
|
||||||
|
}
|
||||||
|
|
||||||
|
// FakeClient implements a dummy OpenAPISchemaInterface that uses the
|
||||||
|
// fake OpenAPI schema given as a parameter, and count the number of
|
||||||
|
// call to the function.
|
||||||
|
type FakeClient struct {
|
||||||
|
Calls int
|
||||||
|
Err error
|
||||||
|
|
||||||
|
fake *Fake
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewFakeClient creates a new FakeClient from the given Fake.
|
||||||
|
func NewFakeClient(f *Fake) *FakeClient {
|
||||||
|
return &FakeClient{fake: f}
|
||||||
|
}
|
||||||
|
|
||||||
|
// OpenAPISchema returns a OpenAPI Document as returned by the fake, but
|
||||||
|
// it also counts the number of calls.
|
||||||
|
func (f *FakeClient) OpenAPISchema() (*openapi_v2.Document, error) {
|
||||||
|
f.Calls = f.Calls + 1
|
||||||
|
|
||||||
|
if f.Err != nil {
|
||||||
|
return nil, f.Err
|
||||||
|
}
|
||||||
|
|
||||||
|
return f.fake.OpenAPISchema()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user