-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
578 lines (511 loc) · 15.3 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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
package gviper
import (
"github.com/ace-zhaoy/errors"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"testing"
"time"
)
func TestDefault(t *testing.T) {
d := t.TempDir()
t.Logf("tmpdir: %s", d)
serverConfigFile := filepath.Join(d, "server.yaml")
err := os.WriteFile(serverConfigFile, []byte("name: gviper\ndate: 2021-11-17T16:25:15+08:00"), 0644)
if err != nil {
t.Fatalf("Failed to create server.yaml: %v", err)
}
type MyServer struct {
Name string `json:"name"`
Date time.Time `json:"date"`
}
var myServer MyServer
config := Default(d)
config.Bind("server", &myServer)
err = config.Load()
if err != nil {
t.Fatalf("Failed to load config: %v", err)
}
td, _ := time.Parse(time.RFC3339, "2021-11-17T16:25:15+08:00")
assert.Equal(t, MyServer{
Name: "gviper",
Date: td,
}, myServer)
}
func TestNewConfig(t *testing.T) {
config := NewConfig("/test/path")
if config.configPath != "/test/path" {
t.Errorf("Expected config path to be /test/path, got %s", config.configPath)
}
}
func TestConfig_AutomaticEnv(t *testing.T) {
config := NewConfig(".")
config.AutomaticEnv()
t.Setenv("TEST_ENV", "test")
t.Setenv("TEST_ENV_2", "")
if config.viper.Get("test_env") != "test" {
t.Errorf("Expected test_env to be test, got %s", config.viper.Get("test_env"))
}
if config.viper.Get("test_env_2") != nil {
t.Errorf("Expected test_env_2 to be nil, got %s", config.viper.Get("test_env_2"))
}
if config.viper.Get("test_env_3") != nil {
t.Errorf("Expected test_env_3 to be nil, got %s", config.viper.Get("test_env_3"))
}
}
func TestConfig_AllowEmptyEnv(t *testing.T) {
config := NewConfig(".")
config.AutomaticEnv()
config.AllowEmptyEnv(true)
t.Setenv("TEST_ENV", "test")
t.Setenv("TEST_ENV_2", "")
if config.viper.Get("test_env") != "test" {
t.Errorf("Expected test_env to be test, got %s", config.viper.Get("test_env"))
}
if config.viper.Get("test_env_2") != "" {
t.Errorf("Expected test_env_2 to be '', got %s", config.viper.Get("test_env_2"))
}
if config.viper.Get("test_env_3") != nil {
t.Errorf("Expected test_env_3 to be nil, got %s", config.viper.Get("test_env_3"))
}
}
func TestConfig_Register(t *testing.T) {
config := NewConfig("/test/path", "config1")
config.Register("config2")
config.Register("config3.json")
wants := []configParam{
{
configName: "config1",
configType: "yaml",
configFile: "/test/path/config1.yaml",
},
{
configName: "config2",
configType: "yaml",
configFile: "/test/path/config2.yaml",
},
{
configName: "config3",
configType: "json",
configFile: "/test/path/config3.json",
},
}
if len(config.configs) != len(wants) {
t.Errorf("Expected %d configs, got %d", len(wants), len(config.configs))
}
for i, want := range wants {
if config.configs[i].configName != want.configName {
t.Errorf("Expected config name to be %s, got %s", want.configName, config.configs[i].configName)
}
if config.configs[i].configType != want.configType {
t.Errorf("Expected config type to be %s, got %s", want.configType, config.configs[i].configType)
}
if config.configs[i].configFile != want.configFile {
t.Errorf("Expected config file to be %s, got %s", want.configFile, config.configs[i].configFile)
}
}
}
func TestDefaultConfigType(t *testing.T) {
type args struct {
defaultConfigType string
}
tests := []struct {
name string
args args
want string
}{
{
name: "test json",
args: args{defaultConfigType: "json"},
want: "json",
},
{
name: "test ini",
args: args{defaultConfigType: "ini"},
want: "ini",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewConfig(".")
got.defaultConfigType = tt.args.defaultConfigType
got.Register("config1")
if got.configs[0].configType != tt.want {
t.Errorf("Expected config type to be %s, got %s", tt.want, got.configs[0].configType)
}
})
}
}
func TestConfig_RegisterNotification(t *testing.T) {
config := NewConfig("/test/path", "config.yaml")
notification1 := &MockNotification{}
notification2 := &MockNotification{}
config.RegisterNotification(notification1, notification2)
if len(config.notifications) != 2 {
t.Errorf("Expected 2 notifications, got %d", len(config.notifications))
}
if config.notifications[0] != notification1 {
t.Error("Expected first notification to be notification1")
}
if config.notifications[1] != notification2 {
t.Error("Expected second notification to be notification2")
}
}
func TestConfig_Load(t *testing.T) {
d := t.TempDir()
t.Logf("tmpdir: %s", d)
err := os.WriteFile(filepath.Join(d, "server.yaml"), []byte("name: gviper\nenv: test\nhttp:\n port: 8080"), 0644)
if err != nil {
t.Fatalf("Failed to create server.yaml: %v", err)
}
err = os.WriteFile(filepath.Join(d, "log.toml"), []byte("type = \"console\"\nlevel = \"ACCESS\""), 0644)
if err != nil {
t.Fatalf("Failed to create log.toml: %v", err)
}
config := NewConfig(d, "server", "log.toml")
err = config.Load()
if err != nil {
t.Fatalf("Failed to load config: %v", err)
}
type args struct {
key string
}
tests := []struct {
name string
args args
want any
}{
{
name: "test1",
args: args{"server.name"},
want: "gviper",
},
{
name: "test2",
args: args{"server.env"},
want: "test",
},
{
name: "test3",
args: args{"server.http.port"},
want: 8080,
},
{
name: "test4",
args: args{"log.type"},
want: "console",
},
{
name: "test5",
args: args{"log.level"},
want: "ACCESS",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if config.Get(tt.args.key) != tt.want {
t.Errorf("Expected config value to be %v, got %v", tt.want, config.Get(tt.args.key))
}
})
}
}
func TestConfig_Load_error(t *testing.T) {
config := NewConfig(".", "server", "log.toml")
err := config.Load()
assert.Error(t, err)
}
func TestConfig_Watch(t *testing.T) {
d := t.TempDir()
t.Logf("tmpdir: %s", d)
serverConfigFile := filepath.Join(d, "server.yaml")
err := os.WriteFile(serverConfigFile, []byte("name: gviper"), 0644)
if err != nil {
t.Fatalf("Failed to create server.yaml: %v", err)
}
logConfigFile := filepath.Join(d, "log.toml")
err = os.WriteFile(logConfigFile, []byte("type = \"console\"\nlevel = \"ACCESS\""), 0644)
if err != nil {
t.Fatalf("Failed to create log.toml: %v", err)
}
config := NewConfig(d, "server", "log.toml")
err = config.Load()
if err != nil {
t.Fatalf("Failed to load config: %v", err)
}
config.Watch()
assert.Equal(t, "gviper", config.Get("server.name"))
assert.Equal(t, nil, config.Get("server.env"))
assert.Equal(t, "ACCESS", config.Get("log.level"))
assert.Equal(t, nil, config.Get("log.rotate"))
f1, err := os.OpenFile(serverConfigFile, os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
t.Fatalf("Failed to open server.yaml: %v", err)
}
_, err = f1.WriteString("\nenv: test")
if err != nil {
t.Fatalf("Failed to write to server.yaml: %v", err)
}
_ = f1.Close()
f2, err := os.OpenFile(logConfigFile, os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
t.Fatalf("Failed to open log.toml: %v", err)
}
_, err = f2.WriteString("\nrotate = 1")
if err != nil {
t.Fatalf("Failed to write to log.toml: %v", err)
}
_ = f2.Close()
time.Sleep(10 * time.Millisecond)
assert.Equal(t, "gviper", config.Get("server.name"))
assert.Equal(t, "test", config.Get("server.env"))
assert.Equal(t, "ACCESS", config.Get("log.level"))
assert.Equal(t, int64(1), config.Get("log.rotate"))
}
func TestConfig_OnChange(t *testing.T) {
d := t.TempDir()
t.Logf("tmpdir: %s", d)
serverConfigFile := filepath.Join(d, "server.yaml")
err := os.WriteFile(serverConfigFile, []byte("name: gviper"), 0644)
if err != nil {
t.Fatalf("Failed to create server.yaml: %v", err)
}
ch := make(chan any, 1)
config := NewConfig(d)
config.OnChange("server", func(v *viper.Viper) error {
ch <- v.Get("env")
return nil
})
err = config.Load()
if err != nil {
t.Fatalf("Failed to load config: %v", err)
}
config.Watch()
f1, err := os.OpenFile(serverConfigFile, os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
t.Fatalf("Failed to open server.yaml: %v", err)
}
_, err = f1.WriteString("\nenv: test")
if err != nil {
t.Fatalf("Failed to write to server.yaml: %v", err)
}
_ = f1.Close()
v := <-ch
t.Logf("load success")
assert.Equal(t, nil, v)
v = <-ch
t.Logf("change success")
assert.Equal(t, "test", v)
close(ch)
}
func TestConfig_Bind(t *testing.T) {
d := t.TempDir()
t.Logf("tmpdir: %s", d)
serverConfigFile := filepath.Join(d, "server.yaml")
err := os.WriteFile(serverConfigFile, []byte("name: gviper"), 0644)
if err != nil {
t.Fatalf("Failed to create server.yaml: %v", err)
}
type MyServer struct {
Name string `json:"name"`
Env string `json:"env"`
}
var myServer MyServer
config := NewConfig(d)
config.Bind("server", &myServer)
err = config.Load()
if err != nil {
t.Fatalf("Failed to load config: %v", err)
}
config.Watch()
assert.Equal(t, MyServer{
Name: "gviper",
Env: "",
}, myServer)
f1, err := os.OpenFile(serverConfigFile, os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
t.Fatalf("Failed to open server.yaml: %v", err)
}
_, err = f1.WriteString("\nenv: test")
if err != nil {
t.Fatalf("Failed to write to server.yaml: %v", err)
}
_ = f1.Close()
time.Sleep(10 * time.Millisecond)
assert.Equal(t, MyServer{
Name: "gviper",
Env: "test",
}, myServer)
}
func TestConfig_BindWithTag(t *testing.T) {
d := t.TempDir()
t.Logf("tmpdir: %s", d)
serverConfigFile := filepath.Join(d, "server.yaml")
err := os.WriteFile(serverConfigFile, []byte("name: gviper"), 0644)
if err != nil {
t.Fatalf("Failed to create server.yaml: %v", err)
}
type MyServer struct {
Name string `yaml:"name"`
Env string `yaml:"env"`
}
var myServer MyServer
config := NewConfig(d)
config.BindWithTag("server", &myServer, "yaml")
err = config.Load()
if err != nil {
t.Fatalf("Failed to load config: %v", err)
}
config.Watch()
assert.Equal(t, MyServer{
Name: "gviper",
Env: "",
}, myServer)
f1, err := os.OpenFile(serverConfigFile, os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
t.Fatalf("Failed to open server.yaml: %v", err)
}
_, err = f1.WriteString("\nenv: test")
if err != nil {
t.Fatalf("Failed to write to server.yaml: %v", err)
}
_ = f1.Close()
time.Sleep(10 * time.Millisecond)
assert.Equal(t, MyServer{
Name: "gviper",
Env: "test",
}, myServer)
}
type MyNotification struct {
configName string
err error
}
func (m *MyNotification) Notify(configName string, err error) {
m.configName = configName
m.err = err
}
func TestConfig_Watch_RegisterNotification(t *testing.T) {
d := t.TempDir()
t.Logf("tmpdir: %s", d)
serverConfigFile := filepath.Join(d, "server.yaml")
err := os.WriteFile(serverConfigFile, []byte("name: gviper"), 0644)
if err != nil {
t.Fatalf("Failed to create server.yaml: %v", err)
}
type MyServer struct {
Name string `json:"name"`
Env string `json:"env"`
Port int64 `json:"port"`
}
var myServer MyServer
myNotification := &MyNotification{}
config := NewConfig(d)
config.Bind("server", &myServer)
config.RegisterNotification(myNotification)
err = config.Load()
if err != nil {
t.Fatalf("Failed to load config: %v", err)
}
config.Watch()
assert.Equal(t, "", myNotification.configName)
assert.Equal(t, nil, myNotification.err)
assert.Equal(t, false, errors.Is(myNotification.err, errors.NewWithMessage("read config [%s] error", "server")))
assert.Equal(t, false, errors.Is(myNotification.err, errors.NewWithMessage("unmarshal config [%s] failed", "server")))
f2, err := os.OpenFile(serverConfigFile, os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
t.Fatalf("Failed to open server.yaml: %v", err)
}
_, err = f2.WriteString("\nport: noport")
if err != nil {
t.Fatalf("Failed to write to server.yaml: %v", err)
}
_ = f2.Close()
time.Sleep(10 * time.Millisecond)
assert.Equal(t, "server", myNotification.configName)
assert.Equal(t, true, errors.Is(myNotification.err, errors.NewWithMessage("unmarshal config [%s] failed", "server")))
myNotification.configName = ""
myNotification.err = nil
f1, err := os.OpenFile(serverConfigFile, os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
t.Fatalf("Failed to open server.yaml: %v", err)
}
_, err = f1.WriteString("env: test")
if err != nil {
t.Fatalf("Failed to write to server.yaml: %v", err)
}
_ = f1.Close()
time.Sleep(10 * time.Millisecond)
assert.Equal(t, "server", myNotification.configName)
assert.Equal(t, true, errors.Is(myNotification.err, errors.NewWithMessage("read config [%s] error", "server")))
}
func TestConfig_find(t *testing.T) {
config := &Config{
configs: []*configParam{
{configName: "config1"},
{configName: "config2"},
},
}
result := config.find("config1")
assert.NotNil(t, result)
assert.Equal(t, "config1", result.configName)
result = config.find("config3")
assert.Nil(t, result)
}
func TestConfig_add(t *testing.T) {
config := &Config{
configs: []*configParam{},
}
configName := "test"
configType := "yaml"
configFile := "test.yaml"
cp := config.add(configName, configType, configFile)
assert.Equal(t, configName, cp.configName)
assert.Equal(t, configType, cp.configType)
assert.Equal(t, configFile, cp.configFile)
assert.Equal(t, 1, len(config.configs))
cp2 := config.add(configName, configType, configFile)
assert.Equal(t, cp, cp2)
assert.Equal(t, 1, len(config.configs))
}
func TestConfig_viper_method(t *testing.T) {
config := NewConfig(".")
m := map[string]any{
"string": "hello",
"bool": true,
"int": 1,
"int32": int32(1),
"int64": int64(1),
"uint": uint(1),
"uint32": uint32(1),
"uint64": uint64(1),
"float64": float64(1.0),
"time": time.Unix(1725271117, 0),
"duration": time.Second,
"intSlice": []int{1, 2, 3},
"stringSlice": []string{"a", "b", "c"},
"stringMap": map[string]any{"a": "b", "c": "d"},
"stringMapString": map[string]string{"a": "b", "c": "d"},
"stringMapStringSlice": map[string][]string{"a": {"b", "c"}, "d": {"e", "f"}},
"sizeInBytes": "1kb",
}
config.viper.Set("test", m)
assert.Equal(t, true, config.Has("test.string"))
assert.Equal(t, false, config.IsSet("test.string11"))
assert.Equal(t, m["string"], config.GetString("test.string"))
assert.Equal(t, m["bool"], config.GetBool("test.bool"))
assert.Equal(t, m["int"], config.GetInt("test.int"))
assert.Equal(t, m["int32"], config.GetInt32("test.int32"))
assert.Equal(t, m["int64"], config.GetInt64("test.int64"))
assert.Equal(t, m["uint"], config.GetUint("test.uint"))
assert.Equal(t, m["uint32"], config.GetUint32("test.uint32"))
assert.Equal(t, m["uint64"], config.GetUint64("test.uint64"))
assert.Equal(t, m["float64"], config.GetFloat64("test.float64"))
assert.Equal(t, m["time"], config.GetTime("test.time"))
assert.Equal(t, m["duration"], config.GetDuration("test.duration"))
assert.Equal(t, m["intSlice"], config.GetIntSlice("test.intSlice"))
assert.Equal(t, m["stringSlice"], config.GetStringSlice("test.stringSlice"))
assert.Equal(t, m["stringMap"], config.GetStringMap("test.stringMap"))
assert.Equal(t, m["stringMapString"], config.GetStringMapString("test.stringMapString"))
assert.Equal(t, m["stringMapStringSlice"], config.GetStringMapStringSlice("test.stringMapStringSlice"))
assert.Equal(t, uint(1024), config.GetSizeInBytes("test.sizeInBytes"))
}