Newer
Older
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
package acmeserver
import (
"context"
"crypto"
"crypto/x509"
"errors"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
)
type fakeACME struct {
}
func (f *fakeACME) GetCertificate(_ context.Context, key crypto.Signer, domains []string) (der [][]byte, leaf *x509.Certificate, err error) {
return nil, nil, errors.New("unimplemented")
}
func TestManager(t *testing.T) {
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
os.Mkdir(filepath.Join(dir, "config"), 0700)
ioutil.WriteFile(
filepath.Join(dir, "config", "test.yml"),
[]byte("- { cn: example.com }\n"),
0644,
)
m, err := NewManager(&Config{
Dir: dir,
Email: "test@example.com",
}, &fakeACME{})
if err != nil {
t.Fatal(err)
}
defer m.Stop()
if err := m.Start(context.Background()); err != nil {
t.Fatal("Start:", err)
}
// Wait just a little bit to give a chance to m.loop() to run.
time.Sleep(50 * time.Millisecond)
// Data race: we read data owned by another goroutine!
if len(m.certs) < 1 {
t.Fatal("configuration not loaded?")
}
if m.certs[0].domains[0] != "example.com" {
t.Fatalf("certs[0].domains[0] is %s, expected example.com", m.certs[0].domains[0])
}
// Try a reload, catch obvious errors.
m.Reload()
time.Sleep(50 * time.Millisecond)
if len(m.certs) != 1 {
t.Fatalf("certs count is %d, expected 1", len(m.certs))
}
if m.certs[0].domains[0] != "example.com" {
t.Fatalf("certs[0].domains[0] is %s, expected example.com", m.certs[0].domains[0])
}
}