-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples_test.go
More file actions
320 lines (276 loc) · 9.37 KB
/
Copy pathexamples_test.go
File metadata and controls
320 lines (276 loc) · 9.37 KB
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
package main
import (
"os"
"testing"
"time"
"github.com/MimoJanra/confkit"
)
func TestWebServiceConfigValidDefaults(t *testing.T) {
os.Clearenv()
_ = os.Setenv("DB_HOST", "localhost")
_ = os.Setenv("DB_USER", "postgres")
_ = os.Setenv("DB_PASSWORD", "secret")
_ = os.Setenv("DB_NAME", "mydb")
cfg, err := confkit.Load[WebServiceConfig](confkit.FromEnv())
if err != nil {
t.Fatalf("failed to load config: %v", err)
}
if cfg.App.Name != "myapp" {
t.Errorf("expected App.Name='myapp', got %q", cfg.App.Name)
}
if cfg.Server.Port != 8080 {
t.Errorf("expected Server.Port=8080, got %d", cfg.Server.Port)
}
if cfg.Database.Host != "localhost" {
t.Errorf("expected Database.Host='localhost', got %q", cfg.Database.Host)
}
if cfg.Cache.Enabled != true {
t.Errorf("expected Cache.Enabled=true, got %v", cfg.Cache.Enabled)
}
if cfg.Logging.Level != "info" {
t.Errorf("expected Logging.Level='info', got %q", cfg.Logging.Level)
}
}
func TestWebServiceConfigEnvironmentOverride(t *testing.T) {
os.Clearenv()
_ = os.Setenv("SERVER_PORT", "9000")
_ = os.Setenv("SERVER_HOST", "127.0.0.1")
_ = os.Setenv("DB_HOST", "db.example.com")
_ = os.Setenv("DB_USER", "admin")
_ = os.Setenv("DB_PASSWORD", "password123")
_ = os.Setenv("DB_NAME", "production")
_ = os.Setenv("LOG_LEVEL", "debug")
cfg, err := confkit.Load[WebServiceConfig](confkit.FromEnv())
if err != nil {
t.Fatalf("failed to load config: %v", err)
}
if cfg.Server.Port != 9000 {
t.Errorf("expected Server.Port=9000, got %d", cfg.Server.Port)
}
if cfg.Server.Host != "127.0.0.1" {
t.Errorf("expected Server.Host='127.0.0.1', got %q", cfg.Server.Host)
}
if cfg.Logging.Level != "debug" {
t.Errorf("expected Logging.Level='debug', got %q", cfg.Logging.Level)
}
}
func TestWebServiceConfigDurationParsing(t *testing.T) {
os.Clearenv()
_ = os.Setenv("SERVER_READ_TIMEOUT", "30s")
_ = os.Setenv("SERVER_WRITE_TIMEOUT", "1m")
_ = os.Setenv("DB_HOST", "localhost")
_ = os.Setenv("DB_USER", "user")
_ = os.Setenv("DB_PASSWORD", "pass")
_ = os.Setenv("DB_NAME", "db")
_ = os.Setenv("CACHE_TTL", "2h")
cfg, err := confkit.Load[WebServiceConfig](confkit.FromEnv())
if err != nil {
t.Fatalf("failed to load config: %v", err)
}
if cfg.Server.ReadTimeout != 30*time.Second {
t.Errorf("expected ReadTimeout=30s, got %v", cfg.Server.ReadTimeout)
}
if cfg.Server.WriteTimeout != 1*time.Minute {
t.Errorf("expected WriteTimeout=1m, got %v", cfg.Server.WriteTimeout)
}
if cfg.Cache.TTL != 2*time.Hour {
t.Errorf("expected Cache.TTL=2h, got %v", cfg.Cache.TTL)
}
}
func TestWebServiceConfigValidation(t *testing.T) {
os.Clearenv()
_ = os.Setenv("SERVER_PORT", "99999")
_ = os.Setenv("DB_HOST", "localhost")
_ = os.Setenv("DB_USER", "user")
_ = os.Setenv("DB_PASSWORD", "pass")
_ = os.Setenv("DB_NAME", "db")
_, err := confkit.Load[WebServiceConfig](confkit.FromEnv())
if err == nil {
t.Fatal("expected validation error for invalid port")
}
report, ok := err.(*confkit.ErrorReport)
if !ok {
t.Fatalf("expected ErrorReport, got %T", err)
}
if len(report.Errors) == 0 {
t.Fatal("expected at least one error")
}
}
func TestMicroserviceConfigCompleteSetup(t *testing.T) {
os.Clearenv()
_ = os.Setenv("SERVICE_NAME", "order-service")
_ = os.Setenv("SERVICE_PORT", "8000")
_ = os.Setenv("AUTH_JWT_SECRET", "secret-key-here")
_ = os.Setenv("POSTGRES_HOST", "postgres.svc")
_ = os.Setenv("POSTGRES_PORT", "5432")
_ = os.Setenv("POSTGRES_USER", "admin")
_ = os.Setenv("POSTGRES_PASSWORD", "dbpass")
_ = os.Setenv("POSTGRES_DATABASE", "orders")
_ = os.Setenv("REDIS_HOST", "redis.svc")
_ = os.Setenv("REDIS_PASSWORD", "redispass")
_ = os.Setenv("MQ_HOST", "rabbitmq.svc")
_ = os.Setenv("MQ_USER", "guest")
_ = os.Setenv("MQ_PASSWORD", "guest")
cfg, err := confkit.Load[MicroserviceConfig](confkit.FromEnv())
if err != nil {
t.Fatalf("failed to load config: %v", err)
}
if cfg.Service.Name != "order-service" {
t.Errorf("expected Service.Name='order-service', got %q", cfg.Service.Name)
}
if cfg.Service.Port != 8000 {
t.Errorf("expected Service.Port=8000, got %d", cfg.Service.Port)
}
if cfg.Auth.TokenExpiry != 24*time.Hour {
t.Errorf("expected Auth.TokenExpiry=24h, got %v", cfg.Auth.TokenExpiry)
}
if cfg.PostgreSQL.MaxOpenConn != 25 {
t.Errorf("expected PostgreSQL.MaxOpenConn=25, got %d", cfg.PostgreSQL.MaxOpenConn)
}
}
func TestMicroserviceConfigRateLimiting(t *testing.T) {
os.Clearenv()
_ = os.Setenv("SERVICE_NAME", "api-service")
_ = os.Setenv("POSTGRES_HOST", "localhost")
_ = os.Setenv("POSTGRES_USER", "user")
_ = os.Setenv("POSTGRES_PASSWORD", "pass")
_ = os.Setenv("POSTGRES_DATABASE", "db")
_ = os.Setenv("REDIS_HOST", "localhost")
_ = os.Setenv("MQ_HOST", "localhost")
_ = os.Setenv("MQ_USER", "user")
_ = os.Setenv("MQ_PASSWORD", "pass")
_ = os.Setenv("AUTH_JWT_SECRET", "secret")
_ = os.Setenv("RATELIMIT_RPS", "5000")
_ = os.Setenv("RATELIMIT_BURST_SIZE", "50000")
cfg, err := confkit.Load[MicroserviceConfig](confkit.FromEnv())
if err != nil {
t.Fatalf("failed to load config: %v", err)
}
if cfg.RateLimiting.RequestsPerSecond != 5000 {
t.Errorf("expected RPS=5000, got %d", cfg.RateLimiting.RequestsPerSecond)
}
if cfg.RateLimiting.BurstSize != 50000 {
t.Errorf("expected BurstSize=50000, got %d", cfg.RateLimiting.BurstSize)
}
}
func TestCLIToolConfigDefaults(t *testing.T) {
os.Clearenv()
_ = os.Setenv("INPUT_FILE", "input.json")
_ = os.Setenv("OUTPUT_FILE", "output.json")
cfg, err := confkit.Load[CLIToolConfig](confkit.FromEnv())
if err != nil {
t.Fatalf("failed to load config: %v", err)
}
if cfg.Input.File != "input.json" {
t.Errorf("expected Input.File='input.json', got %q", cfg.Input.File)
}
if cfg.Input.Format != "json" {
t.Errorf("expected Input.Format='json', got %q", cfg.Input.Format)
}
if cfg.Processing.Threads != 4 {
t.Errorf("expected Processing.Threads=4, got %d", cfg.Processing.Threads)
}
if cfg.Validation.Strict != true {
t.Errorf("expected Validation.Strict=true, got %v", cfg.Validation.Strict)
}
if cfg.Performance.EnableCache != true {
t.Errorf("expected Performance.EnableCache=true, got %v", cfg.Performance.EnableCache)
}
}
func TestCLIToolConfigCustomValues(t *testing.T) {
os.Clearenv()
_ = os.Setenv("INPUT_FILE", "data.csv")
_ = os.Setenv("INPUT_FORMAT", "csv")
_ = os.Setenv("INPUT_ENCODING", "utf-8")
_ = os.Setenv("OUTPUT_FILE", "result.xml")
_ = os.Setenv("OUTPUT_FORMAT", "xml")
_ = os.Setenv("PROCESSING_THREADS", "16")
_ = os.Setenv("PROCESSING_SKIP_ERRORS", "true")
cfg, err := confkit.Load[CLIToolConfig](confkit.FromEnv())
if err != nil {
t.Fatalf("failed to load config: %v", err)
}
if cfg.Input.Format != "csv" {
t.Errorf("expected Input.Format='csv', got %q", cfg.Input.Format)
}
if cfg.Output.Format != "xml" {
t.Errorf("expected Output.Format='xml', got %q", cfg.Output.Format)
}
if cfg.Processing.Threads != 16 {
t.Errorf("expected Processing.Threads=16, got %d", cfg.Processing.Threads)
}
if cfg.Processing.SkipErrors != true {
t.Errorf("expected Processing.SkipErrors=true, got %v", cfg.Processing.SkipErrors)
}
}
func TestCLIToolConfigValidation(t *testing.T) {
os.Clearenv()
_ = os.Setenv("INPUT_FILE", "input.json")
_ = os.Setenv("OUTPUT_FILE", "output.json")
_ = os.Setenv("PROCESSING_THREADS", "64")
_, err := confkit.Load[CLIToolConfig](confkit.FromEnv())
if err == nil {
t.Fatal("expected validation error for threads > 32")
}
}
func TestFullSetupExampleConfig(t *testing.T) {
os.Clearenv()
type AppConfig struct {
Port int `env:"PORT" toml:"port" default:"8080" validate:"min=1,max=65535"`
Host string `env:"HOST" toml:"host" default:"localhost" validate:"required"`
LogLevel string `env:"LOG_LEVEL" toml:"log_level" default:"info" validate:"oneof=debug,info,warn,error"`
}
_ = os.Setenv("PORT", "9000")
_ = os.Setenv("HOST", "0.0.0.0")
_ = os.Setenv("LOG_LEVEL", "debug")
cfg, err := confkit.Load[AppConfig](confkit.FromEnv())
if err != nil {
t.Fatalf("failed to load config: %v", err)
}
if cfg.Port != 9000 {
t.Errorf("expected Port=9000, got %d", cfg.Port)
}
if cfg.Host != "0.0.0.0" {
t.Errorf("expected Host='0.0.0.0', got %q", cfg.Host)
}
if cfg.LogLevel != "debug" {
t.Errorf("expected LogLevel='debug', got %q", cfg.LogLevel)
}
}
func TestNestedStructPrefixMapping(t *testing.T) {
os.Clearenv()
_ = os.Setenv("DB_HOST", "db.local")
_ = os.Setenv("DB_PORT", "5432")
_ = os.Setenv("DB_USER", "postgres")
_ = os.Setenv("DB_PASSWORD", "secret")
_ = os.Setenv("DB_NAME", "mydb")
_ = os.Setenv("CACHE_ENABLED", "true")
_ = os.Setenv("CACHE_TTL", "30m")
cfg, err := confkit.Load[WebServiceConfig](confkit.FromEnv())
if err != nil {
t.Fatalf("failed to load config: %v", err)
}
if cfg.Database.Host != "db.local" {
t.Errorf("expected Database.Host='db.local', got %q", cfg.Database.Host)
}
if cfg.Database.Port != 5432 {
t.Errorf("expected Database.Port=5432, got %d", cfg.Database.Port)
}
if cfg.Cache.TTL != 30*time.Minute {
t.Errorf("expected Cache.TTL=30m, got %v", cfg.Cache.TTL)
}
}
func TestSecretFieldRedaction(t *testing.T) {
os.Clearenv()
_ = os.Setenv("DB_HOST", "localhost")
_ = os.Setenv("DB_USER", "user")
_ = os.Setenv("DB_PASSWORD", "my-secret-password-123")
_ = os.Setenv("DB_NAME", "db")
cfg, err := confkit.Load[WebServiceConfig](confkit.FromEnv())
if err != nil {
t.Fatalf("failed to load config: %v", err)
}
if cfg.Database.Password != "my-secret-password-123" {
t.Errorf("expected password to be loaded correctly")
}
}