Skip to content

Commit de15359

Browse files
authored
Merge pull request #767 from mengqiy/travise2e
🏃 add e2e tests for v2
2 parents a04a5ed + 54d1d1c commit de15359

9 files changed

+646
-216
lines changed

test/e2e/config.go

-50
This file was deleted.

test/e2e/e2ev1.go test/e2e/e2e_v1.go

+28-53
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2018 The Kubernetes Authors.
2+
Copyright 2019 The Kubernetes Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@ package e2e
1818

1919
import (
2020
"fmt"
21-
"os"
2221
"os/exec"
2322
"path/filepath"
2423
"strings"
@@ -30,77 +29,57 @@ import (
3029

3130
var _ = Describe("kubebuilder", func() {
3231
Context("with v1 scaffolding", func() {
33-
imageName := "controller:v0.0.1"
34-
var testSuffix string
35-
var c *config
36-
var kbTest *kubebuilderTest
37-
32+
var kbc *KBTestContext
3833
BeforeEach(func() {
3934
var err error
40-
testSuffix, err = randomSuffix()
41-
Expect(err).NotTo(HaveOccurred())
42-
c, err = configWithSuffix(testSuffix)
35+
kbc, err = TestContext("GO111MODULE=off")
4336
Expect(err).NotTo(HaveOccurred())
44-
kbTest = &kubebuilderTest{
45-
Dir: c.workDir,
46-
Env: []string{"GO111MODULE=off"},
47-
}
48-
prepare(c.workDir)
37+
Expect(kbc.Prepare()).To(Succeed())
4938
})
5039

5140
AfterEach(func() {
5241
By("clean up created API objects during test process")
53-
resources, err := kbTest.RunKustomizeCommand("build", filepath.Join("config", "default"))
54-
if err != nil {
55-
fmt.Fprintf(GinkgoWriter, "error when running kustomize build during cleaning up: %v\n", err)
56-
}
57-
if _, err = kbTest.RunKubectlCommandWithInput(resources, "delete", "--recursive", "-f", "-"); err != nil {
58-
fmt.Fprintf(GinkgoWriter, "error when running kubectl delete during cleaning up: %v\n", err)
59-
}
60-
if _, err = kbTest.RunKubectlCommand(
42+
kbc.CleanupManifests(filepath.Join("config", "default"))
43+
if _, err := kbc.Kubectl.Command(
6144
"delete", "--recursive",
6245
"-f", filepath.Join("config", "crds"),
6346
); err != nil {
6447
fmt.Fprintf(GinkgoWriter, "error when running kubectl delete during cleaning up crd: %v\n", err)
6548
}
6649

67-
By("remove container image created during test")
68-
kbTest.CleanupImage(c.controllerImageName)
69-
70-
By("remove test work dir")
71-
os.RemoveAll(c.workDir)
50+
By("remove container image and work dir")
51+
kbc.Destroy()
7252
})
7353

7454
It("should generate a runnable project", func() {
7555
// prepare v1 vendor
7656
By("untar the vendor tarball")
7757
cmd := exec.Command("tar", "-zxf", "../../../testdata/vendor.v1.tgz")
78-
cmd.Dir = c.workDir
79-
err := cmd.Run()
58+
_, err := kbc.Run(cmd)
8059
Expect(err).Should(Succeed())
8160

8261
var controllerPodName string
8362

8463
By("init v1 project")
85-
err = kbTest.Init(
64+
err = kbc.Init(
8665
"--project-version", "1",
87-
"--domain", c.domain,
66+
"--domain", kbc.Domain,
8867
"--dep=false")
8968
Expect(err).Should(Succeed())
9069

9170
By("creating api definition")
92-
err = kbTest.CreateAPI(
93-
"--group", c.group,
94-
"--version", c.version,
95-
"--kind", c.kind,
71+
err = kbc.CreateAPI(
72+
"--group", kbc.Group,
73+
"--version", kbc.Version,
74+
"--kind", kbc.Kind,
9675
"--namespaced",
9776
"--resource",
9877
"--controller",
9978
"--make=false")
10079
Expect(err).Should(Succeed())
10180

10281
By("creating core-type resource controller")
103-
err = kbTest.CreateAPI(
82+
err = kbc.CreateAPI(
10483
"--group", "apps",
10584
"--version", "v1",
10685
"--kind", "Deployment",
@@ -111,27 +90,27 @@ var _ = Describe("kubebuilder", func() {
11190
Expect(err).Should(Succeed())
11291

11392
By("building image")
114-
err = kbTest.Make("docker-build", "IMG="+imageName)
93+
err = kbc.Make("docker-build", "IMG="+kbc.ImageName)
11594
Expect(err).Should(Succeed())
11695

11796
By("loading docker image into kind cluster")
118-
err = kbTest.LoadImageToKindCluster(imageName)
97+
err = kbc.LoadImageToKindCluster()
11998
Expect(err).Should(Succeed())
12099

121100
// NOTE: If you want to run the test against a GKE cluster, you will need to grant yourself permission.
122101
// Otherwise, you may see "... is forbidden: attempt to grant extra privileges"
123102
// $ kubectl create clusterrolebinding myname-cluster-admin-binding --clusterrole=cluster-admin --user=myname@mycompany.com
124103
// https://cloud.google.com/kubernetes-engine/docs/how-to/role-based-access-control
125104
By("deploying controller manager")
126-
err = kbTest.Make("deploy")
105+
err = kbc.Make("deploy")
127106
Expect(err).Should(Succeed())
128107

129108
By("validate the controller-manager pod running as expected")
130109
verifyControllerUp := func() error {
131110
// Get pod name
132-
podOutput, err := kbTest.RunKubectlGetPodsInNamespace(
133-
testSuffix,
134-
"-l", "control-plane=controller-manager",
111+
podOutput, err := kbc.Kubectl.Get(
112+
true,
113+
"pods", "-l", "control-plane=controller-manager",
135114
"-o", "go-template={{ range .items }}{{ if not .metadata.deletionTimestamp }}{{ .metadata.name }}{{ \"\\n\" }}{{ end }}{{ end }}",
136115
)
137116
Expect(err).NotTo(HaveOccurred())
@@ -143,9 +122,9 @@ var _ = Describe("kubebuilder", func() {
143122
Expect(controllerPodName).Should(ContainSubstring("controller-manager"))
144123

145124
// Validate pod status
146-
status, err := kbTest.RunKubectlGetPodsInNamespace(
147-
testSuffix,
148-
controllerPodName, "-o", "jsonpath={.status.phase}",
125+
status, err := kbc.Kubectl.Get(
126+
true,
127+
"pods", controllerPodName, "-o", "jsonpath={.status.phase}",
149128
)
150129
Expect(err).NotTo(HaveOccurred())
151130
if status != "Running" {
@@ -157,18 +136,14 @@ var _ = Describe("kubebuilder", func() {
157136
Eventually(verifyControllerUp, 2*time.Minute, time.Second).Should(Succeed())
158137

159138
By("creating an instance of CR")
160-
inputFile := filepath.Join("config", "samples", fmt.Sprintf("%s_%s_%s.yaml", c.group, c.version, strings.ToLower(c.kind)))
161-
_, err = kbTest.RunKubectlCommand("apply", "-f", inputFile)
139+
inputFile := filepath.Join("config", "samples", fmt.Sprintf("%s_%s_%s.yaml", kbc.Group, kbc.Version, strings.ToLower(kbc.Kind)))
140+
_, err = kbc.Kubectl.Apply(false, "-f", inputFile)
162141
Expect(err).NotTo(HaveOccurred())
163142

164143
By("validate the created resource object gets reconciled in controller")
165144
controllerContainerLogs := func() string {
166145
// Check container log to validate that the created resource object gets reconciled in controller
167-
logOutput, err := kbTest.RunKubectlCommand(
168-
"logs", controllerPodName,
169-
"-c", "manager",
170-
"-n", fmt.Sprintf("e2e-%s-system", testSuffix),
171-
)
146+
logOutput, err := kbc.Kubectl.Logs(controllerPodName, "-c", "manager")
172147
Expect(err).NotTo(HaveOccurred())
173148

174149
return logOutput

0 commit comments

Comments
 (0)