openapi_getter_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package openapi_test
  14. import (
  15. "fmt"
  16. openapi_v2 "github.com/googleapis/gnostic/OpenAPIv2"
  17. . "github.com/onsi/ginkgo"
  18. . "github.com/onsi/gomega"
  19. "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
  20. )
  21. // FakeCounter returns a "null" document and the specified error. It
  22. // also counts how many times the OpenAPISchema method has been called.
  23. type FakeCounter struct {
  24. Calls int
  25. Err error
  26. }
  27. func (f *FakeCounter) OpenAPISchema() (*openapi_v2.Document, error) {
  28. f.Calls = f.Calls + 1
  29. return nil, f.Err
  30. }
  31. var _ = Describe("Getting the Resources", func() {
  32. var client FakeCounter
  33. var instance openapi.Getter
  34. var expectedData openapi.Resources
  35. BeforeEach(func() {
  36. client = FakeCounter{}
  37. instance = openapi.NewOpenAPIGetter(&client)
  38. var err error
  39. expectedData, err = openapi.NewOpenAPIData(nil)
  40. Expect(err).To(BeNil())
  41. })
  42. Context("when the server returns a successful result", func() {
  43. It("should return the same data for multiple calls", func() {
  44. Expect(client.Calls).To(Equal(0))
  45. result, err := instance.Get()
  46. Expect(err).To(BeNil())
  47. Expect(result).To(Equal(expectedData))
  48. Expect(client.Calls).To(Equal(1))
  49. result, err = instance.Get()
  50. Expect(err).To(BeNil())
  51. Expect(result).To(Equal(expectedData))
  52. // No additional client calls expected
  53. Expect(client.Calls).To(Equal(1))
  54. })
  55. })
  56. Context("when the server returns an unsuccessful result", func() {
  57. It("should return the same instance for multiple calls.", func() {
  58. Expect(client.Calls).To(Equal(0))
  59. client.Err = fmt.Errorf("expected error")
  60. _, err := instance.Get()
  61. Expect(err).To(Equal(client.Err))
  62. Expect(client.Calls).To(Equal(1))
  63. _, err = instance.Get()
  64. Expect(err).To(Equal(client.Err))
  65. // No additional client calls expected
  66. Expect(client.Calls).To(Equal(1))
  67. })
  68. })
  69. })