sigs.k8s.io/kubebuilder/v3@v3.14.0/test/e2e/alphagenerate/generate_test.go (about) 1 /* 2 Copyright 2023 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 alphagenerate 18 19 import ( 20 "fmt" 21 "io" 22 "os" 23 "path/filepath" 24 25 pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util" 26 27 . "github.com/onsi/ginkgo/v2" 28 . "github.com/onsi/gomega" 29 30 "sigs.k8s.io/kubebuilder/v3/test/e2e/utils" 31 ) 32 33 var _ = Describe("kubebuilder", func() { 34 Context("alpha generate ", func() { 35 var ( 36 kbc *utils.TestContext 37 ) 38 39 BeforeEach(func() { 40 var err error 41 kbc, err = utils.NewTestContext(pluginutil.KubebuilderBinName, "GO111MODULE=on") 42 Expect(err).NotTo(HaveOccurred()) 43 Expect(kbc.Prepare()).To(Succeed()) 44 }) 45 46 AfterEach(func() { 47 kbc.Destroy() 48 }) 49 50 It("should regenerate the project with success", func() { 51 ReGenerateProject(kbc) 52 }) 53 54 }) 55 }) 56 57 // ReGenerateProject implements a project that is regenerated by kubebuilder. 58 func ReGenerateProject(kbc *utils.TestContext) { 59 var err error 60 61 By("initializing a project") 62 err = kbc.Init( 63 "--plugins", "go/v4", 64 "--project-version", "3", 65 "--domain", kbc.Domain, 66 ) 67 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 68 69 By("regenerating the project") 70 err = kbc.Regenerate( 71 "--input-dir", kbc.Dir, 72 "--output-dir", filepath.Join(kbc.Dir, "testdir"), 73 ) 74 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 75 76 By("checking if the project file was generated with the expected layout") 77 var layout = `layout: 78 - go.kubebuilder.io/v4 79 ` 80 fileContainsExpr, err := pluginutil.HasFileContentWith( 81 filepath.Join(kbc.Dir, "testdir", "PROJECT"), layout) 82 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 83 ExpectWithOffset(1, fileContainsExpr).To(BeTrue()) 84 85 By("checking if the project file was generated with the expected domain") 86 var domain = fmt.Sprintf("domain: %s", kbc.Domain) 87 fileContainsExpr, err = pluginutil.HasFileContentWith( 88 filepath.Join(kbc.Dir, "testdir", "PROJECT"), domain) 89 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 90 ExpectWithOffset(1, fileContainsExpr).To(BeTrue()) 91 92 By("checking if the project file was generated with the expected version") 93 var version = `version: "3"` 94 fileContainsExpr, err = pluginutil.HasFileContentWith( 95 filepath.Join(kbc.Dir, "testdir", "PROJECT"), version) 96 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 97 ExpectWithOffset(1, fileContainsExpr).To(BeTrue()) 98 99 By("editing a project with multigroup=true") 100 err = kbc.Edit( 101 "--multigroup=true", 102 ) 103 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 104 105 By("create APIs with resource and controller") 106 err = kbc.CreateAPI( 107 "--group", "crew", 108 "--version", "v1", 109 "--kind", "Captain", 110 "--namespaced", 111 "--resource", 112 "--controller", 113 ) 114 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 115 116 By("create Webhooks with conversion and validating webhook") 117 err = kbc.CreateWebhook( 118 "--group", "crew", 119 "--version", "v1", 120 "--kind", "Captain", 121 "--programmatic-validation", 122 "--conversion", 123 ) 124 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 125 126 By("create APIs with deploy-image plugin") 127 err = kbc.CreateAPI( 128 "--group", "crew", 129 "--version", "v1", 130 "--kind", "Memcached", 131 "--image=memcached:1.6.15-alpine", 132 "--image-container-command=memcached,-m=64,modern,-v", 133 "--image-container-port=11211", 134 "--run-as-user=1001", 135 "--plugins=\"deploy-image/v1-alpha\"", 136 ) 137 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 138 139 By("Enable grafana plugin to an existing project") 140 err = kbc.Edit( 141 "--plugins", "grafana.kubebuilder.io/v1-alpha", 142 ) 143 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 144 145 By("Edit the grafana config file") 146 grafanaConfig, err := os.OpenFile(filepath.Join(kbc.Dir, "grafana/custom-metrics/config.yaml"), 147 os.O_APPEND|os.O_WRONLY, 0644) 148 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 149 newLine := "test_new_line" 150 _, err = io.WriteString(grafanaConfig, newLine) 151 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 152 err = grafanaConfig.Close() 153 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 154 155 By("regenerating the project at another output directory") 156 err = kbc.Regenerate( 157 "--input-dir", kbc.Dir, 158 "--output-dir", filepath.Join(kbc.Dir, "testdir2"), 159 ) 160 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 161 162 By("checking if the project file was generated with the expected multigroup flag") 163 var multiGroup = `multigroup: true` 164 fileContainsExpr, err = pluginutil.HasFileContentWith( 165 filepath.Join(kbc.Dir, "testdir2", "PROJECT"), multiGroup) 166 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 167 ExpectWithOffset(1, fileContainsExpr).To(BeTrue()) 168 169 By("checking if the project file was generated with the expected group") 170 var APIGroup = "group: crew" 171 fileContainsExpr, err = pluginutil.HasFileContentWith( 172 filepath.Join(kbc.Dir, "testdir2", "PROJECT"), APIGroup) 173 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 174 ExpectWithOffset(1, fileContainsExpr).To(BeTrue()) 175 176 By("checking if the project file was generated with the expected kind") 177 var APIKind = "kind: Captain" 178 fileContainsExpr, err = pluginutil.HasFileContentWith( 179 filepath.Join(kbc.Dir, "testdir2", "PROJECT"), APIKind) 180 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 181 ExpectWithOffset(1, fileContainsExpr).To(BeTrue()) 182 183 By("checking if the project file was generated with the expected version") 184 var APIVersion = "version: v1" 185 fileContainsExpr, err = pluginutil.HasFileContentWith( 186 filepath.Join(kbc.Dir, "testdir2", "PROJECT"), APIVersion) 187 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 188 ExpectWithOffset(1, fileContainsExpr).To(BeTrue()) 189 190 By("checking if the project file was generated with the expected namespaced") 191 var namespaced = "namespaced: true" 192 fileContainsExpr, err = pluginutil.HasFileContentWith( 193 filepath.Join(kbc.Dir, "testdir2", "PROJECT"), namespaced) 194 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 195 ExpectWithOffset(1, fileContainsExpr).To(BeTrue()) 196 197 By("checking if the project file was generated with the expected controller") 198 var controller = "controller: true" 199 fileContainsExpr, err = pluginutil.HasFileContentWith( 200 filepath.Join(kbc.Dir, "testdir2", "PROJECT"), controller) 201 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 202 ExpectWithOffset(1, fileContainsExpr).To(BeTrue()) 203 204 By("checking if the project file was generated with the expected webhook") 205 var webhook = `webhooks: 206 conversion: true 207 validation: true` 208 fileContainsExpr, err = pluginutil.HasFileContentWith( 209 filepath.Join(kbc.Dir, "testdir2", "PROJECT"), webhook) 210 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 211 ExpectWithOffset(1, fileContainsExpr).To(BeTrue()) 212 213 By("checking if the project file was generated with the expected deploy-image plugin fields") 214 var deployImagePlugin = "deploy-image.go.kubebuilder.io/v1-alpha" 215 fileContainsExpr, err = pluginutil.HasFileContentWith( 216 filepath.Join(kbc.Dir, "testdir2", "PROJECT"), deployImagePlugin) 217 Expect(err).NotTo(HaveOccurred()) 218 Expect(fileContainsExpr).To(BeTrue()) 219 var deployImagePluginFields = `kind: Memcached 220 options: 221 containerCommand: memcached,-m=64,modern,-v 222 containerPort: "11211" 223 image: memcached:1.6.15-alpine 224 runAsUser: "1001"` 225 fileContainsExpr, err = pluginutil.HasFileContentWith( 226 filepath.Join(kbc.Dir, "testdir2", "PROJECT"), deployImagePluginFields) 227 Expect(err).NotTo(HaveOccurred()) 228 Expect(fileContainsExpr).To(BeTrue()) 229 230 By("checking if the project file was generated with the expected grafana plugin fields") 231 var grafanaPlugin = "grafana.kubebuilder.io/v1-alpha" 232 fileContainsExpr, err = pluginutil.HasFileContentWith( 233 filepath.Join(kbc.Dir, "testdir2", "PROJECT"), grafanaPlugin) 234 ExpectWithOffset(1, err).NotTo(HaveOccurred()) 235 ExpectWithOffset(1, fileContainsExpr).To(BeTrue()) 236 237 By("checking if the generated grafana config file has the same content as the old one") 238 grafanaConfigPath := filepath.Join(kbc.Dir, "grafana/custom-metrics/config.yaml") 239 generatedGrafanaConfigPath := filepath.Join(kbc.Dir, "testdir2", "grafana/custom-metrics/config.yaml") 240 Expect(grafanaConfigPath).Should(BeARegularFile()) 241 Expect(generatedGrafanaConfigPath).Should(BeARegularFile()) 242 bytesBefore, err := os.ReadFile(grafanaConfigPath) 243 Expect(err).NotTo(HaveOccurred()) 244 bytesAfter, err := os.ReadFile(generatedGrafanaConfigPath) 245 Expect(err).NotTo(HaveOccurred()) 246 Expect(bytesBefore).Should(Equal(bytesAfter)) 247 }