-
Notifications
You must be signed in to change notification settings - Fork 2
/
env_test.go
243 lines (207 loc) · 6.37 KB
/
env_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
package settings
import (
"errors"
"testing"
"time"
"github.com/go-playground/validator/v10"
)
type emptySettings struct{}
type Settings struct {
SessionName string `env:"SESSION" validate:"required"`
PublicKeyPath string `env:"KEY_PATH" validate:"required"`
}
type settingWithRequiredIf struct {
RequiredIf string `env:"NOTFOUND" validate:"required_if=Trigger true"`
Trigger bool `env:"STDOUT"`
}
type Int16AndFloat64 struct {
PORT int16 `env:"PORT"`
FLOAT float64 `env:"FLOAT"`
}
// settings with logrus.Level and time.Duration 4
type settings4 struct {
Port uint16 `env:"PORT"`
Timeout time.Duration `env:"TIMEOUT"`
Stdout bool `env:"STDOUT"`
}
// settings with unsupported int8
type Int8 struct {
Port int8 `env:"PORT"`
}
type NotAStruct string
// complex example
type settingsWithStruct struct {
Port string `env:"PORT"`
PathToDatabase string `env:"DB"`
Internal *InternalStruct
}
// complex example 2
type settingsWithStruct2 struct {
Port int64 `env:"PORT"`
PathToDatabase string `env:"DB"`
Internal InternalStruct
}
// complex example 3 with omitted field
type settingsWithStruct3 struct {
Port int64 `env:"PORT"`
PathToDatabase string `env:"DB"`
Internal *InternalStruct `env:"-"`
}
type settingsWithRequiredTag struct {
PathToDatabase string `env:"DB2" validate:"required"`
}
type InternalStruct struct {
CacheSize string `env:"CACHE"`
}
// test structure #1
type goodEnvironmentSettings1 struct {
Port string `env:"PORT" validate:"required"`
PathToDatabase string `env:"DB"`
}
// test structure #2
type goodEnvironmentSettings3withEmptyString struct {
Port string
PathToDatabase string `env:"DB"`
}
// test structure #3
type goodEnvironmentSettings2 struct {
Port uint32 `env:"PORT"`
PathToDatabase string `env:"DB"`
CacheSize byte `env:"CACHE"`
}
// test structure #4
type badEnvironmentSettings2 struct {
Port uint32 `env:"PORT"`
PathToDatabase string `env:"DB"`
CacheSize byte `env:"BADCACHE1"`
}
// test structure #5
type badEnvironmentSettings3 struct {
Port int64 `env:"PORT"`
PathToDatabase string `env:"DB"`
CacheSize byte `env:"BADCACHE2"`
}
// test structure #6
type badEnvironmentSettings4 struct {
Port string `env:"PORT"`
PathToDatabase string `env:"DB"`
CacheSize byte `env:"BADCACHE3"`
}
// test structure #7
type goodEnvironmentSettings1PlusValidation struct {
Port string `env:"PORT" validate:"numeric"`
PathToDatabase string `env:"DB" validate:"required"`
}
// test structure #8
type badEnvironmentSettings1PlusValidation struct {
Port string `env:"BADPORT" validate:"numeric"`
PathToDatabase string `env:"DB" validate:"required"`
}
// test structure #9
type badEnvironmentSettings2PlusValidation struct {
Port string `env:"PORT" validate:"numeric"`
PathToDatabase string `env:"DB" validate:"required"`
CacheSize byte `env:"CACHE" validate:"min=10"`
}
type simpleConfig struct {
DBURL string `default:"127.0.0.1" env:"DB_URL" validate:"required"`
Timeout time.Duration `default:"5s" env:"DB_TIMEOUT"`
}
type pigPort struct {
Port uint16 `env:"BIG_PORT" validate:"required"`
}
func TestLoadUsingReflect(t *testing.T) {
// ENV settings PORT=80;DB=db/file;CACHE=5;BADCACHE1=i;BADCACHE2=300
t.Setenv("PORT", "80")
t.Setenv("FLOAT", "80.1")
t.Setenv("DB", "db/file")
t.Setenv("CACHE", "5")
t.Setenv("BADCACHE1", "i")
t.Setenv("BADCACHE2", "300")
t.Setenv("BADCACHE3", "-1")
t.Setenv("LOG_LEVEL", "debug")
t.Setenv("SYSLOG_LEVEL", "info")
t.Setenv("TIMEOUT", "20s")
t.Setenv("BADPORT", "a")
t.Setenv("STDOUT", "true")
t.Setenv("SESSION", "session")
t.Setenv("KEY_PATH", "/etc")
t.Setenv("PROD", "true")
t.Setenv("HAS_DB", "true")
t.Setenv("DOMAIN", "3lines.club")
t.Setenv("EMAIL", "email@3lines.club")
t.Setenv("BIG_PORT", "25060")
var goodSettings1 goodEnvironmentSettings1
var goodSettings3withEmptyString goodEnvironmentSettings3withEmptyString
var good2 goodEnvironmentSettings2
var badSettings2 badEnvironmentSettings2
var badSettings3 badEnvironmentSettings3
var badSettings4 badEnvironmentSettings4
var goodSettings5 goodEnvironmentSettings1PlusValidation
var badSettings5 badEnvironmentSettings1PlusValidation
var badSettings6 badEnvironmentSettings2PlusValidation
var notAStruct NotAStruct
var complex1 = settingsWithStruct{}
var complex2 = &complex1
var complex3 = settingsWithStruct2{}
var requiredField = settingsWithRequiredTag{}
var simple simpleConfig
tests := []struct {
name string
settings interface{}
wantErr error
}{
{"ok1", &goodSettings1, nil},
{"ok2", &goodSettings3withEmptyString, nil},
{"!ok1", good2, ErrNotAddressableField},
{"ok3", &good2, nil},
{"!ok2", &badSettings2, ErrIncorrectFieldValue},
{"!ok3", &badSettings3, ErrIncorrectFieldValue},
{"!ok4", &badSettings4, ErrIncorrectFieldValue},
{"ok4", &goodSettings5, nil},
{"!ok5", &badSettings5, ErrValidationFailed},
{"!ok6", &badSettings6, ErrValidationFailed},
{"!ok7", notAStruct, ErrNotAStruct},
{"!ok8", ¬AStruct, ErrNotAStruct},
{"complex double pointer", &complex2, nil},
{"complex with pointer", &complex1, nil},
{"complex with a struct without pointer", &complex3, nil},
{"complex with required tag", &requiredField, ErrValidationFailed},
{"duration", &settings4{}, nil},
{"int8", &Int8{}, nil},
{"int16", &Int16AndFloat64{}, nil},
{"empty", &emptySettings{}, ErrTheModelHasEmptyStruct},
{"required if failed", &settingWithRequiredIf{}, ErrValidationFailed},
{"not_set_env", &simple, nil},
{"omitted field", &settingsWithStruct3{}, nil},
{"an example", &Settings{}, nil},
{"big port", &pigPort{}, nil},
}
var err error
//nolint
for _, tt := range tests {
t.Log("\n\n")
v := validator.New()
t.Run(tt.name, func(t *testing.T) {
err = Load(tt.settings)
t.Logf("settings: %v", tt.settings)
if errors.Is(err, tt.wantErr) {
if err != nil {
t.Log(err)
}
err = v.Struct(tt.settings)
if err != nil {
t.Logf("additional validation result: %s", err)
}
return
} else if tt.wantErr == ErrValidationFailed {
validationError, ok := err.(validator.ValidationErrors)
if ok {
t.Log(validationError)
return
}
}
t.Errorf("Load() error is incorrect\nhave %v\nwant %v", err, tt.wantErr)
})
}
}