@@ -2,7 +2,9 @@ package e2e
22
33import (
44 "bytes"
5+ "io"
56 "os/exec"
7+ "strings"
68 "testing"
79
810 "github.com/stretchr/testify/require"
@@ -20,11 +22,10 @@ import (
2022// 6. Cleans up all resources created during the test, such as the ClusterRoleBinding and curl pod.
2123func TestOperatorControllerMetricsExportedEndpoint (t * testing.T ) {
2224 var (
23- token string
24- curlPod = "curl-metrics"
25- namespace = "olmv1-system"
26- client = ""
27- clients = []string {"kubectl" , "oc" }
25+ token string
26+ curlPod = "curl-metrics"
27+ client = ""
28+ clients = []string {"kubectl" , "oc" }
2829 )
2930
3031 t .Log ("Looking for k8s client" )
@@ -41,11 +42,17 @@ func TestOperatorControllerMetricsExportedEndpoint(t *testing.T) {
4142 }
4243 t .Logf ("Using %q as k8s client" , client )
4344
45+ t .Log ("Determining operator-controller namespace" )
46+ cmd := exec .Command (client , "get" , "pods" , "--all-namespaces" , "--selector=control-plane=operator-controller-controller-manager" , "--output" )
47+ output , err := cmd .CombinedOutput ()
48+ require .NoError (t , err , "Error creating determining operator-controller namespace: %s" , string (output ))
49+ namespace := strings .Split (string (output ), "\n " )[0 ]
50+
4451 t .Log ("Creating ClusterRoleBinding for operator controller metrics" )
45- cmd : = exec .Command (client , "create" , "clusterrolebinding" , "operator-controller-metrics-binding" ,
52+ cmd = exec .Command (client , "create" , "clusterrolebinding" , "operator-controller-metrics-binding" ,
4653 "--clusterrole=operator-controller-metrics-reader" ,
4754 "--serviceaccount=" + namespace + ":operator-controller-controller-manager" )
48- output , err : = cmd .CombinedOutput ()
55+ output , err = cmd .CombinedOutput ()
4956 require .NoError (t , err , "Error creating ClusterRoleBinding: %s" , string (output ))
5057
5158 defer func () {
@@ -55,8 +62,8 @@ func TestOperatorControllerMetricsExportedEndpoint(t *testing.T) {
5562
5663 t .Log ("Generating ServiceAccount token" )
5764 tokenCmd := exec .Command (client , "create" , "token" , "operator-controller-controller-manager" , "-n" , namespace )
58- tokenOutput , err := tokenCmd . Output ( )
59- require .NoError (t , err , "Error creating token: %s" , string (tokenOutput ))
65+ tokenOutput , tokenCombinedOutput , err := stdoutAndCombined ( tokenCmd )
66+ require .NoError (t , err , "Error creating token: %s" , string (tokenCombinedOutput ))
6067 token = string (bytes .TrimSpace (tokenOutput ))
6168
6269 t .Log ("Creating curl pod to validate the metrics endpoint" )
@@ -105,3 +112,13 @@ func TestOperatorControllerMetricsExportedEndpoint(t *testing.T) {
105112 require .NoError (t , err , "Error calling metrics endpoint: %s" , string (output ))
106113 require .Contains (t , string (output ), "200 OK" , "Metrics endpoint did not return 200 OK" )
107114}
115+
116+ func stdoutAndCombined (cmd * exec.Cmd ) ([]byte , []byte , error ) {
117+ var outOnly bytes.Buffer
118+ var outAndErr bytes.Buffer
119+ allWriter := io .MultiWriter (& outOnly , & outAndErr )
120+ cmd .Stderr = & outAndErr
121+ cmd .Stdout = allWriter
122+ err := cmd .Run ()
123+ return outOnly .Bytes (), outAndErr .Bytes (), err
124+ }
0 commit comments