From 64626eeb5d206641175ebe116aed1c28506acfb4 Mon Sep 17 00:00:00 2001 From: Camila Macedo <7708031+camilamacedo86@users.noreply.github.com> Date: Fri, 13 Dec 2024 00:08:02 +0000 Subject: [PATCH] e2e: add tests to validate metrics endpoint --- test/e2e/metrics_test.go | 87 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 test/e2e/metrics_test.go diff --git a/test/e2e/metrics_test.go b/test/e2e/metrics_test.go new file mode 100644 index 000000000..83112bd09 --- /dev/null +++ b/test/e2e/metrics_test.go @@ -0,0 +1,87 @@ +package e2e + +import ( + "bytes" + "os/exec" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestOperatorControllerMetrics(t *testing.T) { + var ( + token string + curlPod = "curl-metrics" + namespace = "olmv1-system" + ) + + // Step 1: Create ClusterRoleBinding for metrics access + t.Log("Creating ClusterRoleBinding for metrics access") + cmd := exec.Command("kubectl", "create", "clusterrolebinding", "operator-controller-metrics-binding", + "--clusterrole=operator-controller-metrics-reader", + "--serviceaccount="+namespace+":operator-controller-controller-manager") + output, err := cmd.CombinedOutput() + require.NoError(t, err, "Error creating ClusterRoleBinding: %s", string(output)) + + // Step 2: Generate ServiceAccount Token + t.Log("Generating ServiceAccount token") + tokenCmd := exec.Command("kubectl", "create", "token", "operator-controller-controller-manager", "-n", namespace) + tokenOutput, err := tokenCmd.Output() + require.NoError(t, err, "Error creating token: %s", string(tokenOutput)) + token = string(bytes.TrimSpace(tokenOutput)) + + // Step 3: Create the Curl Pod + t.Log("Creating curl pod for validation") + cmd = exec.Command("kubectl", "run", curlPod, + "--image=curlimages/curl:7.87.0", "-n", namespace, + "--restart=Never", + "--overrides", `{ + "spec": { + "containers": [{ + "name": "curl", + "image": "curlimages/curl:7.87.0", + "command": ["sh", "-c", "sleep 3600"], + "securityContext": { + "allowPrivilegeEscalation": false, + "capabilities": { + "drop": ["ALL"] + }, + "runAsNonRoot": true, + "runAsUser": 1000, + "seccompProfile": { + "type": "RuntimeDefault" + } + } + }], + "serviceAccountName": "operator-controller-controller-manager" + } + }`) + output, err = cmd.CombinedOutput() + require.NoError(t, err, "Error creating curl pod: %s", string(output)) + + // Step 4: Validate Pod Readiness + t.Log("Ensuring the curl pod is ready") + require.Eventually(t, func() bool { + checkCmd := exec.Command("kubectl", "get", "pod", curlPod, "-n", namespace, "-o", "jsonpath={.status.phase}") + statusOutput, _ := checkCmd.Output() + return string(statusOutput) == "Running" + }, 120, 5, "Curl pod is not running") + + // Step 5: Call the Metrics Endpoint + t.Log("Validating the metrics endpoint") + metricsURL := "https://operator-controller-controller-manager-metrics-service." + namespace + ".svc.cluster.local:8443/metrics" + curlCmd := []string{ + "exec", curlPod, "-n", namespace, "--", "curl", "-v", "-k", + "-H", "Authorization: Bearer " + token, metricsURL, + } + cmd = exec.Command("kubectl", curlCmd...) + output, err = cmd.CombinedOutput() + require.NoError(t, err, "Error calling metrics endpoint: %s", string(output)) + require.Contains(t, string(output), "200 OK", "Metrics endpoint did not return 200 OK") + require.Contains(t, string(output), "# HELP", "Metrics data not found in response") + + // Step 6: Cleanup + t.Log("Cleaning up resources") + _ = exec.Command("kubectl", "delete", "pod", curlPod, "-n", namespace, "--ignore-not-found=true").Run() + _ = exec.Command("kubectl", "delete", "clusterrolebinding", "operator-controller-metrics-binding", "--ignore-not-found=true").Run() +}