-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig_test.go
356 lines (341 loc) · 9.37 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
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package dbkit
import (
"bytes"
"database/sql"
"encoding/json"
"testing"
"time"
"github.com/acronis/go-appkit/config"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
type AppConfig struct {
DB *Config `mapstructure:"db" json:"db" yaml:"db"`
}
func TestConfig(t *testing.T) {
supportedDialects := []Dialect{DialectSQLite, DialectMySQL, DialectPostgres, DialectPgx, DialectMSSQL}
tests := []struct {
name string
cfgData string
expectedCfg func() *Config
}{
{
name: "mysql dialect",
cfgData: `
db:
maxOpenConns: 20
maxIdleConns: 10
connMaxLifeTime: 2m
dialect: mysql
mysql:
host: mysql-host
port: 3307
database: mysql_db
user: mysql-user
password: mysql-password
txLevel: "Repeatable Read"
`,
expectedCfg: func() *Config {
cfg := NewDefaultConfig(supportedDialects)
cfg.Dialect = DialectMySQL
cfg.MaxOpenConns = 20
cfg.MaxIdleConns = 10
cfg.ConnMaxLifetime = config.TimeDuration(2 * time.Minute)
cfg.MySQL.Host = "mysql-host"
cfg.MySQL.Port = 3307
cfg.MySQL.Database = "mysql_db"
cfg.MySQL.User = "mysql-user"
cfg.MySQL.Password = "mysql-password"
cfg.MySQL.TxIsolationLevel = IsolationLevel(sql.LevelRepeatableRead)
return cfg
},
},
{
name: "postgres dialect, github.com/lib/pq driver",
cfgData: `
db:
dialect: postgres
postgres:
host: pg-host
port: 5433
database: pg_db
user: pg-user
password: pg-password
txLevel: "Read Committed"
sslMode: verify-full
searchPath: pg-search
`,
expectedCfg: func() *Config {
cfg := NewDefaultConfig(supportedDialects)
cfg.Dialect = DialectPostgres
cfg.Postgres.Host = "pg-host"
cfg.Postgres.Port = 5433
cfg.Postgres.Database = "pg_db"
cfg.Postgres.User = "pg-user"
cfg.Postgres.Password = "pg-password"
cfg.Postgres.TxIsolationLevel = IsolationLevel(sql.LevelReadCommitted)
cfg.Postgres.SSLMode = PostgresSSLModeVerifyFull
cfg.Postgres.SearchPath = "pg-search"
return cfg
},
},
{
name: "postgres dialect, github.com/jackc/pgx driver",
cfgData: `
db:
dialect: pgx
postgres:
host: pg-host
port: 5433
database: pg_db
user: pg-user
password: pg-password
txLevel: "Serializable"
sslMode: verify-full
searchPath: pg-search
`,
expectedCfg: func() *Config {
cfg := NewDefaultConfig(supportedDialects)
cfg.Dialect = DialectPgx
cfg.Postgres.Host = "pg-host"
cfg.Postgres.Port = 5433
cfg.Postgres.Database = "pg_db"
cfg.Postgres.User = "pg-user"
cfg.Postgres.Password = "pg-password"
cfg.Postgres.TxIsolationLevel = IsolationLevel(sql.LevelSerializable)
cfg.Postgres.SSLMode = PostgresSSLModeVerifyFull
cfg.Postgres.SearchPath = "pg-search"
return cfg
},
},
{
name: "postgres dialect, github.com/jackc/pgx driver, overridden target_session_attrs",
cfgData: `
db:
dialect: pgx
postgres:
host: pg-host
port: 5433
database: pg_db
user: pg-user
password: pg-password
txLevel: Repeatable Read
sslMode: verify-full
searchPath: pg-search
additionalParameters:
target_session_attrs: read-only
`,
expectedCfg: func() *Config {
cfg := NewDefaultConfig(supportedDialects)
cfg.Dialect = DialectPgx
cfg.Postgres.Host = "pg-host"
cfg.Postgres.Port = 5433
cfg.Postgres.Database = "pg_db"
cfg.Postgres.User = "pg-user"
cfg.Postgres.Password = "pg-password"
cfg.Postgres.TxIsolationLevel = IsolationLevel(sql.LevelRepeatableRead)
cfg.Postgres.SSLMode = PostgresSSLModeVerifyFull
cfg.Postgres.SearchPath = "pg-search"
cfg.Postgres.AdditionalParameters = map[string]string{"target_session_attrs": "read-only"}
return cfg
},
},
{
name: "mssql dialect",
cfgData: `
db:
dialect: mssql
mssql:
host: mssql-host
port: 1433
database: mssql_db
user: mssql-user
password: mssql-password
txLevel: Repeatable Read
`,
expectedCfg: func() *Config {
cfg := NewDefaultConfig(supportedDialects)
cfg.Dialect = DialectMSSQL
cfg.MSSQL.Host = "mssql-host"
cfg.MSSQL.Port = 1433
cfg.MSSQL.Database = "mssql_db"
cfg.MSSQL.User = "mssql-user"
cfg.MSSQL.Password = "mssql-password"
cfg.MSSQL.TxIsolationLevel = IsolationLevel(sql.LevelRepeatableRead)
return cfg
},
},
{
name: "sqlite dialect",
cfgData: `
db:
maxOpenConns: 20
maxIdleConns: 10
connMaxLifeTime: 1m
dialect: sqlite3
sqlite3:
path: ":memory:"
`,
expectedCfg: func() *Config {
cfg := NewDefaultConfig(supportedDialects)
cfg.Dialect = DialectSQLite
cfg.MaxOpenConns = 20
cfg.MaxIdleConns = 10
cfg.ConnMaxLifetime = config.TimeDuration(time.Minute)
cfg.SQLite.Path = ":memory:"
return cfg
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for _, dataType := range []config.DataType{config.DataTypeYAML, config.DataTypeJSON} {
cfgData := tt.cfgData
if dataType == config.DataTypeJSON {
cfgData = string(mustYAMLToJSON([]byte(cfgData)))
}
// Load config using config.Loader.
appCfg := AppConfig{DB: NewDefaultConfig(supportedDialects)}
expectedAppCfg := AppConfig{DB: tt.expectedCfg()}
if expectedAppCfg.DB.Dialect == DialectPgx && expectedAppCfg.DB.Postgres.AdditionalParameters == nil {
expectedAppCfg.DB.Postgres.AdditionalParameters = map[string]string{"target_session_attrs": "read-write"}
}
cfgLoader := config.NewLoader(config.NewViperAdapter())
err := cfgLoader.LoadFromReader(bytes.NewBuffer([]byte(cfgData)), dataType, appCfg.DB)
require.NoError(t, err)
require.Equal(t, expectedAppCfg, appCfg)
// Load config using viper unmarshal.
appCfg = AppConfig{DB: NewDefaultConfig(supportedDialects)}
expectedAppCfg = AppConfig{DB: tt.expectedCfg()}
vpr := viper.New()
vpr.SetConfigType(string(dataType))
require.NoError(t, vpr.ReadConfig(bytes.NewBuffer([]byte(cfgData))))
require.NoError(t, vpr.Unmarshal(&appCfg, func(c *mapstructure.DecoderConfig) {
c.DecodeHook = mapstructure.TextUnmarshallerHookFunc()
}))
require.Equal(t, expectedAppCfg, appCfg)
// Load config using yaml/json unmarshal.
appCfg = AppConfig{DB: NewDefaultConfig(supportedDialects)}
expectedAppCfg = AppConfig{DB: tt.expectedCfg()}
switch dataType {
case config.DataTypeYAML:
require.NoError(t, yaml.Unmarshal([]byte(cfgData), &appCfg))
require.Equal(t, expectedAppCfg, appCfg)
case config.DataTypeJSON:
require.NoError(t, json.Unmarshal([]byte(cfgData), &appCfg))
require.Equal(t, expectedAppCfg, appCfg)
}
}
})
}
}
func TestConfigWithKeyPrefix(t *testing.T) {
t.Run("custom key prefix", func(t *testing.T) {
cfgData := `
customDb:
dialect: mysql
mysql:
host: mysql-host
port: 3307
`
cfg := NewConfig([]Dialect{DialectMySQL}, WithKeyPrefix("customDb"))
err := config.NewDefaultLoader("").LoadFromReader(bytes.NewBuffer([]byte(cfgData)), config.DataTypeYAML, cfg)
require.NoError(t, err)
require.Equal(t, DialectMySQL, cfg.Dialect)
require.Equal(t, "mysql-host", cfg.MySQL.Host)
require.Equal(t, 3307, cfg.MySQL.Port)
})
t.Run("default key prefix, empty struct initialization", func(t *testing.T) {
cfgData := `
db:
dialect: mysql
mysql:
host: mysql-host
port: 3307
`
cfg := &Config{}
err := config.NewDefaultLoader("").LoadFromReader(bytes.NewBuffer([]byte(cfgData)), config.DataTypeYAML, cfg)
require.NoError(t, err)
require.Equal(t, DialectMySQL, cfg.Dialect)
require.Equal(t, "mysql-host", cfg.MySQL.Host)
require.Equal(t, 3307, cfg.MySQL.Port)
})
}
func TestConfigValidationErrors(t *testing.T) {
supportedDialects := []Dialect{DialectSQLite, DialectMySQL, DialectPostgres, DialectPgx, DialectMSSQL}
tests := []struct {
name string
yamlData string
expectedErrMsg string
}{
{
name: "unknown dialect",
yamlData: `
db:
dialect: fake-dialect
`,
expectedErrMsg: `db.dialect: unknown value "fake-dialect", should be one of [sqlite3 mysql postgres pgx mssql]`,
},
{
name: "invalid max open connections",
yamlData: `
db:
dialect: mysql
maxOpenConns: -1
`,
expectedErrMsg: `db.maxOpenConns: must be positive`,
},
{
name: "invalid max idel connections",
yamlData: `
db:
dialect: mysql
maxIdleConns: -1
`,
expectedErrMsg: `db.maxIdleConns: must be positive`,
},
{
name: "max idle connections greater than max open connections",
yamlData: `
db:
dialect: mysql
maxOpenConns: 5
maxIdleConns: 10
`,
expectedErrMsg: `db.maxIdleConns: must be less than maxOpenConns`,
},
{
name: "invalid connection max lifetime",
yamlData: `
db:
dialect: mysql
connMaxLifeTime: "invalid-duration"
`,
expectedErrMsg: `db.connMaxLifeTime: time: invalid duration "invalid-duration"`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := NewConfig(supportedDialects)
err := config.NewDefaultLoader("").LoadFromReader(bytes.NewBuffer([]byte(tt.yamlData)), config.DataTypeYAML, cfg)
require.EqualError(t, err, tt.expectedErrMsg)
})
}
}
func mustYAMLToJSON(yamlData []byte) []byte {
var yamlMap map[string]interface{}
if err := yaml.Unmarshal(yamlData, &yamlMap); err != nil {
panic(err)
}
jsonData, err := json.MarshalIndent(yamlMap, "", " ")
if err != nil {
panic(err)
}
return jsonData
}