@@ -23,14 +23,17 @@ import (
23
23
"crypto/x509"
24
24
"crypto/x509/pkix"
25
25
"encoding/pem"
26
+ "fmt"
26
27
"math/big"
27
28
"net"
28
29
"os"
29
30
"time"
30
31
31
32
. "github.com/onsi/ginkgo"
32
33
. "github.com/onsi/gomega"
34
+ "github.com/prometheus/client_golang/prometheus/testutil"
33
35
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
36
+ "sigs.k8s.io/controller-runtime/pkg/certwatcher/metrics"
34
37
)
35
38
36
39
var _ = Describe ("CertWatcher" , func () {
@@ -109,6 +112,64 @@ var _ = Describe("CertWatcher", func() {
109
112
ctxCancel ()
110
113
Eventually (doneCh , "4s" ).Should (BeClosed ())
111
114
})
115
+
116
+ Context ("prometheus metric read_certificate_total" , func () {
117
+ var readCertificateTotalBefore float64
118
+ var readCertificateErrorsBefore float64
119
+
120
+ BeforeEach (func () {
121
+ readCertificateTotalBefore = testutil .ToFloat64 (metrics .ReadCertificateTotal )
122
+ readCertificateErrorsBefore = testutil .ToFloat64 (metrics .ReadCertificateErrors )
123
+ })
124
+
125
+ It ("should get updated on successful certificate read" , func () {
126
+ doneCh := startWatcher ()
127
+
128
+ Eventually (func () error {
129
+ readCertificateTotalAfter := testutil .ToFloat64 (metrics .ReadCertificateTotal )
130
+ if readCertificateTotalAfter != readCertificateTotalBefore + 1.0 {
131
+ return fmt .Errorf ("metric read certificate total expected: %v and got: %v" , readCertificateTotalBefore + 1.0 , readCertificateTotalAfter )
132
+ }
133
+ return nil
134
+ }, "4s" ).Should (Succeed ())
135
+
136
+ ctxCancel ()
137
+ Eventually (doneCh , "4s" ).Should (BeClosed ())
138
+ })
139
+
140
+ It ("should get updated on read certificate errors" , func () {
141
+ doneCh := startWatcher ()
142
+
143
+ Eventually (func () error {
144
+ readCertificateTotalAfter := testutil .ToFloat64 (metrics .ReadCertificateTotal )
145
+ if readCertificateTotalAfter != readCertificateTotalBefore + 1.0 {
146
+ return fmt .Errorf ("metric read certificate total expected: %v and got: %v" , readCertificateTotalBefore + 1.0 , readCertificateTotalAfter )
147
+ }
148
+ readCertificateTotalBefore = readCertificateTotalAfter
149
+ return nil
150
+ }, "4s" ).Should (Succeed ())
151
+
152
+ Expect (os .Remove (keyPath )).To (BeNil ())
153
+
154
+ Eventually (func () error {
155
+ readCertificateTotalAfter := testutil .ToFloat64 (metrics .ReadCertificateTotal )
156
+ if readCertificateTotalAfter != readCertificateTotalBefore + 1.0 {
157
+ return fmt .Errorf ("metric read certificate total expected: %v and got: %v" , readCertificateTotalBefore + 1.0 , readCertificateTotalAfter )
158
+ }
159
+ return nil
160
+ }, "4s" ).Should (Succeed ())
161
+ Eventually (func () error {
162
+ readCertificateErrorsAfter := testutil .ToFloat64 (metrics .ReadCertificateErrors )
163
+ if readCertificateErrorsAfter != readCertificateErrorsBefore + 1.0 {
164
+ return fmt .Errorf ("metric read certificate errors expected: %v and got: %v" , readCertificateErrorsBefore + 1.0 , readCertificateErrorsAfter )
165
+ }
166
+ return nil
167
+ }, "4s" ).Should (Succeed ())
168
+
169
+ ctxCancel ()
170
+ Eventually (doneCh , "4s" ).Should (BeClosed ())
171
+ })
172
+ })
112
173
})
113
174
})
114
175
0 commit comments