Skip to content

Commit

Permalink
Add certwatcher test for file rename
Browse files Browse the repository at this point in the history
  • Loading branch information
m-messiah authored and k8s-infra-cherrypick-robot committed Aug 12, 2024
1 parent 9fe6db5 commit 4b8b9e6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/certwatcher/certwatcher_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var _ = BeforeSuite(func() {
})

var _ = AfterSuite(func() {
for _, file := range []string{certPath, keyPath} {
for _, file := range []string{certPath, keyPath, certPath + ".new", keyPath + ".new", certPath + ".old", keyPath + ".old"} {
_ = os.Remove(file)
}
})
30 changes: 30 additions & 0 deletions pkg/certwatcher/certwatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,36 @@ var _ = Describe("CertWatcher", func() {
Expect(called.Load()).To(BeNumerically(">=", 1))
})

It("should reload currentCert when changed with rename", func() {
doneCh := startWatcher()
called := atomic.Int64{}
watcher.RegisterCallback(func(crt tls.Certificate) {
called.Add(1)
Expect(crt.Certificate).ToNot(BeEmpty())
})

firstcert, _ := watcher.GetCertificate(nil)

err := writeCerts(certPath+".new", keyPath+".new", "192.168.0.2")
Expect(err).ToNot(HaveOccurred())

Expect(os.Link(certPath, certPath+".old")).To(Succeed())
Expect(os.Rename(certPath+".new", certPath)).To(Succeed())

Expect(os.Link(keyPath, keyPath+".old")).To(Succeed())
Expect(os.Rename(keyPath+".new", keyPath)).To(Succeed())

Eventually(func() bool {
secondcert, _ := watcher.GetCertificate(nil)
first := firstcert.PrivateKey.(*rsa.PrivateKey)
return first.Equal(secondcert.PrivateKey)
}).ShouldNot(BeTrue())

ctxCancel()
Eventually(doneCh, "4s").Should(BeClosed())
Expect(called.Load()).To(BeNumerically(">=", 1))
})

Context("prometheus metric read_certificate_total", func() {
var readCertificateTotalBefore float64
var readCertificateErrorsBefore float64
Expand Down

0 comments on commit 4b8b9e6

Please sign in to comment.