sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/config/v2/config_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 //go:deprecated This package has been deprecated 18 package v2 19 20 import ( 21 "testing" 22 23 . "github.com/onsi/ginkgo/v2" 24 . "github.com/onsi/gomega" 25 26 "sigs.k8s.io/kubebuilder/v3/pkg/model/resource" 27 ) 28 29 func TestConfigV2(t *testing.T) { 30 RegisterFailHandler(Fail) 31 RunSpecs(t, "Config V2 Suite") 32 } 33 34 var _ = Describe("cfg", func() { 35 const ( 36 domain = "my.domain" 37 repo = "myrepo" 38 39 otherDomain = "other.domain" 40 otherRepo = "otherrepo" 41 ) 42 43 var c cfg 44 45 BeforeEach(func() { 46 c = cfg{ 47 Version: Version, 48 Domain: domain, 49 Repository: repo, 50 } 51 }) 52 53 Context("Version", func() { 54 It("GetVersion should return version 2", func() { 55 Expect(c.GetVersion().Compare(Version)).To(Equal(0)) 56 }) 57 }) 58 59 Context("Domain", func() { 60 It("GetDomain should return the domain", func() { 61 Expect(c.GetDomain()).To(Equal(domain)) 62 }) 63 64 It("SetDomain should set the domain", func() { 65 Expect(c.SetDomain(otherDomain)).To(Succeed()) 66 Expect(c.Domain).To(Equal(otherDomain)) 67 }) 68 }) 69 70 Context("Repository", func() { 71 It("GetRepository should return the repository", func() { 72 Expect(c.GetRepository()).To(Equal(repo)) 73 }) 74 75 It("SetRepository should set the repository", func() { 76 Expect(c.SetRepository(otherRepo)).To(Succeed()) 77 Expect(c.Repository).To(Equal(otherRepo)) 78 }) 79 }) 80 81 Context("Project name", func() { 82 It("GetProjectName should return an empty name", func() { 83 Expect(c.GetProjectName()).To(Equal("")) 84 }) 85 86 It("SetProjectName should fail to set the name", func() { 87 Expect(c.SetProjectName("name")).NotTo(Succeed()) 88 }) 89 }) 90 91 Context("Plugin chain", func() { 92 It("GetPluginChain should return the only supported plugin", func() { 93 Expect(c.GetPluginChain()).To(Equal([]string{"go.kubebuilder.io/v2"})) 94 }) 95 96 It("SetPluginChain should fail to set the plugin chain", func() { 97 Expect(c.SetPluginChain([]string{})).NotTo(Succeed()) 98 }) 99 }) 100 101 Context("Multi group", func() { 102 It("IsMultiGroup should return false if not set", func() { 103 Expect(c.IsMultiGroup()).To(BeFalse()) 104 }) 105 106 It("IsMultiGroup should return true if set", func() { 107 c.MultiGroup = true 108 Expect(c.IsMultiGroup()).To(BeTrue()) 109 }) 110 111 It("SetMultiGroup should enable multi-group support", func() { 112 Expect(c.SetMultiGroup()).To(Succeed()) 113 Expect(c.MultiGroup).To(BeTrue()) 114 }) 115 116 It("ClearMultiGroup should disable multi-group support", func() { 117 c.MultiGroup = true 118 Expect(c.ClearMultiGroup()).To(Succeed()) 119 Expect(c.MultiGroup).To(BeFalse()) 120 }) 121 }) 122 123 Context("Component config", func() { 124 It("IsComponentConfig should return false", func() { 125 Expect(c.IsComponentConfig()).To(BeFalse()) 126 }) 127 128 It("SetComponentConfig should fail to enable component config support", func() { 129 Expect(c.SetComponentConfig()).NotTo(Succeed()) 130 }) 131 132 It("ClearComponentConfig should fail to disable component config support", func() { 133 Expect(c.ClearComponentConfig()).NotTo(Succeed()) 134 }) 135 }) 136 137 Context("Resources", func() { 138 res := resource.Resource{ 139 GVK: resource.GVK{ 140 Group: "group", 141 Version: "v1", 142 Kind: "Kind", 143 }, 144 } 145 146 DescribeTable("ResourcesLength should return the number of resources", 147 func(n int) { 148 for i := 0; i < n; i++ { 149 c.Gvks = append(c.Gvks, res.GVK) 150 } 151 Expect(c.ResourcesLength()).To(Equal(n)) 152 }, 153 Entry("for no resources", 0), 154 Entry("for one resource", 1), 155 Entry("for several resources", 3), 156 ) 157 158 It("HasResource should return false for a non-existent resource", func() { 159 Expect(c.HasResource(res.GVK)).To(BeFalse()) 160 }) 161 162 It("HasResource should return true for an existent resource", func() { 163 c.Gvks = append(c.Gvks, res.GVK) 164 Expect(c.HasResource(res.GVK)).To(BeTrue()) 165 }) 166 167 It("GetResource should fail for a non-existent resource", func() { 168 _, err := c.GetResource(res.GVK) 169 Expect(err).To(HaveOccurred()) 170 }) 171 172 It("GetResource should return an existent resource", func() { 173 c.Gvks = append(c.Gvks, res.GVK) 174 r, err := c.GetResource(res.GVK) 175 Expect(err).NotTo(HaveOccurred()) 176 Expect(r.GVK.IsEqualTo(res.GVK)).To(BeTrue()) 177 }) 178 179 It("GetResources should return a slice of the tracked resources", func() { 180 c.Gvks = append(c.Gvks, res.GVK, res.GVK, res.GVK) 181 resources, err := c.GetResources() 182 Expect(err).NotTo(HaveOccurred()) 183 Expect(resources).To(Equal([]resource.Resource{res, res, res})) 184 }) 185 186 It("AddResource should add the provided resource if non-existent", func() { 187 l := len(c.Gvks) 188 Expect(c.AddResource(res)).To(Succeed()) 189 Expect(len(c.Gvks)).To(Equal(l + 1)) 190 Expect(c.Gvks[0].IsEqualTo(res.GVK)).To(BeTrue()) 191 }) 192 193 It("AddResource should do nothing if the resource already exists", func() { 194 c.Gvks = append(c.Gvks, res.GVK) 195 l := len(c.Gvks) 196 Expect(c.AddResource(res)).To(Succeed()) 197 Expect(len(c.Gvks)).To(Equal(l)) 198 }) 199 200 It("UpdateResource should add the provided resource if non-existent", func() { 201 l := len(c.Gvks) 202 Expect(c.UpdateResource(res)).To(Succeed()) 203 Expect(len(c.Gvks)).To(Equal(l + 1)) 204 Expect(c.Gvks[0].IsEqualTo(res.GVK)).To(BeTrue()) 205 }) 206 207 It("UpdateResource should do nothing if the resource already exists", func() { 208 c.Gvks = append(c.Gvks, res.GVK) 209 l := len(c.Gvks) 210 Expect(c.UpdateResource(res)).To(Succeed()) 211 Expect(len(c.Gvks)).To(Equal(l)) 212 }) 213 214 It("HasGroup should return false with no tracked resources", func() { 215 Expect(c.HasGroup(res.Group)).To(BeFalse()) 216 }) 217 218 It("HasGroup should return true with tracked resources in the same group", func() { 219 c.Gvks = append(c.Gvks, res.GVK) 220 Expect(c.HasGroup(res.Group)).To(BeTrue()) 221 }) 222 223 It("HasGroup should return false with tracked resources in other group", func() { 224 c.Gvks = append(c.Gvks, res.GVK) 225 Expect(c.HasGroup("other-group")).To(BeFalse()) 226 }) 227 228 It("ListCRDVersions should return an empty list", func() { 229 Expect(c.ListCRDVersions()).To(BeEmpty()) 230 }) 231 232 It("ListWebhookVersions should return an empty list", func() { 233 Expect(c.ListWebhookVersions()).To(BeEmpty()) 234 }) 235 }) 236 237 Context("Plugins", func() { 238 It("DecodePluginConfig should fail", func() { 239 Expect(c.DecodePluginConfig("", nil)).NotTo(Succeed()) 240 }) 241 242 It("EncodePluginConfig should fail", func() { 243 Expect(c.EncodePluginConfig("", nil)).NotTo(Succeed()) 244 }) 245 }) 246 247 Context("Persistence", func() { 248 var ( 249 // BeforeEach is called after the entries are evaluated, and therefore, c is not available 250 c1 = cfg{ 251 Version: Version, 252 Domain: domain, 253 Repository: repo, 254 } 255 c2 = cfg{ 256 Version: Version, 257 Domain: otherDomain, 258 Repository: otherRepo, 259 MultiGroup: true, 260 Gvks: []resource.GVK{ 261 {Group: "group", Version: "v1", Kind: "Kind"}, 262 {Group: "group", Version: "v1", Kind: "Kind2"}, 263 {Group: "group", Version: "v1-beta", Kind: "Kind"}, 264 {Group: "group2", Version: "v1", Kind: "Kind"}, 265 }, 266 } 267 s1 = `domain: my.domain 268 repo: myrepo 269 version: "2" 270 ` 271 s2 = `domain: other.domain 272 multigroup: true 273 repo: otherrepo 274 resources: 275 - group: group 276 kind: Kind 277 version: v1 278 - group: group 279 kind: Kind2 280 version: v1 281 - group: group 282 kind: Kind 283 version: v1-beta 284 - group: group2 285 kind: Kind 286 version: v1 287 version: "2" 288 ` 289 ) 290 291 DescribeTable("MarshalYAML should succeed", 292 func(c cfg, content string) { 293 b, err := c.MarshalYAML() 294 Expect(err).NotTo(HaveOccurred()) 295 Expect(string(b)).To(Equal(content)) 296 }, 297 Entry("for a basic configuration", c1, s1), 298 Entry("for a full configuration", c2, s2), 299 ) 300 DescribeTable("UnmarshalYAML should succeed", 301 func(content string, c cfg) { 302 var unmarshalled cfg 303 Expect(unmarshalled.UnmarshalYAML([]byte(content))).To(Succeed()) 304 Expect(unmarshalled.Version.Compare(c.Version)).To(Equal(0)) 305 Expect(unmarshalled.Domain).To(Equal(c.Domain)) 306 Expect(unmarshalled.Repository).To(Equal(c.Repository)) 307 Expect(unmarshalled.MultiGroup).To(Equal(c.MultiGroup)) 308 Expect(unmarshalled.Gvks).To(Equal(c.Gvks)) 309 }, 310 Entry("basic", s1, c1), 311 Entry("full", s2, c2), 312 ) 313 314 DescribeTable("UnmarshalYAML should fail", 315 func(content string) { 316 var c cfg 317 Expect(c.UnmarshalYAML([]byte(content))).NotTo(Succeed()) 318 }, 319 Entry("for unknown fields", `field: 1 320 version: "2"`), 321 ) 322 }) 323 }) 324 325 var _ = Describe("New", func() { 326 It("should return a new config for project configuration 2", func() { 327 Expect(New().GetVersion().Compare(Version)).To(Equal(0)) 328 }) 329 })