-
Notifications
You must be signed in to change notification settings - Fork 18
/
settings_test.go
149 lines (137 loc) · 3.34 KB
/
settings_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package stats
import (
"os"
"reflect"
"testing"
"time"
"github.com/kelseyhightower/envconfig"
)
func testSetenv(t *testing.T, pairs ...string) (reset func()) {
var fns []func()
for i := 0; i < len(pairs); i += 2 {
key := pairs[i+0]
val := pairs[i+1]
prev, exists := os.LookupEnv(key)
if val == "" {
if err := os.Unsetenv(key); err != nil {
t.Fatalf("deleting env key: %s: %s", key, err)
}
} else {
if err := os.Setenv(key, val); err != nil {
t.Fatalf("setting env key: %s: %s", key, err)
}
}
if exists {
fns = append(fns, func() { os.Setenv(key, prev) })
} else {
fns = append(fns, func() { os.Unsetenv(key) })
}
}
return func() {
for _, fn := range fns {
fn()
}
}
}
func TestSettingsCompat(t *testing.T) {
reset := testSetenv(t,
"USE_STATSD", "",
"STATSD_HOST", "",
"STATSD_PROTOCOL", "",
"STATSD_PORT", "",
"GOSTATS_FLUSH_INTERVAL_SECONDS", "",
"GOSTATS_LOGGING_SINK_DISABLED", "",
)
defer reset()
var e Settings
if err := envconfig.Process("", &e); err != nil {
t.Fatal(err)
}
s := GetSettings()
if !reflect.DeepEqual(e, s) {
t.Fatalf("Default Settings: want: %+v got: %+v", e, s)
}
}
func TestSettingsDefault(t *testing.T) {
reset := testSetenv(t,
"USE_STATSD", "",
"STATSD_HOST", "",
"STATSD_PROTOCOL", "",
"STATSD_PORT", "",
"GOSTATS_FLUSH_INTERVAL_SECONDS", "",
"GOSTATS_LOGGING_SINK_DISABLED", "",
)
defer reset()
exp := Settings{
UseStatsd: DefaultUseStatsd,
StatsdHost: DefaultStatsdHost,
StatsdProtocol: DefaultStatsdProtocol,
StatsdPort: DefaultStatsdPort,
FlushIntervalS: DefaultFlushIntervalS,
LoggingSinkDisabled: DefaultLoggingSinkDisabled,
}
settings := GetSettings()
if exp != settings {
t.Errorf("Default: want: %+v got: %+v", exp, settings)
}
}
func TestSettingsOverride(t *testing.T) {
reset := testSetenv(t,
"USE_STATSD", "true",
"STATSD_HOST", "10.0.0.1",
"STATSD_PROTOCOL", "udp",
"STATSD_PORT", "1234",
"GOSTATS_FLUSH_INTERVAL_SECONDS", "3",
"GOSTATS_LOGGING_SINK_DISABLED", "true",
)
defer reset()
exp := Settings{
UseStatsd: true,
StatsdHost: "10.0.0.1",
StatsdProtocol: "udp",
StatsdPort: 1234,
FlushIntervalS: 3,
LoggingSinkDisabled: true,
}
settings := GetSettings()
if exp != settings {
t.Errorf("Default: want: %+v got: %+v", exp, settings)
}
}
func TestSettingsErrors(t *testing.T) {
// STATSD_HOST doesn't error so we don't check it
tests := map[string]string{
"USE_STATSD": "FOO!",
"STATSD_PORT": "not-an-int",
"GOSTATS_FLUSH_INTERVAL_SECONDS": "true",
"GOSTATS_LOGGING_SINK_DISABLED": "1337",
}
for key, val := range tests {
t.Run(key, func(t *testing.T) {
reset := testSetenv(t, key, val)
defer reset()
var panicked bool
func() {
defer func() {
panicked = recover() != nil
}()
GetSettings()
}()
if !panicked {
t.Errorf("Settings expected a panic for invalid value %s=%s", key, val)
}
})
}
}
func TestFlushInterval(t *testing.T) {
reset := testSetenv(t,
"GOSTATS_FLUSH_INTERVAL_SECONDS", "3",
)
defer reset()
settings := GetSettings()
interval := settings.FlushInterval()
expected := time.Duration(3) * time.Second
if interval != expected {
t.Errorf("Flush interval does not match expected duration %s != %s", interval, expected)
}
}