Skip to content

Commit

Permalink
Run test
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesMurkin committed Jul 10, 2023
1 parent 02b1817 commit aed41ed
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
8 changes: 5 additions & 3 deletions internal/common/certs/cached_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ type CachedCertificateService struct {
refreshInterval time.Duration
}

func NewCachedCertificateService(certPath string, keyPath string) *CachedCertificateService {
func NewCachedCertificateService(certPath string, keyPath string, refreshInternal time.Duration) *CachedCertificateService {
cert := &CachedCertificateService{
certPath: certPath,
keyPath: keyPath,
certificateLock: sync.Mutex{},
fileInfoLock: sync.Mutex{},
refreshInterval: time.Minute,
refreshInterval: refreshInternal,
}
// Initialise the certificate
err := cert.refresh()
Expand Down Expand Up @@ -60,7 +60,9 @@ func (c *CachedCertificateService) Run(ctx context.Context) error {
return nil
case <-ticker.C:
err := c.refresh()
log.WithError(err).Errorf("failed refreshing tls cert for key %s cert %s", c.keyPath, c.certPath)
if err != nil {
log.WithError(err).Errorf("failed refreshing tls cert for key %s cert %s", c.keyPath, c.certPath)
}
}
}
}
Expand Down
29 changes: 24 additions & 5 deletions internal/common/certs/cached_certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package certs

import (
"bytes"
"context"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
Expand All @@ -25,7 +26,7 @@ func TestCachedCertificateService_LoadsCertificateOnStartup(t *testing.T) {
cert, certData, keyData := createCerts()
writeCerts(t, certData, keyData)

cachedCertService := NewCachedCertificateService(certFilePath, keyFilePath)
cachedCertService := NewCachedCertificateService(certFilePath, keyFilePath, time.Second)

result := cachedCertService.GetCertificate()

Expand All @@ -35,14 +36,14 @@ func TestCachedCertificateService_LoadsCertificateOnStartup(t *testing.T) {
func TestCachedCertificateService_PanicIfInitialLoadFails(t *testing.T) {
defer cleanup()

assert.Panics(t, func() { NewCachedCertificateService(certFilePath, keyFilePath) })
assert.Panics(t, func() { NewCachedCertificateService(certFilePath, keyFilePath, time.Second) })
}

func TestCachedCertificateService_ReloadsCert_IfFileOnDiskChanges(t *testing.T) {
defer cleanup()
cert, certData, keyData := createCerts()
writeCerts(t, certData, keyData)
cachedCertService := NewCachedCertificateService(certFilePath, keyFilePath)
cachedCertService := NewCachedCertificateService(certFilePath, keyFilePath, time.Second)

assert.Equal(t, cert, cachedCertService.GetCertificate())

Expand All @@ -62,10 +63,10 @@ func TestCachedCertificateService_HandlesPartialUpdates(t *testing.T) {
defer cleanup()
originalCert, certData, keyData := createCerts()
writeCerts(t, certData, keyData)
cachedCertService := NewCachedCertificateService(certFilePath, keyFilePath)
cachedCertService := NewCachedCertificateService(certFilePath, keyFilePath, time.Second)

assert.Equal(t, originalCert, cachedCertService.GetCertificate())

newCert, certData, keyData := createCerts()

// Update only 1 file on disk - which leaves the representation on disk in an invalid state
Expand All @@ -84,6 +85,24 @@ func TestCachedCertificateService_HandlesPartialUpdates(t *testing.T) {
assert.Equal(t, newCert, cachedCertService.GetCertificate())
}

func TestCachedCertificateService_ReloadsCertPeriodically_WhenUsingRun(t *testing.T) {
defer cleanup()
cert, certData, keyData := createCerts()
writeCerts(t, certData, keyData)
cachedCertService := NewCachedCertificateService(certFilePath, keyFilePath, time.Second)
assert.Equal(t, cert, cachedCertService.GetCertificate())

go func() {
err := cachedCertService.Run(context.Background())
require.NoError(t, err)
}()

newCert, certData, keyData := createCerts()
writeCerts(t, certData, keyData)
time.Sleep(time.Second * 2)
assert.Equal(t, newCert, cachedCertService.GetCertificate())
}

func writeCerts(t *testing.T, certData *bytes.Buffer, keyData *bytes.Buffer) {
if certData != nil {
err := os.WriteFile(certFilePath, certData.Bytes(), 0644)

Check failure on line 108 in internal/common/certs/cached_certificate_test.go

View workflow job for this annotation

GitHub Actions / lint / Lint Go

File is not `gofumpt`-ed (gofumpt)
Expand Down

0 comments on commit aed41ed

Please sign in to comment.