Skip to content

Commit e4c1c38

Browse files
authored
Merge pull request #3457 from alvaroaleman/fix-flaky
🌱 Deflake certwatcher tests
2 parents 80d56e7 + 04d8320 commit e4c1c38

1 file changed

Lines changed: 5 additions & 42 deletions

File tree

pkg/certwatcher/certwatcher_test.go

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"crypto/x509"
2525
"crypto/x509/pkix"
2626
"encoding/pem"
27-
"fmt"
2827
"math/big"
2928
"net"
3029
"os"
@@ -201,54 +200,18 @@ var _ = Describe("CertWatcher", func() {
201200
})
202201

203202
It("should get updated on successful certificate read", func() {
204-
// This test verifies fsnotify, so interval doesn't matter.
205-
doneCh := startWatcher(10 * time.Second)
203+
Expect(watcher.ReadCertificate()).To(Succeed())
206204

207-
Eventually(func() error {
208-
readCertificateTotalAfter := testutil.ToFloat64(metrics.ReadCertificateTotal)
209-
if readCertificateTotalAfter < readCertificateTotalBefore+1.0 {
210-
return fmt.Errorf("metric read certificate total expected at least: %v and got: %v", readCertificateTotalBefore+1.0, readCertificateTotalAfter)
211-
}
212-
return nil
213-
}, "4s").Should(Succeed())
214-
215-
ctxCancel()
216-
Eventually(doneCh, "4s").Should(BeClosed())
205+
Expect(testutil.ToFloat64(metrics.ReadCertificateTotal)).To(Equal(readCertificateTotalBefore + 1))
217206
})
218207

219208
It("should get updated on read certificate errors", func() {
220-
// This test works with fsnotify, so interval doesn't matter.
221-
doneCh := startWatcher(10 * time.Second)
222-
223-
Eventually(func() error {
224-
readCertificateTotalAfter := testutil.ToFloat64(metrics.ReadCertificateTotal)
225-
if readCertificateTotalAfter < readCertificateTotalBefore+1.0 {
226-
return fmt.Errorf("metric read certificate total expected at least: %v and got: %v", readCertificateTotalBefore+1.0, readCertificateTotalAfter)
227-
}
228-
readCertificateTotalBefore = readCertificateTotalAfter
229-
return nil
230-
}, "4s").Should(Succeed())
231-
232209
Expect(os.Remove(keyPath)).To(Succeed())
233210

234-
// Note, we are checking two errors here, because os.Remove generates two fsnotify events: Chmod + Remove
235-
Eventually(func() error {
236-
readCertificateTotalAfter := testutil.ToFloat64(metrics.ReadCertificateTotal)
237-
if readCertificateTotalAfter < readCertificateTotalBefore+2.0 {
238-
return fmt.Errorf("metric read certificate total expected at least: %v and got: %v", readCertificateTotalBefore+2.0, readCertificateTotalAfter)
239-
}
240-
return nil
241-
}, "4s").Should(Succeed())
242-
Eventually(func() error {
243-
readCertificateErrorsAfter := testutil.ToFloat64(metrics.ReadCertificateErrors)
244-
if readCertificateErrorsAfter < readCertificateErrorsBefore+2.0 {
245-
return fmt.Errorf("metric read certificate errors expected at least: %v and got: %v", readCertificateErrorsBefore+2.0, readCertificateErrorsAfter)
246-
}
247-
return nil
248-
}, "4s").Should(Succeed())
211+
Expect(watcher.ReadCertificate()).To(HaveOccurred())
249212

250-
ctxCancel()
251-
Eventually(doneCh, "4s").Should(BeClosed())
213+
Expect(testutil.ToFloat64(metrics.ReadCertificateTotal)).To(Equal(readCertificateTotalBefore + 1))
214+
Expect(testutil.ToFloat64(metrics.ReadCertificateErrors)).To(Equal(readCertificateErrorsBefore + 1))
252215
})
253216
})
254217
})

0 commit comments

Comments
 (0)