Skip to content

Commit 004d509

Browse files
e2e: add tests to validate metrics endpoint
1 parent 53f15b5 commit 004d509

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

test/e2e/metrics_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package e2e
2+
3+
import (
4+
"bytes"
5+
"github.com/stretchr/testify/require"
6+
"os/exec"
7+
"testing"
8+
)
9+
10+
// nolint:gosec
11+
// TestOperatorControllerMetricsExportedEndpoint verifies that the metrics endpoint for the operator controller
12+
// is exported correctly and accessible by authorized users through RBAC and a ServiceAccount token.
13+
// The test performs the following steps:
14+
// 1. Creates a ClusterRoleBinding to grant necessary permissions for accessing metrics.
15+
// 2. Generates a ServiceAccount token for authentication.
16+
// 3. Deploys a curl pod to interact with the metrics endpoint.
17+
// 4. Waits for the curl pod to become ready.
18+
// 5. Executes a curl command from the pod to validate the metrics endpoint.
19+
// 6. Cleans up all resources created during the test, such as the ClusterRoleBinding and curl pod.
20+
func TestOperatorControllerMetricsExportedEndpoint(t *testing.T) {
21+
var (
22+
token string
23+
curlPod = "curl-metrics"
24+
namespace = "olmv1-system"
25+
)
26+
27+
t.Log("Creating ClusterRoleBinding for operator controller metrics")
28+
cmd := exec.Command("kubectl", "create", "clusterrolebinding", "operator-controller-metrics-binding",
29+
"--clusterrole=operator-controller-metrics-reader",
30+
"--serviceaccount="+namespace+":operator-controller-controller-manager")
31+
output, err := cmd.CombinedOutput()
32+
require.NoError(t, err, "Error creating ClusterRoleBinding: %s", string(output))
33+
34+
defer func() {
35+
t.Log("Cleaning up ClusterRoleBinding")
36+
_ = exec.Command("kubectl", "delete", "clusterrolebinding", "operator-controller-metrics-binding", "--ignore-not-found=true").Run()
37+
}()
38+
39+
t.Log("Generating ServiceAccount token")
40+
tokenCmd := exec.Command("kubectl", "create", "token", "operator-controller-controller-manager", "-n", namespace)
41+
tokenOutput, err := tokenCmd.Output()
42+
require.NoError(t, err, "Error creating token: %s", string(tokenOutput))
43+
token = string(bytes.TrimSpace(tokenOutput))
44+
45+
t.Log("Creating curl pod to validate the metrics endpoint")
46+
cmd = exec.Command("kubectl", "run", curlPod,
47+
"--image=curlimages/curl:7.87.0", "-n", namespace,
48+
"--restart=Never",
49+
"--overrides", `{
50+
"spec": {
51+
"containers": [{
52+
"name": "curl",
53+
"image": "curlimages/curl:7.87.0",
54+
"command": ["sh", "-c", "sleep 3600"],
55+
"securityContext": {
56+
"allowPrivilegeEscalation": false,
57+
"capabilities": {
58+
"drop": ["ALL"]
59+
},
60+
"runAsNonRoot": true,
61+
"runAsUser": 1000,
62+
"seccompProfile": {
63+
"type": "RuntimeDefault"
64+
}
65+
}
66+
}],
67+
"serviceAccountName": "operator-controller-controller-manager"
68+
}
69+
}`)
70+
output, err = cmd.CombinedOutput()
71+
require.NoError(t, err, "Error creating curl pod: %s", string(output))
72+
73+
defer func() {
74+
t.Log("Cleaning up curl pod")
75+
_ = exec.Command("kubectl", "delete", "pod", curlPod, "-n", namespace, "--ignore-not-found=true").Run()
76+
}()
77+
78+
t.Log("Waiting for the curl pod to be ready")
79+
waitCmd := exec.Command("kubectl", "wait", "--for=condition=Ready", "pod", curlPod, "-n", namespace, "--timeout=60s")
80+
waitOutput, waitErr := waitCmd.CombinedOutput()
81+
require.NoError(t, waitErr, "Error waiting for curl pod to be ready: %s", string(waitOutput))
82+
83+
t.Log("Validating the metrics endpoint")
84+
metricsURL := "https://operator-controller-controller-manager-metrics-service." + namespace + ".svc.cluster.local:8443/metrics"
85+
curlCmd := exec.Command("kubectl", "exec", curlPod, "-n", namespace, "--",
86+
"curl", "-v", "-k", "-H", "Authorization: Bearer "+token, metricsURL)
87+
output, err = curlCmd.CombinedOutput()
88+
require.NoError(t, err, "Error calling metrics endpoint: %s", string(output))
89+
require.Contains(t, string(output), "200 OK", "Metrics endpoint did not return 200 OK")
90+
}

0 commit comments

Comments
 (0)