-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathconfig_test.go
More file actions
369 lines (305 loc) · 9.1 KB
/
config_test.go
File metadata and controls
369 lines (305 loc) · 9.1 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
package trace2receiver
import (
"os"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Test Validate with minimal valid config on Windows
func Test_Config_Validate_MinimalWindows(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Skipping Windows-specific test")
}
cfg := &Config{
NamedPipePath: "test-pipe",
}
err := cfg.Validate()
assert.NoError(t, err)
assert.Equal(t, `\\.\pipe\test-pipe`, cfg.NamedPipePath)
}
// Test Validate with minimal valid config on Unix
func Test_Config_Validate_MinimalUnix(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping Unix-specific test")
}
cfg := &Config{
UnixSocketPath: "/tmp/test.socket",
}
err := cfg.Validate()
assert.NoError(t, err)
assert.Equal(t, "/tmp/test.socket", cfg.UnixSocketPath)
}
// Test Validate fails when pipe is missing on Windows
func Test_Config_Validate_MissingPipeWindows(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Skipping Windows-specific test")
}
cfg := &Config{}
err := cfg.Validate()
assert.Error(t, err)
assert.Contains(t, err.Error(), "pipe not defined")
}
// Test Validate fails when socket is missing on Unix
func Test_Config_Validate_MissingSocketUnix(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping Unix-specific test")
}
cfg := &Config{}
err := cfg.Validate()
assert.Error(t, err)
assert.Contains(t, err.Error(), "socket not defined")
}
// Test Validate with full pipe path on Windows
func Test_Config_Validate_FullPipePathWindows(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Skipping Windows-specific test")
}
cfg := &Config{
NamedPipePath: `\\.\pipe\my-test-pipe`,
}
err := cfg.Validate()
assert.NoError(t, err)
assert.Equal(t, `\\.\pipe\my-test-pipe`, cfg.NamedPipePath)
}
// Test Validate with af_unix prefix on Unix
func Test_Config_Validate_AfUnixPrefixUnix(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping Unix-specific test")
}
cfg := &Config{
UnixSocketPath: "af_unix:/tmp/test.socket",
}
err := cfg.Validate()
assert.NoError(t, err)
assert.Equal(t, "/tmp/test.socket", cfg.UnixSocketPath)
}
// Test Validate with af_unix:stream prefix on Unix
func Test_Config_Validate_AfUnixStreamPrefixUnix(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping Unix-specific test")
}
cfg := &Config{
UnixSocketPath: "af_unix:stream:/tmp/test.socket",
}
err := cfg.Validate()
assert.NoError(t, err)
assert.Equal(t, "/tmp/test.socket", cfg.UnixSocketPath)
}
// Test Validate rejects UNC paths on Windows
func Test_Config_Validate_RejectUNCWindows(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Skipping Windows-specific test")
}
cfg := &Config{
NamedPipePath: `\\server\share\pipe`,
}
err := cfg.Validate()
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid")
}
// Test Validate rejects drive letter paths on Windows
func Test_Config_Validate_RejectDriveLetterWindows(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Skipping Windows-specific test")
}
cfg := &Config{
NamedPipePath: `C:\temp\pipe`,
}
err := cfg.Validate()
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid")
}
// Test Validate rejects SOCK_DGRAM on Unix
func Test_Config_Validate_RejectDgramUnix(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping Unix-specific test")
}
cfg := &Config{
UnixSocketPath: "af_unix:dgram:/tmp/test.socket",
}
err := cfg.Validate()
assert.Error(t, err)
assert.Contains(t, err.Error(), "SOCK_DGRAM sockets are not supported")
}
// Test Validate with valid PII settings file
func Test_Config_Validate_WithValidPiiSettings(t *testing.T) {
// Create a temporary PII settings file
tmpDir := t.TempDir()
piiPath := filepath.Join(tmpDir, "pii.yml")
piiContent := `
pii_filter:
domains:
- pattern: "example.com"
replace: "<domain>"
`
err := os.WriteFile(piiPath, []byte(piiContent), 0644)
require.NoError(t, err)
cfg := createMinimalValidConfig()
cfg.PiiSettingsPath = piiPath
err = cfg.Validate()
assert.NoError(t, err)
assert.NotNil(t, cfg.piiSettings)
}
// Test Validate with invalid PII settings file
func Test_Config_Validate_WithInvalidPiiSettings(t *testing.T) {
cfg := createMinimalValidConfig()
cfg.PiiSettingsPath = "/nonexistent/pii.yml"
err := cfg.Validate()
assert.Error(t, err)
}
// Test Validate with valid filter settings file
func Test_Config_Validate_WithValidFilterSettings(t *testing.T) {
// Create a temporary filter settings file
tmpDir := t.TempDir()
filterPath := filepath.Join(tmpDir, "filter.yml")
filterContent := `
default_action: accept
`
err := os.WriteFile(filterPath, []byte(filterContent), 0644)
require.NoError(t, err)
cfg := createMinimalValidConfig()
cfg.FilterSettingsPath = filterPath
err = cfg.Validate()
assert.NoError(t, err)
assert.NotNil(t, cfg.filterSettings)
}
// Test Validate with invalid filter settings file
func Test_Config_Validate_WithInvalidFilterSettings(t *testing.T) {
cfg := createMinimalValidConfig()
cfg.FilterSettingsPath = "/nonexistent/filter.yml"
err := cfg.Validate()
assert.Error(t, err)
}
// Test Validate with valid summary settings file
func Test_Config_Validate_WithValidSummary(t *testing.T) {
// Create a temporary summary settings file
tmpDir := t.TempDir()
summaryPath := filepath.Join(tmpDir, "summary.yml")
summaryContent := `
message_patterns:
- prefix: "error:"
field_name: "error_count"
- prefix: "warning:"
field_name: "warning_count"
region_timers:
- category: "index"
label: "do_read_index"
count_field: "index_read_count"
time_field: "index_read_time"
`
err := os.WriteFile(summaryPath, []byte(summaryContent), 0644)
require.NoError(t, err)
cfg := createMinimalValidConfig()
cfg.SummaryPath = summaryPath
err = cfg.Validate()
assert.NoError(t, err)
assert.NotNil(t, cfg.summary)
assert.Equal(t, 2, len(cfg.summary.MessagePatterns))
assert.Equal(t, 1, len(cfg.summary.RegionTimers))
}
// Test Validate with invalid summary settings file (nonexistent)
func Test_Config_Validate_WithNonexistentSummary(t *testing.T) {
cfg := createMinimalValidConfig()
cfg.SummaryPath = "/nonexistent/summary.yml"
err := cfg.Validate()
assert.Error(t, err)
}
// Test Validate with invalid summary settings file (malformed YAML)
func Test_Config_Validate_WithMalformedSummary(t *testing.T) {
// Create a temporary malformed summary settings file
tmpDir := t.TempDir()
summaryPath := filepath.Join(tmpDir, "summary.yml")
summaryContent := `
message_patterns:
- prefix: "error:"
field_name: ""
`
err := os.WriteFile(summaryPath, []byte(summaryContent), 0644)
require.NoError(t, err)
cfg := createMinimalValidConfig()
cfg.SummaryPath = summaryPath
err = cfg.Validate()
assert.Error(t, err)
assert.Contains(t, err.Error(), "field_name cannot be empty")
}
// Test Validate with summary settings with duplicate field names
func Test_Config_Validate_WithDuplicateSummaryFields(t *testing.T) {
// Create a temporary summary settings file with duplicate fields
tmpDir := t.TempDir()
summaryPath := filepath.Join(tmpDir, "summary.yml")
summaryContent := `
message_patterns:
- prefix: "error:"
field_name: "count"
- prefix: "warning:"
field_name: "count"
`
err := os.WriteFile(summaryPath, []byte(summaryContent), 0644)
require.NoError(t, err)
cfg := createMinimalValidConfig()
cfg.SummaryPath = summaryPath
err = cfg.Validate()
assert.Error(t, err)
assert.Contains(t, err.Error(), "duplicate field_name")
}
// Test Validate with all optional settings valid
func Test_Config_Validate_WithAllOptionalSettings(t *testing.T) {
// Create temporary files for all settings
tmpDir := t.TempDir()
piiPath := filepath.Join(tmpDir, "pii.yml")
piiContent := `
pii_filter:
domains:
- pattern: "example.com"
replace: "<domain>"
`
err := os.WriteFile(piiPath, []byte(piiContent), 0644)
require.NoError(t, err)
filterPath := filepath.Join(tmpDir, "filter.yml")
filterContent := `
default_action: accept
`
err = os.WriteFile(filterPath, []byte(filterContent), 0644)
require.NoError(t, err)
summaryPath := filepath.Join(tmpDir, "summary.yml")
summaryContent := `
message_patterns:
- prefix: "error:"
field_name: "error_count"
region_timers:
- category: "index"
label: "do_read_index"
time_field: "index_read_time"
`
err = os.WriteFile(summaryPath, []byte(summaryContent), 0644)
require.NoError(t, err)
cfg := createMinimalValidConfig()
cfg.PiiSettingsPath = piiPath
cfg.FilterSettingsPath = filterPath
cfg.SummaryPath = summaryPath
err = cfg.Validate()
assert.NoError(t, err)
assert.NotNil(t, cfg.piiSettings)
assert.NotNil(t, cfg.filterSettings)
assert.NotNil(t, cfg.summary)
}
// Test Validate with command control enabled
func Test_Config_Validate_WithCommandControlEnabled(t *testing.T) {
cfg := createMinimalValidConfig()
cfg.AllowCommandControlVerbs = true
err := cfg.Validate()
assert.NoError(t, err)
}
// Helper function to create a minimal valid config for the current platform
func createMinimalValidConfig() *Config {
if runtime.GOOS == "windows" {
return &Config{
NamedPipePath: "test-pipe",
}
}
return &Config{
UnixSocketPath: "/tmp/test.socket",
}
}