Skip to content
This repository was archived by the owner on Mar 3, 2025. It is now read-only.

Commit 0dbef9b

Browse files
authored
Add check for client in e2e (#485)
Depending on how the e2e is run, we may not have a k8s client available. Signed-off-by: Todd Short <[email protected]>
1 parent f91558f commit 0dbef9b

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

test/e2e/metrics_endpoint_test.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,44 @@ func TestCatalogdMetricsExportedEndpoint(t *testing.T) {
2323
token string
2424
curlPod = "curl-metrics"
2525
namespace = "olmv1-system"
26+
client = ""
27+
clients = []string{"kubectl", "oc"}
2628
)
2729

30+
t.Log("Looking for k8s client")
31+
for _, c := range clients {
32+
// Would prefer to use `command -v`, but even that may not be installed!
33+
err := exec.Command(c, "version", "--client").Run()
34+
if err == nil {
35+
client = c
36+
break
37+
}
38+
}
39+
if client == "" {
40+
t.Skip("k8s client not found, skipping test")
41+
}
42+
t.Logf("Using %q as k8s client", client)
43+
2844
t.Log("Creating ClusterRoleBinding for metrics access")
29-
cmd := exec.Command("kubectl", "create", "clusterrolebinding", "catalogd-metrics-binding",
45+
cmd := exec.Command(client, "create", "clusterrolebinding", "catalogd-metrics-binding",
3046
"--clusterrole=catalogd-metrics-reader",
3147
"--serviceaccount="+namespace+":catalogd-controller-manager")
3248
output, err := cmd.CombinedOutput()
3349
require.NoError(t, err, "Error creating ClusterRoleBinding: %s", string(output))
3450

3551
defer func() {
3652
t.Log("Cleaning up ClusterRoleBinding")
37-
_ = exec.Command("kubectl", "delete", "clusterrolebinding", "catalogd-metrics-binding", "--ignore-not-found=true").Run()
53+
_ = exec.Command(client, "delete", "clusterrolebinding", "catalogd-metrics-binding", "--ignore-not-found=true").Run()
3854
}()
3955

4056
t.Log("Creating service account token for authentication")
41-
tokenCmd := exec.Command("kubectl", "create", "token", "catalogd-controller-manager", "-n", namespace)
57+
tokenCmd := exec.Command(client, "create", "token", "catalogd-controller-manager", "-n", namespace)
4258
tokenOutput, err := tokenCmd.Output()
4359
require.NoError(t, err, "Error creating token: %s", string(tokenOutput))
4460
token = string(bytes.TrimSpace(tokenOutput))
4561

4662
t.Log("Creating a pod to run curl commands")
47-
cmd = exec.Command("kubectl", "run", curlPod,
63+
cmd = exec.Command(client, "run", curlPod,
4864
"--image=curlimages/curl:7.87.0", "-n", namespace,
4965
"--restart=Never",
5066
"--overrides", `{
@@ -73,17 +89,17 @@ func TestCatalogdMetricsExportedEndpoint(t *testing.T) {
7389

7490
defer func() {
7591
t.Log("Cleaning up curl pod")
76-
_ = exec.Command("kubectl", "delete", "pod", curlPod, "-n", namespace, "--ignore-not-found=true").Run()
92+
_ = exec.Command(client, "delete", "pod", curlPod, "-n", namespace, "--ignore-not-found=true").Run()
7793
}()
7894

7995
t.Log("Waiting for the curl pod to become ready")
80-
waitCmd := exec.Command("kubectl", "wait", "--for=condition=Ready", "pod", curlPod, "-n", namespace, "--timeout=60s")
96+
waitCmd := exec.Command(client, "wait", "--for=condition=Ready", "pod", curlPod, "-n", namespace, "--timeout=60s")
8197
waitOutput, waitErr := waitCmd.CombinedOutput()
8298
require.NoError(t, waitErr, "Error waiting for curl pod to be ready: %s", string(waitOutput))
8399

84100
t.Log("Validating the metrics endpoint")
85101
metricsURL := "https://catalogd-service.olmv1-system.svc.cluster.local:7443/metrics"
86-
curlCmd := exec.Command("kubectl", "exec", curlPod, "-n", namespace, "--",
102+
curlCmd := exec.Command(client, "exec", curlPod, "-n", namespace, "--",
87103
"curl", "-v", "-k", "-H", "Authorization: Bearer "+token, metricsURL)
88104
output, err = curlCmd.CombinedOutput()
89105
require.NoError(t, err, "Error calling metrics endpoint: %s", string(output))

0 commit comments

Comments
 (0)