Skip to content

Commit 77864a7

Browse files
e2e: add tests to validate metrics endpoint
1 parent 53f15b5 commit 77864a7

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

test/e2e/metrics_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package e2e
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"os/exec"
7+
"testing"
8+
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestOperatorControllerMetrics(t *testing.T) {
13+
var (
14+
token string
15+
curlPod = "curl-metrics"
16+
namespace = "olmv1-system"
17+
)
18+
19+
// Step 1: Create ClusterRoleBinding for metrics access
20+
t.Log("Creating ClusterRoleBinding for metrics access")
21+
cmd := exec.Command("kubectl", "create", "clusterrolebinding", "operator-controller-metrics-binding",
22+
"--clusterrole=operator-controller-metrics-reader",
23+
"--serviceaccount="+namespace+":operator-controller-controller-manager")
24+
output, err := cmd.CombinedOutput()
25+
require.NoError(t, err, fmt.Sprintf("Error creating ClusterRoleBinding: %s", string(output)))
26+
27+
// Step 2: Generate ServiceAccount Token
28+
t.Log("Generating ServiceAccount token")
29+
tokenCmd := exec.Command("kubectl", "create", "token", "operator-controller-controller-manager", "-n", namespace)
30+
tokenOutput, err := tokenCmd.Output()
31+
require.NoError(t, err, fmt.Sprintf("Error creating token: %s", string(tokenOutput)))
32+
token = string(bytes.TrimSpace(tokenOutput))
33+
34+
// Step 3: Create the Curl Pod
35+
t.Log("Creating curl pod for validation")
36+
cmd = exec.Command("kubectl", "run", curlPod,
37+
"--image=curlimages/curl:7.87.0", "-n", namespace,
38+
"--restart=Never",
39+
"--overrides", `{
40+
"spec": {
41+
"containers": [{
42+
"name": "curl",
43+
"image": "curlimages/curl:7.87.0",
44+
"command": ["sh", "-c", "sleep 3600"],
45+
"securityContext": {
46+
"allowPrivilegeEscalation": false,
47+
"capabilities": {
48+
"drop": ["ALL"]
49+
},
50+
"runAsNonRoot": true,
51+
"runAsUser": 1000,
52+
"seccompProfile": {
53+
"type": "RuntimeDefault"
54+
}
55+
}
56+
}],
57+
"serviceAccountName": "operator-controller-controller-manager"
58+
}
59+
}`)
60+
output, err = cmd.CombinedOutput()
61+
require.NoError(t, err, fmt.Sprintf("Error creating curl pod: %s", string(output)))
62+
63+
// Step 4: Validate Pod Readiness
64+
t.Log("Ensuring the curl pod is ready")
65+
require.Eventually(t, func() bool {
66+
checkCmd := exec.Command("kubectl", "get", "pod", curlPod, "-n", namespace, "-o", "jsonpath={.status.phase}")
67+
statusOutput, _ := checkCmd.Output()
68+
return string(statusOutput) == "Running"
69+
}, 120, 5, "Curl pod is not running")
70+
71+
// Step 5: Call the Metrics Endpoint
72+
t.Log("Validating the metrics endpoint")
73+
metricsURL := "https://operator-controller-controller-manager-metrics-service." + namespace + ".svc.cluster.local:8443/metrics"
74+
curlCmd := []string{
75+
"exec", curlPod, "-n", namespace, "--", "curl", "-v", "-k",
76+
"-H", "Authorization: Bearer " + token, metricsURL,
77+
}
78+
cmd = exec.Command("kubectl", curlCmd...)
79+
output, err = cmd.CombinedOutput()
80+
require.NoError(t, err, fmt.Sprintf("Error calling metrics endpoint: %s", string(output)))
81+
require.Contains(t, string(output), "200 OK", "Metrics endpoint did not return 200 OK")
82+
require.Contains(t, string(output), "# HELP", "Metrics data not found in response")
83+
84+
// Step 6: Cleanup
85+
t.Log("Cleaning up resources")
86+
_ = exec.Command("kubectl", "delete", "pod", curlPod, "-n", namespace, "--ignore-not-found=true").Run()
87+
_ = exec.Command("kubectl", "delete", "clusterrolebinding", "operator-controller-metrics-binding", "--ignore-not-found=true").Run()
88+
}

0 commit comments

Comments
 (0)