Skip to content

Commit 2c65e83

Browse files
committed
:sapling: Clean up plugin_cluster-test.go
Part of #4135 This commits cleans up "plugin_cluster_test.go" to be less verbose and use better use of Gomega's capabilities. Notably, variables are used only when the output of some command is used later on in the test. If we only assert things about the command output, it is generally in-lined. Eventually functions are converted from `() error` to `(g Gomega)`, and Expects are converted to g.Expect inside the functions. All offsets have been removed, since this isn't "helper" code, we want to know which line the test failed on. Finally, the test for verifying available status has been fixed so that it checks the correct pod. Also, the "run-as-user" flag is passed in the "no options" case, since kubernetes doesn't allow the target pod to run as root.
1 parent 33a2f3d commit 2c65e83

File tree

2 files changed

+40
-73
lines changed

2 files changed

+40
-73
lines changed

test/e2e/deployimage/generate_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func creatingAPI(kbc *utils.TestContext) {
4747
"--kind", kbc.Kind,
4848
"--plugins", "deploy-image/v1-alpha",
4949
"--image", "busybox:1.36.1",
50+
"--run-as-user", "1001",
5051
"--make=false",
5152
"--manifests=false",
5253
)

test/e2e/deployimage/plugin_cluster_test.go

Lines changed: 39 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package deployimage
1818

