sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/model/resource/api_test.go (about) 1 /* 2 Copyright 2022 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package resource 18 19 import ( 20 . "github.com/onsi/ginkgo/v2" 21 . "github.com/onsi/gomega" 22 ) 23 24 //nolint:dupl 25 var _ = Describe("API", func() { 26 Context("Validate", func() { 27 It("should succeed for a valid API", func() { 28 Expect(API{CRDVersion: v1}.Validate()).To(Succeed()) 29 }) 30 31 DescribeTable("should fail for invalid APIs", 32 func(api API) { Expect(api.Validate()).NotTo(Succeed()) }, 33 // Ensure that the rest of the fields are valid to check each part 34 Entry("empty CRD version", API{}), 35 Entry("invalid CRD version", API{CRDVersion: "1"}), 36 ) 37 }) 38 39 Context("Update", func() { 40 var api, other API 41 42 It("should do nothing if provided a nil pointer", func() { 43 api = API{} 44 Expect(api.Update(nil)).To(Succeed()) 45 Expect(api.CRDVersion).To(Equal("")) 46 Expect(api.Namespaced).To(BeFalse()) 47 48 api = API{ 49 CRDVersion: v1, 50 Namespaced: true, 51 } 52 Expect(api.Update(nil)).To(Succeed()) 53 Expect(api.CRDVersion).To(Equal(v1)) 54 Expect(api.Namespaced).To(BeTrue()) 55 }) 56 57 Context("CRD version", func() { 58 It("should modify the CRD version if provided and not previously set", func() { 59 api = API{} 60 other = API{CRDVersion: v1} 61 Expect(api.Update(&other)).To(Succeed()) 62 Expect(api.CRDVersion).To(Equal(v1)) 63 }) 64 65 It("should keep the CRD version if not provided", func() { 66 api = API{CRDVersion: v1} 67 other = API{} 68 Expect(api.Update(&other)).To(Succeed()) 69 Expect(api.CRDVersion).To(Equal(v1)) 70 }) 71 72 It("should keep the CRD version if provided the same as previously set", func() { 73 api = API{CRDVersion: v1} 74 other = API{CRDVersion: v1} 75 Expect(api.Update(&other)).To(Succeed()) 76 Expect(api.CRDVersion).To(Equal(v1)) 77 }) 78 79 It("should fail if previously set and provided CRD versions do not match", func() { 80 api = API{CRDVersion: v1} 81 other = API{CRDVersion: "v1beta1"} 82 Expect(api.Update(&other)).NotTo(Succeed()) 83 }) 84 }) 85 86 Context("Namespaced", func() { 87 It("should set the namespace scope if provided and not previously set", func() { 88 api = API{} 89 other = API{Namespaced: true} 90 Expect(api.Update(&other)).To(Succeed()) 91 Expect(api.Namespaced).To(BeTrue()) 92 }) 93 94 It("should keep the namespace scope if previously set", func() { 95 api = API{Namespaced: true} 96 97 By("not providing it") 98 other = API{} 99 Expect(api.Update(&other)).To(Succeed()) 100 Expect(api.Namespaced).To(BeTrue()) 101 102 By("providing it") 103 other = API{Namespaced: true} 104 Expect(api.Update(&other)).To(Succeed()) 105 Expect(api.Namespaced).To(BeTrue()) 106 }) 107 108 It("should not set the namespace scope if not provided and not previously set", func() { 109 api = API{} 110 other = API{} 111 Expect(api.Update(&other)).To(Succeed()) 112 Expect(api.Namespaced).To(BeFalse()) 113 }) 114 }) 115 }) 116 117 Context("IsEmpty", func() { 118 var ( 119 none = API{} 120 cluster = API{ 121 CRDVersion: v1, 122 } 123 namespaced = API{ 124 CRDVersion: v1, 125 Namespaced: true, 126 } 127 ) 128 129 It("should return true fo an empty object", func() { 130 Expect(none.IsEmpty()).To(BeTrue()) 131 }) 132 133 DescribeTable("should return false for non-empty objects", 134 func(api API) { Expect(api.IsEmpty()).To(BeFalse()) }, 135 Entry("cluster-scope", cluster), 136 Entry("namespace-scope", namespaced), 137 ) 138 }) 139 })