Skip to content

Commit 40ec611

Browse files
authored
Merge pull request #3419 from Eileen-Yu/external-plugin-e2e
🌱 Add e2e tests for sample external plugin
2 parents 7711b8f + 926dd60 commit 40ec611

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed

test/e2e/ci.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
source "$(dirname "$0")/../common.sh"
1818
source "$(dirname "$0")/setup.sh"
1919

20+
build_sample_external_plugin
21+
2022
export KIND_CLUSTER="kind"
2123
create_cluster ${KIND_K8S_VERSION}
2224
trap delete_cluster EXIT
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
"fmt"
21+
"testing"
22+
23+
. "github.com/onsi/ginkgo/v2"
24+
. "github.com/onsi/gomega"
25+
)
26+
27+
// Run e2e tests using the Ginkgo runner.
28+
func TestE2E(t *testing.T) {
29+
RegisterFailHandler(Fail)
30+
fmt.Fprintf(GinkgoWriter, "Starting sample external plugin kubebuilder suite\n")
31+
RunSpecs(t, "Kubebuilder sample external plugin e2e suite")
32+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
}

test/e2e/local.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
source "$(dirname "$0")/../common.sh"
1818
source "$(dirname "$0")/setup.sh"
1919

20+
build_sample_external_plugin
21+
2022
export KIND_CLUSTER="local-kubebuilder-e2e"
2123
create_cluster ${KIND_K8S_VERSION}
2224
if [ -z "${SKIP_KIND_CLEANUP:-}" ]; then

test/e2e/setup.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,35 @@ function test_cluster {
6565
go test $(dirname "$0")/grafana $flags -timeout 30m
6666
go test $(dirname "$0")/deployimage $flags -timeout 30m
6767
go test $(dirname "$0")/v4 $flags -timeout 30m
68+
go test $(dirname "$0")/externalplugin $flags -timeout 30m
69+
}
70+
71+
function build_sample_external_plugin {
72+
# TODO: Dynamically set exteranl plugin destination based on OS platform
73+
# EXTERNAL_PLUGIN_DESTINATION_PREFIX="${HOME}/Library/Application Support/kubebuilder/plugins"
74+
# For Linux:
75+
XDG_CONFIG_HOME="${HOME}/.config"
76+
EXTERNAL_PLUGIN_DESTINATION_PREFIX="$XDG_CONFIG_HOME/kubebuilder/plugins"
77+
78+
PLUGIN_NAME="sampleexternalplugin"
79+
PLUGIN_VERSION="v1"
80+
EXTERNAL_PLUGIN_DESTINATION="${EXTERNAL_PLUGIN_DESTINATION_PREFIX}/${PLUGIN_NAME}/${PLUGIN_VERSION}"
81+
EXTERNAL_PLUGIN_PATH="${EXTERNAL_PLUGIN_DESTINATION}/${PLUGIN_NAME}"
82+
83+
if [ -d "$EXTERNAL_PLUGIN_DESTINATION" ]; then
84+
echo "$EXTERNAL_PLUGIN_DESTINATION does exist."
85+
if [ -e "$EXTERNAL_PLUGIN_PATH" ]; then
86+
echo "clean up old binary..."
87+
rm "$EXTERNAL_PLUGIN_PATH"
88+
fi
89+
else
90+
mkdir -p "$EXTERNAL_PLUGIN_DESTINATION"
91+
fi
92+
93+
REPO_ROOT_DIR="$(git rev-parse --show-toplevel)"
94+
SOURCE_DIR="${REPO_ROOT_DIR}/docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1"
95+
96+
cd $SOURCE_DIR && go build -o $PLUGIN_NAME && mv $PLUGIN_NAME "$EXTERNAL_PLUGIN_PATH"
97+
98+
cd $REPO_ROOT_DIR
6899
}

0 commit comments

Comments
 (0)