From 597d2d6858df048a110e7e26d9d085b943766c39 Mon Sep 17 00:00:00 2001 From: Eileen Date: Thu, 18 May 2023 18:52:30 -0400 Subject: [PATCH 1/2] test: add e2e tests for sample external plugin Co-authored-by: Bryce Palmer --- test/e2e/externalplugin/e2e_suite_test.go | 32 +++++++ test/e2e/externalplugin/generate_test.go | 111 ++++++++++++++++++++++ test/e2e/setup.sh | 1 + 3 files changed, 144 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 0000000000..de30d1c309 --- /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 0000000000..fa49548568 --- /dev/null +++ b/test/e2e/externalplugin/generate_test.go @@ -0,0 +1,111 @@ +/* +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(), "Prepare NewTestContext should return no error.") + 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()) + + var initFileContentsTmpl = "A simple text file created with the `init` subcommand\nDOMAIN: sample.domain.com" + initFileContainsExpr, err := pluginutil.HasFileContentWith( + filepath.Join(kbc.Dir, "initFile.txt"), initFileContentsTmpl) + 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()) + + var apiFileContentsTmpl = "A simple text file created with the `create api` subcommand\nNUMBER: 2" + apiFileContainsExpr, err := pluginutil.HasFileContentWith( + filepath.Join(kbc.Dir, "apiFile.txt"), apiFileContentsTmpl) + 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()) + + var webhookFileContentsTmpl = "A simple text file created with the `create webhook` subcommand\nHOOKED!" + webhookFileContainsExpr, err := pluginutil.HasFileContentWith( + filepath.Join(kbc.Dir, "webhookFile.txt"), webhookFileContentsTmpl) + 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.") +} diff --git a/test/e2e/setup.sh b/test/e2e/setup.sh index 9c8f8b8404..ae6fe2d68b 100755 --- a/test/e2e/setup.sh +++ b/test/e2e/setup.sh @@ -65,4 +65,5 @@ function test_cluster { go test $(dirname "$0")/grafana $flags -timeout 30m go test $(dirname "$0")/deployimage $flags -timeout 30m go test $(dirname "$0")/v4 $flags -timeout 30m + go test $(dirname "$0")/externalplugin $flags -timeout 30m } From 926dd605a94c187fa23e70306b3aeb7fbc1aef31 Mon Sep 17 00:00:00 2001 From: Eileen Date: Tue, 23 May 2023 21:18:40 -0400 Subject: [PATCH 2/2] ci: add a script to install sample external plugin --- test/e2e/ci.sh | 2 ++ test/e2e/externalplugin/e2e_suite_test.go | 2 +- test/e2e/local.sh | 2 ++ test/e2e/setup.sh | 30 +++++++++++++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/test/e2e/ci.sh b/test/e2e/ci.sh index d6fe18c0db..c9e1b1c0e9 100755 --- a/test/e2e/ci.sh +++ b/test/e2e/ci.sh @@ -17,6 +17,8 @@ source "$(dirname "$0")/../common.sh" source "$(dirname "$0")/setup.sh" +build_sample_external_plugin + export KIND_CLUSTER="kind" create_cluster ${KIND_K8S_VERSION} trap delete_cluster EXIT diff --git a/test/e2e/externalplugin/e2e_suite_test.go b/test/e2e/externalplugin/e2e_suite_test.go index de30d1c309..5c29990003 100644 --- a/test/e2e/externalplugin/e2e_suite_test.go +++ b/test/e2e/externalplugin/e2e_suite_test.go @@ -28,5 +28,5 @@ import ( func TestE2E(t *testing.T) { RegisterFailHandler(Fail) fmt.Fprintf(GinkgoWriter, "Starting sample external plugin kubebuilder suite\n") - RunSpecs(t, "Kubebuilder grafana plugin e2e suite") + RunSpecs(t, "Kubebuilder sample external plugin e2e suite") } diff --git a/test/e2e/local.sh b/test/e2e/local.sh index e939159802..fddf813937 100755 --- a/test/e2e/local.sh +++ b/test/e2e/local.sh @@ -17,6 +17,8 @@ source "$(dirname "$0")/../common.sh" source "$(dirname "$0")/setup.sh" +build_sample_external_plugin + export KIND_CLUSTER="local-kubebuilder-e2e" create_cluster ${KIND_K8S_VERSION} if [ -z "${SKIP_KIND_CLEANUP:-}" ]; then diff --git a/test/e2e/setup.sh b/test/e2e/setup.sh index ae6fe2d68b..20e7a9852f 100755 --- a/test/e2e/setup.sh +++ b/test/e2e/setup.sh @@ -67,3 +67,33 @@ function test_cluster { go test $(dirname "$0")/v4 $flags -timeout 30m go test $(dirname "$0")/externalplugin $flags -timeout 30m } + +function build_sample_external_plugin { + # TODO: Dynamically set exteranl plugin destination based on OS platform + # EXTERNAL_PLUGIN_DESTINATION_PREFIX="${HOME}/Library/Application Support/kubebuilder/plugins" + # For Linux: + XDG_CONFIG_HOME="${HOME}/.config" + EXTERNAL_PLUGIN_DESTINATION_PREFIX="$XDG_CONFIG_HOME/kubebuilder/plugins" + + PLUGIN_NAME="sampleexternalplugin" + PLUGIN_VERSION="v1" + EXTERNAL_PLUGIN_DESTINATION="${EXTERNAL_PLUGIN_DESTINATION_PREFIX}/${PLUGIN_NAME}/${PLUGIN_VERSION}" + EXTERNAL_PLUGIN_PATH="${EXTERNAL_PLUGIN_DESTINATION}/${PLUGIN_NAME}" + + if [ -d "$EXTERNAL_PLUGIN_DESTINATION" ]; then + echo "$EXTERNAL_PLUGIN_DESTINATION does exist." + if [ -e "$EXTERNAL_PLUGIN_PATH" ]; then + echo "clean up old binary..." + rm "$EXTERNAL_PLUGIN_PATH" + fi + else + mkdir -p "$EXTERNAL_PLUGIN_DESTINATION" + fi + + REPO_ROOT_DIR="$(git rev-parse --show-toplevel)" + SOURCE_DIR="${REPO_ROOT_DIR}/docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1" + + cd $SOURCE_DIR && go build -o $PLUGIN_NAME && mv $PLUGIN_NAME "$EXTERNAL_PLUGIN_PATH" + + cd $REPO_ROOT_DIR +}