Skip to content

Commit f0b1428

Browse files
authored
Merge pull request #310 from mengqiy/v1e2e
add e2e test for v1 kubebuilder
2 parents 6bb9d63 + 14bffff commit f0b1428

File tree

7 files changed

+321
-40
lines changed

7 files changed

+321
-40
lines changed

cmd/kubebuilder/v1/api.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ func (o *apiOptions) RunAddAPI() {
7777
if err != nil {
7878
log.Fatal(err)
7979
}
80+
} else {
81+
// disable generation of example reconcile body if not scaffolding resource
82+
// because this could result in a fork-bomb of k8s resources where watching a
83+
// deployment, replicaset etc. results in generating deployment which
84+
// end up genrating replicaset, pod etc recursively.
85+
r.CreateExampleReconcileBody = false
8086
}
8187

8288
if o.doController {
@@ -172,6 +178,6 @@ func ResourceForFlags(f *flag.FlagSet) *resource.Resource {
172178
f.StringVar(&r.Version, "version", "", "resource Version")
173179
f.BoolVar(&r.Namespaced, "namespaced", true, "resource is namespaced")
174180
f.BoolVar(&r.CreateExampleReconcileBody, "example", true,
175-
"true if an example reconcile body should be written")
181+
"if true an example reconcile body should be written while scaffolding a resource.")
176182
return r
177183
}

