6
6
"context"
7
7
"fmt"
8
8
"regexp"
9
+ "time"
9
10
10
11
. "github.com/onsi/ginkgo"
11
12
. "github.com/onsi/gomega"
@@ -15,61 +16,172 @@ import (
15
16
"k8s.io/apimachinery/pkg/util/net"
16
17
17
18
"github.com/operator-framework/api/pkg/operators/v1alpha1"
19
+ "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
18
20
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
21
+ "github.com/operator-framework/operator-lifecycle-manager/pkg/metrics"
19
22
)
20
23
21
24
var _ = Describe ("Metrics" , func () {
22
- It ("endpoint" , func () {
23
-
24
- // TestMetrics tests the metrics endpoint of the OLM pod.
25
-
26
- c := newKubeClient ()
27
- crc := newCRClient ()
28
-
29
- failingCSV := v1alpha1.ClusterServiceVersion {
30
- TypeMeta : metav1.TypeMeta {
31
- Kind : v1alpha1 .ClusterServiceVersionKind ,
32
- APIVersion : v1alpha1 .ClusterServiceVersionAPIVersion ,
33
- },
34
- ObjectMeta : metav1.ObjectMeta {
35
- Name : genName ("failing-csv-test-" ),
36
- },
37
- Spec : v1alpha1.ClusterServiceVersionSpec {
38
- InstallStrategy : v1alpha1.NamedInstallStrategy {
39
- StrategyName : v1alpha1 .InstallStrategyNameDeployment ,
40
- StrategySpec : strategy ,
41
- },
42
- },
43
- }
44
25
45
- cleanupCSV , err := createCSV (GinkgoT (), c , crc , failingCSV , testNamespace , false , false )
46
- Expect (err ).ToNot (HaveOccurred ())
26
+ var (
27
+ c operatorclient.ClientInterface
28
+ crc versioned.Interface
29
+ )
47
30
48
- _ , err = fetchCSV (GinkgoT (), crc , failingCSV .Name , testNamespace , csvFailedChecker )
49
- Expect (err ).ToNot (HaveOccurred ())
31
+ BeforeEach (func () {
32
+ c = newKubeClient ()
33
+ crc = newCRClient ()
34
+
35
+ })
36
+
37
+ Context ("CSV metrics" , func () {
38
+ It ("endpoint" , func () {
39
+
40
+ // TestMetrics tests the metrics endpoint of the OLM pod.
41
+
42
+ failingCSV := v1alpha1.ClusterServiceVersion {
43
+ TypeMeta : metav1.TypeMeta {
44
+ Kind : v1alpha1 .ClusterServiceVersionKind ,
45
+ APIVersion : v1alpha1 .ClusterServiceVersionAPIVersion ,
46
+ },
47
+ ObjectMeta : metav1.ObjectMeta {
48
+ Name : genName ("failing-csv-test-" ),
49
+ },
50
+ Spec : v1alpha1.ClusterServiceVersionSpec {
51
+ InstallStrategy : v1alpha1.NamedInstallStrategy {
52
+ StrategyName : v1alpha1 .InstallStrategyNameDeployment ,
53
+ StrategySpec : strategy ,
54
+ },
55
+ },
56
+ }
57
+
58
+ cleanupCSV , err := createCSV (GinkgoT (), c , crc , failingCSV , testNamespace , false , false )
59
+ Expect (err ).ToNot (HaveOccurred ())
60
+
61
+ _ , err = fetchCSV (GinkgoT (), crc , failingCSV .Name , testNamespace , csvFailedChecker )
62
+ Expect (err ).ToNot (HaveOccurred ())
63
+
64
+ // Verify metrics have been emitted for packageserver csv
65
+ Expect (getMetricsFromPod (c , getPodWithLabel (c , "app=olm-operator" ), "8081" )).To (And (
66
+ ContainSubstring ("csv_abnormal" ),
67
+ ContainSubstring (fmt .Sprintf ("name=\" %s\" " , failingCSV .Name )),
68
+ ContainSubstring ("phase=\" Failed\" " ),
69
+ ContainSubstring ("reason=\" UnsupportedOperatorGroup\" " ),
70
+ ContainSubstring ("version=\" 0.0.0\" " ),
71
+ ContainSubstring ("csv_succeeded" ),
72
+ ))
73
+
74
+ cleanupCSV ()
75
+
76
+ // Verify that when the csv has been deleted, it deletes the corresponding CSV metrics
77
+ Expect (getMetricsFromPod (c , getPodWithLabel (c , "app=olm-operator" ), "8081" )).ToNot (And (
78
+ ContainSubstring ("csv_abnormal{name=\" %s\" " , failingCSV .Name ),
79
+ ContainSubstring ("csv_succeeded{name=\" %s\" " , failingCSV .Name ),
80
+ ))
81
+ })
82
+ })
50
83
51
- // Verify metrics have been emitted for packageserver csv
52
- Expect (getMetricsFromPod (c , getOLMPod (c ), "8081" )).To (And (
53
- ContainSubstring ("csv_abnormal" ),
54
- ContainSubstring (fmt .Sprintf ("name=\" %s\" " , failingCSV .Name )),
55
- ContainSubstring ("phase=\" Failed\" " ),
56
- ContainSubstring ("reason=\" UnsupportedOperatorGroup\" " ),
57
- ContainSubstring ("version=\" 0.0.0\" " ),
58
- ContainSubstring ("csv_succeeded" ),
59
- ))
60
-
61
- cleanupCSV ()
62
-
63
- // Verify that when the csv has been deleted, it deletes the corresponding CSV metrics
64
- Expect (getMetricsFromPod (c , getOLMPod (c ), "8081" )).ToNot (And (
65
- ContainSubstring ("csv_abnormal{name=\" %s\" " , failingCSV .Name ),
66
- ContainSubstring ("csv_succeeded{name=\" %s\" " , failingCSV .Name ),
67
- ))
84
+ Context ("Subscription Metric" , func () {
85
+ var (
86
+ subscriptionCleanup cleanupFunc
87
+ subscription * v1alpha1.Subscription
88
+ )
89
+ When ("A subscription object is created" , func () {
90
+
91
+ BeforeEach (func () {
92
+ subscriptionCleanup , subscription = createSubscription (GinkgoT (), crc , testNamespace , "metric-subscription" , testPackageName , stableChannel , v1alpha1 .ApprovalManual )
93
+ })
94
+
95
+ It ("generates subscription_sync_total metric" , func () {
96
+
97
+ // Verify metrics have been emitted for subscription
98
+ Eventually (func () string {
99
+ return getMetricsFromPod (c , getPodWithLabel (c , "app=catalog-operator" ), "8081" )
100
+ }, time .Minute , 5 * time .Second ).Should (And (
101
+ ContainSubstring ("subscription_sync_total" ),
102
+ ContainSubstring (fmt .Sprintf ("%s=\" %s\" " , metrics .NAME_LABEL , "metric-subscription" )),
103
+ ContainSubstring (fmt .Sprintf ("%s=\" %s\" " , metrics .CHANNEL_LABEL , stableChannel )),
104
+ ContainSubstring (fmt .Sprintf ("%s=\" %s\" " , metrics .PACKAGE_LABEL , testPackageName ))))
105
+ })
106
+
107
+ When ("The subscription object is updated" , func () {
108
+
109
+ BeforeEach (func () {
110
+ updatedSubscription , err := crc .OperatorsV1alpha1 ().Subscriptions (subscription .GetNamespace ()).Get (context .TODO (), subscription .GetName (), metav1.GetOptions {})
111
+ Expect (err ).ToNot (HaveOccurred ())
112
+ updatedSubscription .Spec .Channel = betaChannel
113
+ updateSubscription (GinkgoT (), crc , updatedSubscription )
114
+ })
115
+
116
+ It ("deletes the old Subscription metric and emits the new metric" , func () {
117
+ Eventually (func () string {
118
+ return getMetricsFromPod (c , getPodWithLabel (c , "app=catalog-operator" ), "8081" )
119
+ }, time .Minute , 5 * time .Second ).ShouldNot (And (
120
+ ContainSubstring ("subscription_sync_total{name=\" metric-subscription\" " ),
121
+ ContainSubstring (fmt .Sprintf ("%s=\" %s\" " , metrics .CHANNEL_LABEL , stableChannel ))))
122
+
123
+ Eventually (func () string {
124
+ return getMetricsFromPod (c , getPodWithLabel (c , "app=catalog-operator" ), "8081" )
125
+ }, time .Minute , 5 * time .Second ).Should (And (
126
+ ContainSubstring ("subscription_sync_total" ),
127
+ ContainSubstring (fmt .Sprintf ("%s=\" %s\" " , metrics .NAME_LABEL , "metric-subscription" )),
128
+ ContainSubstring (fmt .Sprintf ("%s=\" %s\" " , metrics .CHANNEL_LABEL , betaChannel )),
129
+ ContainSubstring (fmt .Sprintf ("%s=\" %s\" " , metrics .PACKAGE_LABEL , testPackageName ))))
130
+ })
131
+
132
+ When ("The Subscription object is updated again" , func () {
133
+
134
+ BeforeEach (func () {
135
+ updatedSubscription , err := crc .OperatorsV1alpha1 ().Subscriptions (subscription .GetNamespace ()).Get (context .TODO (), subscription .GetName (), metav1.GetOptions {})
136
+ Expect (err ).ToNot (HaveOccurred ())
137
+ updatedSubscription .Spec .Channel = alphaChannel
138
+ updateSubscription (GinkgoT (), crc , updatedSubscription )
139
+ })
140
+
141
+ It ("deletes the old subscription metric and emits the new metric(there is only one metric for the subscription)" , func () {
142
+ Eventually (func () string {
143
+ return getMetricsFromPod (c , getPodWithLabel (c , "app=catalog-operator" ), "8081" )
144
+ }, time .Minute , 5 * time .Second ).ShouldNot (And (
145
+ ContainSubstring ("subscription_sync_total{name=\" metric-subscription-for-update\" " ),
146
+ ContainSubstring (fmt .Sprintf ("%s=\" %s\" " , metrics .CHANNEL_LABEL , stableChannel )),
147
+ ContainSubstring (fmt .Sprintf ("%s=\" %s\" " , metrics .CHANNEL_LABEL , betaChannel ))))
148
+
149
+ Eventually (func () string {
150
+ return getMetricsFromPod (c , getPodWithLabel (c , "app=catalog-operator" ), "8081" )
151
+ }, time .Minute , 5 * time .Second ).Should (And (
152
+ ContainSubstring ("subscription_sync_total" ),
153
+ ContainSubstring (fmt .Sprintf ("%s=\" %s\" " , metrics .NAME_LABEL , "metric-subscription" )),
154
+ ContainSubstring (fmt .Sprintf ("%s=\" %s\" " , metrics .CHANNEL_LABEL , alphaChannel )),
155
+ ContainSubstring (fmt .Sprintf ("%s=\" %s\" " , metrics .PACKAGE_LABEL , testPackageName ))))
156
+ })
157
+ })
158
+ })
159
+
160
+ AfterEach (func () {
161
+ if subscriptionCleanup != nil {
162
+ subscriptionCleanup ()
163
+ }
164
+ })
165
+ })
166
+ When ("A Subscription object is deleted" , func () {
167
+
168
+ BeforeEach (func () {
169
+ subscriptionCleanup , _ = createSubscription (GinkgoT (), crc , testNamespace , "metric-subscription-for-deletion" , testPackageName , stableChannel , v1alpha1 .ApprovalManual )
170
+ if subscriptionCleanup != nil {
171
+ subscriptionCleanup ()
172
+ }
173
+ })
174
+ It ("deletes the Subscription metric" , func () {
175
+ Eventually (func () string {
176
+ return getMetricsFromPod (c , getPodWithLabel (c , "app=catalog-operator" ), "8081" )
177
+ }, time .Minute , 5 * time .Second ).ShouldNot (ContainSubstring ("subscription_sync_total{name=\" metric-subscription-for-deletion\" " ))
178
+ })
179
+ })
68
180
})
69
181
})
70
182
71
- func getOLMPod (client operatorclient.ClientInterface ) * corev1.Pod {
72
- listOptions := metav1.ListOptions {LabelSelector : "app=olm-operator" }
183
+ func getPodWithLabel (client operatorclient.ClientInterface , label string ) * corev1.Pod {
184
+ listOptions := metav1.ListOptions {LabelSelector : label }
73
185
var podList * corev1.PodList
74
186
Eventually (func () (err error ) {
75
187
podList , err = client .KubernetesInterface ().CoreV1 ().Pods (operatorNamespace ).List (context .TODO (), listOptions )
0 commit comments