sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v4/scaffolds/internal/templates/api/webhook_test_template.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 api 18 19 import ( 20 "fmt" 21 "path/filepath" 22 "strings" 23 24 log "github.com/sirupsen/logrus" 25 26 "sigs.k8s.io/kubebuilder/v3/pkg/machinery" 27 ) 28 29 var _ machinery.Template = &WebhookTest{} 30 31 // WebhookTest scaffolds the file that sets up the webhook unit tests 32 type WebhookTest struct { // nolint:maligned 33 machinery.TemplateMixin 34 machinery.MultiGroupMixin 35 machinery.BoilerplateMixin 36 machinery.ResourceMixin 37 38 Force bool 39 } 40 41 // SetTemplateDefaults implements file.Template 42 func (f *WebhookTest) SetTemplateDefaults() error { 43 if f.Path == "" { 44 if f.MultiGroup && f.Resource.Group != "" { 45 f.Path = filepath.Join("api", "%[group]", "%[version]", "%[kind]_webhook_test.go") 46 } else { 47 f.Path = filepath.Join("api", "%[version]", "%[kind]_webhook_test.go") 48 } 49 } 50 f.Path = f.Resource.Replacer().Replace(f.Path) 51 log.Println(f.Path) 52 53 webhookTestTemplate := webhookTestTemplate 54 templates := make([]string, 0) 55 if f.Resource.HasDefaultingWebhook() { 56 templates = append(templates, defaultWebhookTestTemplate) 57 } 58 if f.Resource.HasValidationWebhook() { 59 templates = append(templates, validateWebhookTestTemplate) 60 } 61 if f.Resource.HasConversionWebhook() { 62 templates = append(templates, conversionWebhookTestTemplate) 63 } 64 f.TemplateBody = fmt.Sprintf(webhookTestTemplate, strings.Join(templates, "\n")) 65 66 if f.Force { 67 f.IfExistsAction = machinery.OverwriteFile 68 } 69 70 return nil 71 } 72 73 const webhookTestTemplate = `{{ .Boilerplate }} 74 75 package {{ .Resource.Version }} 76 77 import ( 78 . "github.com/onsi/ginkgo/v2" 79 ) 80 81 var _ = Describe("{{ .Resource.Kind }} Webhook", func() { 82 %s 83 }) 84 ` 85 86 const conversionWebhookTestTemplate = ` 87 Context("When creating {{ .Resource.Kind }} under Conversion Webhook", func() { 88 It("Should get the converted version of {{ .Resource.Kind }}" , func() { 89 90 // TODO(user): Add your logic here 91 92 }) 93 }) 94 ` 95 96 const validateWebhookTestTemplate = ` 97 Context("When creating {{ .Resource.Kind }} under Validating Webhook", func() { 98 It("Should deny if a required field is empty", func() { 99 100 // TODO(user): Add your logic here 101 102 }) 103 104 It("Should admit if all required fields are provided", func() { 105 106 // TODO(user): Add your logic here 107 108 }) 109 }) 110 ` 111 112 const defaultWebhookTestTemplate = ` 113 Context("When creating {{ .Resource.Kind }} under Defaulting Webhook", func() { 114 It("Should fill in the default value if a required field is empty", func() { 115 116 // TODO(user): Add your logic here 117 118 }) 119 }) 120 `