-
Notifications
You must be signed in to change notification settings - Fork 3
/
config_test.go
451 lines (376 loc) · 10.7 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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package config
import (
"encoding/base64"
"os"
"reflect"
"testing"
"time"
"github.com/num30/config/lib"
"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
)
type GlobalConfig struct {
Verbose bool
}
type FullConfig struct {
GlobalConfig `mapstructure:",squash"`
App LocalConfig
}
type LocalConfig struct {
Id string `flag:"id"`
FromEnvVar string
FromConfig string
OverriddenByEvnVar string
OverriddenByArg string
EnvVarName string `envvar:"CUSTOM_ENV_VAR"`
}
func Test_ConfigReader(t *testing.T) {
// arrange
resetFlags()
valFromVar := "valFromVar"
overriddenVar := "overriddenVar"
fromArgVal := "fromArgValue"
nc := &FullConfig{}
confReader := NewConfReader("myapp").WithSearchDirs("testdata")
os.Args = []string{"get", "--id", "10", "--verbose", "--app.overriddenbyarg", fromArgVal}
os.Setenv("APP_FROMENVVAR", valFromVar)
defer os.Unsetenv("APP_FROMENVVAR")
os.Setenv("APP_OVERRIDDENBYEVNVAR", overriddenVar)
defer os.Unsetenv("APP_OVERRIDDENBYEVNVAR")
os.Setenv("CUSTOM_ENV_VAR", "valFromEnvVar")
defer os.Unsetenv("CUSTOM_ENV_VAR")
// act
err := confReader.Read(nc)
// assert
if assert.NoError(t, err) {
assert.Equal(t, "10", nc.App.Id)
assert.Equal(t, true, nc.GlobalConfig.Verbose)
assert.Equal(t, "valFromConf", nc.App.FromConfig)
assert.Equal(t, valFromVar, nc.App.FromEnvVar)
assert.Equal(t, fromArgVal, nc.App.OverriddenByArg)
assert.Equal(t, "valFromEnvVar", nc.App.EnvVarName)
}
}
// flags are static so we have to reset them before each test
func resetFlags() {
pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)
os.Args = []string{"app"}
}
func Test_ReadFromFile(t *testing.T) {
t.Run("readSuccessfully", func(t *testing.T) {
resetFlags()
nc := &FullConfig{}
confReader := NewConfReader("myapp").WithSearchDirs("testdata")
err := confReader.Read(nc)
if assert.NoError(t, err) {
assert.Equal(t, "valFromConf", nc.App.FromConfig)
assert.Equal(t, true, nc.Verbose)
}
})
t.Run("malformedFile", func(t *testing.T) {
resetFlags()
nc := &FullConfig{}
confReader := NewConfReader("myapp-malformed").WithSearchDirs("testdata")
err := confReader.Read(nc)
if assert.Error(t, err, "failed to read") {
assert.Contains(t, err.Error(), "failed to unmarshal struct")
}
})
}
func Test_EnvVarsNoPrefix(t *testing.T) {
t.Run("readSuccessfully", func(t *testing.T) {
resetFlags()
nc := &FullConfig{}
confReader := NewConfReader("myapp")
os.Setenv("APP_FROMENVVAR", "valFromEnvVar")
err := confReader.Read(nc)
if assert.NoError(t, err) {
assert.Equal(t, "valFromEnvVar", nc.App.FromEnvVar)
}
})
}
func Test_EnvVarsWithPrefix(t *testing.T) {
resetFlags()
nc := &FullConfig{}
confReader := NewConfReader("myapp").WithPrefix("MYAPP")
os.Setenv("MYAPP_APP_FROMENVVAR", "valFromEnvVar")
defer os.Unsetenv("APP_FROMENVVAR")
err := confReader.Read(nc)
if assert.NoError(t, err) {
assert.Equal(t, "valFromEnvVar", nc.App.FromEnvVar)
}
}
func Test_ReadFromJsonFile(t *testing.T) {
resetFlags()
nc := &FullConfig{}
confReader := NewConfReader("myappjson")
confReader.configDirs = []string{"testdata"}
err := confReader.Read(nc)
if assert.NoError(t, err) {
assert.Equal(t, "valFromConf", nc.App.FromConfig)
assert.Equal(t, true, nc.Verbose)
}
}
func Test_WatchWithFile(t *testing.T) {
resetFlags()
nc := &FullConfig{}
if err := os.Mkdir("testdata/tmp", 0755); err != nil {
t.Fatal(err)
}
defer os.RemoveAll("testdata/tmp")
if err := os.WriteFile("testdata/tmp/changing_file.json", []byte(`{"verbose":"true"}`), 0644); err != nil {
t.Fatal(err)
}
confReader := NewConfReader("changing_file")
confReader.configDirs = []string{"testdata/tmp"}
t.Run("callWatchBeforeRead", func(t *testing.T) {
assert.Panics(t, func() {
confReader.Watch()
})
})
err := confReader.Read(nc)
if assert.NoError(t, err) {
assert.Equal(t, true, nc.Verbose)
}
mutex := confReader.Watch()
t.Run("configChanges", func(t *testing.T) {
err = os.WriteFile("testdata/tmp/changing_file.json", []byte(`{"verbose":"false"}`), 0644)
if assert.NoError(t, err) {
time.Sleep(10 * time.Millisecond)
assert.Equal(t, false, nc.Verbose)
}
})
t.Run("configStructLock", func(t *testing.T) {
mutex.RLock()
err = os.WriteFile("testdata/tmp/changing_file.json", []byte(`{"verbose":"true"}`), 0644)
if err != nil {
t.Fatal(err)
}
//time.Sleep(time.Second)
assert.Equal(t, false, nc.Verbose)
mutex.RUnlock()
time.Sleep(10 * time.Millisecond)
assert.Equal(t, true, nc.Verbose)
})
t.Run("doesNotFilaIfFileDeleted", func(t *testing.T) {
err = os.Remove("testdata/tmp/changing_file.json")
if err != nil {
t.Fatal(err)
}
time.Sleep(10 * time.Millisecond)
assert.Equal(t, true, nc.Verbose)
})
}
type dmParent struct {
GlobalConfig `mapstructure:",squash"`
Conf dmSibling `flag:"notAllowed"`
PtrConf *dmSibling
Par float64 `flag:"par"`
Duration time.Duration
}
type dmSibling struct {
Id string `flag:"id"`
FromEnvVar string
}
func Test_dumpStruct(t *testing.T) {
m := map[string]*flagInfo{}
c := &ConfReader{
Verbose: true,
}
c.dumpStruct(reflect.TypeOf(dmParent{}), "", m)
if assert.NotNil(t, m["verbose"]) {
assert.Equal(t, "verbose", m["verbose"].Name)
assert.Equal(t, "bool", m["verbose"].Type.String())
}
if assert.NotNil(t, m["conf.id"]) {
assert.Equal(t, "id", m["conf.id"].Name)
assert.Equal(t, "string", m["conf.id"].Type.String())
}
if assert.NotNil(t, m["ptrconf.id"]) {
assert.Equal(t, "id", m["ptrconf.id"].Name)
assert.Equal(t, "string", m["ptrconf.id"].Type.String())
}
if assert.NotNil(t, m["par"]) {
assert.Equal(t, "par", m["par"].Name)
assert.Equal(t, "float64", m["par"].Type.String())
}
if assert.NotNil(t, m["duration"]) {
assert.Equal(t, "duration", m["duration"].Name)
assert.Equal(t, "time.Duration", m["duration"].Type.String())
}
}
type ValidationConfig struct {
Host string `validate:"required"`
}
func Test_Validation(t *testing.T) {
cf := &ValidationConfig{}
reader := NewConfReader("myapp")
t.Run("fails", func(t *testing.T) {
resetFlags()
err := reader.Read(cf)
if assert.Error(t, err) {
assert.Equal(t, "validation error: Key: 'ValidationConfig.Host' Error:Field validation for 'Host' failed on the 'required' tag", err.Error())
}
})
t.Run("passes", func(t *testing.T) {
resetFlags()
os.Setenv("HOST", "localhost")
defer os.Unsetenv("HOST")
err := reader.Read(cf)
if assert.NoError(t, err) {
assert.Equal(t, "localhost", cf.Host)
}
})
}
func Test_SetDefault(t *testing.T) {
t.Run("defaultValuesSet", func(t *testing.T) {
type DefaultVals struct {
DB lib.PostgresqlDb
Test string `default:"test"`
}
resetFlags()
cf := &DefaultVals{}
reader := NewConfReader("def-vals")
err := reader.Read(cf)
if assert.NoError(t, err) {
assert.Equal(t, "localhost", cf.DB.Host)
assert.Equal(t, "test", cf.Test)
}
})
t.Run("failedToSetDefault", func(t *testing.T) {
type DefaultValsFail struct {
Test map[string]string `default:"test"`
}
resetFlags()
cf := &DefaultValsFail{}
reader := NewConfReader("def-vals-failed")
err := reader.Read(cf)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "failed to set default values")
}
})
}
type AllTypes struct {
Bool bool
Int int
Int8 int8
Int16 int16
Int32 int32
Int64 int64
Uint uint
Uint8 uint8
Uint16 uint16
Uint32 uint32
Uint64 uint64
Float32 float32
Float64 float64
String string
Bytes []byte
Duration time.Duration
Slice []string
}
func Test_AllTypesFlags(t *testing.T) {
resetFlags()
cf := &AllTypes{}
reader := NewConfReader("all-types")
// encode to base64
encoded := base64.StdEncoding.EncodeToString([]byte("test"))
os.Args = []string{"myapp", "--bool", "true", "--int", "1", "--int8", "1", "--int16", "1", "--int32", "1",
"--int64", "1", "--uint", "1", "--uint8", "1", "--uint16", "1", "--uint32", "1", "--uint64", "1",
"--float32", "1.1", "--float64", "1.1", "--string", "test", "--bytes", encoded, "--duration", "1m",
"--slice", "a", "--slice", "b"}
err := reader.Read(cf)
if assert.NoError(t, err) {
assert.Equal(t, true, cf.Bool)
assert.Equal(t, 1, cf.Int)
assert.Equal(t, int8(1), cf.Int8)
assert.Equal(t, int16(1), cf.Int16)
assert.Equal(t, int32(1), cf.Int32)
assert.Equal(t, int64(1), cf.Int64)
assert.Equal(t, uint(1), cf.Uint)
assert.Equal(t, uint8(1), cf.Uint8)
assert.Equal(t, uint16(1), cf.Uint16)
assert.Equal(t, uint32(1), cf.Uint32)
assert.Equal(t, uint64(1), cf.Uint64)
assert.Equal(t, float32(1.1), cf.Float32)
assert.Equal(t, float64(1.1), cf.Float64)
assert.Equal(t, "test", cf.String)
assert.Equal(t, []byte("test"), cf.Bytes)
assert.Equal(t, 1*time.Minute, cf.Duration)
assert.Equal(t, []string{"a", "b"}, cf.Slice)
}
}
type SliceConf struct {
Slice []string
}
func Test_Slice(t *testing.T) {
resetFlags()
t.Run("envVar", func(t *testing.T) {
cfg := SliceConf{}
os.Setenv("SLICE", "a,b,c")
defer os.Unsetenv("SLICE")
reader := NewConfReader("slice")
err := reader.Read(&cfg)
if assert.NoError(t, err) {
assert.Equal(t, []string{"a", "b", "c"}, cfg.Slice)
}
})
t.Run("file", func(t *testing.T) {
cfg := SliceConf{}
reader := NewConfReader("slice").WithSearchDirs("testdata")
err := reader.Read(&cfg)
if assert.NoError(t, err) {
assert.Equal(t, []string{"a", "b", "c"}, cfg.Slice)
}
})
t.Run("cmdArgs", func(t *testing.T) {
cfg := SliceConf{}
os.Args = []string{"myapp", "--slice", "a", "--slice", "b"}
reader := NewConfReader("slice-args")
err := reader.Read(&cfg)
if assert.NoError(t, err) {
assert.Equal(t, []string{"a", "b"}, cfg.Slice)
}
})
}
func Test_ByteArray(t *testing.T) {
resetFlags()
cfg := struct {
Bytes []byte
}{}
t.Run("envVar", func(t *testing.T) {
encoded := base64.StdEncoding.EncodeToString([]byte("test"))
os.Args = []string{"myapp", "--bytes", encoded}
reader := NewConfReader("slice")
err := reader.Read(&cfg)
if assert.NoError(t, err) {
assert.Equal(t, "test", string(cfg.Bytes))
}
})
}
func Test_InputStructErrors(t *testing.T) {
resetFlags()
confReader := NewConfReader("myapp").WithSearchDirs("testdata")
t.Run("notAPointer", func(t *testing.T) {
var nc FullConfig
err := confReader.Read(nc)
if assert.Error(t, err, "failed to read") {
assert.Contains(t, err.Error(), "config struct must be pointer")
}
})
t.Run("nil", func(t *testing.T) {
err := confReader.Read(nil)
if assert.Error(t, err, "failed to read") {
assert.Contains(t, err.Error(), "config struct is nil")
}
})
t.Run("zero", func(t *testing.T) {
var nc *FullConfig
err := confReader.Read(nc)
if assert.Error(t, err, "failed to read") {
assert.Contains(t, err.Error(), "config struct is nil")
}
})
}
func Test_ErrorHandling(t *testing.T) {
}