Skip to content

Commit 854432d

Browse files
committed
Add check for client in e2e
Depending on how e2e is run, we may not have a k8s client available. Signed-off-by: Todd Short <[email protected]>
1 parent d3f267a commit 854432d

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

test/e2e/metrics_test.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,44 @@ func TestOperatorControllerMetricsExportedEndpoint(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 operator controller metrics")
29-
cmd := exec.Command("kubectl", "create", "clusterrolebinding", "operator-controller-metrics-binding",
45+
cmd := exec.Command(client, "create", "clusterrolebinding", "operator-controller-metrics-binding",
3046
"--clusterrole=operator-controller-metrics-reader",
3147
"--serviceaccount="+namespace+":operator-controller-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", "operator-controller-metrics-binding", "--ignore-not-found=true").Run()
53+
_ = exec.Command(client, "delete", "clusterrolebinding", "operator-controller-metrics-binding", "--ignore-not-found=true").Run()
3854
}()
3955

4056
t.Log("Generating ServiceAccount token")
41-
tokenCmd := exec.Command("kubectl", "create", "token", "operator-controller-controller-manager", "-n", namespace)
57+
tokenCmd := exec.Command(client, "create", "token", "operator-controller-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 curl pod to validate the metrics endpoint")
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 TestOperatorControllerMetricsExportedEndpoint(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 be 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://operator-controller-controller-manager-metrics-service." + namespace + ".svc.cluster.local:8443/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)