-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode_test.go
389 lines (315 loc) · 7.94 KB
/
decode_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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package config
import (
"os"
"path/filepath"
"reflect"
"testing"
"time"
)
func Test_DecodeSimpleConfig(t *testing.T) {
var cfg struct {
Log map[string]string
Net struct {
Listen string
TLS struct {
Cert string
Key string
}
}
Drivers []string
Cache struct {
Redis struct {
Addr string
}
CleanupInterval time.Duration `config:"cleanup_interval"`
}
Store map[string]struct {
Type string
Path string
Limit int64
}
}
if err := DecodeFile(&cfg, filepath.Join("testdata", "server.conf"), ErrorHandler(errh(t))); err != nil {
t.Fatal(err)
}
}
func Test_DecodeArrays(t *testing.T) {
type Block struct {
String string
}
var cfg struct {
Strings []string
Ints []int64
Floats []float64
Bools []bool
Durations []time.Duration
Sizes []int64
Blocks []Block
}
if err := DecodeFile(&cfg, filepath.Join("testdata", "array.conf"), ErrorHandler(errh(t))); err != nil {
t.Fatal(err)
}
Strings := []string{"one", "two", "three", "four", `"five"`}
Ints := []int64{1, 2, 3, 4}
Floats := []float64{1.2, 3.4, 5.6, 7.8}
Bools := []bool{true, false}
Durations := []time.Duration{time.Second, time.Minute * 2, time.Hour * 3}
Sizes := []int64{1, 2048, 3145728, 4294967296, 5497558138880}
Blocks := []Block{{"foo"}, {"bar"}, {"baz"}}
for i, str := range Strings {
if cfg.Strings[i] != str {
t.Errorf("Strings[%d] - unexpected string, expected=%q, got=%q\n", i, str, cfg.Strings[i])
}
}
for i, i64 := range Ints {
if cfg.Ints[i] != i64 {
t.Errorf("Ints[%d] - unexpected int64, expected=%d, got=%d\n", i, i64, cfg.Ints[i])
}
}
for i, f64 := range Floats {
if cfg.Floats[i] != f64 {
t.Errorf("Floats[%d] - unexpected float64, expected=%f, got=%f\n", i, f64, cfg.Floats[i])
}
}
for i, b := range Bools {
if cfg.Bools[i] != b {
t.Errorf("Bools[%d] - unexpected bool, expected=%v, got=%v\n", i, b, cfg.Bools[i])
}
}
for i, dur := range Durations {
if cfg.Durations[i] != dur {
t.Errorf("Durations[%d] - unexpected time.Duration, expected=%v, got=%v\n", i, dur, cfg.Durations[i])
}
}
for i, siz := range Sizes {
if cfg.Sizes[i] != siz {
t.Errorf("Sizes[%d] - unexpected int64, expected=%d, got=%d\n", i, siz, cfg.Sizes[i])
}
}
for i, block := range Blocks {
if cfg.Blocks[i].String != block.String {
t.Errorf("Blocks[%d] - unexpected string, expected=%q, got=%q\n", i, block.String, cfg.Blocks[i].String)
}
}
}
func Test_DecodeNoGroupLabel(t *testing.T) {
var cfg struct {
Driver struct {
SSH struct {
Addr string
Auth struct {
Username string
Identity string
}
}
Docker struct {
Host string
Version string
}
QEMU struct {
Disks string
CPUs int64
Memory int64
}
} `config:",nogroup"`
}
if err := DecodeFile(&cfg, filepath.Join("testdata", "nogroup.conf"), ErrorHandler(errh(t))); err != nil {
t.Fatal(err)
}
t.Log(cfg.Driver)
}
func Test_DecodeLabel(t *testing.T) {
type TLS struct {
CA string
}
type Auth struct {
Addr string
TLS TLS
}
var cfg struct {
Auth map[string]Auth
Ports map[string][]string
Provider map[string]struct {
ClientID string `config:"client_id"`
ClientSecret string `config:"client_secret"`
}
}
if err := DecodeFile(&cfg, filepath.Join("testdata", "label.conf"), ErrorHandler(errh(t))); err != nil {
t.Fatal(err)
}
expectedAuth := map[string]Auth{
"internal": {
Addr: "postgres://localhost:5432",
TLS: TLS{},
},
"ldap": {
Addr: "ldap://example.com",
TLS: TLS{CA: "/var/lib/ssl/ca.crt"},
},
"saml": {
Addr: "https://idp.example.com",
TLS: TLS{CA: "/var/lib/ssl/ca.crt"},
},
}
for label, auth := range expectedAuth {
cfg, ok := cfg.Auth[label]
if !ok {
t.Fatalf("could not find label %q\n", label)
}
if cfg.Addr != auth.Addr {
t.Fatalf("unexpected Addr, expected=%q, got=%q\n", cfg.Addr, auth.Addr)
}
if cfg.TLS.CA != auth.TLS.CA {
t.Fatalf("unexpected TLS.CA, expected=%q, got=%q\n", cfg.TLS.CA, auth.TLS.CA)
}
}
expectedPorts := map[string][]string{
"open": {"8080", "8443"},
"close": {"80", "443"},
}
for label, ports := range expectedPorts {
cfg, ok := cfg.Ports[label]
if !ok {
t.Fatalf("could not find label %q\n", label)
}
for i := range cfg {
if ports[i] != cfg[i] {
t.Fatalf("unxepected ports[%d], expected=%q, got=%q\n", i, ports[i], cfg[i])
}
}
}
expectedProviders := []string{"github", "gitlab"}
for _, name := range expectedProviders {
if _, ok := cfg.Provider[name]; !ok {
t.Fatalf("expected provider %q in map\n", name)
}
}
}
func Test_DecodeUTF8(t *testing.T) {
var cfg struct {
Block map[string]struct {
Strings []string
}
}
if err := DecodeFile(&cfg, filepath.Join("testdata", "utf8.conf"), ErrorHandler(errh(t))); err != nil {
t.Fatal(err)
}
label := "标签"
block, ok := cfg.Block[label]
if !ok {
t.Fatalf("could not find label %q\n", label)
}
expected := "细绳"
for i, s := range block.Strings {
if s != expected {
t.Fatalf("cfg.Block[%q].Strings[%d] - unexpected string, expected=%q, got=%q\n", label, i, expected, s)
}
}
}
func Test_DecodeDuration(t *testing.T) {
var cfg struct {
Hour time.Duration
HourHalf time.Duration `config:"hour_half"`
HourHalfSeconds time.Duration `config:"hour_half_seconds"`
}
if err := DecodeFile(&cfg, filepath.Join("testdata", "duration.conf"), ErrorHandler(errh(t))); err != nil {
t.Fatal(err)
}
}
func Test_DecodeInclude(t *testing.T) {
var cfg struct {
Block map[string]struct {
Strings []string
}
Hour time.Duration
HourHalf time.Duration `config:"hour_half"`
HourHalfSeconds time.Duration `config:"hour_half_seconds"`
}
opts := []Option{
ErrorHandler(errh(t)),
Includes,
}
if err := DecodeFile(&cfg, filepath.Join("testdata", "include.conf"), opts...); err != nil {
t.Fatal(err)
}
t.Log(cfg)
}
func Test_DecodeEnvVars(t *testing.T) {
var cfg struct {
Database struct {
Addr string
Username string
Password string
TLS struct {
KeyPassword string
}
}
}
os.Setenv("DB_USERNAME", "admin")
os.Setenv("DB_PASSWORD", "secret")
opts := []Option{
ErrorHandler(errh(t)),
Envvars,
Expand("vault", func(key string) (string, error) {
m := map[string]string{
"/secrets/ssl/TLS_KEY_PASSWORD": "terces",
}
return m[key], nil
}),
}
if err := DecodeFile(&cfg, filepath.Join("testdata", "envvars.conf"), opts...); err != nil {
t.Fatal(err)
}
if cfg.Database.Username != "admin" {
t.Fatalf("unexpected Database.Username, expected=%q, got=%q\n", "admin", cfg.Database.Username)
}
if cfg.Database.Password != "secret" {
t.Fatalf("unexpected Database.Password, expected=%q, got=%q\n", "secret", cfg.Database.Password)
}
if cfg.Database.TLS.KeyPassword != "terces" {
t.Fatalf("unexpected Database.TLS.Keypassword, expected=%q, got=%q\n", "terces", cfg.Database.Password)
}
}
func Test_DecodeDeprecated(t *testing.T) {
var cfg struct {
TLS struct {
CA string
}
SSL struct {
CA string
} `config:",deprecated:tls"`
}
errs := make([]string, 0, 1)
errh := func(pos Pos, msg string) {
errs = append(errs, msg)
}
if err := DecodeFile(&cfg, filepath.Join("testdata", "deprecated.conf"), ErrorHandler(errh)); err != nil {
t.Fatal(err)
}
if errs[0] != "ssl is deprecated use tls instead" {
t.Fatalf("could not find deprecated message")
}
}
func Test_DecodeMap(t *testing.T) {
type labelCfg struct {
Labels map[string]map[string][]string
}
var cfg labelCfg
if err := DecodeFile(&cfg, filepath.Join("testdata", "map.conf"), ErrorHandler(errh(t))); err != nil {
t.Fatal(err)
}
expected := labelCfg{
Labels: map[string]map[string][]string{
"qemu": {
"arch": {"x86_64", "aarch64"},
"os": {"debian", "alpine"},
},
"docker": {
"programming": {"go", "js", "python"},
},
},
}
if !reflect.DeepEqual(cfg, expected) {
t.Fatalf("decoded configuration does not match\n\texpected =%v\n\tgot = %v\n", expected, cfg)
}
}