crdfinder_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. Copyright 2018 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 util_test
  14. import (
  15. "errors"
  16. "testing"
  17. "k8s.io/apimachinery/pkg/runtime/schema"
  18. "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  19. )
  20. func TestCacheCRDFinder(t *testing.T) {
  21. called := 0
  22. getter := func() ([]schema.GroupKind, error) {
  23. called += 1
  24. return nil, nil
  25. }
  26. finder := util.NewCRDFinder(getter)
  27. if called != 0 {
  28. t.Fatalf("Creating the finder shouldn't call the getter, has called = %v", called)
  29. }
  30. _, err := finder.HasCRD(schema.GroupKind{Group: "", Kind: "Pod"})
  31. if err != nil {
  32. t.Fatalf("Failed to call HasCRD: %v", err)
  33. }
  34. if called != 1 {
  35. t.Fatalf("First call should call the getter, has called = %v", called)
  36. }
  37. _, err = finder.HasCRD(schema.GroupKind{Group: "", Kind: "Pod"})
  38. if err != nil {
  39. t.Fatalf("Failed to call HasCRD: %v", err)
  40. }
  41. if called != 1 {
  42. t.Fatalf("Second call should NOT call the getter, has called = %v", called)
  43. }
  44. }
  45. func TestCRDFinderErrors(t *testing.T) {
  46. getter := func() ([]schema.GroupKind, error) {
  47. return nil, errors.New("not working")
  48. }
  49. finder := util.NewCRDFinder(getter)
  50. found, err := finder.HasCRD(schema.GroupKind{Group: "", Kind: "Pod"})
  51. if found == true {
  52. t.Fatalf("Found the CRD with non-working getter function")
  53. }
  54. if err == nil {
  55. t.Fatalf("Error in getter should be reported")
  56. }
  57. }
  58. func TestCRDFinder(t *testing.T) {
  59. getter := func() ([]schema.GroupKind, error) {
  60. return []schema.GroupKind{
  61. {
  62. Group: "crd.com",
  63. Kind: "MyCRD",
  64. },
  65. {
  66. Group: "crd.com",
  67. Kind: "MyNewCRD",
  68. },
  69. }, nil
  70. }
  71. finder := util.NewCRDFinder(getter)
  72. if found, _ := finder.HasCRD(schema.GroupKind{Group: "crd.com", Kind: "MyCRD"}); !found {
  73. t.Fatalf("Failed to find CRD MyCRD")
  74. }
  75. if found, _ := finder.HasCRD(schema.GroupKind{Group: "crd.com", Kind: "Random"}); found {
  76. t.Fatalf("Found crd Random that doesn't exist")
  77. }
  78. }