Skip to content

Commit

Permalink
Use more concise expectations in verifyControllerUp
Browse files Browse the repository at this point in the history
Inlined variables directly into Expect() and use Error() when we only
care that it succeeded.
  • Loading branch information
mogsie committed Sep 4, 2024
1 parent 9ed2ea5 commit 1151a1a
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 133 deletions.
32 changes: 10 additions & 22 deletions pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,15 @@ var _ = Describe("Manager", Ordered, func() {
BeforeAll(func() {
By("creating manager namespace")
cmd := exec.Command("kubectl", "create", "ns", namespace)
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to create namespace")
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create namespace")
By("installing CRDs")
cmd = exec.Command("make", "install")
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to install CRDs")
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to install CRDs")
By("deploying the controller-manager")
cmd = exec.Command("make", "deploy", fmt.Sprintf("IMG=%s", projectImage))
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to deploy the controller-manager")
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to deploy the controller-manager")
})
// After all tests have been executed, clean up by undeploying the controller, uninstalling CRDs,
Expand Down Expand Up @@ -111,7 +108,7 @@ var _ = Describe("Manager", Ordered, func() {
var controllerPodName string
It("should run successfully", func() {
By("validating that the controller-manager pod is running as expected")
verifyControllerUp := func() error {
verifyControllerUp := func(g Gomega) {
// Get the name of the controller-manager pod
cmd := exec.Command("kubectl", "get",
"pods", "-l", "control-plane=controller-manager",
Expand All @@ -123,28 +120,21 @@ var _ = Describe("Manager", Ordered, func() {
)
podOutput, err := utils.Run(cmd)
ExpectWithOffset(2, err).NotTo(HaveOccurred(), "Failed to retrieve controller-manager pod information")
g.Expect(err).NotTo(HaveOccurred(), "Failed to retrieve controller-manager pod information")
podNames := utils.GetNonEmptyLines(string(podOutput))
if len(podNames) != 1 {
return fmt.Errorf("expected 1 controller pod running, but got %d", len(podNames))
}
g.Expect(podNames).To(HaveLen(1), "expected 1 controller pod running")
controllerPodName = podNames[0]
ExpectWithOffset(2, controllerPodName).Should(ContainSubstring("controller-manager"))
g.ExpectWithOffset(controllerPodName).To(ContainSubstring("controller-manager"))
// Validate the pod's status
cmd = exec.Command("kubectl", "get",
"pods", controllerPodName, "-o", "jsonpath={.status.phase}",
"-n", namespace,
)
status, err := utils.Run(cmd)
ExpectWithOffset(2, err).NotTo(HaveOccurred(), "Failed to retrieve controller-manager pod status")
if string(status) != "Running" {
return fmt.Errorf("controller pod in %s status", status)
}
return nil
g.Expect(utils.Run(cmd)).To(Equal("Running"), "Incorrect controller-manager pod status")
}
// Repeatedly check if the controller-manager pod is running until it succeeds or times out.
EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed())
Eventually(verifyControllerUp, time.Minute, time.Second).Should(Succeed())
})
It("should ensure the metrics endpoint is serving metrics", func() {
Expand Down Expand Up @@ -203,9 +193,7 @@ var _ = Describe("Manager", Ordered, func() {
cmd := exec.Command("kubectl", "get", "pods", "curl-metrics",
"-o", "jsonpath={.status.phase}",
"-n", namespace)
status, err := utils.Run(cmd)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(status).To(Equal("Succeeded"), fmt.Sprintf("curl pod in %s status", status))
g.Expect(utils.Run(cmd)).To(Equal("Succeeded"), fmt.Sprintf("curl pod in %s status", status))
}
Eventually(verifyCurlUp).WithTimeout(5 * time.Minute).Should(Succeed())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,15 @@ var _ = Describe("Manager", Ordered, func() {
BeforeAll(func() {
By("creating manager namespace")
cmd := exec.Command("kubectl", "create", "ns", namespace)
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to create namespace")
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create namespace")

By("installing CRDs")
cmd = exec.Command("make", "install")
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to install CRDs")
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to install CRDs")

By("deploying the controller-manager")
cmd = exec.Command("make", "deploy", fmt.Sprintf("IMG=%s", projectImage))
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to deploy the controller-manager")
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to deploy the controller-manager")
})

// After all tests have been executed, clean up by undeploying the controller, uninstalling CRDs,
Expand Down Expand Up @@ -87,7 +84,7 @@ var _ = Describe("Manager", Ordered, func() {
var controllerPodName string
It("should run successfully", func() {
By("validating that the controller-manager pod is running as expected")
verifyControllerUp := func() error {
verifyControllerUp := func(g Gomega) {
// Get the name of the controller-manager pod
cmd := exec.Command("kubectl", "get",
"pods", "-l", "control-plane=controller-manager",
Expand All @@ -99,28 +96,21 @@ var _ = Describe("Manager", Ordered, func() {
)

podOutput, err := utils.Run(cmd)
ExpectWithOffset(2, err).NotTo(HaveOccurred(), "Failed to retrieve controller-manager pod information")
g.Expect(err).NotTo(HaveOccurred(), "Failed to retrieve controller-manager pod information")
podNames := utils.GetNonEmptyLines(string(podOutput))
if len(podNames) != 1 {
return fmt.Errorf("expected 1 controller pod running, but got %d", len(podNames))
}
g.Expect(podNames).To(HaveLen(1), "expected 1 controller pod running")
controllerPodName = podNames[0]
ExpectWithOffset(2, controllerPodName).Should(ContainSubstring("controller-manager"))
g.ExpectWithOffset(controllerPodName).To(ContainSubstring("controller-manager"))

// Validate the pod's status
cmd = exec.Command("kubectl", "get",
"pods", controllerPodName, "-o", "jsonpath={.status.phase}",
"-n", namespace,
)
status, err := utils.Run(cmd)
ExpectWithOffset(2, err).NotTo(HaveOccurred(), "Failed to retrieve controller-manager pod status")
if string(status) != "Running" {
return fmt.Errorf("controller pod in %s status", status)
}
return nil
g.Expect(utils.Run(cmd)).To(Equal("Running"), "Incorrect controller-manager pod status")
}
// Repeatedly check if the controller-manager pod is running until it succeeds or times out.
EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed())
Eventually(verifyControllerUp, time.Minute, time.Second).Should(Succeed())
})

It("should ensure the metrics endpoint is serving metrics", func() {
Expand Down Expand Up @@ -179,9 +169,7 @@ var _ = Describe("Manager", Ordered, func() {
cmd := exec.Command("kubectl", "get", "pods", "curl-metrics",
"-o", "jsonpath={.status.phase}",
"-n", namespace)
status, err := utils.Run(cmd)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(status).To(Equal("Succeeded"), fmt.Sprintf("curl pod in %s status", status))
g.Expect(utils.Run(cmd)).To(Equal("Succeeded"), fmt.Sprintf("curl pod in %s status", status))
}
Eventually(verifyCurlUp).WithTimeout(5 * time.Minute).Should(Succeed())

Expand Down
32 changes: 10 additions & 22 deletions testdata/project-v4-multigroup/test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,15 @@ var _ = Describe("Manager", Ordered, func() {
BeforeAll(func() {
By("creating manager namespace")
cmd := exec.Command("kubectl", "create", "ns", namespace)
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to create namespace")
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create namespace")

By("installing CRDs")
cmd = exec.Command("make", "install")
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to install CRDs")
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to install CRDs")

By("deploying the controller-manager")
cmd = exec.Command("make", "deploy", fmt.Sprintf("IMG=%s", projectImage))
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to deploy the controller-manager")
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to deploy the controller-manager")
})

// After all tests have been executed, clean up by undeploying the controller, uninstalling CRDs,
Expand Down Expand Up @@ -87,7 +84,7 @@ var _ = Describe("Manager", Ordered, func() {
var controllerPodName string
It("should run successfully", func() {
By("validating that the controller-manager pod is running as expected")
verifyControllerUp := func() error {
verifyControllerUp := func(g Gomega) {
// Get the name of the controller-manager pod
cmd := exec.Command("kubectl", "get",
"pods", "-l", "control-plane=controller-manager",
Expand All @@ -99,28 +96,21 @@ var _ = Describe("Manager", Ordered, func() {
)

podOutput, err := utils.Run(cmd)
ExpectWithOffset(2, err).NotTo(HaveOccurred(), "Failed to retrieve controller-manager pod information")
g.Expect(err).NotTo(HaveOccurred(), "Failed to retrieve controller-manager pod information")
podNames := utils.GetNonEmptyLines(string(podOutput))
if len(podNames) != 1 {
return fmt.Errorf("expected 1 controller pod running, but got %d", len(podNames))
}
g.Expect(podNames).To(HaveLen(1), "expected 1 controller pod running")
controllerPodName = podNames[0]
ExpectWithOffset(2, controllerPodName).Should(ContainSubstring("controller-manager"))
g.ExpectWithOffset(controllerPodName).To(ContainSubstring("controller-manager"))

// Validate the pod's status
cmd = exec.Command("kubectl", "get",
"pods", controllerPodName, "-o", "jsonpath={.status.phase}",
"-n", namespace,
)
status, err := utils.Run(cmd)
ExpectWithOffset(2, err).NotTo(HaveOccurred(), "Failed to retrieve controller-manager pod status")
if string(status) != "Running" {
return fmt.Errorf("controller pod in %s status", status)
}
return nil
g.Expect(utils.Run(cmd)).To(Equal("Running"), "Incorrect controller-manager pod status")
}
// Repeatedly check if the controller-manager pod is running until it succeeds or times out.
EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed())
Eventually(verifyControllerUp, time.Minute, time.Second).Should(Succeed())
})

It("should ensure the metrics endpoint is serving metrics", func() {
Expand Down Expand Up @@ -179,9 +169,7 @@ var _ = Describe("Manager", Ordered, func() {
cmd := exec.Command("kubectl", "get", "pods", "curl-metrics",
"-o", "jsonpath={.status.phase}",
"-n", namespace)
status, err := utils.Run(cmd)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(status).To(Equal("Succeeded"), fmt.Sprintf("curl pod in %s status", status))
g.Expect(utils.Run(cmd)).To(Equal("Succeeded"), fmt.Sprintf("curl pod in %s status", status))
}
Eventually(verifyCurlUp).WithTimeout(5 * time.Minute).Should(Succeed())

Expand Down
34 changes: 11 additions & 23 deletions testdata/project-v4-with-deploy-image/test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,15 @@ var _ = Describe("Manager", Ordered, func() {
BeforeAll(func() {
By("creating manager namespace")
cmd := exec.Command("kubectl", "create", "ns", namespace)
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to create namespace")
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create namespace")

By("installing CRDs")
cmd = exec.Command("make", "install")
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to install CRDs")
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to install CRDs")

By("deploying the controller-manager")
cmd = exec.Command("make", "deploy", fmt.Sprintf("IMG=%s", projectImage))
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to deploy the controller-manager")
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to deploy the controller-manager")
})

// After all tests have been executed, clean up by undeploying the controller, uninstalling CRDs,
Expand Down Expand Up @@ -87,7 +84,7 @@ var _ = Describe("Manager", Ordered, func() {
var controllerPodName string
It("should run successfully", func() {
By("validating that the controller-manager pod is running as expected")
verifyControllerUp := func() error {
verifyControllerUp := func(g Gomega) {
// Get the name of the controller-manager pod
cmd := exec.Command("kubectl", "get",
"pods", "-l", "control-plane=controller-manager",
Expand All @@ -99,28 +96,21 @@ var _ = Describe("Manager", Ordered, func() {
)

podOutput, err := utils.Run(cmd)
ExpectWithOffset(2, err).NotTo(HaveOccurred(), "Failed to retrieve controller-manager pod information")
g.Expect(err).NotTo(HaveOccurred(), "Failed to retrieve controller-manager pod information")
podNames := utils.GetNonEmptyLines(string(podOutput))
if len(podNames) != 1 {
return fmt.Errorf("expected 1 controller pod running, but got %d", len(podNames))
}
g.Expect(podNames).To(HaveLen(1), "expected 1 controller pod running")
controllerPodName = podNames[0]
ExpectWithOffset(2, controllerPodName).Should(ContainSubstring("controller-manager"))
g.ExpectWithOffset(controllerPodName).To(ContainSubstring("controller-manager"))

// Validate the pod's status
cmd = exec.Command("kubectl", "get",
"pods", controllerPodName, "-o", "jsonpath={.status.phase}",
"-n", namespace,
)
status, err := utils.Run(cmd)
ExpectWithOffset(2, err).NotTo(HaveOccurred(), "Failed to retrieve controller-manager pod status")
if string(status) != "Running" {
return fmt.Errorf("controller pod in %s status", status)
}
return nil
g.Expect(utils.Run(cmd)).To(Equal("Running"), "Incorrect controller-manager pod status")
}
// Repeatedly check if the controller-manager pod is running until it succeeds or times out.
EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed())
Eventually(verifyControllerUp, time.Minute, time.Second).Should(Succeed())
})

It("should ensure the metrics endpoint is serving metrics", func() {
Expand Down Expand Up @@ -172,16 +162,14 @@ var _ = Describe("Manager", Ordered, func() {
"--", "/bin/sh", "-c", fmt.Sprintf(
"curl -v -k -H 'Authorization: Bearer %s' https://%s.%s.svc.cluster.local:8443/metrics",
token, metricsServiceName, namespace))
Expect(utils.Run(cmd)).ToNot(BeEmpty(), "Failed to create curl-metrics pod")
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create curl-metrics pod")

By("waiting for the curl-metrics pod to complete.")
verifyCurlUp := func(g Gomega) {
cmd := exec.Command("kubectl", "get", "pods", "curl-metrics",
"-o", "jsonpath={.status.phase}",
"-n", namespace)
status, err := utils.Run(cmd)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(status).To(Equal("Succeeded"), fmt.Sprintf("curl pod in %s status", status))
g.Expect(utils.Run(cmd)).To(Equal("Succeeded"), fmt.Sprintf("curl pod in %s status", status))
}
Eventually(verifyCurlUp).WithTimeout(5 * time.Minute).Should(Succeed())

Expand Down
32 changes: 10 additions & 22 deletions testdata/project-v4-with-grafana/test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,15 @@ var _ = Describe("Manager", Ordered, func() {
BeforeAll(func() {
By("creating manager namespace")
cmd := exec.Command("kubectl", "create", "ns", namespace)
_, err := utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to create namespace")
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to create namespace")

By("installing CRDs")
cmd = exec.Command("make", "install")
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to install CRDs")
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to install CRDs")

By("deploying the controller-manager")
cmd = exec.Command("make", "deploy", fmt.Sprintf("IMG=%s", projectImage))
_, err = utils.Run(cmd)
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to deploy the controller-manager")
Expect(utils.Run(cmd)).Error().NotTo(HaveOccurred(), "Failed to deploy the controller-manager")
})

// After all tests have been executed, clean up by undeploying the controller, uninstalling CRDs,
Expand Down Expand Up @@ -87,7 +84,7 @@ var _ = Describe("Manager", Ordered, func() {
var controllerPodName string
It("should run successfully", func() {
By("validating that the controller-manager pod is running as expected")
verifyControllerUp := func() error {
verifyControllerUp := func(g Gomega) {
// Get the name of the controller-manager pod
cmd := exec.Command("kubectl", "get",
"pods", "-l", "control-plane=controller-manager",
Expand All @@ -99,28 +96,21 @@ var _ = Describe("Manager", Ordered, func() {
)

podOutput, err := utils.Run(cmd)
ExpectWithOffset(2, err).NotTo(HaveOccurred(), "Failed to retrieve controller-manager pod information")
g.Expect(err).NotTo(HaveOccurred(), "Failed to retrieve controller-manager pod information")
podNames := utils.GetNonEmptyLines(string(podOutput))
if len(podNames) != 1 {
return fmt.Errorf("expected 1 controller pod running, but got %d", len(podNames))
}
g.Expect(podNames).To(HaveLen(1), "expected 1 controller pod running")
controllerPodName = podNames[0]
ExpectWithOffset(2, controllerPodName).Should(ContainSubstring("controller-manager"))
g.ExpectWithOffset(controllerPodName).To(ContainSubstring("controller-manager"))

// Validate the pod's status
cmd = exec.Command("kubectl", "get",
"pods", controllerPodName, "-o", "jsonpath={.status.phase}",
"-n", namespace,
)
status, err := utils.Run(cmd)
ExpectWithOffset(2, err).NotTo(HaveOccurred(), "Failed to retrieve controller-manager pod status")
if string(status) != "Running" {
return fmt.Errorf("controller pod in %s status", status)
}
return nil
g.Expect(utils.Run(cmd)).To(Equal("Running"), "Incorrect controller-manager pod status")
}
// Repeatedly check if the controller-manager pod is running until it succeeds or times out.
EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed())
Eventually(verifyControllerUp, time.Minute, time.Second).Should(Succeed())
})

It("should ensure the metrics endpoint is serving metrics", func() {
Expand Down Expand Up @@ -179,9 +169,7 @@ var _ = Describe("Manager", Ordered, func() {
cmd := exec.Command("kubectl", "get", "pods", "curl-metrics",
"-o", "jsonpath={.status.phase}",
"-n", namespace)
status, err := utils.Run(cmd)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(status).To(Equal("Succeeded"), fmt.Sprintf("curl pod in %s status", status))
g.Expect(utils.Run(cmd)).To(Equal("Succeeded"), fmt.Sprintf("curl pod in %s status", status))
}
Eventually(verifyCurlUp).WithTimeout(5 * time.Minute).Should(Succeed())

Expand Down
Loading

0 comments on commit 1151a1a

Please sign in to comment.