Skip to content

Commit c226965

Browse files
committed
Add certwatcher test for file rename
1 parent 6785442 commit c226965

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

pkg/certwatcher/certwatcher_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var _ = BeforeSuite(func() {
4141
})
4242

4343
var _ = AfterSuite(func() {
44-
for _, file := range []string{certPath, keyPath} {
44+
for _, file := range []string{certPath, keyPath, certPath + ".new", keyPath + ".new", certPath + ".old", keyPath + ".old"} {
4545
_ = os.Remove(file)
4646
}
4747
})

pkg/certwatcher/certwatcher_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,39 @@ 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+
Expect(os.Remove(certPath + ".old")).To(Succeed())
144+
Expect(os.Remove(keyPath + ".old")).To(Succeed())
145+
146+
Eventually(func() bool {
147+
secondcert, _ := watcher.GetCertificate(nil)
148+
first := firstcert.PrivateKey.(*rsa.PrivateKey)
149+
return first.Equal(secondcert.PrivateKey)
150+
}).ShouldNot(BeTrue())
151+
152+
ctxCancel()
153+
Eventually(doneCh, "4s").Should(BeClosed())
154+
Expect(called.Load()).To(BeNumerically(">=", 1))
155+
})
156+
124157
Context("prometheus metric read_certificate_total", func() {
125158
var readCertificateTotalBefore float64
126159
var readCertificateErrorsBefore float64

0 commit comments

Comments
 (0)