Skip to content

Commit

Permalink
cleanup: encapsulate metrics code for e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed May 25, 2024
1 parent 576e29d commit 3913173
Showing 1 changed file with 95 additions and 95 deletions.
190 changes: 95 additions & 95 deletions test/e2e/v4/plugin_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ var _ = Describe("kubebuilder", func() {
})

AfterEach(func() {
By("cleaning up the curl pod")
_, _ = kbc.Kubectl.Delete(true, "pods/curl")

By("clean up API objects created during the test")
kbc.CleanupManifests(filepath.Join("config", "default"))

Expand Down Expand Up @@ -175,8 +178,8 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, hasMetrics bool)
ExpectWithOffset(1, podOutput).To(ContainSubstring("health-probe-bind-address"),
"Expected manager pod to have --health-probe-bind-address flag")

By("validating the metrics endpoint")
_ = curlMetrics(kbc, hasMetrics)
By("checking the metrics endpoint")
_ = checkMetrics(kbc, hasMetrics)

if hasWebhook {
By("validating that cert-manager has provisioned the certificate Secret")
Expand Down Expand Up @@ -255,7 +258,7 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, hasMetrics bool)

if hasMetrics {
By("checking the metrics values to validate that the created resource object gets reconciled")
metricsOutput := curlMetrics(kbc, hasMetrics)
metricsOutput := checkMetrics(kbc, hasMetrics)
ExpectWithOffset(1, metricsOutput).To(ContainSubstring(fmt.Sprintf(
`controller_runtime_reconcile_total{controller="%s",result="success"} 1`,
strings.ToLower(kbc.Kind),
Expand All @@ -273,112 +276,109 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, hasMetrics bool)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
ExpectWithOffset(1, count).To(BeNumerically("==", 5))
}

}

// curlMetrics curl's the /metrics endpoint, returning all logs once a 200 status is returned.
func curlMetrics(kbc *utils.TestContext, hasMetrics bool) string {
// checkMetrics curl's the /metrics endpoint, returning all logs once a 200 status is returned.
func checkMetrics(kbc *utils.TestContext, hasMetrics bool) string {
var metricsOutput string
if hasMetrics {
By("validating that the controller-manager service is available")
_, err := kbc.Kubectl.Get(
true,
"service", fmt.Sprintf("e2e-%s-controller-manager-metrics-service", kbc.TestSuffix),
)
ExpectWithOffset(2, err).NotTo(HaveOccurred(), "Controller-manager service should exist")
metricsOutput = getMetrics(kbc, metricsOutput)
}
if !hasMetrics {
ensureMetricsProtections(kbc)
}
return metricsOutput
}

By("ensuring the service endpoint is ready")
eventuallyCheckServiceEndpoint := func() error {
output, err := kbc.Kubectl.Get(
true,
"endpoints", fmt.Sprintf("e2e-%s-controller-manager-metrics-service", kbc.TestSuffix),
"-o", "jsonpath={.subsets[*].addresses[*].ip}",
)
if err != nil {
return err
}
if output == "" {
return fmt.Errorf("no endpoints found")
}
return nil
}
EventuallyWithOffset(2, eventuallyCheckServiceEndpoint, 2*time.Minute, time.Second).Should(Succeed(),
"Service endpoint should be ready")

By("creating a curl pod to access the metrics endpoint")
// nolint:lll
cmdOpts := []string{
"run", "curl",
"--restart=Never",
"--namespace", kbc.Kubectl.Namespace,
"--image=curlimages/curl:7.78.0",
"--",
"/bin/sh", "-c", fmt.Sprintf("curl -v -k http://e2e-%s-controller-manager-metrics-service.%s.svc.cluster.local:8080/metrics",
kbc.TestSuffix, kbc.Kubectl.Namespace),
}
_, err = kbc.Kubectl.CommandInNamespace(cmdOpts...)
ExpectWithOffset(2, err).NotTo(HaveOccurred())
func ensureMetricsProtections(kbc *utils.TestContext) {
createCurlPod(kbc)

By("validating that the curl pod is running as expected")
verifyCurlUp := func() error {
status, err := kbc.Kubectl.Get(
true,
"pods", "curl", "-o", "jsonpath={.status.phase}")
ExpectWithOffset(3, err).NotTo(HaveOccurred())
if status != "Succeeded" {
return fmt.Errorf("curl pod in %s status", status)
}
return nil
By("validating that the curl pod fail as expected")
verifyCurlUp := func() error {
status, err := kbc.Kubectl.Get(
true,
"pods", "curl", "-o", "jsonpath={.status.phase}")
ExpectWithOffset(3, err).NotTo(HaveOccurred())
if status != "Failed" {
return fmt.Errorf(
"curl pod in %s status when should fail with an error", status)
}
EventuallyWithOffset(2, verifyCurlUp, 240*time.Second, time.Second).Should(Succeed())
return nil
}
EventuallyWithOffset(2, verifyCurlUp, 240*time.Second, time.Second).Should(Succeed())

By("validating that the metrics endpoint is serving as expected")
getCurlLogs := func() string {
metricsOutput, err = kbc.Kubectl.Logs("curl")
ExpectWithOffset(3, err).NotTo(HaveOccurred())
return metricsOutput
By("validating that the metrics endpoint is not working as expected")
getCurlLogs := func() string {
metricsOutput, err := kbc.Kubectl.Logs("curl")
ExpectWithOffset(3, err).NotTo(HaveOccurred())
return metricsOutput
}
EventuallyWithOffset(2, getCurlLogs, 10*time.Second, time.Second).Should(ContainSubstring("Could not resolve host"))
}

func createCurlPod(kbc *utils.TestContext) {
By("creating a curl pod to access the metrics endpoint")
// nolint:lll
cmdOpts := []string{
"run", "curl",
"--restart=Never",
"--namespace", kbc.Kubectl.Namespace,
"--image=curlimages/curl:7.78.0",
"--",
"/bin/sh", "-c", fmt.Sprintf("curl -v -k http://e2e-%s-controller-manager-metrics-service.%s.svc.cluster.local:8080/metrics",
kbc.TestSuffix, kbc.Kubectl.Namespace),
}
_, err := kbc.Kubectl.CommandInNamespace(cmdOpts...)
ExpectWithOffset(2, err).NotTo(HaveOccurred())
}

func getMetrics(kbc *utils.TestContext, metricsOutput string) string {
By("validating that the controller-manager service is available")
_, err := kbc.Kubectl.Get(
true,
"service", fmt.Sprintf("e2e-%s-controller-manager-metrics-service", kbc.TestSuffix),
)
ExpectWithOffset(2, err).NotTo(HaveOccurred(), "Controller-manager service should exist")

By("ensuring the service endpoint is ready")
eventuallyCheckServiceEndpoint := func() error {
output, err := kbc.Kubectl.Get(
true,
"endpoints", fmt.Sprintf("e2e-%s-controller-manager-metrics-service", kbc.TestSuffix),
"-o", "jsonpath={.subsets[*].addresses[*].ip}",
)
if err != nil {
return err
}
EventuallyWithOffset(2, getCurlLogs, 10*time.Second, time.Second).Should(ContainSubstring("< HTTP/1.1 200 OK"))
} else {
By("creating a curl pod to access the metrics endpoint")
// nolint:lll
cmdOpts := []string{
"run", "curl",
"--restart=Never",
"--namespace", kbc.Kubectl.Namespace,
"--image=curlimages/curl:7.78.0",
"--",
"/bin/sh", "-c", fmt.Sprintf("curl -v -k http://e2e-%s-controller-manager-metrics-service.%s.svc.cluster.local:8080/metrics",
kbc.TestSuffix, kbc.Kubectl.Namespace),
if output == "" {
return fmt.Errorf("no endpoints found")
}
_, err := kbc.Kubectl.CommandInNamespace(cmdOpts...)
ExpectWithOffset(2, err).NotTo(HaveOccurred())
return nil
}
EventuallyWithOffset(2, eventuallyCheckServiceEndpoint, 2*time.Minute, time.Second).Should(Succeed(),
"Service endpoint should be ready")

By("validating that the curl pod fail as expected")
verifyCurlUp := func() error {
status, err := kbc.Kubectl.Get(
true,
"pods", "curl", "-o", "jsonpath={.status.phase}")
ExpectWithOffset(3, err).NotTo(HaveOccurred())
if status != "Failed" {
return fmt.Errorf(
"curl pod in %s status when should fail with an error", status)
}
return nil
}
EventuallyWithOffset(2, verifyCurlUp, 240*time.Second, time.Second).Should(Succeed())
createCurlPod(kbc)

By("validating that the metrics endpoint is not working as expected")
getCurlLogs := func() string {
metricsOutput, err := kbc.Kubectl.Logs("curl")
ExpectWithOffset(3, err).NotTo(HaveOccurred())
return metricsOutput
By("validating that the curl pod is running as expected")
verifyCurlUp := func() error {
status, err := kbc.Kubectl.Get(
true,
"pods", "curl", "-o", "jsonpath={.status.phase}")
ExpectWithOffset(3, err).NotTo(HaveOccurred())
if status != "Succeeded" {
return fmt.Errorf("curl pod in %s status", status)
}
EventuallyWithOffset(2, getCurlLogs, 10*time.Second, time.Second).Should(ContainSubstring("Could not resolve host"))
return nil
}
By("cleaning up the curl pod")
_, err := kbc.Kubectl.Delete(true, "pods/curl")
ExpectWithOffset(3, err).NotTo(HaveOccurred())
EventuallyWithOffset(2, verifyCurlUp, 240*time.Second, time.Second).Should(Succeed())

By("validating that the metrics endpoint is serving as expected")
getCurlLogs := func() string {
metricsOutput, err = kbc.Kubectl.Logs("curl")
ExpectWithOffset(3, err).NotTo(HaveOccurred())
return metricsOutput
}
EventuallyWithOffset(2, getCurlLogs, 10*time.Second, time.Second).Should(ContainSubstring("< HTTP/1.1 200 OK"))
return metricsOutput
}

0 comments on commit 3913173

Please sign in to comment.