@@ -4,12 +4,16 @@ package e2e
44
55import (
66 "context"
7+ "fmt"
8+ "regexp"
9+
10+ . "github.com/onsi/ginkgo"
11+ . "github.com/onsi/gomega"
712 log "github.com/sirupsen/logrus"
8- "github.com/stretchr/testify/require "
13+ corev1 "k8s.io/api/core/v1 "
914 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1015 "k8s.io/apimachinery/pkg/util/net"
1116
12- . "github.com/onsi/ginkgo"
1317 "github.com/operator-framework/api/pkg/operators/v1alpha1"
1418 "github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
1519)
@@ -39,64 +43,49 @@ var _ = Describe("Metrics", func() {
3943 }
4044
4145 cleanupCSV , err := createCSV (GinkgoT (), c , crc , failingCSV , testNamespace , false , false )
42- require . NoError ( GinkgoT (), err )
46+ Expect ( err ). ToNot ( HaveOccurred () )
4347 defer cleanupCSV ()
4448
4549 _ , err = fetchCSV (GinkgoT (), crc , failingCSV .Name , testNamespace , csvFailedChecker )
46- require .NoError (GinkgoT (), err )
47-
48- rawOutput , err := getMetricsFromPod (GinkgoT (), c , getOLMPodName (GinkgoT (), c ), operatorNamespace , "8081" )
49- if err != nil {
50- GinkgoT ().Fatalf ("Metrics test failed: %v\n " , err )
51- }
50+ Expect (err ).ToNot (HaveOccurred ())
5251
5352 // Verify metrics have been emitted for packageserver csv
54- require . Contains ( GinkgoT (), rawOutput , "csv_abnormal" )
55- require . Contains ( GinkgoT (), rawOutput , "name= \" " + failingCSV . Name + " \" " )
56- require . Contains ( GinkgoT (), rawOutput , "phase =\" Failed \" ")
57- require . Contains ( GinkgoT (), rawOutput , "reason =\" UnsupportedOperatorGroup \" " )
58- require . Contains ( GinkgoT (), rawOutput , "version =\" 0.0.0 \" " )
59-
60- require . Contains ( GinkgoT (), rawOutput , "csv_succeeded" )
61- log . Info ( rawOutput )
53+ Expect ( getMetricsFromPod ( c , getOLMPod ( c ) , "8081" )). To ( And (
54+ ContainSubstring ( "csv_abnormal" ),
55+ ContainSubstring ( fmt . Sprintf ( "name =\" %s \" ", failingCSV . Name )),
56+ ContainSubstring ( "phase =\" Failed \" " ),
57+ ContainSubstring ( "reason =\" UnsupportedOperatorGroup \" " ),
58+ ContainSubstring ( "version= \" 0.0.0 \" " ),
59+ ContainSubstring ( "csv_succeeded" ),
60+ ) )
6261 })
6362})
6463
65- func getOLMPodName ( t GinkgoTInterface , client operatorclient.ClientInterface ) string {
64+ func getOLMPod ( client operatorclient.ClientInterface ) * corev1. Pod {
6665 listOptions := metav1.ListOptions {LabelSelector : "app=olm-operator" }
67- podList , err := client .KubernetesInterface ().CoreV1 ().Pods (operatorNamespace ).List (context .TODO (), listOptions )
68- if err != nil {
69- log .Infof ("Error %v\n " , err )
70- t .Fatalf ("Listing pods failed: %v\n " , err )
71- }
72- if len (podList .Items ) != 1 {
73- t .Fatalf ("Expected 1 olm-operator pod, got %v" , len (podList .Items ))
74- }
75-
76- podName := podList .Items [0 ].GetName ()
77- log .Infof ("Looking at pod %v in namespace %v" , podName , operatorNamespace )
78- return podName
79-
66+ var podList * corev1.PodList
67+ Eventually (func () (err error ) {
68+ podList , err = client .KubernetesInterface ().CoreV1 ().Pods (operatorNamespace ).List (context .TODO (), listOptions )
69+ return
70+ }).Should (Succeed (), "Failed to list OLM pods" )
71+ Expect (len (podList .Items )).To (Equal (1 ))
72+
73+ return & podList .Items [0 ]
8074}
8175
82- func getMetricsFromPod (t GinkgoTInterface , client operatorclient.ClientInterface , podName string , namespace string , port string ) (string , error ) {
83- olmPod , err := client .KubernetesInterface ().CoreV1 ().Pods (namespace ).Get (context .TODO (), podName , metav1.GetOptions {})
84- if err != nil {
85- return "" , err
86- }
87- if len (olmPod .Spec .Containers ) != 1 {
88- t .Fatalf ("Expected only 1 container in olm-operator pod, got %v" , len (olmPod .Spec .Containers ))
89- }
76+ func getMetricsFromPod (client operatorclient.ClientInterface , pod * corev1.Pod , port string ) string {
77+ By (fmt .Sprintf ("querying pod %s/%s" , pod .GetNamespace (), pod .GetName ()))
9078
91- var foundCert bool
92- var foundKey bool
9379 // assuming -tls-cert and -tls-key aren't used anywhere else as a parameter value
94- for _ , param := range olmPod .Spec .Containers [0 ].Args {
95- if param == "-tls-cert" {
96- foundCert = true
97- } else if param == "-tls-key" {
98- foundKey = true
99- }
80+ var foundCert , foundKey bool
81+ for _ , arg := range pod .Spec .Containers [0 ].Args {
82+ matched , err := regexp .MatchString (`^-?-tls-cert` , arg )
83+ Expect (err ).ToNot (HaveOccurred ())
84+ foundCert = foundCert || matched
85+
86+ matched , err = regexp .MatchString (`^-?-tls-key` , arg )
87+ Expect (err ).ToNot (HaveOccurred ())
88+ foundKey = foundKey || matched
10089 }
10190
10291 var scheme string
@@ -107,15 +96,17 @@ func getMetricsFromPod(t GinkgoTInterface, client operatorclient.ClientInterface
10796 }
10897 log .Infof ("Retrieving metrics using scheme %v\n " , scheme )
10998
110- rawOutput , err := client .KubernetesInterface ().CoreV1 ().RESTClient ().Get ().
111- Namespace (namespace ).
112- Resource ("pods" ).
113- SubResource ("proxy" ).
114- Name (net .JoinSchemeNamePort (scheme , podName , port )).
115- Suffix ("metrics" ).
116- Do (context .TODO ()).Raw ()
117- if err != nil {
118- return "" , err
119- }
120- return string (rawOutput ), nil
99+ var raw []byte
100+ Eventually (func () (err error ) {
101+ raw , err = client .KubernetesInterface ().CoreV1 ().RESTClient ().Get ().
102+ Namespace (pod .GetNamespace ()).
103+ Resource ("pods" ).
104+ SubResource ("proxy" ).
105+ Name (net .JoinSchemeNamePort (scheme , pod .GetName (), port )).
106+ Suffix ("metrics" ).
107+ Do (context .Background ()).Raw ()
108+ return
109+ }).Should (Succeed ())
110+
111+ return string (raw )
121112}
0 commit comments