common.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ function setup_envs {
142142
header_text "setting up env vars"
143143

144144
# Setup env vars
145-
export PATH=/tmp/kubebuilder/bin/:$PATH
145+
export PATH=/tmp/kubebuilder/bin:$PATH
146146
export TEST_ASSET_KUBECTL=/tmp/kubebuilder/bin/kubectl
147147
export TEST_ASSET_KUBE_APISERVER=/tmp/kubebuilder/bin/kube-apiserver
148148
export TEST_ASSET_ETCD=/tmp/kubebuilder/bin/etcd

test/e2e/common.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2018 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 e2e
18+
19+
import (
20+
"log"
21+
"os"
22+
"path/filepath"
23+
24+
"github.com/kubernetes-sigs/kubebuilder/test/e2e/framework"
25+
e2einternal "github.com/kubernetes-sigs/kubebuilder/test/internal/e2e"
26+
. "github.com/onsi/ginkgo"
27+
. "github.com/onsi/gomega"
28+
)
29+
30+
func prepare(workDir string) {
31+
By("create a path under given project dir, as the test work dir")
32+
err := os.MkdirAll(workDir, 0755)
33+
Expect(err).NotTo(HaveOccurred())
34+
}
35+
36+
func cleanupv0(builderTest *e2einternal.KubebuilderTest, workDir string, imageName string) {
37+
By("clean up created API objects during test process")
38+
inputFile := filepath.Join(workDir, "hack", "install.yaml")
39+
deleteOptions := []string{"delete", "-f", inputFile}
40+
builderTest.RunKubectlCommand(framework.GetKubectlArgs(deleteOptions))
41+
42+
By("remove container image created during test")
43+
builderTest.CleanupImage([]string{imageName})
44+
45+
By("remove test work dir")
46+
os.RemoveAll(workDir)
47+
}
48+
49+
func cleanupv1(builderTest *e2einternal.KubebuilderTest, workDir string, imageName string) {
50+
By("clean up created API objects during test process")
51+
52+
kustomizeOptions := []string{"build", filepath.Join("config", "default")}
53+
resources, err := builderTest.RunKustomizeCommand(kustomizeOptions)
54+
if err != nil {
55+
log.Printf("error when runing kustomize build during cleaning up: %v", err)
56+
}
57+
58+
deleteOptions := []string{"delete", "--recursive", "-f", "-"}
59+
_, err = builderTest.RunKubectlCommandWithInput(framework.GetKubectlArgs(deleteOptions), resources)
60+
if err != nil {
61+
log.Printf("error when runing kubectl delete during cleaning up: %v", err)
62+
}
63+
64+
deleteOptions = []string{"delete", "--recursive", "-f", filepath.Join("config", "crds")}
65+
_, err = builderTest.RunKubectlCommand(framework.GetKubectlArgs(deleteOptions))
66+
67+
By("remove container image created during test")
68+
builderTest.CleanupImage([]string{imageName})
69+
70+
By("remove test work dir")
71+
os.RemoveAll(workDir)
72+
}

test/e2e/e2e_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,18 @@ limitations under the License.
1717
package e2e
1818

1919
import (
20+
"log"
21+
"os"
22+
"os/exec"
23+
"path"
24+
"strings"
2025
"testing"
2126

27+
"github.com/golang/glog"
2228
"github.com/kubernetes-sigs/kubebuilder/test/e2e/framework"
29+
"github.com/kubernetes-sigs/kubebuilder/test/e2e/framework/ginkgowrapper"
30+
. "github.com/onsi/ginkgo"
31+
. "github.com/onsi/gomega"
2332
"github.com/spf13/pflag"
2433
)
2534

@@ -28,6 +37,51 @@ func init() {
2837
pflag.Parse()
2938
}
3039

40+
var kubebuilderBinDir string
41+
42+
/*
43+
2 steps to do before running the e2e test suite
44+
1) go build a new kubebuilder binary
45+
2) install all required binaries, such as etcd and kube-apiserver
46+
If you have run testv0.sh or testv1.sh before running this e2e suite,
47+
it will build the new kubebuilder.
48+
*/
49+
// TODO: ensure the required binaries are installed when integrate with Prow.
50+
var _ = BeforeSuite(func(done Done) {
51+
// Uncomment the following line to set the image name before runing the e2e test
52+
//os.Setenv("IMG", "gcr.io/<my-project-name>/<iamge-name:tag>")
53+
// If you want to run the test against a GKE cluster, run the following command first
54+
// $ kubectl create clusterrolebinding myname-cluster-admin-binding --clusterrole=cluster-admin [email protected]
55+
framework.TestContext.BinariesDir = "/tmp/kubebuilder/bin/"
56+
// build a kubebuilder
57+
targets := []string{"kubebuilder", "kubebuilder-gen"}
58+
for _, target := range targets {
59+
buildOptions := []string{
60+
"build", "-o", path.Join(framework.TestContext.BinariesDir, target), path.Join("github.com/kubernetes-sigs/kubebuilder/cmd", target)}
61+
cmd := exec.Command("go", buildOptions...)
62+
cmd.Env = os.Environ()
63+
command := strings.Join(cmd.Args, " ")
64+
log.Printf("running %v", command)
65+
output, err := cmd.CombinedOutput()
66+
log.Printf("output when running:\n%s", output)
67+
Expect(err).NotTo(HaveOccurred())
68+
}
69+
70+
close(done)
71+
}, 60)
72+
73+
var _ = AfterSuite(func() {
74+
os.RemoveAll(kubebuilderBinDir)
75+
})
76+
77+
// RunE2ETests checks configuration parameters (specified through flags) and then runs
78+
// E2E tests using the Ginkgo runner.
79+
func RunE2ETests(t *testing.T) {
80+
RegisterFailHandler(ginkgowrapper.Fail)
81+
glog.Infof("Starting kubebuilder suite")
82+
RunSpecs(t, "Kubebuilder e2e suite")
83+
}
84+
3185
func TestE2E(t *testing.T) {
3286
RunE2ETests(t)
3387
}

test/e2e/e2e.go renamed to test/e2e/e2e_v0.go

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
66
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
http://www.apache.org/licenses/LICENSE-2.0
99
1010
Unless required by applicable law or agreed to in writing, software
1111
distributed under the License is distributed on an "AS IS" BASIS,
@@ -18,42 +18,30 @@ package e2e
1818

1919
import (
2020
"fmt"
21-
"os"
2221
"path/filepath"
2322
"strings"
24-
"testing"
2523
"time"
2624

27-
"github.com/golang/glog"
2825
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/util"
2926
"github.com/kubernetes-sigs/kubebuilder/test/e2e/framework"
30-
"github.com/kubernetes-sigs/kubebuilder/test/e2e/framework/ginkgowrapper"
3127
e2einternal "github.com/kubernetes-sigs/kubebuilder/test/internal/e2e"
3228
. "github.com/onsi/ginkgo"
3329
. "github.com/onsi/gomega"
3430
)
3531

36-
// RunE2ETests checks configuration parameters (specified through flags) and then runs
37-
// E2E tests using the Ginkgo runner.
38-
func RunE2ETests(t *testing.T) {
39-
RegisterFailHandler(ginkgowrapper.Fail)
40-
glog.Infof("Starting kubebuilder suite")
41-
RunSpecs(t, "Kubebuilder e2e suite")
42-
}
43-
44-
var _ = Describe("main workflow", func() {
32+
var _ = Describe("v0 main workflow", func() {
4533
It("should perform main kubebuilder workflow successfully", func() {
4634
testSuffix := framework.RandomSuffix()
4735
c := initConfig(testSuffix)
4836
kubebuilderTest := e2einternal.NewKubebuilderTest(c.workDir, framework.TestContext.BinariesDir)
4937

5038
prepare(c.workDir)
51-
defer cleanup(kubebuilderTest, c.workDir, c.controllerImageName)
39+
defer cleanupv0(kubebuilderTest, c.workDir, c.controllerImageName)
5240

5341
var controllerPodName string
5442

5543
By("init project")
56-
initOptions := []string{"--domain", c.domain}
44+
initOptions := []string{"--domain", c.domain, "--project-version", "v0"}
5745
err := kubebuilderTest.Init(initOptions)
5846
Expect(err).NotTo(HaveOccurred())
5947

@@ -86,7 +74,7 @@ var _ = Describe("main workflow", func() {
8674
Expect(err).NotTo(HaveOccurred())
8775

8876
By("installing controller-manager in cluster")
89-
inputFile := filepath.Join(kubebuilderTest.Dir, "hack", "install.yaml")
77+
inputFile := filepath.Join("hack", "install.yaml")
9078
installOptions := []string{"apply", "-f", inputFile}
9179
_, err = kubebuilderTest.RunKubectlCommand(framework.GetKubectlArgs(installOptions))
9280
Expect(err).NotTo(HaveOccurred())
@@ -104,7 +92,7 @@ var _ = Describe("main workflow", func() {
10492
return fmt.Errorf("expect 1 controller pods running, but got %d", len(podNames))
10593
}
10694
controllerPodName = podNames[0]
107-
Expect(controllerPodName).Should(HavePrefix(c.installName+"-controller-manager"))
95+
Expect(controllerPodName).Should(HavePrefix(c.installName + "-controller-manager"))
10896

10997
// Validate pod status
11098
getOptions = []string{"get", "pods", controllerPodName, "-n", c.namespace, "-o", "jsonpath={.status.phase}"}
@@ -150,22 +138,3 @@ var _ = Describe("main workflow", func() {
150138
Eventually(controllerContainerLogs, 1*time.Minute, 500*time.Millisecond).Should(ContainSubstring("to reconcile deployment-example"))
151139
})
152140
})
153-
154-
func prepare(workDir string) {
155-
By("create a path under given project dir, as the test work dir")
156-
err := os.MkdirAll(workDir, 0755)
157-
Expect(err).NotTo(HaveOccurred())
158-
}
159-
160-
func cleanup(builderTest *e2einternal.KubebuilderTest, workDir string, imageName string) {
161-
By("clean up created API objects during test process")
162-
inputFile := filepath.Join(workDir, "hack", "install.yaml")
163-
createOptions := []string{"delete", "-f", inputFile}
164-
builderTest.RunKubectlCommand(framework.GetKubectlArgs(createOptions))
165-
166-
By("remove container image created during test")
167-
builderTest.CleanupImage([]string{imageName})
168-
169-
By("remove test work dir")
170-
os.RemoveAll(workDir)
171-
}

0 commit comments

Comments
 (0)