From 1e971df110553afab71baa62409edbb2644e8492 Mon Sep 17 00:00:00 2001 From: Eileen Date: Thu, 18 May 2023 18:52:30 -0400 Subject: [PATCH] test: add e2e tests for sample external plugin --- test/e2e/externalplugin/e2e_suite_test.go | 32 ++++++ test/e2e/externalplugin/generate_test.go | 113 ++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 test/e2e/externalplugin/e2e_suite_test.go create mode 100644 test/e2e/externalplugin/generate_test.go diff --git a/test/e2e/externalplugin/e2e_suite_test.go b/test/e2e/externalplugin/e2e_suite_test.go new file mode 100644 index 00000000000..de30d1c3096 --- /dev/null +++ b/test/e2e/externalplugin/e2e_suite_test.go @@ -0,0 +1,32 @@ +/* +Copyright 2023 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package externalplugin + +import ( + "fmt" + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +// Run e2e tests using the Ginkgo runner. +func TestE2E(t *testing.T) { + RegisterFailHandler(Fail) + fmt.Fprintf(GinkgoWriter, "Starting sample external plugin kubebuilder suite\n") + RunSpecs(t, "Kubebuilder grafana plugin e2e suite") +} diff --git a/test/e2e/externalplugin/generate_test.go b/test/e2e/externalplugin/generate_test.go new file mode 100644 index 00000000000..6e2403c0534 --- /dev/null +++ b/test/e2e/externalplugin/generate_test.go @@ -0,0 +1,113 @@ +/* +Copyright 2023 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package externalplugin + +import ( + "path/filepath" + pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util" + + //nolint:golint + //nolint:revive + . "github.com/onsi/ginkgo/v2" + + //nolint:golint + //nolint:revive + . "github.com/onsi/gomega" + + //nolint:golint + //nolint:revive + //nolint:golint + //nolint:revive + "sigs.k8s.io/kubebuilder/v3/test/e2e/utils" +) + +var _ = Describe("kubebuilder", func() { + Context("plugin sampleexternalplugin/v1", func() { + var ( + kbc *utils.TestContext + ) + + BeforeEach(func() { + var err error + kbc, err = utils.NewTestContext(pluginutil.KubebuilderBinName, "GO111MODULE=on") + Expect(err).NotTo(HaveOccurred()) + Expect(kbc.Prepare()).To(Succeed()) + }) + + AfterEach(func() { + kbc.Destroy() + }) + + It("should generate a runnable project with sample external plugin", func() { + GenerateProject(kbc) + }) + + }) +}) + +// GenerateProject implements a sampleexternalplugin/v1 external plugin project defined by a TestContext. +func GenerateProject(kbc *utils.TestContext) { + var err error + + By("initializing a project") + err = kbc.Init( + "--plugins", "sampleexternalplugin/v1", + "--domain", "sample.domain.com", + ) + ExpectWithOffset(1, err).NotTo(HaveOccurred()) + + initFileContainsExpr, err := pluginutil.HasFileContentWith( + filepath.Join(kbc.Dir, "initFile.txt"), + `A simple text file created with the`+" `"+`init`+"`"+` subcommand +DOMAIN: sample.domain.com`) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Check initFile.txt should return no error.") + ExpectWithOffset(1, initFileContainsExpr).To(BeTrue(), "The init file does not contain the expected expression.") + + By("creating API definition") + err = kbc.CreateAPI( + "--plugins", "sampleexternalplugin/v1", + "--number=2", + "--group", kbc.Group, + "--version", kbc.Version, + "--kind", kbc.Kind, + ) + ExpectWithOffset(1, err).NotTo(HaveOccurred()) + + apiFileContainsExpr, err := pluginutil.HasFileContentWith( + filepath.Join(kbc.Dir, "apiFile.txt"), + `A simple text file created with the`+" `"+`create api`+"`"+` subcommand +NUMBER: 2`) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Check apiFile.txt should return no error.") + ExpectWithOffset(1, apiFileContainsExpr).To(BeTrue(), "The api file does not contain the expected expression.") + + By("scaffolding webhook") + err = kbc.CreateWebhook( + "--plugins", "sampleexternalplugin/v1", + "--hooked", + "--group", kbc.Group, + "--version", kbc.Version, + "--kind", kbc.Kind, + ) + ExpectWithOffset(1, err).NotTo(HaveOccurred()) + + webhookFileContainsExpr, err := pluginutil.HasFileContentWith( + filepath.Join(kbc.Dir, "webhookFile.txt"), + `A simple text file created with the`+" `"+`create webhook`+"`"+` subcommand +HOOKED!`) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Check webhookFile.txt should return no error.") + ExpectWithOffset(1, webhookFileContainsExpr).To(BeTrue(), "The webhook file does not contain the expected expression.") +}