|
| 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 externalplugin |
| 18 | + |
| 19 | +import ( |
| 20 | + "path/filepath" |
| 21 | + |
| 22 | + pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util" |
| 23 | + |
| 24 | + //nolint:golint |
| 25 | + //nolint:revive |
| 26 | + . "github.com/onsi/ginkgo/v2" |
| 27 | + |
| 28 | + //nolint:golint |
| 29 | + //nolint:revive |
| 30 | + . "github.com/onsi/gomega" |
| 31 | + |
| 32 | + //nolint:golint |
| 33 | + //nolint:revive |
| 34 | + //nolint:golint |
| 35 | + //nolint:revive |
| 36 | + "sigs.k8s.io/kubebuilder/v3/test/e2e/utils" |
| 37 | +) |
| 38 | + |
| 39 | +var _ = Describe("kubebuilder", func() { |
| 40 | + Context("plugin sampleexternalplugin/v1", func() { |
| 41 | + var ( |
| 42 | + kbc *utils.TestContext |
| 43 | + ) |
| 44 | + |
| 45 | + BeforeEach(func() { |
| 46 | + var err error |
| 47 | + kbc, err = utils.NewTestContext(pluginutil.KubebuilderBinName, "GO111MODULE=on") |
| 48 | + Expect(err).NotTo(HaveOccurred(), "Prepare NewTestContext should return no error.") |
| 49 | + Expect(kbc.Prepare()).To(Succeed()) |
| 50 | + }) |
| 51 | + |
| 52 | + AfterEach(func() { |
| 53 | + kbc.Destroy() |
| 54 | + }) |
| 55 | + |
| 56 | + It("should generate a runnable project with sample external plugin", func() { |
| 57 | + GenerateProject(kbc) |
| 58 | + }) |
| 59 | + |
| 60 | + }) |
| 61 | +}) |
| 62 | + |
| 63 | +// GenerateProject implements a sampleexternalplugin/v1 external plugin project defined by a TestContext. |
| 64 | +func GenerateProject(kbc *utils.TestContext) { |
| 65 | + var err error |
| 66 | + |
| 67 | + By("initializing a project") |
| 68 | + err = kbc.Init( |
| 69 | + "--plugins", "sampleexternalplugin/v1", |
| 70 | + "--domain", "sample.domain.com", |
| 71 | + ) |
| 72 | + ExpectWithOffset(1, err).NotTo(HaveOccurred()) |
| 73 | + |
| 74 | + var initFileContentsTmpl = "A simple text file created with the `init` subcommand\nDOMAIN: sample.domain.com" |
| 75 | + initFileContainsExpr, err := pluginutil.HasFileContentWith( |
| 76 | + filepath.Join(kbc.Dir, "initFile.txt"), initFileContentsTmpl) |
| 77 | + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Check initFile.txt should return no error.") |
| 78 | + ExpectWithOffset(1, initFileContainsExpr).To(BeTrue(), "The init file does not contain the expected expression.") |
| 79 | + |
| 80 | + By("creating API definition") |
| 81 | + err = kbc.CreateAPI( |
| 82 | + "--plugins", "sampleexternalplugin/v1", |
| 83 | + "--number=2", |
| 84 | + "--group", kbc.Group, |
| 85 | + "--version", kbc.Version, |
| 86 | + "--kind", kbc.Kind, |
| 87 | + ) |
| 88 | + ExpectWithOffset(1, err).NotTo(HaveOccurred()) |
| 89 | + |
| 90 | + var apiFileContentsTmpl = "A simple text file created with the `create api` subcommand\nNUMBER: 2" |
| 91 | + apiFileContainsExpr, err := pluginutil.HasFileContentWith( |
| 92 | + filepath.Join(kbc.Dir, "apiFile.txt"), apiFileContentsTmpl) |
| 93 | + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Check apiFile.txt should return no error.") |
| 94 | + ExpectWithOffset(1, apiFileContainsExpr).To(BeTrue(), "The api file does not contain the expected expression.") |
| 95 | + |
| 96 | + By("scaffolding webhook") |
| 97 | + err = kbc.CreateWebhook( |
| 98 | + "--plugins", "sampleexternalplugin/v1", |
| 99 | + "--hooked", |
| 100 | + "--group", kbc.Group, |
| 101 | + "--version", kbc.Version, |
| 102 | + "--kind", kbc.Kind, |
| 103 | + ) |
| 104 | + ExpectWithOffset(1, err).NotTo(HaveOccurred()) |
| 105 | + |
| 106 | + var webhookFileContentsTmpl = "A simple text file created with the `create webhook` subcommand\nHOOKED!" |
| 107 | + webhookFileContainsExpr, err := pluginutil.HasFileContentWith( |
| 108 | + filepath.Join(kbc.Dir, "webhookFile.txt"), webhookFileContentsTmpl) |
| 109 | + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Check webhookFile.txt should return no error.") |
| 110 | + ExpectWithOffset(1, webhookFileContainsExpr).To(BeTrue(), "The webhook file does not contain the expected expression.") |
| 111 | +} |
0 commit comments