@@ -23,14 +23,17 @@ import (
2323 "crypto/x509"
2424 "crypto/x509/pkix"
2525 "encoding/pem"
26+ "fmt"
2627 "math/big"
2728 "net"
2829 "os"
2930 "time"
3031
3132 . "github.com/onsi/ginkgo"
3233 . "github.com/onsi/gomega"
34+ "github.com/prometheus/client_golang/prometheus/testutil"
3335 "sigs.k8s.io/controller-runtime/pkg/certwatcher"
36+ "sigs.k8s.io/controller-runtime/pkg/certwatcher/metrics"
3437)
3538
3639var _ = Describe ("CertWatcher" , func () {
@@ -109,6 +112,64 @@ var _ = Describe("CertWatcher", func() {
109112 ctxCancel ()
110113 Eventually (doneCh , "4s" ).Should (BeClosed ())
111114 })
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+ })
112173 })
113174})
114175
0 commit comments