@@ -121,6 +121,36 @@ var _ = Describe("CertWatcher", func() {
121121 Expect (called .Load ()).To (BeNumerically (">=" , 1 ))
122122 })
123123
124+ It ("should reload currentCert when changed with rename" , func () {
125+ doneCh := startWatcher ()
126+ called := atomic.Int64 {}
127+ watcher .RegisterCallback (func (crt tls.Certificate ) {
128+ called .Add (1 )
129+ Expect (crt .Certificate ).ToNot (BeEmpty ())
130+ })
131+
132+ firstcert , _ := watcher .GetCertificate (nil )
133+
134+ err := writeCerts (certPath + ".new" , keyPath + ".new" , "192.168.0.2" )
135+ Expect (err ).ToNot (HaveOccurred ())
136+
137+ Expect (os .Link (certPath , certPath + ".old" )).To (Succeed ())
138+ Expect (os .Rename (certPath + ".new" , certPath )).To (Succeed ())
139+
140+ Expect (os .Link (keyPath , keyPath + ".old" )).To (Succeed ())
141+ Expect (os .Rename (keyPath + ".new" , keyPath )).To (Succeed ())
142+
143+ Eventually (func () bool {
144+ secondcert , _ := watcher .GetCertificate (nil )
145+ first := firstcert .PrivateKey .(* rsa.PrivateKey )
146+ return first .Equal (secondcert .PrivateKey )
147+ }).ShouldNot (BeTrue ())
148+
149+ ctxCancel ()
150+ Eventually (doneCh , "4s" ).Should (BeClosed ())
151+ Expect (called .Load ()).To (BeNumerically (">=" , 1 ))
152+ })
153+
124154 Context ("prometheus metric read_certificate_total" , func () {
125155 var readCertificateTotalBefore float64
126156 var readCertificateErrorsBefore float64
@@ -159,17 +189,18 @@ var _ = Describe("CertWatcher", func() {
159189
160190 Expect (os .Remove (keyPath )).To (Succeed ())
161191
192+ // Note, we are checking two errors here, because os.Remove generates two fsnotify events: Chmod + Remove
162193 Eventually (func () error {
163194 readCertificateTotalAfter := testutil .ToFloat64 (metrics .ReadCertificateTotal )
164- if readCertificateTotalAfter != readCertificateTotalBefore + 1 .0 {
165- return fmt .Errorf ("metric read certificate total expected: %v and got: %v" , readCertificateTotalBefore + 1 .0 , readCertificateTotalAfter )
195+ if readCertificateTotalAfter != readCertificateTotalBefore + 2 .0 {
196+ return fmt .Errorf ("metric read certificate total expected: %v and got: %v" , readCertificateTotalBefore + 2 .0 , readCertificateTotalAfter )
166197 }
167198 return nil
168199 }, "4s" ).Should (Succeed ())
169200 Eventually (func () error {
170201 readCertificateErrorsAfter := testutil .ToFloat64 (metrics .ReadCertificateErrors )
171- if readCertificateErrorsAfter != readCertificateErrorsBefore + 1 .0 {
172- return fmt .Errorf ("metric read certificate errors expected: %v and got: %v" , readCertificateErrorsBefore + 1 .0 , readCertificateErrorsAfter )
202+ if readCertificateErrorsAfter != readCertificateErrorsBefore + 2 .0 {
203+ return fmt .Errorf ("metric read certificate errors expected: %v and got: %v" , readCertificateErrorsBefore + 2 .0 , readCertificateErrorsAfter )
173204 }
174205 return nil
175206 }, "4s" ).Should (Succeed ())
0 commit comments