Skip to content

Commit c89fae9

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 <tshort@redhat.com>
1 parent d3f267a commit c89fae9

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

test/e2e/metrics_test.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,43 @@ 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+
err := exec.Command("command", "-v", c).Run()
33+
if err == nil {
34+
client = c
35+
break
36+
}
37+
}
38+
if client == "" {
39+
t.Skip("k8s client not found, skipping test")
40+
}
41+
t.Logf("Using %q as k8s client", client)
42+
2843
t.Log("Creating ClusterRoleBinding for operator controller metrics")
29-
cmd := exec.Command("kubectl", "create", "clusterrolebinding", "operator-controller-metrics-binding",
44+
cmd := exec.Command(client, "create", "clusterrolebinding", "operator-controller-metrics-binding",
3045
"--clusterrole=operator-controller-metrics-reader",
3146
"--serviceaccount="+namespace+":operator-controller-controller-manager")
3247
output, err := cmd.CombinedOutput()
3348
require.NoError(t, err, "Error creating ClusterRoleBinding: %s", string(output))
3449

3550
defer func() {
3651
t.Log("Cleaning up ClusterRoleBinding")
37-
_ = exec.Command("kubectl", "delete", "clusterrolebinding", "operator-controller-metrics-binding", "--ignore-not-found=true").Run()
52+
_ = exec.Command(client, "delete", "clusterrolebinding", "operator-controller-metrics-binding", "--ignore-not-found=true").Run()
3853
}()
3954

4055
t.Log("Generating ServiceAccount token")
41-
tokenCmd := exec.Command("kubectl", "create", "token", "operator-controller-controller-manager", "-n", namespace)
56+
tokenCmd := exec.Command(client, "create", "token", "operator-controller-controller-manager", "-n", namespace)
4257
tokenOutput, err := tokenCmd.Output()
4358
require.NoError(t, err, "Error creating token: %s", string(tokenOutput))
4459
token = string(bytes.TrimSpace(tokenOutput))
4560

4661
t.Log("Creating curl pod to validate the metrics endpoint")
47-
cmd = exec.Command("kubectl", "run", curlPod,
62+
cmd = exec.Command(client, "run", curlPod,
4863
"--image=curlimages/curl:7.87.0", "-n", namespace,
4964
"--restart=Never",
5065
"--overrides", `{
@@ -73,17 +88,17 @@ func TestOperatorControllerMetricsExportedEndpoint(t *testing.T) {
7388

7489
defer func() {
7590
t.Log("Cleaning up curl pod")
76-
_ = exec.Command("kubectl", "delete", "pod", curlPod, "-n", namespace, "--ignore-not-found=true").Run()
91+
_ = exec.Command(client, "delete", "pod", curlPod, "-n", namespace, "--ignore-not-found=true").Run()
7792
}()
7893

7994
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")
95+
waitCmd := exec.Command(client, "wait", "--for=condition=Ready", "pod", curlPod, "-n", namespace, "--timeout=60s")
8196
waitOutput, waitErr := waitCmd.CombinedOutput()
8297
require.NoError(t, waitErr, "Error waiting for curl pod to be ready: %s", string(waitOutput))
8398

8499
t.Log("Validating the metrics endpoint")
85100
metricsURL := "https://operator-controller-controller-manager-metrics-service." + namespace + ".svc.cluster.local:8443/metrics"
86-
curlCmd := exec.Command("kubectl", "exec", curlPod, "-n", namespace, "--",
101+
curlCmd := exec.Command(client, "exec", curlPod, "-n", namespace, "--",
87102
"curl", "-v", "-k", "-H", "Authorization: Bearer "+token, metricsURL)
88103
output, err = curlCmd.CombinedOutput()
89104
require.NoError(t, err, "Error calling metrics endpoint: %s", string(output))

0 commit comments

Comments
 (0)