sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/options_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 golang 18 19 import ( 20 "path" 21 22 . "github.com/onsi/ginkgo/v2" 23 . "github.com/onsi/gomega" 24 25 "sigs.k8s.io/kubebuilder/v3/pkg/config" 26 cfgv2 "sigs.k8s.io/kubebuilder/v3/pkg/config/v2" 27 cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3" 28 "sigs.k8s.io/kubebuilder/v3/pkg/model/resource" 29 ) 30 31 var _ = Describe("Options", func() { 32 Context("UpdateResource", func() { 33 const ( 34 group = "crew" 35 domain = "test.io" 36 version = "v1" 37 kind = "FirstMate" 38 ) 39 var ( 40 gvk = resource.GVK{ 41 Group: group, 42 Domain: domain, 43 Version: version, 44 Kind: kind, 45 } 46 47 cfg config.Config 48 ) 49 50 BeforeEach(func() { 51 cfg = cfgv3.New() 52 _ = cfg.SetRepository("test") 53 }) 54 55 DescribeTable("should succeed", 56 func(options Options) { 57 for _, multiGroup := range []bool{false, true} { 58 if multiGroup { 59 Expect(cfg.SetMultiGroup()).To(Succeed()) 60 } else { 61 Expect(cfg.ClearMultiGroup()).To(Succeed()) 62 } 63 64 res := resource.Resource{ 65 GVK: gvk, 66 Plural: "firstmates", 67 API: &resource.API{}, 68 Webhooks: &resource.Webhooks{}, 69 } 70 71 options.UpdateResource(&res, cfg) 72 Expect(res.Validate()).To(Succeed()) 73 Expect(res.GVK.IsEqualTo(gvk)).To(BeTrue()) 74 if options.Plural != "" { 75 Expect(res.Plural).To(Equal(options.Plural)) 76 } 77 if options.DoAPI || options.DoDefaulting || options.DoValidation || options.DoConversion { 78 if multiGroup { 79 Expect(res.Path).To(Equal( 80 path.Join(cfg.GetRepository(), "api", gvk.Group, gvk.Version))) 81 } else { 82 Expect(res.Path).To(Equal(path.Join(cfg.GetRepository(), "api", gvk.Version))) 83 } 84 } else { 85 // Core-resources have a path despite not having an API/Webhook but they are not tested here 86 Expect(res.Path).To(Equal("")) 87 } 88 Expect(res.API).NotTo(BeNil()) 89 if options.DoAPI { 90 Expect(res.API.CRDVersion).To(Equal(options.CRDVersion)) 91 Expect(res.API.Namespaced).To(Equal(options.Namespaced)) 92 Expect(res.API.IsEmpty()).To(BeFalse()) 93 } else { 94 Expect(res.API.IsEmpty()).To(BeTrue()) 95 } 96 Expect(res.Controller).To(Equal(options.DoController)) 97 Expect(res.Webhooks).NotTo(BeNil()) 98 if options.DoDefaulting || options.DoValidation || options.DoConversion { 99 Expect(res.Webhooks.WebhookVersion).To(Equal(options.WebhookVersion)) 100 Expect(res.Webhooks.Defaulting).To(Equal(options.DoDefaulting)) 101 Expect(res.Webhooks.Validation).To(Equal(options.DoValidation)) 102 Expect(res.Webhooks.Conversion).To(Equal(options.DoConversion)) 103 Expect(res.Webhooks.IsEmpty()).To(BeFalse()) 104 } else { 105 Expect(res.Webhooks.IsEmpty()).To(BeTrue()) 106 } 107 Expect(res.QualifiedGroup()).To(Equal(gvk.Group + "." + gvk.Domain)) 108 Expect(res.PackageName()).To(Equal(gvk.Group)) 109 Expect(res.ImportAlias()).To(Equal(gvk.Group + gvk.Version)) 110 } 111 }, 112 Entry("when updating nothing", Options{}), 113 Entry("when updating the plural", Options{Plural: "mates"}), 114 Entry("when updating the API", Options{DoAPI: true, CRDVersion: "v1", Namespaced: true}), 115 Entry("when updating the Controller", Options{DoController: true}), 116 Entry("when updating Webhooks", 117 Options{WebhookVersion: "v1", DoDefaulting: true, DoValidation: true, DoConversion: true}), 118 ) 119 120 DescribeTable("should use core apis", 121 func(group, qualified string) { 122 options := Options{} 123 for _, multiGroup := range []bool{false, true} { 124 if multiGroup { 125 Expect(cfg.SetMultiGroup()).To(Succeed()) 126 } else { 127 Expect(cfg.ClearMultiGroup()).To(Succeed()) 128 } 129 130 res := resource.Resource{ 131 GVK: resource.GVK{ 132 Group: group, 133 Domain: domain, 134 Version: version, 135 Kind: kind, 136 }, 137 Plural: "firstmates", 138 API: &resource.API{}, 139 Webhooks: &resource.Webhooks{}, 140 } 141 142 options.UpdateResource(&res, cfg) 143 Expect(res.Validate()).To(Succeed()) 144 145 Expect(res.Path).To(Equal(path.Join("k8s.io", "api", group, version))) 146 Expect(res.HasAPI()).To(BeFalse()) 147 Expect(res.QualifiedGroup()).To(Equal(qualified)) 148 } 149 }, 150 Entry("for `apps`", "apps", "apps"), 151 Entry("for `authentication`", "authentication", "authentication.k8s.io"), 152 ) 153 154 DescribeTable("should use core apis with project version 2", 155 // This needs a separate test because project version 2 didn't store API and therefore 156 // the `HasAPI` method of the resource obtained with `GetResource` will always return false. 157 // Instead, the existence of a resource in the list means the API was scaffolded. 158 func(group, qualified string) { 159 cfg = cfgv2.New() 160 _ = cfg.SetRepository("test") 161 162 options := Options{} 163 for _, multiGroup := range []bool{false, true} { 164 if multiGroup { 165 Expect(cfg.SetMultiGroup()).To(Succeed()) 166 } else { 167 Expect(cfg.ClearMultiGroup()).To(Succeed()) 168 } 169 170 res := resource.Resource{ 171 GVK: resource.GVK{ 172 Group: group, 173 Domain: domain, 174 Version: version, 175 Kind: kind, 176 }, 177 Plural: "firstmates", 178 API: &resource.API{}, 179 Webhooks: &resource.Webhooks{}, 180 } 181 182 options.UpdateResource(&res, cfg) 183 Expect(res.Validate()).To(Succeed()) 184 185 Expect(res.Path).To(Equal(path.Join("k8s.io", "api", group, version))) 186 Expect(res.HasAPI()).To(BeFalse()) 187 Expect(res.QualifiedGroup()).To(Equal(qualified)) 188 } 189 }, 190 Entry("for `apps`", "apps", "apps"), 191 Entry("for `authentication`", "authentication", "authentication.k8s.io"), 192 ) 193 }) 194 })