k3s/pkg/kubectl/cmd/util/openapi/openapi_getter_test.go

78 lines
2.1 KiB
Go
Raw Normal View History

2017-04-14 01:01:38 +00:00
/*
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 openapi_test
2017-04-14 01:01:38 +00:00
import (
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
tst "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/testing"
2017-04-14 01:01:38 +00:00
)
var _ = Describe("Getting the Resources", func() {
2017-08-06 16:35:17 +00:00
var client *tst.FakeClient
var expectedData openapi.Resources
var instance openapi.Getter
2017-04-14 01:01:38 +00:00
BeforeEach(func() {
2017-08-06 16:35:17 +00:00
client = tst.NewFakeClient(&fakeSchema)
d, err := fakeSchema.OpenAPISchema()
2017-04-14 01:01:38 +00:00
Expect(err).To(BeNil())
expectedData, err = openapi.NewOpenAPIData(d)
2017-04-14 01:01:38 +00:00
Expect(err).To(BeNil())
instance = openapi.NewOpenAPIGetter(client)
2017-04-14 01:01:38 +00:00
})
Context("when the server returns a successful result", func() {
It("should return the same data for multiple calls", func() {
2017-08-06 16:35:17 +00:00
Expect(client.Calls).To(Equal(0))
2017-04-14 01:01:38 +00:00
result, err := instance.Get()
Expect(err).To(BeNil())
Expect(result).To(Equal(expectedData))
2017-08-06 16:35:17 +00:00
Expect(client.Calls).To(Equal(1))
2017-04-14 01:01:38 +00:00
result, err = instance.Get()
Expect(err).To(BeNil())
Expect(result).To(Equal(expectedData))
2017-04-14 01:01:38 +00:00
// No additional client calls expected
2017-08-06 16:35:17 +00:00
Expect(client.Calls).To(Equal(1))
2017-04-14 01:01:38 +00:00
})
})
Context("when the server returns an unsuccessful result", func() {
It("should return the same instance for multiple calls.", func() {
2017-08-06 16:35:17 +00:00
Expect(client.Calls).To(Equal(0))
2017-04-14 01:01:38 +00:00
2017-08-06 16:35:17 +00:00
client.Err = fmt.Errorf("expected error")
2017-04-14 01:01:38 +00:00
_, err := instance.Get()
2017-08-06 16:35:17 +00:00
Expect(err).To(Equal(client.Err))
Expect(client.Calls).To(Equal(1))
2017-04-14 01:01:38 +00:00
_, err = instance.Get()
2017-08-06 16:35:17 +00:00
Expect(err).To(Equal(client.Err))
2017-04-14 01:01:38 +00:00
// No additional client calls expected
2017-08-06 16:35:17 +00:00
Expect(client.Calls).To(Equal(1))
2017-04-14 01:01:38 +00:00
})
})
})