|
| 1 | +/* |
| 2 | +Copyright 2019 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 v2 |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "path/filepath" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/markbates/inflect" |
| 25 | + "sigs.k8s.io/kubebuilder/pkg/scaffold/input" |
| 26 | + "sigs.k8s.io/kubebuilder/pkg/scaffold/v1/resource" |
| 27 | + "sigs.k8s.io/kubebuilder/pkg/scaffold/v2/internal" |
| 28 | +) |
| 29 | + |
| 30 | +var _ input.File = &ControllerSuiteTest{} |
| 31 | + |
| 32 | +// ControllerSuiteTest scaffolds the suite_test.go file to setup the controller test |
| 33 | +type ControllerSuiteTest struct { |
| 34 | + input.Input |
| 35 | + |
| 36 | + // Resource is the resource to scaffold the controller_kind_test.go file for |
| 37 | + Resource *resource.Resource |
| 38 | + |
| 39 | + // ResourcePackage is the package of the Resource |
| 40 | + ResourcePackage string |
| 41 | + |
| 42 | + // Plural is the plural lowercase of kind |
| 43 | + Plural string |
| 44 | + |
| 45 | + // Is the Group + "." + Domain for the Resource |
| 46 | + GroupDomain string |
| 47 | +} |
| 48 | + |
| 49 | +// GetInput implements input.File |
| 50 | +func (v *ControllerSuiteTest) GetInput() (input.Input, error) { |
| 51 | + if v.Path == "" { |
| 52 | + v.Path = filepath.Join("controllers", "suite_test.go") |
| 53 | + } |
| 54 | + v.TemplateBody = controllerSuiteTestTemplate |
| 55 | + return v.Input, nil |
| 56 | +} |
| 57 | + |
| 58 | +// Validate validates the values |
| 59 | +func (v *ControllerSuiteTest) Validate() error { |
| 60 | + return v.Resource.Validate() |
| 61 | +} |
| 62 | + |
| 63 | +var controllerSuiteTestTemplate = `{{ .Boilerplate }} |
| 64 | +
|
| 65 | +package controllers |
| 66 | +
|
| 67 | +import ( |
| 68 | + "path/filepath" |
| 69 | + "testing" |
| 70 | +
|
| 71 | + . "github.com/onsi/ginkgo" |
| 72 | + . "github.com/onsi/gomega" |
| 73 | +
|
| 74 | + "k8s.io/client-go/kubernetes/scheme" |
| 75 | + "k8s.io/client-go/rest" |
| 76 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 77 | + "sigs.k8s.io/controller-runtime/pkg/envtest" |
| 78 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 79 | + "sigs.k8s.io/controller-runtime/pkg/log/zap" |
| 80 | +
|
| 81 | + // +kubebuilder:scaffold:imports |
| 82 | +) |
| 83 | +
|
| 84 | +// These tests use Ginkgo (BDD-style Go testing framework). Refer to |
| 85 | +// http://onsi.github.io/ginkgo/ to learn more about Ginkgo. |
| 86 | +
|
| 87 | +var cfg *rest.Config |
| 88 | +var k8sClient client.Client |
| 89 | +var testEnv *envtest.Environment |
| 90 | +
|
| 91 | +func TestAPIs(t *testing.T) { |
| 92 | + RegisterFailHandler(Fail) |
| 93 | +
|
| 94 | + RunSpecsWithDefaultAndCustomReporters(t, |
| 95 | + "Controller Suite", |
| 96 | + []Reporter{envtest.NewlineReporter{}}) |
| 97 | +} |
| 98 | +
|
| 99 | +var _ = BeforeSuite(func(done Done) { |
| 100 | + logf.SetLogger(zap.LoggerTo(GinkgoWriter, true)) |
| 101 | +
|
| 102 | + By("bootstrapping test environment") |
| 103 | + testEnv = &envtest.Environment{ |
| 104 | + CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, |
| 105 | + } |
| 106 | + |
| 107 | + cfg, err := testEnv.Start() |
| 108 | + Expect(err).ToNot(HaveOccurred()) |
| 109 | + Expect(cfg).ToNot(BeNil()) |
| 110 | +
|
| 111 | + // +kubebuilder:scaffold:scheme |
| 112 | +
|
| 113 | +
|
| 114 | + k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) |
| 115 | + Expect(err).ToNot(HaveOccurred()) |
| 116 | + Expect(k8sClient).ToNot(BeNil()) |
| 117 | +
|
| 118 | + close(done) |
| 119 | +}, 60) |
| 120 | +
|
| 121 | +var _ = AfterSuite(func() { |
| 122 | + By("tearing down the test environment") |
| 123 | + err := testEnv.Stop() |
| 124 | + Expect(err).ToNot(HaveOccurred()) |
| 125 | +}) |
| 126 | +` |
| 127 | + |
| 128 | +// UpdateTestSuite updates given file (suite_test.go) with code fragments required for |
| 129 | +// adding import paths and code setup for new types. |
| 130 | +func (a *ControllerSuiteTest) UpdateTestSuite() error { |
| 131 | + |
| 132 | + a.ResourcePackage, a.GroupDomain = getResourceInfo(a.Resource, a.Input) |
| 133 | + if a.Plural == "" { |
| 134 | + rs := inflect.NewDefaultRuleset() |
| 135 | + a.Plural = rs.Pluralize(strings.ToLower(a.Resource.Kind)) |
| 136 | + } |
| 137 | + |
| 138 | + apiImportCodeFragment := fmt.Sprintf(`"%s/controllers" |
| 139 | +%s%s "%s/%s" |
| 140 | +`, a.Repo, a.Resource.Group, a.Resource.Version, a.ResourcePackage, a.Resource.Version) |
| 141 | + |
| 142 | + addschemeCodeFragment := fmt.Sprintf(`err = %s%s.AddToScheme(scheme.Scheme) |
| 143 | +Expect(err).NotTo(HaveOccurred()) |
| 144 | +
|
| 145 | +`, a.Resource.Group, a.Resource.Version) |
| 146 | + |
| 147 | + err := internal.InsertStringsInFile(a.Path, |
| 148 | + apiPkgImportScaffoldMarker, apiImportCodeFragment, |
| 149 | + apiSchemeScaffoldMarker, addschemeCodeFragment) |
| 150 | + if err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + |
| 154 | + return nil |
| 155 | +} |
0 commit comments