-
Notifications
You must be signed in to change notification settings - Fork 1
/
flag_loader_test.go
201 lines (150 loc) · 5.16 KB
/
flag_loader_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package bconf_test
import (
"fmt"
"strings"
"testing"
"github.com/rheisen/bconf"
)
func TestFlagLoaderFunctions(t *testing.T) {
l := bconf.NewFlagLoader()
if l == nil {
t.Fatalf("unexpected nil flag loader")
}
l = bconf.NewFlagLoaderWithKeyPrefix("key_prefix")
if l.KeyPrefix != "key_prefix" {
t.Fatalf("unexpected key prefix: %s", l.KeyPrefix)
}
}
func TestFlagLoaderGetMap(t *testing.T) {
const appFieldSetKey = "app"
const idFieldKey = "id"
const secretFieldKey = "secret"
const appIDValue = "test-app-id"
const appSecretValue = "test-sensitive-value"
appIDFlagVariable := fmt.Sprintf("--%s_%s=%s", appFieldSetKey, idFieldKey, appIDValue)
loader := bconf.NewFlagLoader()
loader.OverrideLookup = []string{
appIDFlagVariable,
fmt.Sprintf("--%s_%s", appFieldSetKey, secretFieldKey),
appSecretValue,
}
appMap := loader.GetMap(appFieldSetKey, []string{idFieldKey, secretFieldKey, "invalid_field_key"})
if len(appMap) != 2 {
t.Fatalf("unexpected appMap length '%d', expected '2'", len(appMap))
}
}
func TestFlagLoader(t *testing.T) {
const sessionFieldSet = "session"
const sessionTokenKey = "token"
const logFieldSet = "log"
const logLevelKey = "level"
const logColorKey = "color"
const sessionKeyValue = "abc123"
const logLevelValue = "error"
sessionTokenFlag := fmt.Sprintf("--%s_%s", sessionFieldSet, sessionTokenKey)
l := bconf.FlagLoader{
OverrideLookup: []string{
fmt.Sprintf("--%s_%s=%s", sessionFieldSet, sessionTokenKey, sessionKeyValue),
fmt.Sprintf("--%s_%s", logFieldSet, logLevelKey),
logLevelValue,
fmt.Sprintf("-%s_%s", logFieldSet, logColorKey),
},
}
clone := l.Clone()
if l.Name() != "bconf_flags" {
t.Errorf("unexpected loader name: '%s'", l.Name())
}
if !strings.Contains(l.HelpString(sessionFieldSet, sessionTokenKey), sessionTokenFlag) {
t.Errorf("unexpected loader help string contents: '%s'", l.HelpString(sessionFieldSet, sessionTokenKey))
}
if !strings.Contains(clone.HelpString(sessionFieldSet, sessionTokenKey), sessionTokenFlag) {
t.Errorf(
"unexpected loader clone help string contents: '%s'",
clone.HelpString(sessionFieldSet, sessionTokenKey),
)
}
sessionKeyLookup, found := l.Get(sessionFieldSet, sessionTokenKey)
if !found {
t.Errorf("unexpected problem getting session_key value")
}
if sessionKeyLookup != sessionKeyValue {
t.Errorf("unexpected value for session key: '%s'", sessionKeyLookup)
}
logLevel, found := l.Get(logFieldSet, logLevelKey)
if !found {
t.Errorf("unexpected problem getting log_level value")
}
if logLevel != logLevelValue {
t.Errorf("unexpected value for log level: '%s'", logLevel)
}
cloneSessionKeyLookup, cloneFound := clone.Get(sessionFieldSet, sessionTokenKey)
if !cloneFound {
t.Errorf("unexpected problem getting session_key value from loader clone")
}
if cloneSessionKeyLookup != sessionKeyValue {
t.Errorf("unexpected value for session_key from loader clone: '%s'", cloneSessionKeyLookup)
}
logColor, found := l.Get(logFieldSet, logColorKey)
if !found {
t.Errorf("unexpected problem getting log_color value")
}
if logColor != "true" {
t.Errorf("unexpected log color value: '%s'", logColor)
}
_, found = l.Get("random_key", "random_key")
if found {
t.Errorf("not expecting to find a value for unset key")
}
}
func TestFlagLoaderWithKeyPrefix(t *testing.T) {
const sessionFieldSet = "session"
const sessionTokenKey = "token"
const logFieldSet = "log"
const logLevelKey = "level"
const sessionKeyValue = "abc123"
const logLevelValue = "error"
const keyPrefix = "ext_http_api"
sessionTokenFlag := fmt.Sprintf("--%s_%s_%s", keyPrefix, sessionFieldSet, sessionTokenKey)
l := bconf.FlagLoader{
KeyPrefix: keyPrefix,
OverrideLookup: []string{
fmt.Sprintf("--%s_%s=%s", sessionFieldSet, sessionTokenKey, sessionKeyValue),
fmt.Sprintf("--%s_%s", logFieldSet, logLevelKey),
logLevelValue,
},
}
clone := l.Clone()
if l.Name() != "bconf_flags" {
t.Errorf("unexpected loader name: '%s'", l.Name())
}
if !strings.Contains(l.HelpString(sessionFieldSet, sessionTokenKey), sessionTokenFlag) {
t.Errorf("unexpected loader help string contents: '%s'", l.HelpString(sessionFieldSet, sessionTokenKey))
}
if !strings.Contains(clone.HelpString(sessionFieldSet, sessionTokenKey), sessionTokenFlag) {
t.Errorf(
"unexpected loader clone help string contents: '%s'",
clone.HelpString(sessionFieldSet, sessionTokenKey),
)
}
sessionKeyLookup, found := l.Get(sessionFieldSet, sessionTokenKey)
if !found {
t.Errorf("unexpected problem getting session_key value")
}
if sessionKeyLookup != sessionKeyValue {
t.Errorf("unexpected value for session key: '%s'", sessionKeyLookup)
}
logLevel, found := l.Get(logFieldSet, logLevelKey)
if !found {
t.Errorf("unexpected problem getting log_level value")
}
if logLevel != logLevelValue {
t.Errorf("unexpected value for log level: '%s'", logLevel)
}
cloneSessionKeyLookup, cloneFound := clone.Get(sessionFieldSet, sessionTokenKey)
if !cloneFound {
t.Errorf("unexpected problem getting session_key value from loader clone")
}
if cloneSessionKeyLookup != sessionKeyValue {
t.Errorf("unexpected value for session_key from loader clone: '%s'", cloneSessionKeyLookup)
}
}