@@ -2,6 +2,7 @@ package e2e
22
33import (
44 "bytes"
5+ "io"
56 "os/exec"
67 "testing"
78
@@ -20,11 +21,10 @@ import (
2021// 6. Cleans up all resources created during the test, such as the ClusterRoleBinding and curl pod.
2122func TestCatalogdMetricsExportedEndpoint (t * testing.T ) {
2223 var (
23- token string
24- curlPod = "curl-metrics"
25- namespace = "olmv1-system"
26- client = ""
27- clients = []string {"kubectl" , "oc" }
24+ token string
25+ curlPod = "curl-metrics"
26+ client = ""
27+ clients = []string {"kubectl" , "oc" }
2828 )
2929
3030 t .Log ("Looking for k8s client" )
@@ -37,15 +37,25 @@ func TestCatalogdMetricsExportedEndpoint(t *testing.T) {
3737 }
3838 }
3939 if client == "" {
40- t .Skip ("k8s client not found, skipping test " )
40+ t .Fatal ("k8s client not found" )
4141 }
4242 t .Logf ("Using %q as k8s client" , client )
4343
44+ t .Log ("Determining catalogd namespace" )
45+ cmd := exec .Command (client , "get" , "pods" , "--all-namespaces" , "--selector=control-plane=catalogd-controller-manager" , "--output=jsonpath={.items[0].metadata.namespace}" )
46+ output , err := cmd .CombinedOutput ()
47+ require .NoError (t , err , "Error creating determining catalogd namespace: %s" , string (output ))
48+ namespace := string (output )
49+ if namespace == "" {
50+ t .Fatal ("No catalogd namespace found" )
51+ }
52+ t .Logf ("Using %q as catalogd namespace" , namespace )
53+
4454 t .Log ("Creating ClusterRoleBinding for metrics access" )
45- cmd : = exec .Command (client , "create" , "clusterrolebinding" , "catalogd-metrics-binding" ,
55+ cmd = exec .Command (client , "create" , "clusterrolebinding" , "catalogd-metrics-binding" ,
4656 "--clusterrole=catalogd-metrics-reader" ,
4757 "--serviceaccount=" + namespace + ":catalogd-controller-manager" )
48- output , err : = cmd .CombinedOutput ()
58+ output , err = cmd .CombinedOutput ()
4959 require .NoError (t , err , "Error creating ClusterRoleBinding: %s" , string (output ))
5060
5161 defer func () {
@@ -55,8 +65,8 @@ func TestCatalogdMetricsExportedEndpoint(t *testing.T) {
5565
5666 t .Log ("Creating service account token for authentication" )
5767 tokenCmd := exec .Command (client , "create" , "token" , "catalogd-controller-manager" , "-n" , namespace )
58- tokenOutput , err := tokenCmd . Output ( )
59- require .NoError (t , err , "Error creating token: %s" , string (tokenOutput ))
68+ tokenOutput , tokenCombinedOutput , err := stdoutAndCombined ( tokenCmd )
69+ require .NoError (t , err , "Error creating token: %s" , string (tokenCombinedOutput ))
6070 token = string (bytes .TrimSpace (tokenOutput ))
6171
6272 t .Log ("Creating a pod to run curl commands" )
@@ -105,3 +115,13 @@ func TestCatalogdMetricsExportedEndpoint(t *testing.T) {
105115 require .NoError (t , err , "Error calling metrics endpoint: %s" , string (output ))
106116 require .Contains (t , string (output ), "200 OK" , "Metrics endpoint did not return 200 OK" )
107117}
118+
119+ func stdoutAndCombined (cmd * exec.Cmd ) ([]byte , []byte , error ) {
120+ var outOnly bytes.Buffer
121+ var outAndErr bytes.Buffer
122+ allWriter := io .MultiWriter (& outOnly , & outAndErr )
123+ cmd .Stderr = & outAndErr
124+ cmd .Stdout = allWriter
125+ err := cmd .Run ()
126+ return outOnly .Bytes (), outAndErr .Bytes (), err
127+ }
0 commit comments