-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
439 lines (364 loc) · 11.4 KB
/
integration_test.go
File metadata and controls
439 lines (364 loc) · 11.4 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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package main
import (
"os"
"path/filepath"
"sync"
"testing"
"time"
amqp "github.com/rabbitmq/amqp091-go"
)
func TestMainApplicationFlow(_ *testing.T) {
// Test main application initialization without running full main()
// Save original args
originalArgs := os.Args
defer func() { os.Args = originalArgs }()
// Test help flag
os.Args = []string{"bucketsyncd", "-h"}
// We can't easily test main() directly as it runs indefinitely,
// but we can test the flag parsing logic
// This would require refactoring main() to be more testable
}
// createIntegrationTestConfig creates integration test configuration file
func createIntegrationTestConfig(t *testing.T) string {
tmpDir := t.TempDir()
configFile := filepath.Join(tmpDir, "integration-test-config.yaml")
configContent := `
log_level: "debug"
log_json: false
outbound:
- name: "integration-outbound"
description: "Integration test outbound"
source: "/tmp/integration-test/*"
destination: "s3://test-bucket/integration/"
sensitive: false
inbound:
- name: "integration-inbound"
description: "Integration test inbound"
source: "amqp://guest:guest@localhost:5672/"
exchange: "integration-exchange"
queue: "integration-queue"
remote: "integration-remote"
destination: "/tmp/integration-downloads"
remotes:
- name: "integration-remote"
endpoint: "localhost:9000"
accessKey: "integration-access"
secretKey: "integration-secret"
`
err := os.WriteFile(configFile, []byte(configContent), 0600)
if err != nil {
t.Fatalf("Failed to create integration test config: %v", err)
}
return configFile
}
// verifyIntegrationConfigCounts verifies configuration counts
func verifyIntegrationConfigCounts(t *testing.T) {
if config.LogLevel != debugLevel {
t.Errorf("Expected log level 'debug', got '%s'", config.LogLevel)
}
if len(config.Outbound) != 1 {
t.Errorf("Expected 1 outbound config, got %d", len(config.Outbound))
}
if len(config.Inbound) != 1 {
t.Errorf("Expected 1 inbound config, got %d", len(config.Inbound))
}
if len(config.Remotes) != 1 {
t.Errorf("Expected 1 remote config, got %d", len(config.Remotes))
}
}
// verifyIntegrationConfigDetails verifies specific configuration details
func verifyIntegrationConfigDetails(t *testing.T) {
// Test outbound configuration
outbound := config.Outbound[0]
if outbound.Name != "integration-outbound" {
t.Errorf("Expected outbound name 'integration-outbound', got '%s'", outbound.Name)
}
if outbound.Source != "/tmp/integration-test/*" {
t.Errorf("Expected source '/tmp/integration-test/*', got '%s'", outbound.Source)
}
// Test inbound configuration
inbound := config.Inbound[0]
if inbound.Name != "integration-inbound" {
t.Errorf("Expected inbound name 'integration-inbound', got '%s'", inbound.Name)
}
if inbound.Remote != "integration-remote" {
t.Errorf("Expected remote 'integration-remote', got '%s'", inbound.Remote)
}
// Test remote configuration
remote := config.Remotes[0]
if remote.Name != "integration-remote" {
t.Errorf("Expected remote name 'integration-remote', got '%s'", remote.Name)
}
if remote.Endpoint != "localhost:9000" {
t.Errorf("Expected endpoint 'localhost:9000', got '%s'", remote.Endpoint)
}
}
func TestConfigurationFlow(t *testing.T) {
// Create a temporary configuration file
configFile := createIntegrationTestConfig(t)
// Test configuration reading
err := readConfig(configFile)
if err != nil {
t.Fatalf("Failed to read integration config: %v", err)
}
// Verify configuration was loaded correctly
verifyIntegrationConfigCounts(t)
verifyIntegrationConfigDetails(t)
}
func TestErrorHandling(t *testing.T) {
// Test various error conditions
// Test reading non-existent config file
err := readConfig("/non/existent/config.yaml")
if err == nil {
t.Error("Expected error reading non-existent config file")
}
// Test malformed YAML
tmpDir := t.TempDir()
malformedConfig := filepath.Join(tmpDir, "malformed.yaml")
malformedContent := `
log_level: "info"
invalid_yaml: [
`
err = os.WriteFile(malformedConfig, []byte(malformedContent), 0600)
if err != nil {
t.Fatalf("Failed to create malformed config: %v", err)
}
err = readConfig(malformedConfig)
if err == nil {
t.Error("Expected error reading malformed YAML config")
}
}
// createMultiConfig creates configuration file with multiple sections
func createMultiConfig(t *testing.T) string {
tmpDir := t.TempDir()
configFile := filepath.Join(tmpDir, "multi-config.yaml")
configContent := `
log_level: "info"
log_json: true
outbound:
- name: "outbound-1"
description: "First outbound"
source: "/tmp/source1/*"
destination: "s3://bucket1/path1/"
sensitive: false
- name: "outbound-2"
description: "Second outbound"
source: "/tmp/source2/*.log"
destination: "s3://bucket2/logs/"
sensitive: true
process_with: "/usr/bin/encrypt"
inbound:
- name: "inbound-1"
description: "First inbound"
source: "amqp://user1:pass1@host1:5672/"
exchange: "exchange1"
queue: "queue1"
remote: "remote1"
destination: "/tmp/dest1"
- name: "inbound-2"
description: "Second inbound"
source: "amqp://user2:pass2@host2:5672/"
exchange: "exchange2"
queue: "queue2"
remote: "remote2"
destination: "/tmp/dest2"
remotes:
- name: "remote1"
endpoint: "s3.amazonaws.com"
accessKey: "aws-key"
secretKey: "aws-secret"
- name: "remote2"
endpoint: "localhost:9000"
accessKey: "local-key"
secretKey: "local-secret"
`
err := os.WriteFile(configFile, []byte(configContent), 0600)
if err != nil {
t.Fatalf("Failed to create multi-config file: %v", err)
}
return configFile
}
// verifyMultiConfigCounts verifies multiple configuration counts
func verifyMultiConfigCounts(t *testing.T) {
if len(config.Outbound) != 2 {
t.Errorf("Expected 2 outbound configs, got %d", len(config.Outbound))
}
if len(config.Inbound) != 2 {
t.Errorf("Expected 2 inbound configs, got %d", len(config.Inbound))
}
if len(config.Remotes) != 2 {
t.Errorf("Expected 2 remote configs, got %d", len(config.Remotes))
}
}
// verifyMultiConfigSpecifics verifies specific configuration details
func verifyMultiConfigSpecifics(t *testing.T) {
// Test specific configurations
if config.LogJSON != true {
t.Error("Expected log_json to be true")
}
// Test sensitive outbound config
sensitiveOutbound := config.Outbound[1]
if !sensitiveOutbound.Sensitive {
t.Error("Expected second outbound to be sensitive")
}
if sensitiveOutbound.ProcessWith != "/usr/bin/encrypt" {
t.Errorf("Expected ProcessWith '/usr/bin/encrypt', got '%s'", sensitiveOutbound.ProcessWith)
}
}
func TestMultipleConfigurations(t *testing.T) {
// Test configuration with multiple outbound and inbound configs
configFile := createMultiConfig(t)
// Test configuration reading
err := readConfig(configFile)
if err != nil {
t.Fatalf("Failed to read multi-config: %v", err)
}
// Verify multiple configurations
verifyMultiConfigCounts(t)
verifyMultiConfigSpecifics(t)
}
func TestConcurrentSafety(t *testing.T) {
// Test that global variables are handled safely with proper synchronization
// This is a basic test - real concurrent testing would require more setup
originalConfig := config
defer func() { config = originalConfig }()
// Test that connections slice can be safely accessed
originalConnections := connections
localConnections := make([]*amqp.Connection, 0)
var mu sync.RWMutex
// Simulate concurrent access with proper synchronization
done := make(chan bool, 2)
go func() {
mu.Lock()
localConnections = append(localConnections, nil)
mu.Unlock()
done <- true
}()
go func() {
mu.RLock()
_ = len(localConnections)
mu.RUnlock()
done <- true
}()
// Wait for both goroutines
timeout := time.After(1 * time.Second)
for i := 0; i < 2; i++ {
select {
case <-done:
// Success
case <-timeout:
t.Error("Timeout waiting for concurrent operations")
return
}
}
connections = originalConnections
}
func TestConfigDefaults(t *testing.T) {
// Test configuration with minimal settings
tmpDir := t.TempDir()
configFile := filepath.Join(tmpDir, "minimal-config.yaml")
configContent := `
outbound: []
inbound: []
remotes: []
`
err := os.WriteFile(configFile, []byte(configContent), 0600)
if err != nil {
t.Fatalf("Failed to create minimal config: %v", err)
}
err = readConfig(configFile)
if err != nil {
t.Fatalf("Failed to read minimal config: %v", err)
}
// Check defaults (values may be retained from previous tests)
// Note: config is a global variable that may retain values from other tests
if len(config.Outbound) != 0 {
t.Errorf("Expected 0 outbound configs, got %d", len(config.Outbound))
}
if len(config.Inbound) != 0 {
t.Errorf("Expected 0 inbound configs, got %d", len(config.Inbound))
}
if len(config.Remotes) != 0 {
t.Errorf("Expected 0 remote configs, got %d", len(config.Remotes))
}
}
// generateLargeConfigContent generates large configuration content
func generateLargeConfigContent() string {
configContent := `
log_level: "info"
log_json: false
outbound:
`
// Add multiple outbound configurations
for i := 0; i < 10; i++ {
configContent += ` - name: "outbound-` + string(rune('0'+i)) + `"
description: "Outbound ` + string(rune('0'+i)) + `"
source: "/tmp/source` + string(rune('0'+i)) + `/*"
destination: "s3://bucket` + string(rune('0'+i)) + `/path/"
sensitive: false
`
}
configContent += `inbound:
`
// Add multiple inbound configurations
for i := 0; i < 5; i++ {
configContent += ` - name: "inbound-` + string(rune('0'+i)) + `"
description: "Inbound ` + string(rune('0'+i)) + `"
source: "amqp://user:pass@host` + string(rune('0'+i)) + `:5672/"
exchange: "exchange` + string(rune('0'+i)) + `"
queue: "queue` + string(rune('0'+i)) + `"
remote: "remote` + string(rune('0'+i)) + `"
destination: "/tmp/dest` + string(rune('0'+i)) + `"
`
}
configContent += `remotes:
`
// Add multiple remote configurations
for i := 0; i < 5; i++ {
configContent += ` - name: "remote` + string(rune('0'+i)) + `"
endpoint: "host` + string(rune('0'+i)) + `:9000"
accessKey: "key` + string(rune('0'+i)) + `"
secretKey: "secret` + string(rune('0'+i)) + `"
`
}
return configContent
}
// createLargeConfigFile creates large configuration file
func createLargeConfigFile(t *testing.T) string {
tmpDir := t.TempDir()
configFile := filepath.Join(tmpDir, "large-config.yaml")
configContent := generateLargeConfigContent()
err := os.WriteFile(configFile, []byte(configContent), 0600)
if err != nil {
t.Fatalf("Failed to create large config: %v", err)
}
return configFile
}
// verifyLargeConfigCounts verifies large configuration counts
func verifyLargeConfigCounts(t *testing.T) {
// Check that all items were loaded
if len(config.Outbound) != 10 {
t.Errorf("Expected 10 outbound configs, got %d", len(config.Outbound))
}
if len(config.Inbound) != 5 {
t.Errorf("Expected 5 inbound configs, got %d", len(config.Inbound))
}
if len(config.Remotes) != 5 {
t.Errorf("Expected 5 remote configs, got %d", len(config.Remotes))
}
}
func TestLargeConfiguration(t *testing.T) {
// Test configuration with many items to check performance
configFile := createLargeConfigFile(t)
start := time.Now()
err := readConfig(configFile)
duration := time.Since(start)
if err != nil {
t.Fatalf("Failed to read large config: %v", err)
}
verifyLargeConfigCounts(t)
// Performance check - should be fast for reasonable config sizes
if duration > 100*time.Millisecond {
t.Errorf("Config reading took too long: %v", duration)
}
}