1919
import (
20-
"errors"
2120
"fmt"
2221
"os/exec"
2322
"path/filepath"
@@ -68,128 +67,95 @@ func Run(kbc *utils.TestContext) {
6867
var controllerPodName string
6968
var err error
7069

70+
SetDefaultEventuallyPollingInterval(time.Second)
71+
SetDefaultEventuallyTimeout(time.Minute)
72+
7173
By("updating the go.mod")
72-
err = kbc.Tidy()
73-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
74+
Expect(kbc.Tidy()).To(Succeed())
7475

7576
By("run make manifests")
76-
err = kbc.Make("manifests")
77-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
77+
Expect(kbc.Make("manifests")).To(Succeed())
7878

7979
By("run make generate")
80-
err = kbc.Make("generate")
81-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
80+
Expect(kbc.Make("generate")).To(Succeed())
8281

8382
By("run make all")
84-
err = kbc.Make("all")
85-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
83+
Expect(kbc.Make("all")).To(Succeed())
8684

8785
By("run make install")
88-
err = kbc.Make("install")
89-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
86+
Expect(kbc.Make("install")).To(Succeed())
9087

9188
By("building the controller image")
92-
err = kbc.Make("docker-build", "IMG="+kbc.ImageName)
93-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
89+
Expect(kbc.Make("docker-build", "IMG="+kbc.ImageName)).To(Succeed())
9490

9591
By("loading the controller docker image into the kind cluster")
96-
err = kbc.LoadImageToKindCluster()
97-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
92+
Expect(kbc.LoadImageToKindCluster()).To(Succeed())
9893

9994
By("deploying the controller-manager")
10095
cmd := exec.Command("make", "deploy", "IMG="+kbc.ImageName)
101-
outputMake, err := kbc.Run(cmd)
102-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
103-
104-
By("validating that manager Pod/container(s) are restricted")
105-
ExpectWithOffset(1, outputMake).NotTo(ContainSubstring("Warning: would violate PodSecurity"))
96+
Expect(kbc.Run(cmd)).NotTo(ContainSubstring("Warning: would violate PodSecurity"))
10697

10798
By("validating that the controller-manager pod is running as expected")
108-
verifyControllerUp := func() error {
99+
verifyControllerUp := func(g Gomega) {
109100
// Get pod name
110101
podOutput, err := kbc.Kubectl.Get(
111102
true,
112103
"pods", "-l", "control-plane=controller-manager",
113104
"-o", "go-template={{ range .items }}{{ if not .metadata.deletionTimestamp }}{{ .metadata.name }}"+
114105
"{{ \"\\n\" }}{{ end }}{{ end }}")
115-
ExpectWithOffset(2, err).NotTo(HaveOccurred())
106+
g.Expect(err).NotTo(HaveOccurred())
116107
podNames := util.GetNonEmptyLines(podOutput)
117-
if len(podNames) != 1 {
118-
return fmt.Errorf("expect 1 controller pods running, but got %d", len(podNames))
119-
}
108+
g.Expect(podNames).To(HaveLen(1), "wrong number of controller-manager pods")
120109
controllerPodName = podNames[0]
121-
ExpectWithOffset(2, controllerPodName).Should(ContainSubstring("controller-manager"))
110+
g.Expect(controllerPodName).To(ContainSubstring("controller-manager"))
122111

123112
// Validate pod status
124-
status, err := kbc.Kubectl.Get(
125-
true,
126-
"pods", controllerPodName, "-o", "jsonpath={.status.phase}")
127-
ExpectWithOffset(2, err).NotTo(HaveOccurred())
128-
if status != "Running" {
129-
return fmt.Errorf("controller pod in %s status", status)
130-
}
131-
return nil
113+
g.Expect(kbc.Kubectl.Get(true, "pods", controllerPodName, "-o", "jsonpath={.status.phase}")).
114+
To(Equal("Running"), "incorrect controller pod status")
132115
}
133116
defer func() {
134117
out, err := kbc.Kubectl.CommandInNamespace("describe", "all")
135-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
118+
Expect(err).NotTo(HaveOccurred())
136119
_, _ = fmt.Fprintln(GinkgoWriter, out)
137120
}()
138-
EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed())
121+
Eventually(verifyControllerUp).Should(Succeed())
139122
By("creating an instance of the CR")
140123
sampleFile := filepath.Join("config", "samples",
141124
fmt.Sprintf("%s_%s_%s.yaml", kbc.Group, kbc.Version, strings.ToLower(kbc.Kind)))
142125

143126
sampleFilePath, err := filepath.Abs(filepath.Join(fmt.Sprintf("e2e-%s", kbc.TestSuffix), sampleFile))
144127
Expect(err).To(Not(HaveOccurred()))
145128

146-
EventuallyWithOffset(1, func() error {
147-
_, err = kbc.Kubectl.Apply(true, "-f", sampleFilePath)
148-
return err
149-
}, time.Minute, time.Second).Should(Succeed())
129+
Eventually(func(g Gomega) {
130+
g.Expect(kbc.Kubectl.Apply(true, "-f", sampleFilePath)).Error().NotTo(HaveOccurred())
131+
}).Should(Succeed())
150132

151133
By("validating that pod(s) status.phase=Running")
152-
getMemcachedPodStatus := func() error {
153-
status, err := kbc.Kubectl.Get(true, "pods", "-l",
154-
fmt.Sprintf("app.kubernetes.io/name=%s", kbc.Kind),
134+
verifyMemcachedPodStatus := func(g Gomega) {
135+
g.Expect(kbc.Kubectl.Get(true, "pods", "-l",
136+
fmt.Sprintf("app.kubernetes.io/name=e2e-%s", kbc.TestSuffix),
155137
"-o", "jsonpath={.items[*].status}",
156-
)
157-
ExpectWithOffset(2, err).NotTo(HaveOccurred())
158-
if !strings.Contains(status, "\"phase\":\"Running\"") {
159-
return err
160-
}
161-
return nil
138+
)).To(ContainSubstring("\"phase\":\"Running\""))
162139
}
163-
EventuallyWithOffset(1, getMemcachedPodStatus, time.Minute, time.Second).Should(Succeed())
140+
Eventually(verifyMemcachedPodStatus).Should(Succeed())
164141

165142
By("validating that the status of the custom resource created is updated or not")
166-
var status string
167-
getStatus := func() error {
168-
status, err = kbc.Kubectl.Get(true, strings.ToLower(kbc.Kind),
143+
verifyAvailableStatus := func(g Gomega) {
144+
g.Expect(kbc.Kubectl.Get(true, strings.ToLower(kbc.Kind),
169145
strings.ToLower(kbc.Kind)+"-sample",
170-
"-o", "jsonpath={.status.conditions}")
171-
ExpectWithOffset(2, err).NotTo(HaveOccurred())
172-
if !strings.Contains(status, "Available") {
173-
return errors.New(`status condition with type "Available" should be set`)
174-
}
175-
return nil
146+
"-o", "jsonpath={.status.conditions}")).To(ContainSubstring("Available"),
147+
`status condition with type "Available" should be set`)
176148
}
177-
Eventually(getStatus, time.Minute, time.Second).Should(Succeed())
149+
Eventually(verifyAvailableStatus).Should(Succeed())
178150

179151
By("validating the finalizer")
180-
EventuallyWithOffset(1, func() error {
181-
_, err = kbc.Kubectl.Delete(true, "-f", sampleFilePath)
182-
return err
183-
}, time.Minute, time.Second).Should(Succeed())
152+
Eventually(func(g Gomega) {
153+
g.Expect(kbc.Kubectl.Delete(true, "-f", sampleFilePath)).Error().NotTo(HaveOccurred())
154+
}).Should(Succeed())
184155

185-
EventuallyWithOffset(1, func() error {
186-
events, err := kbc.Kubectl.Get(true, "events", "--field-selector=type=Warning",
156+
Eventually(func(g Gomega) {
157+
g.Expect(kbc.Kubectl.Get(true, "events", "--field-selector=type=Warning",
187158
"-o", "jsonpath={.items[*].message}",
188-
)
189-
ExpectWithOffset(2, err).NotTo(HaveOccurred())
190-
if !strings.Contains(events, "is being deleted from the namespace") {
191-
return err
192-
}
193-
return nil
194-
}, time.Minute, time.Second).Should(Succeed())
159+
)).To(ContainSubstring("is being deleted from the namespace"))
160+
}).Should(Succeed())
195161
}

0 commit comments

Comments
 (0)