-
Notifications
You must be signed in to change notification settings - Fork 10
/
configmap_source_test.go
125 lines (119 loc) · 2.91 KB
/
configmap_source_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
118
119
120
121
122
123
124
125
package conf
import (
"context"
"os"
"path/filepath"
"sync"
"testing"
"time"
)
func TestConfigMap(t *testing.T) {
t.Run("Source", func(t *testing.T) {
src := NewKubernetesConfigMapSource("", "./testdata/configmap")
cfg := struct {
CollectorKinesisEndpoint string
}{}
loader := Loader{
Name: "collector",
Args: []string{},
Sources: []Source{src},
}
if _, _, err := loader.Load(&cfg); err != nil {
t.Fatal(err)
}
if cfg.CollectorKinesisEndpoint != "https://example.com/blah" {
t.Fatalf("bad value: want example.com/blah got %q", cfg.CollectorKinesisEndpoint)
}
})
t.Run("NestedConfig", func(t *testing.T) {
a := testConfig{}
loader := Loader{
Name: "collector",
Args: []string{},
Sources: []Source{
NewKubernetesConfigMapSource("", "./testdata/configmap"),
},
}
loader.Load(&a)
if a.Kinesis.StreamName != "segment-logs" {
t.Errorf("loading nested config did not work correctly")
}
})
t.Run("Prefix", func(t *testing.T) {
a := struct {
Kinesis struct {
Endpoint string
}
}{}
loader := Loader{
Name: "name",
Args: []string{},
Sources: []Source{
NewKubernetesConfigMapSource("collector", "./testdata/configmap"),
},
}
loader.Load(&a)
if a.Kinesis.Endpoint != "https://example.com/blah" {
t.Errorf("loading config with prefix did not work correctly")
}
})
}
func TestSubscriber(t *testing.T) {
tmp, _ := os.MkdirTemp("", "conf-configmap-")
defer os.RemoveAll(tmp)
oldInterval := kubernetesSleepInterval
defer func() {
kubernetesSleepInterval = oldInterval
}()
kubernetesSleepInterval = 3 * time.Millisecond
t.Run("ValueExists", func(t *testing.T) {
path := filepath.Join(tmp, "test1")
if err := os.WriteFile(path, []byte("5\n"), 0640); err != nil {
t.Fatal(err)
}
sc := NewKubernetesSubscriber("", tmp)
ctx, cancel := context.WithCancel(context.Background())
count := 0
sc.Subscribe(ctx, func(key, newValue string) {
count++
})
time.Sleep(10 * time.Millisecond)
cancel()
if count != 0 {
t.Fatalf("expected f to get called zero times, got called %d times", count)
}
})
t.Run("ValueChanges", func(t *testing.T) {
path := filepath.Join(tmp, "test2")
if err := os.WriteFile(path, []byte("7\n"), 0640); err != nil {
t.Fatal(err)
}
sc := NewKubernetesSubscriber("", tmp)
ctx, cancel := context.WithCancel(context.Background())
count := 0
value := ""
var mu sync.Mutex
sc.Subscribe(ctx, func(key, newValue string) {
mu.Lock()
defer mu.Unlock()
count++
value = newValue
})
go func() {
time.Sleep(2 * time.Millisecond)
if err := os.WriteFile(path, []byte("11\n"), 0640); err != nil {
panic(err)
}
}()
time.Sleep(10 * time.Millisecond)
cancel()
mu.Lock()
defer mu.Unlock()
if count == 0 {
t.Fatalf("expected f to get called at least once, got called %d times", count)
}
if value != "11" {
t.Fatalf("bad value: want 11 got %q", value)
}
})
}