-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleaner_test.go
More file actions
67 lines (58 loc) · 1.76 KB
/
Copy pathcleaner_test.go
File metadata and controls
67 lines (58 loc) · 1.76 KB
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
package agent_keyring
import (
"context"
"testing"
"time"
)
func TestCleaner_RemovesRetiredKeyOnTick(t *testing.T) {
rotator, mock, db, box := setupRotator(t)
if _, err := rotator.RotateBox(box.ID, 24*time.Hour); err != nil {
t.Fatalf("RotateBox: %v", err)
}
if mock.entryCount() != 2 {
t.Fatalf("post-rotation agent entries = %d, want 2", mock.entryCount())
}
// Tiny overlap + tiny interval so the cleaner sweeps quickly and
// considers the just-retired legacy entry eligible.
cleaner := NewCleaner(rotator, db, 1*time.Millisecond, 20*time.Millisecond, rotator.logger)
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
done := make(chan struct{})
go func() {
cleaner.Run(ctx)
close(done)
}()
// Give it time for the immediate-on-start sweep to fire.
time.Sleep(50 * time.Millisecond)
cancel()
<-done
if got := mock.entryCount(); got != 1 {
t.Errorf("agent entries after cleaner sweep = %d, want 1", got)
}
keys, _ := db.GetBoxAgentKeys(box.ID)
if len(keys) != 1 {
t.Errorf("db keys after cleaner sweep = %d, want 1", len(keys))
}
}
func TestCleaner_NoopWhenNothingRetired(t *testing.T) {
rotator, mock, db, box := setupRotator(t)
// No rotation = no retired keys. Cleaner sweep should leave the
// single legacy primary alone.
cleaner := NewCleaner(rotator, db, 1*time.Millisecond, 1*time.Hour, rotator.logger)
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() {
cleaner.Run(ctx)
close(done)
}()
time.Sleep(20 * time.Millisecond)
cancel()
<-done
if got := mock.entryCount(); got != 1 {
t.Errorf("mock entries = %d, want 1", got)
}
keys, _ := db.GetBoxAgentKeys(box.ID)
if len(keys) != 1 {
t.Errorf("db keys = %d, want 1", len(keys))
}
}