@@ -23,28 +23,44 @@ func TestOperatorControllerMetricsExportedEndpoint(t *testing.T) {
23
23
token string
24
24
curlPod = "curl-metrics"
25
25
namespace = "olmv1-system"
26
+ client = ""
27
+ clients = []string {"kubectl" , "oc" }
26
28
)
27
29
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
+
28
44
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" ,
30
46
"--clusterrole=operator-controller-metrics-reader" ,
31
47
"--serviceaccount=" + namespace + ":operator-controller-controller-manager" )
32
48
output , err := cmd .CombinedOutput ()
33
49
require .NoError (t , err , "Error creating ClusterRoleBinding: %s" , string (output ))
34
50
35
51
defer func () {
36
52
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 ()
38
54
}()
39
55
40
56
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 )
42
58
tokenOutput , err := tokenCmd .Output ()
43
59
require .NoError (t , err , "Error creating token: %s" , string (tokenOutput ))
44
60
token = string (bytes .TrimSpace (tokenOutput ))
45
61
46
62
t .Log ("Creating curl pod to validate the metrics endpoint" )
47
- cmd = exec .Command ("kubectl" , "run" , curlPod ,
63
+ cmd = exec .Command (client , "run" , curlPod ,
48
64
"--image=curlimages/curl:7.87.0" , "-n" , namespace ,
49
65
"--restart=Never" ,
50
66
"--overrides" , `{
@@ -73,17 +89,17 @@ func TestOperatorControllerMetricsExportedEndpoint(t *testing.T) {
73
89
74
90
defer func () {
75
91
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 ()
77
93
}()
78
94
79
95
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" )
81
97
waitOutput , waitErr := waitCmd .CombinedOutput ()
82
98
require .NoError (t , waitErr , "Error waiting for curl pod to be ready: %s" , string (waitOutput ))
83
99
84
100
t .Log ("Validating the metrics endpoint" )
85
101
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 , "--" ,
87
103
"curl" , "-v" , "-k" , "-H" , "Authorization: Bearer " + token , metricsURL )
88
104
output , err = curlCmd .CombinedOutput ()
89
105
require .NoError (t , err , "Error calling metrics endpoint: %s" , string (output ))
0 commit comments