3
3
package e2e
4
4
5
5
import (
6
+ "strings"
6
7
"testing"
8
+ "time"
7
9
8
10
log "github.com/sirupsen/logrus"
9
11
"github.com/stretchr/testify/require"
12
+ corev1 "k8s.io/api/core/v1"
10
13
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11
14
"k8s.io/apimachinery/pkg/util/net"
15
+ "k8s.io/apimachinery/pkg/util/wait"
12
16
13
17
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
14
18
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
15
19
)
16
20
17
- // TestMetrics tests the metrics endpoint of the OLM pod.
18
- func TestMetricsEndpoint (t * testing.T ) {
21
+ const (
22
+ // RetryInterval defines the frequency at which we check for updates against the
23
+ // k8s api when waiting for a specific condition to be true.
24
+ RetryInterval = time .Second * 5
25
+
26
+ // Timeout defines the amount of time we should spend querying the k8s api
27
+ // when waiting for a specific condition to be true.
28
+ Timeout = time .Minute * 5
29
+ )
30
+
31
+ // TestCSVMetrics tests the metrics endpoint of the OLM pod for metrics emitted by CSVs.
32
+ func TestCSVMetrics (t * testing.T ) {
19
33
c := newKubeClient (t )
20
34
crc := newCRClient (t )
21
35
@@ -41,7 +55,7 @@ func TestMetricsEndpoint(t *testing.T) {
41
55
_ , err = fetchCSV (t , crc , failingCSV .Name , testNamespace , csvFailedChecker )
42
56
require .NoError (t , err )
43
57
44
- rawOutput , err := getMetricsFromPod (t , c , getOLMPodName (t , c ), operatorNamespace , "8081" )
58
+ rawOutput , err := getMetricsFromPod (t , c , getPodWithLabel (t , c , "app=olm-operator" ), operatorNamespace , "8081" )
45
59
if err != nil {
46
60
t .Fatalf ("Metrics test failed: %v\n " , err )
47
61
}
@@ -56,16 +70,128 @@ func TestMetricsEndpoint(t *testing.T) {
56
70
57
71
cleanupCSV ()
58
72
59
- rawOutput , err = getMetricsFromPod (t , c , getOLMPodName (t , c ), operatorNamespace , "8081" )
73
+ rawOutput , err = getMetricsFromPod (t , c , getPodWithLabel (t , c , "app=olm-operator" ), operatorNamespace , "8081" )
60
74
if err != nil {
61
75
t .Fatalf ("Failed to retrieve metrics from OLM pod because of: %v\n " , err )
62
76
}
63
77
require .NotContains (t , rawOutput , "csv_abnormal{name=\" " + failingCSV .Name + "\" " )
64
78
require .NotContains (t , rawOutput , "csv_succeeded{name=\" " + failingCSV .Name + "\" " )
65
79
}
66
80
67
- func getOLMPodName (t * testing.T , client operatorclient.ClientInterface ) string {
68
- listOptions := metav1.ListOptions {LabelSelector : "app=olm-operator" }
81
+ func TestSubscriptionMetrics (t * testing.T ) {
82
+ c := newKubeClient (t )
83
+ crc := newCRClient (t )
84
+
85
+ subscriptionCleanup , subscription := createSubscription (t , crc , testNamespace , "metric-subscription" , testPackageName , stableChannel , v1alpha1 .ApprovalManual )
86
+
87
+ err := wait .PollImmediate (RetryInterval , Timeout , func () (done bool , err error ) {
88
+ rawOutput , err := getMetricsFromPod (t , c , getPodWithLabel (t , c , "app=catalog-operator" ), operatorNamespace , "8081" )
89
+ if err != nil {
90
+ return false , err
91
+ }
92
+ if strings .Contains (rawOutput , "subscription_sync_total" ) &&
93
+ strings .Contains (rawOutput , "name=\" metric-subscription\" " ) &&
94
+ strings .Contains (rawOutput , "channel=\" " + stableChannel + "\" " ) &&
95
+ strings .Contains (rawOutput , "package=\" " + testPackageName + "\" " ) {
96
+ return true , nil
97
+ }
98
+ return false , nil
99
+ })
100
+ require .NoError (t , err )
101
+
102
+ updatedSubscription , err := crc .OperatorsV1alpha1 ().Subscriptions (subscription .GetNamespace ()).Get (subscription .GetName (), metav1.GetOptions {})
103
+ require .NoError (t , err )
104
+
105
+ updatedSubscription .Spec .Channel = betaChannel
106
+ updateSubscription (t , crc , updatedSubscription )
107
+
108
+ err = wait .PollImmediate (RetryInterval , Timeout , func () (done bool , err error ) {
109
+ rawOutput , err := getMetricsFromPod (t , c , getPodWithLabel (t , c , "app=catalog-operator" ), operatorNamespace , "8081" )
110
+ if err != nil {
111
+ return false , err
112
+ }
113
+ if strings .Contains (rawOutput , "subscription_sync_total" ) &&
114
+ strings .Contains (rawOutput , "name=\" metric-subscription\" " ) &&
115
+ strings .Contains (rawOutput , "channel=\" " + stableChannel + "\" " ) &&
116
+ strings .Contains (rawOutput , "package=\" " + testPackageName + "\" " ) {
117
+ return false , nil
118
+ }
119
+ return true , nil
120
+ })
121
+ require .NoError (t , err )
122
+
123
+ err = wait .PollImmediate (RetryInterval , Timeout , func () (done bool , err error ) {
124
+ rawOutput , err := getMetricsFromPod (t , c , getPodWithLabel (t , c , "app=catalog-operator" ), operatorNamespace , "8081" )
125
+ if err != nil {
126
+ return false , err
127
+ }
128
+ if strings .Contains (rawOutput , "subscription_sync_total" ) &&
129
+ strings .Contains (rawOutput , "name=\" metric-subscription\" " ) &&
130
+ strings .Contains (rawOutput , "channel=\" " + betaChannel + "\" " ) &&
131
+ strings .Contains (rawOutput , "package=\" " + testPackageName + "\" " ) {
132
+ return true , nil
133
+ }
134
+ return false , nil
135
+ })
136
+ require .NoError (t , err )
137
+
138
+ updatedSubscription , err = crc .OperatorsV1alpha1 ().Subscriptions (subscription .GetNamespace ()).Get (subscription .GetName (), metav1.GetOptions {})
139
+ require .NoError (t , err )
140
+
141
+ updatedSubscription .Spec .Channel = alphaChannel
142
+ updateSubscription (t , crc , updatedSubscription )
143
+
144
+ err = wait .PollImmediate (RetryInterval , Timeout , func () (done bool , err error ) {
145
+ rawOutput , err := getMetricsFromPod (t , c , getPodWithLabel (t , c , "app=catalog-operator" ), operatorNamespace , "8081" )
146
+ if err != nil {
147
+ return false , err
148
+ }
149
+ if strings .Contains (rawOutput , "subscription_sync_total" ) &&
150
+ strings .Contains (rawOutput , "name=\" metric-subscription\" " ) &&
151
+ strings .Contains (rawOutput , "channel=\" " + betaChannel + "\" " ) &&
152
+ strings .Contains (rawOutput , "package=\" " + testPackageName + "\" " ) {
153
+ return false , nil
154
+ }
155
+ return true , nil
156
+ })
157
+ require .NoError (t , err )
158
+
159
+ err = wait .PollImmediate (RetryInterval , Timeout , func () (done bool , err error ) {
160
+ rawOutput , err := getMetricsFromPod (t , c , getPodWithLabel (t , c , "app=catalog-operator" ), operatorNamespace , "8081" )
161
+ if err != nil {
162
+ return false , err
163
+ }
164
+ if strings .Contains (rawOutput , "subscription_sync_total" ) &&
165
+ strings .Contains (rawOutput , "name=\" metric-subscription\" " ) &&
166
+ strings .Contains (rawOutput , "channel=\" " + alphaChannel + "\" " ) &&
167
+ strings .Contains (rawOutput , "package=\" " + testPackageName + "\" " ) {
168
+ return true , nil
169
+ }
170
+ return false , nil
171
+ })
172
+ require .NoError (t , err )
173
+
174
+ if subscriptionCleanup != nil {
175
+ subscriptionCleanup ()
176
+ }
177
+ err = wait .PollImmediate (RetryInterval , Timeout , func () (done bool , err error ) {
178
+ rawOutput , err := getMetricsFromPod (t , c , getPodWithLabel (t , c , "app=catalog-operator" ), operatorNamespace , "8081" )
179
+ if err != nil {
180
+ return false , err
181
+ }
182
+ if strings .Contains (rawOutput , "subscription_sync_total" ) &&
183
+ strings .Contains (rawOutput , "name=metric-subscription" ) &&
184
+ strings .Contains (rawOutput , "channel=\" " + alphaChannel + "\" " ) &&
185
+ strings .Contains (rawOutput , "package=\" " + testPackageName + "\" " ) {
186
+ return false , nil
187
+ }
188
+ return true , nil
189
+ })
190
+ require .NoError (t , err )
191
+ }
192
+
193
+ func getPodWithLabel (t * testing.T , client operatorclient.ClientInterface , label string ) * corev1.Pod {
194
+ listOptions := metav1.ListOptions {LabelSelector : label }
69
195
podList , err := client .KubernetesInterface ().CoreV1 ().Pods (operatorNamespace ).List (listOptions )
70
196
if err != nil {
71
197
log .Infof ("Error %v\n " , err )
@@ -74,15 +200,11 @@ func getOLMPodName(t *testing.T, client operatorclient.ClientInterface) string {
74
200
if len (podList .Items ) != 1 {
75
201
t .Fatalf ("Expected 1 olm-operator pod, got %v" , len (podList .Items ))
76
202
}
77
-
78
- podName := podList .Items [0 ].GetName ()
79
- log .Infof ("Looking at pod %v in namespace %v" , podName , operatorNamespace )
80
- return podName
81
-
203
+ return & podList .Items [0 ]
82
204
}
83
205
84
- func getMetricsFromPod (t * testing.T , client operatorclient.ClientInterface , podName string , namespace string , port string ) (string , error ) {
85
- olmPod , err := client .KubernetesInterface ().CoreV1 ().Pods (namespace ).Get (podName , metav1.GetOptions {})
206
+ func getMetricsFromPod (t * testing.T , client operatorclient.ClientInterface , pod * corev1. Pod , namespace string , port string ) (string , error ) {
207
+ olmPod , err := client .KubernetesInterface ().CoreV1 ().Pods (namespace ).Get (pod . GetName () , metav1.GetOptions {})
86
208
if err != nil {
87
209
return "" , err
88
210
}
@@ -113,7 +235,7 @@ func getMetricsFromPod(t *testing.T, client operatorclient.ClientInterface, podN
113
235
Namespace (namespace ).
114
236
Resource ("pods" ).
115
237
SubResource ("proxy" ).
116
- Name (net .JoinSchemeNamePort (scheme , podName , port )).
238
+ Name (net .JoinSchemeNamePort (scheme , pod . GetName () , port )).
117
239
Suffix ("metrics" ).
118
240
Do ().Raw ()
119
241
if err != nil {
0 commit comments