-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider_test.go
401 lines (375 loc) · 11.5 KB
/
provider_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
package infrastructure
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRenderTemplate(t *testing.T) {
os.Setenv("INFRA_TEST_VAR", "test_value")
provider, err := NewProvider(ProviderSettings{
EnvName: "test",
SystemName: "system",
ComponentName: "comp",
})
assert.Nil(t, err)
assert.Equal(t, "test", provider.RenderSecret("test"))
assert.Equal(t, "{{ test }}", provider.RenderSecret("{{ test }}"))
assert.Equal(t, "this is a test", provider.RenderSecret("this is a {{ .Environment }}"))
// Test invalid template
result, err := provider.RenderSecrets("{{ .InvalidProp }}")
assert.NotNil(t, err)
assert.Empty(t, result)
// Test environment variable rendering
assert.Equal(t, "prefix_test_value_suffix", provider.RenderSecret("prefix_{{ .Env.INFRA_TEST_VAR }}_suffix"))
// Test empty template
assert.Equal(t, "", provider.RenderSecret(""))
// Test component name rendering
assert.Equal(t, "comp", provider.RenderSecret("{{ .Component }}"))
}
func TestProviderEnvironmentInfo(t *testing.T) {
provider, err := NewProvider(ProviderSettings{
EnvName: "test",
SystemName: "system",
ComponentName: "comp",
})
assert.Nil(t, err)
assert.Equal(t, "test", provider.Environment())
assert.Equal(t, "system", provider.SystemName())
assert.Equal(t, "comp", provider.ComponentName())
}
func TestProviderWithCustomSettings(t *testing.T) {
settings := ProviderSettings{
EnvName: "test",
SystemName: "customsys",
ComponentName: "customcomp",
CertFolders: []string{
"testdata/custom/certs",
},
AppConfigFolders: []string{
"testdata/custom/config",
},
InfraConfigFolders: []string{
"testdata/custom/infra",
},
}
provider, err := NewProvider(settings)
assert.Nil(t, err)
assert.Equal(t, "test", provider.Environment())
assert.Equal(t, "customsys", provider.SystemName())
assert.Equal(t, "customcomp", provider.ComponentName())
}
func TestProviderInvalidSettings(t *testing.T) {
testCases := []struct {
name string
settings ProviderSettings
errMsg string
}{
{
name: "empty all",
settings: ProviderSettings{
EnvName: "",
SystemName: "",
ComponentName: "",
},
errMsg: "could not determine environment name",
},
{
name: "empty system and component",
settings: ProviderSettings{
EnvName: "test",
SystemName: "",
ComponentName: "",
},
errMsg: "could not determine system name from environment",
},
{
name: "empty component only",
settings: ProviderSettings{
EnvName: "test",
SystemName: "sys",
ComponentName: "",
},
errMsg: "could not determine component name from environment",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.settings.Validate()
assert.NotNil(t, err)
assert.Contains(t, err.Error(), tc.errMsg)
})
}
}
func BenchmarkRenderTemplate(b *testing.B) {
os.Setenv("INFRA_TEST_VAR", "test_value")
provider, err := NewProvider(ProviderSettings{
EnvName: "test",
SystemName: "system",
ComponentName: "comp",
})
assert.Nil(b, err)
benchCases := []struct {
name string
template string
}{
{
name: "simple text",
template: "simple text without template",
},
{
name: "with env var",
template: "this is a {{ .Env.INFRA_TEST_VAR }}",
},
{
name: "with component",
template: "component: {{ .Component }}",
},
{
name: "complex template",
template: "env: {{ .Environment }}, system: {{ .System }}, component: {{ .Component }}",
},
}
for _, bc := range benchCases {
b.Run(bc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
result := provider.RenderSecret(bc.template)
if result == "" {
b.FailNow()
}
}
})
}
}
func TestProviderLoadConfig(t *testing.T) {
settings := ProviderSettings{
EnvName: "test",
SystemName: "sys",
ComponentName: "cmp",
}
t.Run("incomplete struct", func(t *testing.T) {
provider, err := NewProvider(settings)
if !assert.Nil(t, err) {
t.FailNow()
}
type myTestConfig struct {
Repo string `json:"repo-1"`
}
var cfg myTestConfig
assert.NotNil(t, provider.LoadConfig("app", &cfg))
provider.Locator()
})
t.Run("complete struct", func(t *testing.T) {
provider, err := NewProvider(settings)
if !assert.Nil(t, err) {
t.FailNow()
}
type myTestConfig struct {
SampleRepo01 struct {
Resource string `json:"arn"`
Params struct {
Timeout int `json:"timeout"`
MaxIdleConnections int `json:"max_idle_connections"`
} `json:"params"`
} `json:"repo-1"`
}
var cfg myTestConfig
assert.Nil(t, provider.LoadConfig("app", &cfg))
assert.Equal(t, "arn://storage/elasticsearch/sample-1", cfg.SampleRepo01.Resource)
assert.Equal(t, 10, cfg.SampleRepo01.Params.Timeout)
provider.Locator()
})
t.Run("load specific config for env", func(t *testing.T) {
provider, err := NewProvider(ProviderSettings{
EnvName: "testenv",
SystemName: "sys",
ComponentName: "cmp",
})
if !assert.Nil(t, err) {
t.FailNow()
}
type myTestConfig struct {
SampleRepo01 struct { // partial override in the env config
Resource string `json:"arn"`
Params struct {
Timeout int `json:"timeout"`
MaxIdleConnections int `json:"max_idle_connections"`
} `json:"params"`
} `json:"repo-1"`
SampleRepo02 struct { // full override in the env config
Resource string `json:"arn"`
Params struct {
Timeout int `json:"timeout"`
MaxIdleConnections int `json:"max_idle_connections"`
} `json:"params"`
} `json:"repo-2"`
SampleRepo03 struct { // not in the env config
Resource string `json:"arn"`
} `json:"repo-6"`
}
var cfg myTestConfig
assert.Nil(t, provider.LoadConfig("app", &cfg))
assert.Equal(t, "arn://storage/elasticsearch/sample-1", cfg.SampleRepo01.Resource)
assert.Equal(t, 15, cfg.SampleRepo01.Params.Timeout)
assert.Equal(t, "arn://storage/elasticsearch/sample-2", cfg.SampleRepo02.Resource)
assert.Equal(t, 20, cfg.SampleRepo02.Params.Timeout)
assert.Equal(t, "arn://storage/elasticsearch/sample-1", cfg.SampleRepo03.Resource)
provider.Locator()
})
t.Run("load specific config for env without global", func(t *testing.T) {
provider, err := NewProvider(ProviderSettings{
EnvName: "testenv",
SystemName: "sys",
ComponentName: "cmp",
})
if !assert.Nil(t, err) {
t.FailNow()
}
type myTestConfig struct {
SampleRepo struct { // partial override in the env config
Resource string `json:"arn"`
Params struct {
Timeout int `json:"timeout"`
MaxIdleConnections int `json:"max_idle_connections"`
} `json:"params"`
} `json:"repo"`
}
var cfg myTestConfig
assert.Nil(t, provider.LoadConfig("env-app", &cfg))
assert.Equal(t, "arn://storage/elasticsearch/sample-1", cfg.SampleRepo.Resource)
assert.Equal(t, 15, cfg.SampleRepo.Params.Timeout)
provider.Locator()
})
t.Run("render config with secrets", func(t *testing.T) {
provider, err := NewProvider(settings)
if !assert.Nil(t, err) {
t.FailNow()
}
type myTestConfig struct {
SampleRepo01 struct {
Resource string `json:"arn"`
Params struct {
Password string `json:"password"`
Password2 string `json:"password2"`
Password3 string `json:"password3"`
} `json:"params"`
} `json:"repo-6"`
}
var cfg myTestConfig
assert.Nil(t, provider.LoadConfig("app", &cfg))
assert.Equal(t, "arn://storage/elasticsearch/sample-1", cfg.SampleRepo01.Resource)
assert.Equal(t, "never-use-this-pass-test-sys-cmp", cfg.SampleRepo01.Params.Password)
assert.Equal(t, "", cfg.SampleRepo01.Params.Password3)
provider.Locator()
})
t.Run("failed to render config with secrets", func(t *testing.T) {
provider, err := NewProvider(settings)
if !assert.Nil(t, err) {
t.FailNow()
}
type myTestConfig struct {
SampleRepo01 struct {
Resource string `json:"arn"`
Params struct {
Password string `json:"password"`
Password2 string `json:"password2"`
Password3 string `json:"password3"`
} `json:"params"`
} `json:"repo-6"`
}
var cfg myTestConfig
assert.NotNil(t, provider.LoadConfig("app-invalid", &cfg))
provider.Locator()
})
t.Run("render config with Infra secrets", func(t *testing.T) {
os.Setenv("INFRA_KAFKA_USERNAME", "useruser")
provider, err := NewProvider(settings)
if !assert.Nil(t, err) {
t.FailNow()
}
type myTestConfig struct {
SampleRepo01 struct {
Resource string `json:"arn"`
Params struct {
UserName string `json:"username"`
UserName2 string `json:"username2"`
UserName3 string `json:"username3"`
} `json:"params"`
} `json:"repo-7"`
}
var cfg myTestConfig
assert.Nil(t, provider.LoadConfig("app", &cfg))
assert.Equal(t, "arn://storage/elasticsearch/sample-1", cfg.SampleRepo01.Resource)
assert.Equal(t, "useruser", cfg.SampleRepo01.Params.UserName)
assert.Empty(t, cfg.SampleRepo01.Params.UserName2)
assert.Equal(t, "this is a useruser", cfg.SampleRepo01.Params.UserName3)
provider.Locator()
})
t.Run("render config with Infra secrets with special character", func(t *testing.T) {
os.Setenv("INFRA_KAFKA_USERNAME", "user=user")
provider, err := NewProvider(settings)
if !assert.Nil(t, err) {
t.FailNow()
}
type myTestConfig struct {
SampleRepo01 struct {
Resource string `json:"arn"`
Params struct {
UserName string `json:"username"`
UserName2 string `json:"username2"`
UserName3 string `json:"username3"`
} `json:"params"`
} `json:"repo-7"`
}
var cfg myTestConfig
assert.Nil(t, provider.LoadConfig("app", &cfg))
assert.Equal(t, "arn://storage/elasticsearch/sample-1", cfg.SampleRepo01.Resource)
assert.Equal(t, "useruser", cfg.SampleRepo01.Params.UserName)
assert.Empty(t, cfg.SampleRepo01.Params.UserName2)
assert.Equal(t, "this is a user=user", cfg.SampleRepo01.Params.UserName3)
provider.Locator()
})
t.Run("render config with Infra secrets with special character", func(t *testing.T) {
os.Setenv("INFRA_KAFKA_USERNAME", "user=user")
provider, err := NewProvider(settings)
if !assert.Nil(t, err) {
t.FailNow()
}
type myTestConfig struct {
SampleRepo01 struct {
Resource string `json:"arn"`
Params struct {
UserName string `json:"username"`
UserName2 string `json:"username2"`
UserName3 string `json:"username3"`
} `json:"params"`
} `json:"repo-7"`
}
var cfg myTestConfig
assert.Nil(t, provider.LoadConfig("app", &cfg))
assert.Equal(t, "arn://storage/elasticsearch/sample-1", cfg.SampleRepo01.Resource)
assert.Equal(t, "useruser", cfg.SampleRepo01.Params.UserName)
assert.Empty(t, cfg.SampleRepo01.Params.UserName2)
assert.Equal(t, "this is a user=user", cfg.SampleRepo01.Params.UserName3)
provider.Locator()
})
t.Run("load_config_from_file_with_Infra_secrets", func(t *testing.T) {
os.Setenv("INFRA_KAFKA_USERNAME", "user")
os.Setenv("INFRA_KAFKA_PASSWORD", "pAss=Word")
provider, err := NewProvider(settings)
if !assert.Nil(t, err) {
t.FailNow()
}
type myTestConfig struct {
SampleRepo01 struct {
Resource string `json:"arn"`
} `json:"repo"`
}
var cfg myTestConfig
assert.Nil(t, provider.LoadConfigFromFile("./testdata/config/from-file.json", &cfg))
assert.Equal(t, "arn://messaging/kafka/clusters/c1", cfg.SampleRepo01.Resource)
assert.Len(t, provider.infraConfig.Messaging.Kafka.Clusters, 1)
cluster := provider.infraConfig.Messaging.Kafka.Clusters["c1"]
assert.Equal(t, cluster.Username, "user")
assert.Equal(t, cluster.Password, "pAss=Word")
})
}