|
1 | 1 | package trace2receiver |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
4 | 6 | "runtime" |
5 | 7 | "testing" |
6 | 8 |
|
7 | 9 | "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
8 | 11 | ) |
9 | 12 |
|
10 | 13 | // Test Validate with minimal valid config on Windows |
@@ -269,6 +272,127 @@ func Test_Config_Validate_WithCommandControlEnabled(t *testing.T) { |
269 | 272 | assert.NoError(t, err) |
270 | 273 | } |
271 | 274 |
|
| 275 | +// Test Validate with PII settings from file path |
| 276 | +func Test_Config_Validate_WithPiiFilePath(t *testing.T) { |
| 277 | + tmpDir := t.TempDir() |
| 278 | + piiPath := filepath.Join(tmpDir, "pii.yml") |
| 279 | + piiContent := ` |
| 280 | +include: |
| 281 | + hostname: true |
| 282 | + username: false |
| 283 | +` |
| 284 | + err := os.WriteFile(piiPath, []byte(piiContent), 0644) |
| 285 | + require.NoError(t, err) |
| 286 | + |
| 287 | + cfg := createMinimalValidConfig() |
| 288 | + cfg.rawPii = piiPath |
| 289 | + |
| 290 | + err = cfg.Validate() |
| 291 | + assert.NoError(t, err) |
| 292 | + assert.NotNil(t, cfg.Pii) |
| 293 | + assert.True(t, cfg.Pii.Include.Hostname) |
| 294 | + assert.False(t, cfg.Pii.Include.Username) |
| 295 | +} |
| 296 | + |
| 297 | +// Test Validate with invalid PII file path |
| 298 | +func Test_Config_Validate_WithInvalidPiiFilePath(t *testing.T) { |
| 299 | + cfg := createMinimalValidConfig() |
| 300 | + cfg.rawPii = "/nonexistent/pii.yml" |
| 301 | + |
| 302 | + err := cfg.Validate() |
| 303 | + assert.Error(t, err) |
| 304 | + assert.Contains(t, err.Error(), "pii:") |
| 305 | +} |
| 306 | + |
| 307 | +// Test Validate with filter settings from file path |
| 308 | +func Test_Config_Validate_WithFilterFilePath(t *testing.T) { |
| 309 | + tmpDir := t.TempDir() |
| 310 | + filterPath := filepath.Join(tmpDir, "filter.yml") |
| 311 | + filterContent := ` |
| 312 | +defaults: |
| 313 | + ruleset: "dl:verbose" |
| 314 | +` |
| 315 | + err := os.WriteFile(filterPath, []byte(filterContent), 0644) |
| 316 | + require.NoError(t, err) |
| 317 | + |
| 318 | + cfg := createMinimalValidConfig() |
| 319 | + cfg.rawFilter = filterPath |
| 320 | + |
| 321 | + err = cfg.Validate() |
| 322 | + assert.NoError(t, err) |
| 323 | + assert.NotNil(t, cfg.Filter) |
| 324 | +} |
| 325 | + |
| 326 | +// Test Validate with invalid filter file path |
| 327 | +func Test_Config_Validate_WithInvalidFilterFilePath(t *testing.T) { |
| 328 | + cfg := createMinimalValidConfig() |
| 329 | + cfg.rawFilter = "/nonexistent/filter.yml" |
| 330 | + |
| 331 | + err := cfg.Validate() |
| 332 | + assert.Error(t, err) |
| 333 | + assert.Contains(t, err.Error(), "filter:") |
| 334 | +} |
| 335 | + |
| 336 | +// Test Validate with summary settings from file path |
| 337 | +func Test_Config_Validate_WithSummaryFilePath(t *testing.T) { |
| 338 | + tmpDir := t.TempDir() |
| 339 | + summaryPath := filepath.Join(tmpDir, "summary.yml") |
| 340 | + summaryContent := ` |
| 341 | +message_patterns: |
| 342 | + - prefix: "error:" |
| 343 | + field_name: "error_count" |
| 344 | + - prefix: "warning:" |
| 345 | + field_name: "warning_count" |
| 346 | +
|
| 347 | +region_timers: |
| 348 | + - category: "index" |
| 349 | + label: "do_read_index" |
| 350 | + count_field: "index_read_count" |
| 351 | + time_field: "index_read_time" |
| 352 | +` |
| 353 | + err := os.WriteFile(summaryPath, []byte(summaryContent), 0644) |
| 354 | + require.NoError(t, err) |
| 355 | + |
| 356 | + cfg := createMinimalValidConfig() |
| 357 | + cfg.rawSummary = summaryPath |
| 358 | + |
| 359 | + err = cfg.Validate() |
| 360 | + assert.NoError(t, err) |
| 361 | + assert.NotNil(t, cfg.Summary) |
| 362 | + assert.Equal(t, 2, len(cfg.Summary.MessagePatterns)) |
| 363 | + assert.Equal(t, 1, len(cfg.Summary.RegionTimers)) |
| 364 | +} |
| 365 | + |
| 366 | +// Test Validate with invalid summary file path |
| 367 | +func Test_Config_Validate_WithInvalidSummaryFilePath(t *testing.T) { |
| 368 | + cfg := createMinimalValidConfig() |
| 369 | + cfg.rawSummary = "/nonexistent/summary.yml" |
| 370 | + |
| 371 | + err := cfg.Validate() |
| 372 | + assert.Error(t, err) |
| 373 | + assert.Contains(t, err.Error(), "summary:") |
| 374 | +} |
| 375 | + |
| 376 | +// Test Validate with malformed summary from file path |
| 377 | +func Test_Config_Validate_WithMalformedSummaryFilePath(t *testing.T) { |
| 378 | + tmpDir := t.TempDir() |
| 379 | + summaryPath := filepath.Join(tmpDir, "summary.yml") |
| 380 | + summaryContent := ` |
| 381 | +message_patterns: |
| 382 | + - prefix: "error:" |
| 383 | + field_name: "" |
| 384 | +` |
| 385 | + err := os.WriteFile(summaryPath, []byte(summaryContent), 0644) |
| 386 | + require.NoError(t, err) |
| 387 | + |
| 388 | + cfg := createMinimalValidConfig() |
| 389 | + cfg.rawSummary = summaryPath |
| 390 | + |
| 391 | + err = cfg.Validate() |
| 392 | + assert.Error(t, err) |
| 393 | + assert.Contains(t, err.Error(), "field_name cannot be empty") |
| 394 | +} |
| 395 | + |
272 | 396 | // Helper function to create a minimal valid config for the current platform |
273 | 397 | func createMinimalValidConfig() *Config { |
274 | 398 | if runtime.GOOS == "windows" { |
|
0 commit comments