sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v2/scaffolds/internal/templates/controllers/controller_suitetest.go (about) 1 /* 2 Copyright 2020 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 controllers 18 19 import ( 20 "fmt" 21 "path/filepath" 22 23 "sigs.k8s.io/kubebuilder/v3/pkg/machinery" 24 ) 25 26 var _ machinery.Template = &SuiteTest{} 27 var _ machinery.Inserter = &SuiteTest{} 28 29 // SuiteTest scaffolds the file that sets up the controller tests 30 // nolint:maligned 31 type SuiteTest struct { 32 machinery.TemplateMixin 33 machinery.MultiGroupMixin 34 machinery.BoilerplateMixin 35 machinery.ResourceMixin 36 37 // CRDDirectoryRelativePath define the Path for the CRD 38 CRDDirectoryRelativePath string 39 40 Force bool 41 } 42 43 // SetTemplateDefaults implements file.Template 44 func (f *SuiteTest) SetTemplateDefaults() error { 45 if f.Path == "" { 46 if f.MultiGroup { 47 f.Path = filepath.Join("controllers", "%[group]", "suite_test.go") 48 } else { 49 f.Path = filepath.Join("controllers", "suite_test.go") 50 } 51 } 52 f.Path = f.Resource.Replacer().Replace(f.Path) 53 54 f.TemplateBody = fmt.Sprintf(controllerSuiteTestTemplate, 55 machinery.NewMarkerFor(f.Path, importMarker), 56 machinery.NewMarkerFor(f.Path, addSchemeMarker), 57 ) 58 59 // If is multigroup the path needs to be ../../ since it has 60 // the group dir. 61 f.CRDDirectoryRelativePath = `".."` 62 if f.MultiGroup { 63 f.CRDDirectoryRelativePath = `"..", ".."` 64 } 65 66 if f.Force { 67 f.IfExistsAction = machinery.OverwriteFile 68 } 69 70 return nil 71 } 72 73 const ( 74 importMarker = "imports" 75 addSchemeMarker = "scheme" 76 ) 77 78 // GetMarkers implements file.Inserter 79 func (f *SuiteTest) GetMarkers() []machinery.Marker { 80 return []machinery.Marker{ 81 machinery.NewMarkerFor(f.Path, importMarker), 82 machinery.NewMarkerFor(f.Path, addSchemeMarker), 83 } 84 } 85 86 const ( 87 apiImportCodeFragment = `%s "%s" 88 ` 89 addschemeCodeFragment = `err = %s.AddToScheme(scheme.Scheme) 90 Expect(err).NotTo(HaveOccurred()) 91 92 ` 93 ) 94 95 // GetCodeFragments implements file.Inserter 96 func (f *SuiteTest) GetCodeFragments() machinery.CodeFragmentsMap { 97 fragments := make(machinery.CodeFragmentsMap, 2) 98 99 // Generate import code fragments 100 imports := make([]string, 0) 101 if f.Resource.Path != "" { 102 imports = append(imports, fmt.Sprintf(apiImportCodeFragment, f.Resource.ImportAlias(), f.Resource.Path)) 103 } 104 105 // Generate add scheme code fragments 106 addScheme := make([]string, 0) 107 if f.Resource.Path != "" { 108 addScheme = append(addScheme, fmt.Sprintf(addschemeCodeFragment, f.Resource.ImportAlias())) 109 } 110 111 // Only store code fragments in the map if the slices are non-empty 112 if len(imports) != 0 { 113 fragments[machinery.NewMarkerFor(f.Path, importMarker)] = imports 114 } 115 if len(addScheme) != 0 { 116 fragments[machinery.NewMarkerFor(f.Path, addSchemeMarker)] = addScheme 117 } 118 119 return fragments 120 } 121 122 const controllerSuiteTestTemplate = `{{ .Boilerplate }} 123 124 package controllers 125 126 import ( 127 "path/filepath" 128 "testing" 129 . "github.com/onsi/ginkgo" 130 . "github.com/onsi/gomega" 131 "k8s.io/client-go/kubernetes/scheme" 132 "k8s.io/client-go/rest" 133 "sigs.k8s.io/controller-runtime/pkg/client" 134 "sigs.k8s.io/controller-runtime/pkg/envtest" 135 "sigs.k8s.io/controller-runtime/pkg/envtest/printer" 136 logf "sigs.k8s.io/controller-runtime/pkg/log" 137 "sigs.k8s.io/controller-runtime/pkg/log/zap" 138 %s 139 ) 140 141 // These tests use Ginkgo (BDD-style Go testing framework). Refer to 142 // http://onsi.github.io/ginkgo/ to learn more about Ginkgo. 143 144 var cfg *rest.Config 145 var k8sClient client.Client 146 var testEnv *envtest.Environment 147 148 func TestAPIs(t *testing.T) { 149 RegisterFailHandler(Fail) 150 151 RunSpecsWithDefaultAndCustomReporters(t, 152 "Controller Suite", 153 []Reporter{printer.NewlineReporter{}}) 154 } 155 156 var _ = BeforeSuite(func(done Done) { 157 logf.SetLogger(zap.LoggerTo(GinkgoWriter, true)) 158 159 By("bootstrapping test environment") 160 testEnv = &envtest.Environment{ 161 CRDDirectoryPaths: []string{filepath.Join({{ .CRDDirectoryRelativePath }}, "config", "crd", "bases")}, 162 } 163 164 var err error 165 // cfg is defined in this file globally. 166 cfg, err = testEnv.Start() 167 Expect(err).ToNot(HaveOccurred()) 168 Expect(cfg).ToNot(BeNil()) 169 170 %s 171 172 k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) 173 Expect(err).ToNot(HaveOccurred()) 174 Expect(k8sClient).ToNot(BeNil()) 175 176 close(done) 177 }, 60) 178 179 var _ = AfterSuite(func() { 180 By("tearing down the test environment") 181 err := testEnv.Stop() 182 Expect(err).ToNot(HaveOccurred()) 183 }) 184 `