-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyamlcfg_test.go
134 lines (117 loc) · 3.05 KB
/
yamlcfg_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
package yamlcfg
import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
"path/filepath"
"regexp"
"testing"
)
type testCfg1 struct {
ConfigSource_ string
Test1 string `yaml:"test1"`
Test2 map[string]string `yaml:"test2"`
Test3 string
Bool1 bool `yaml:"bool1"`
Bool2 bool `yaml:"bool2"`
}
func (c *testCfg1) SetConfigPath(s string) {
c.ConfigSource_ = s
}
type testCfg2 struct {
Test1 string `yaml:"test1"`
}
type testCfg3 struct {
Test3 string `yaml:"test3"`
path string
}
var testCfg3Default = `---
test3: testing
`
func (c *testCfg3) GetDefaultConfig() string {
return testCfg3Default
}
func (c *testCfg3) SetConfigPath(s string) {
c.path = s
}
func TestService(t *testing.T) {
var c testCfg1
y := testCfg1{
ConfigSource_: "t-data/t1.cfg",
Test1: "aaa",
Test2: map[string]string{"test3": "123"},
Test3: "bbb",
Bool1: true,
Bool2: true,
}
t.Run("load yaml", func(t *testing.T) {
err := LoadConfig([]string{"./t-data/t1.cfg"}, &c)
require.NoError(t, err)
assert.Contains(t, y.ConfigSource_, "t-data/t1.cfg")
y.ConfigSource_ = c.ConfigSource_
assert.Equal(t, y, c)
})
t.Run("nonexistent error", func(t *testing.T) {
err := LoadConfig([]string{"/nonexisting/file"}, &c)
assert.Error(t, err)
})
t.Run("partial config", func(t *testing.T) {
var partial testCfg2
err := LoadConfig([]string{"./t-data/t1.cfg"}, &partial)
require.NoError(t, err)
assert.Equal(t, "aaa", partial.Test1)
})
t.Run("no config", func(t *testing.T) {
err := LoadConfig([]string{}, &c)
require.Error(t, err)
})
}
func TestDefaultGeneration(t *testing.T) {
var c testCfg3
tPath, err := filepath.Abs(os.ExpandEnv(`./t-data/t3.cfg.local`))
require.NoError(t, err)
// ensure file does not exist
if _, err := os.Stat(tPath); err == nil {
err := os.Remove(tPath)
if err != nil {
t.Fatal("test setup error - can't remove file")
}
}
err = LoadConfig([]string{"./t-data/t3.cfg.local"}, &c)
assert.NoError(t, err)
assert.Contains(t, c.path, `/t-data/t3.cfg.local`)
}
func TestRandomString(t *testing.T) {
str1 := RandomString("abcd1", 100)
abcdRe := regexp.MustCompile(`^[abcd1]+$`)
matched := abcdRe.MatchString(str1)
assert.Len(t, str1, 100)
assert.True(t, matched)
}
type testCfgValidate struct {
Test1 string `yaml:"test1"`
}
func (c *testCfgValidate) Validate() error {
return fmt.Errorf("err out on validate")
}
func TestValidate(t *testing.T) {
var cfg testCfgValidate
err := LoadConfig([]string{"./t-data/t1.cfg"}, &cfg)
assert.EqualError(t, err, "err out on validate")
}
type testCfgTemplated struct {
Templated string `yaml:"templated"`
Env string `yaml:"env"`
}
func (t *testCfgTemplated) GetSecret(s string) string {
return "secret" + s
}
func TestTemplated(t *testing.T) {
c := testCfgTemplated{}
os.Setenv("_yamlcfg_test", "test env key")
err := LoadConfig([]string{"./t-data/t1.cfg"}, &c)
require.NoError(t, err)
assert.Equal(t, "secrettest", c.Templated)
assert.Equal(t, "test env key", c.Env)
}