-
Notifications
You must be signed in to change notification settings - Fork 58
/
cert_reloader_test.go
117 lines (96 loc) · 2.99 KB
/
cert_reloader_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"context"
"crypto/tls"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
// TestCertReloader tests the reloading functionality of the certificate.
func TestCertReloader(t *testing.T) {
// Create temporary cert and key files
tmpCertFile, err := os.CreateTemp("", "cert*.pem")
if err != nil {
t.Fatalf("Failed to create temp cert file: %v", err)
}
defer os.Remove(tmpCertFile.Name()) // clean up
tmpKeyFile, err := os.CreateTemp("", "key*.pem")
if err != nil {
t.Fatalf("Failed to create temp key file: %v", err)
}
defer os.Remove(tmpKeyFile.Name()) // clean up
// Write initial cert and key to temp files
initialCertData, _ := os.ReadFile("testdata/cert.pem")
if err := os.WriteFile(tmpCertFile.Name(), initialCertData, 0644); err != nil {
t.Fatalf("Failed to write to temp cert file: %v", err)
}
initialKeyData, _ := os.ReadFile("testdata/key.pem")
if err := os.WriteFile(tmpKeyFile.Name(), initialKeyData, 0644); err != nil {
t.Fatalf("Failed to write to temp key file: %v", err)
}
// Setup CertReloader with temp files
certReloader := NewCertReloader(tmpCertFile.Name(), tmpKeyFile.Name())
_, err = certReloader.LoadCertificate()
if err != nil {
t.Fatalf("Failed to load initial certificate: %v", err)
}
// Mocking a certificate change by writing new data to the files
newCertData, _ := os.ReadFile("testdata/cert.pem")
if err := os.WriteFile(tmpCertFile.Name(), newCertData, 0644); err != nil {
t.Fatalf("Failed to write new data to cert file: %v", err)
}
// Simulate reloading
_, err = certReloader.LoadCertificate()
if err != nil {
t.Fatalf("Failed to reload certificate: %v", err)
}
}
type mockCertLoader struct {
certPath string
keyPath string
loadCertFunc func() (*tls.Certificate, error)
}
func (m *mockCertLoader) CertPath() string {
return m.certPath
}
func (m *mockCertLoader) KeyPath() string {
return m.keyPath
}
func (m *mockCertLoader) LoadCertificate() (*tls.Certificate, error) {
return m.loadCertFunc()
}
func TestWatchingCertFiles(t *testing.T) {
tmpCertFile, err := os.CreateTemp("", "cert*.pem")
if err != nil {
t.Fatalf("Failed to create temp cert file: %v", err)
}
defer os.Remove(tmpCertFile.Name())
tmpKeyFile, err := os.CreateTemp("", "key*.pem")
if err != nil {
t.Fatalf("Failed to create temp key file: %v", err)
}
defer os.Remove(tmpKeyFile.Name())
loadCertFuncChan := make(chan bool)
done := make(chan bool)
cl := &mockCertLoader{
certPath: tmpCertFile.Name(),
keyPath: tmpKeyFile.Name(),
loadCertFunc: func() (*tls.Certificate, error) {
loadCertFuncChan <- true
return &tls.Certificate{}, nil
},
}
go func() {
called := <-loadCertFuncChan
assert.True(t, called)
done <- true
}()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
watchCertFiles(ctx, cl)
newCertData, _ := os.ReadFile("testdata/cert.pem")
if err := os.WriteFile(tmpCertFile.Name(), newCertData, 0644); err != nil {
t.Fatalf("Failed to write new data to cert file: %v", err)
}
<-done
}