-
Notifications
You must be signed in to change notification settings - Fork 35
/
config_test.go
92 lines (81 loc) · 2.15 KB
/
config_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
package main
import (
"os"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/require"
)
// clearConfigEnv clears the environment variables for the config fields.
func clearConfigEnv(t *testing.T) {
t.Helper()
var cfg config
typ := reflect.TypeOf(cfg)
for i := range typ.NumField() {
field := typ.Field(i)
if name := field.Tag.Get("env"); name != "" {
if os.Getenv(name) != "" {
t.Setenv(name, "")
}
}
}
}
func Test_loadConfig(t *testing.T) {
clearConfigEnv(t)
t.Run("defaults", func(t *testing.T) {
expected := config{
Port: 8080,
ConnectionTimeout: time.Minute,
ReconnectionTimeout: time.Second * 10,
ShutdownTimeout: time.Minute * 10,
RemoveRetries: 10,
RequestTimeout: time.Second * 10,
RetryOffset: -time.Second,
ChangesRetryInterval: time.Second,
}
cfg, err := loadConfig()
require.NoError(t, err)
require.Equal(t, expected, *cfg)
})
t.Run("custom", func(t *testing.T) {
t.Setenv("RYUK_PORT", "1234")
t.Setenv("RYUK_CONNECTION_TIMEOUT", "2s")
t.Setenv("RYUK_RECONNECTION_TIMEOUT", "3s")
t.Setenv("RYUK_SHUTDOWN_TIMEOUT", "7s")
t.Setenv("RYUK_VERBOSE", "true")
t.Setenv("RYUK_REQUEST_TIMEOUT", "4s")
t.Setenv("RYUK_REMOVE_RETRIES", "5")
t.Setenv("RYUK_RETRY_OFFSET", "-6s")
t.Setenv("RYUK_CHANGES_RETRY_INTERVAL", "8s")
expected := config{
Port: 1234,
ConnectionTimeout: time.Second * 2,
ReconnectionTimeout: time.Second * 3,
ShutdownTimeout: time.Second * 7,
Verbose: true,
RemoveRetries: 5,
RequestTimeout: time.Second * 4,
RetryOffset: -time.Second * 6,
ChangesRetryInterval: time.Second * 8,
}
cfg, err := loadConfig()
require.NoError(t, err)
require.Equal(t, expected, *cfg)
})
for _, name := range []string{
"RYUK_PORT",
"RYUK_CONNECTION_TIMEOUT",
"RYUK_RECONNECTION_TIMEOUT",
"RYUK_SHUTDOWN_TIMEOUT",
"RYUK_VERBOSE",
"RYUK_REQUEST_TIMEOUT",
"RYUK_REMOVE_RETRIES",
"RYUK_RETRY_OFFSET",
} {
t.Run("invalid-"+name, func(t *testing.T) {
t.Setenv(name, "invalid")
_, err := loadConfig()
require.Error(t, err)
})
}
}