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) {
|
func TestLoad_ValidConfig(t *testing.T) {
|
||||||
content := `
|
content := `
|
||||||
# Test configuration
|
service:
|
||||||
service.name logcorrelator
|
name: logcorrelator
|
||||||
service.language go
|
language: go
|
||||||
|
|
||||||
input.unix_socket apache_source /var/run/logcorrelator/apache.sock json
|
inputs:
|
||||||
input.unix_socket network_source /var/run/logcorrelator/network.sock json
|
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
|
outputs:
|
||||||
output.file.path /var/log/logcorrelator/correlated.log
|
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
|
correlation:
|
||||||
output.clickhouse.dsn clickhouse://user:pass@localhost:9000/db
|
key:
|
||||||
output.clickhouse.table correlated_logs
|
- src_ip
|
||||||
|
- src_port
|
||||||
correlation.key src_ip,src_port
|
time_window:
|
||||||
correlation.time_window.value 1
|
value: 1
|
||||||
correlation.time_window.unit s
|
unit: s
|
||||||
|
orphan_policy:
|
||||||
correlation.orphan_policy.apache_always_emit true
|
apache_always_emit: true
|
||||||
correlation.orphan_policy.network_emit false
|
network_emit: false
|
||||||
`
|
`
|
||||||
|
|
||||||
tmpDir := t.TempDir()
|
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 {
|
if err := os.WriteFile(configPath, []byte(content), 0644); err != nil {
|
||||||
t.Fatalf("failed to write config: %v", err)
|
t.Fatalf("failed to write config: %v", err)
|
||||||
}
|
}
|
||||||
@ -54,36 +66,41 @@ correlation.orphan_policy.network_emit false
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestLoad_InvalidPath(t *testing.T) {
|
func TestLoad_InvalidPath(t *testing.T) {
|
||||||
_, err := Load("/nonexistent/path/config.conf")
|
_, err := Load("/nonexistent/path/config.yml")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("expected error for nonexistent path")
|
t.Error("expected error for nonexistent path")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoad_InvalidDirective(t *testing.T) {
|
func TestLoad_InvalidYAML(t *testing.T) {
|
||||||
tmpDir := t.TempDir()
|
tmpDir := t.TempDir()
|
||||||
configPath := filepath.Join(tmpDir, "config.conf")
|
configPath := filepath.Join(tmpDir, "config.yml")
|
||||||
content := `invalid.directive value`
|
content := `invalid: yaml: content: [`
|
||||||
if err := os.WriteFile(configPath, []byte(content), 0644); err != nil {
|
if err := os.WriteFile(configPath, []byte(content), 0644); err != nil {
|
||||||
t.Fatalf("failed to write config: %v", err)
|
t.Fatalf("failed to write config: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := Load(configPath)
|
_, err := Load(configPath)
|
||||||
if err == nil {
|
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()
|
tmpDir := t.TempDir()
|
||||||
configPath := filepath.Join(tmpDir, "config.conf")
|
configPath := filepath.Join(tmpDir, "config.yml")
|
||||||
content := `
|
content := `
|
||||||
# This is a comment
|
service:
|
||||||
service.name logcorrelator
|
name: test-service
|
||||||
# Another comment
|
inputs:
|
||||||
input.unix_socket test /tmp/test.sock json
|
unix_sockets:
|
||||||
input.unix_socket test2 /tmp/test2.sock json
|
- name: a
|
||||||
output.file.enabled true
|
path: /tmp/a.sock
|
||||||
|
- name: b
|
||||||
|
path: /tmp/b.sock
|
||||||
|
outputs:
|
||||||
|
file:
|
||||||
|
enabled: true
|
||||||
`
|
`
|
||||||
if err := os.WriteFile(configPath, []byte(content), 0644); err != nil {
|
if err := os.WriteFile(configPath, []byte(content), 0644); err != nil {
|
||||||
t.Fatalf("failed to write config: %v", err)
|
t.Fatalf("failed to write config: %v", err)
|
||||||
@ -94,8 +111,15 @@ output.file.enabled true
|
|||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Service.Name != "logcorrelator" {
|
if cfg.Service.Name != "test-service" {
|
||||||
t.Errorf("expected service name logcorrelator, got %s", cfg.Service.Name)
|
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