-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_test.go
More file actions
228 lines (198 loc) · 5.4 KB
/
config_test.go
File metadata and controls
228 lines (198 loc) · 5.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
package main
import (
"os"
"path/filepath"
"testing"
)
const (
testInboundName = "test-inbound"
testOutboundName = "test-outbound"
)
func createTestConfig() string {
return `
log_level: "info"
log_json: false
outbound:
- name: "` + testOutboundName + `"
description: "Test outbound configuration"
source: "/tmp/test"
destination: "test-bucket/path"
ignore_patterns:
- "*.tmp"
sensitive: false
inbound:
- name: "` + testInboundName + `"
description: "Test inbound configuration"
source: "amqp://user:pass@localhost:5672/"
exchange: "test-exchange"
queue: "test-queue"
remote: "test-remote"
destination: "/tmp/downloads"
remotes:
- name: "test-remote"
endpoint: "http://localhost:9000"
accessKey: "testkey"
secretKey: "testsecret"
`
}
func TestReadConfig(t *testing.T) {
configContent := createTestConfig()
// Create temporary file
tmpDir := t.TempDir()
configFile := filepath.Join(tmpDir, "test-config.yaml")
err := os.WriteFile(configFile, []byte(configContent), 0600)
if err != nil {
t.Fatalf("Failed to create test config file: %v", err)
}
// Test reading the config
err = readConfig(configFile)
if err != nil {
t.Fatalf("Failed to read config: %v", err)
}
validateConfig(t)
}
func validateConfig(t *testing.T) {
configMutex.RLock()
// Verify config was parsed correctly
if config.LogLevel != infoLevel {
t.Errorf("Expected log level 'info', got '%s'", config.LogLevel)
}
if config.LogJSON {
t.Errorf("Expected log_json false, got %v", config.LogJSON)
}
if len(config.Outbound) != 1 {
t.Errorf("Expected 1 outbound config, got %d", len(config.Outbound))
}
validateOutboundConfig(t)
validateInboundConfig(t)
validateRemoteConfig(t)
configMutex.RUnlock()
}
func validateOutboundConfig(t *testing.T) {
if len(config.Outbound) == 0 {
t.Fatal("No outbound config found")
}
outbound := config.Outbound[0]
if outbound.Name != testOutboundName {
t.Errorf("Expected outbound name '%s', got '%s'", testOutboundName, outbound.Name)
}
if outbound.Sensitive {
t.Errorf("Expected outbound sensitive false, got %v", outbound.Sensitive)
}
if len(outbound.IgnorePatterns) != 1 || outbound.IgnorePatterns[0] != "*.tmp" {
t.Errorf("Expected ignore patterns ['*.tmp'], got %v", outbound.IgnorePatterns)
}
}
// TestIgnorePatternsMatching tests the ignore patterns functionality
func TestIgnorePatternsMatching(t *testing.T) {
outbound := Outbound{
IgnorePatterns: []string{"*.crdownload", "*.tmp", ".*"},
}
testCases := []struct {
filename string
ignored bool
}{
{"file.txt", false},
{"download.crdownload", true},
{"temp.tmp", true},
{".hidden", true},
{"normal.pdf", false},
}
for _, tc := range testCases {
ignored := false
for _, pattern := range outbound.IgnorePatterns {
if matched, _ := filepath.Match(pattern, tc.filename); matched {
ignored = true
break
}
}
if ignored != tc.ignored {
t.Errorf("File %s: expected ignored=%v, got %v", tc.filename, tc.ignored, ignored)
}
}
}
func validateInboundConfig(t *testing.T) {
if len(config.Inbound) == 0 {
t.Fatal("No inbound config found")
}
inbound := config.Inbound[0]
if inbound.Name != testInboundName {
t.Errorf("Expected inbound name '%s', got '%s'", testInboundName, inbound.Name)
}
if inbound.Exchange != "test-exchange" {
t.Errorf("Expected exchange 'test-exchange', got '%s'", inbound.Exchange)
}
}
func validateRemoteConfig(t *testing.T) {
if len(config.Remotes) == 0 {
t.Fatal("No remote config found")
}
remote := config.Remotes[0]
if remote.Name != "test-remote" {
t.Errorf("Expected remote name 'test-remote', got '%s'", remote.Name)
}
if remote.Endpoint != "http://localhost:9000" {
t.Errorf("Expected endpoint 'http://localhost:9000', got '%s'", remote.Endpoint)
}
}
func TestReadConfigNonExistentFile(t *testing.T) {
err := readConfig("/non/existent/file.yaml")
if err == nil {
t.Error("Expected error when reading non-existent file, got nil")
}
}
func TestReadConfigInvalidYAML(t *testing.T) {
// Create temporary file with invalid YAML
tmpDir := t.TempDir()
configFile := filepath.Join(tmpDir, "invalid-config.yaml")
invalidContent := `
log_level: "info"
invalid_yaml: [
`
err := os.WriteFile(configFile, []byte(invalidContent), 0600)
if err != nil {
t.Fatalf("Failed to create invalid config file: %v", err)
}
err = readConfig(configFile)
if err == nil {
t.Error("Expected error when reading invalid YAML, got nil")
}
}
func TestConfigStructures(t *testing.T) {
// Test Remote struct
remote := Remote{
Name: "test",
Endpoint: "http://localhost:9000",
AccessKey: "key",
SecretKey: "secret",
}
if remote.Name != "test" {
t.Errorf("Expected remote name 'test', got '%s'", remote.Name)
}
// Test Outbound struct
outbound := Outbound{
Name: "test-out",
Description: "Test description",
Sensitive: true,
Source: "/tmp/source",
Destination: "bucket/dest",
IgnorePatterns: []string{"*.tmp", ".*"},
ProcessWith: "script.sh",
}
if !outbound.Sensitive {
t.Error("Expected outbound sensitive to be true")
}
// Test Inbound struct
inbound := Inbound{
Name: "test-in",
Description: "Test inbound",
Source: "amqp://localhost",
Exchange: "exchange",
Queue: "queue",
Remote: "remote",
Destination: "/tmp/dest",
}
if inbound.Exchange != "exchange" {
t.Errorf("Expected exchange 'exchange', got '%s'", inbound.Exchange)
}
}