test: update config tests for YAML format
- Replace custom directive tests with YAML-based tests - Test valid YAML config loading - Test invalid YAML handling - Test default values with partial YAML config Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
@ -9,30 +9,42 @@ import (
|
||||
|
||||
func TestLoad_ValidConfig(t *testing.T) {
|
||||
content := `
|
||||
# Test configuration
|
||||
service.name logcorrelator
|
||||
service.language go
|
||||
service:
|
||||
name: logcorrelator
|
||||
language: go
|
||||
|
||||
input.unix_socket apache_source /var/run/logcorrelator/apache.sock json
|
||||
input.unix_socket network_source /var/run/logcorrelator/network.sock json
|
||||
inputs:
|
||||
unix_sockets:
|
||||
- name: apache_source
|
||||
path: /var/run/logcorrelator/apache.sock
|
||||
format: json
|
||||
- name: network_source
|
||||
path: /var/run/logcorrelator/network.sock
|
||||
format: json
|
||||
|
||||
output.file.enabled true
|
||||
output.file.path /var/log/logcorrelator/correlated.log
|
||||
outputs:
|
||||
file:
|
||||
enabled: true
|
||||
path: /var/log/logcorrelator/correlated.log
|
||||
clickhouse:
|
||||
enabled: false
|
||||
dsn: clickhouse://user:pass@localhost:9000/db
|
||||
table: correlated_logs
|
||||
|
||||
output.clickhouse.enabled false
|
||||
output.clickhouse.dsn clickhouse://user:pass@localhost:9000/db
|
||||
output.clickhouse.table correlated_logs
|
||||
|
||||
correlation.key src_ip,src_port
|
||||
correlation.time_window.value 1
|
||||
correlation.time_window.unit s
|
||||
|
||||
correlation.orphan_policy.apache_always_emit true
|
||||
correlation.orphan_policy.network_emit false
|
||||
correlation:
|
||||
key:
|
||||
- src_ip
|
||||
- src_port
|
||||
time_window:
|
||||
value: 1
|
||||
unit: s
|
||||
orphan_policy:
|
||||
apache_always_emit: true
|
||||
network_emit: false
|
||||
`
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
configPath := filepath.Join(tmpDir, "config.conf")
|
||||
configPath := filepath.Join(tmpDir, "config.yml")
|
||||
if err := os.WriteFile(configPath, []byte(content), 0644); err != nil {
|
||||
t.Fatalf("failed to write config: %v", err)
|
||||
}
|
||||
@ -54,36 +66,41 @@ correlation.orphan_policy.network_emit false
|
||||
}
|
||||
|
||||
func TestLoad_InvalidPath(t *testing.T) {
|
||||
_, err := Load("/nonexistent/path/config.conf")
|
||||
_, err := Load("/nonexistent/path/config.yml")
|
||||
if err == nil {
|
||||
t.Error("expected error for nonexistent path")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_InvalidDirective(t *testing.T) {
|
||||
func TestLoad_InvalidYAML(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
configPath := filepath.Join(tmpDir, "config.conf")
|
||||
content := `invalid.directive value`
|
||||
configPath := filepath.Join(tmpDir, "config.yml")
|
||||
content := `invalid: yaml: content: [`
|
||||
if err := os.WriteFile(configPath, []byte(content), 0644); err != nil {
|
||||
t.Fatalf("failed to write config: %v", err)
|
||||
}
|
||||
|
||||
_, err := Load(configPath)
|
||||
if err == nil {
|
||||
t.Error("expected error for invalid directive")
|
||||
t.Error("expected error for invalid YAML")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_Comments(t *testing.T) {
|
||||
func TestLoad_DefaultValues(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
configPath := filepath.Join(tmpDir, "config.conf")
|
||||
configPath := filepath.Join(tmpDir, "config.yml")
|
||||
content := `
|
||||
# This is a comment
|
||||
service.name logcorrelator
|
||||
# Another comment
|
||||
input.unix_socket test /tmp/test.sock json
|
||||
input.unix_socket test2 /tmp/test2.sock json
|
||||
output.file.enabled true
|
||||
service:
|
||||
name: test-service
|
||||
inputs:
|
||||
unix_sockets:
|
||||
- name: a
|
||||
path: /tmp/a.sock
|
||||
- name: b
|
||||
path: /tmp/b.sock
|
||||
outputs:
|
||||
file:
|
||||
enabled: true
|
||||
`
|
||||
if err := os.WriteFile(configPath, []byte(content), 0644); err != nil {
|
||||
t.Fatalf("failed to write config: %v", err)
|
||||
@ -94,8 +111,15 @@ output.file.enabled true
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if cfg.Service.Name != "logcorrelator" {
|
||||
t.Errorf("expected service name logcorrelator, got %s", cfg.Service.Name)
|
||||
if cfg.Service.Name != "test-service" {
|
||||
t.Errorf("expected service name test-service, got %s", cfg.Service.Name)
|
||||
}
|
||||
// Check defaults
|
||||
if cfg.Correlation.TimeWindow.Value != 1 {
|
||||
t.Errorf("expected default time window value 1, got %d", cfg.Correlation.TimeWindow.Value)
|
||||
}
|
||||
if cfg.Correlation.OrphanPolicy.ApacheAlwaysEmit != true {
|
||||
t.Error("expected default apache_always_emit to be true")
|
||||
}
|
||||
}
|
||||
|
||||
@ -183,42 +207,3 @@ func TestGetTimeWindow(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseBool(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
hasError bool
|
||||
}{
|
||||
{"true", true, false},
|
||||
{"True", true, false},
|
||||
{"TRUE", true, false},
|
||||
{"yes", true, false},
|
||||
{"1", true, false},
|
||||
{"on", true, false},
|
||||
{"false", false, false},
|
||||
{"False", false, false},
|
||||
{"no", false, false},
|
||||
{"0", false, false},
|
||||
{"off", false, false},
|
||||
{"invalid", false, true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.input, func(t *testing.T) {
|
||||
result, err := parseBool(tt.input)
|
||||
if tt.hasError {
|
||||
if err == nil {
|
||||
t.Error("expected error, got nil")
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if result != tt.expected {
|
||||
t.Errorf("expected %v, got %v", tt.expected, result)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user