Skip to content

Commit 61fe142

Browse files
e2e: add tests to validate metrics endpoint (#1522)
1 parent d1db17f commit 61fe142

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

test/e2e/metrics_test.go

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

0 commit comments

Comments
 (0)