chore: version 1.0.6 - simplify YAML configuration
Some checks failed
Build and Test / test (push) Has been cancelled
Build and Test / build (push) Has been cancelled
Build and Test / docker (push) Has been cancelled

- Remove service.name and service.language (unused)
- Remove enabled flags on outputs (presence = enabled)
- Simplify correlation config: time_window_s (integer) instead of nested object
- Simplify orphan_policy to emit_orphans boolean
- Rename apache socket to http.socket
- Add socket_permissions option for unix sockets (default: 0660)
- Update tests for new configuration format

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
Jacquin Antoine
2026-03-01 01:59:59 +01:00
parent efeb7e455f
commit 56c2923121
10 changed files with 194 additions and 242 deletions

View File

@ -9,38 +9,25 @@ import (
func TestLoad_ValidConfig(t *testing.T) {
content := `
service:
name: logcorrelator
language: go
inputs:
unix_sockets:
- name: apache_source
path: /var/run/logcorrelator/apache.sock
- name: http
path: /var/run/logcorrelator/http.socket
format: json
- name: network_source
path: /var/run/logcorrelator/network.sock
- name: network
path: /var/run/logcorrelator/network.socket
format: json
outputs:
file:
enabled: true
path: /var/log/logcorrelator/correlated.log
clickhouse:
enabled: false
dsn: clickhouse://user:pass@localhost:9000/db
table: correlated_logs
correlation:
key:
- src_ip
- src_port
time_window:
value: 1
unit: s
orphan_policy:
apache_always_emit: true
network_emit: false
time_window_s: 1
emit_orphans: true
`
tmpDir := t.TempDir()
@ -54,14 +41,11 @@ correlation:
t.Fatalf("unexpected error: %v", err)
}
if cfg.Service.Name != "logcorrelator" {
t.Errorf("expected service name logcorrelator, got %s", cfg.Service.Name)
}
if len(cfg.Inputs.UnixSockets) != 2 {
t.Errorf("expected 2 unix sockets, got %d", len(cfg.Inputs.UnixSockets))
}
if !cfg.Outputs.File.Enabled {
t.Error("expected file output enabled")
if cfg.Outputs.File.Path != "/var/log/logcorrelator/correlated.log" {
t.Errorf("expected file path /var/log/logcorrelator/correlated.log, got %s", cfg.Outputs.File.Path)
}
}
@ -90,8 +74,6 @@ func TestLoad_DefaultValues(t *testing.T) {
tmpDir := t.TempDir()
configPath := filepath.Join(tmpDir, "config.yml")
content := `
service:
name: test-service
inputs:
unix_sockets:
- name: a
@ -100,7 +82,7 @@ inputs:
path: /tmp/b.sock
outputs:
file:
enabled: true
path: /var/log/test.log
`
if err := os.WriteFile(configPath, []byte(content), 0644); err != nil {
t.Fatalf("failed to write config: %v", err)
@ -111,15 +93,12 @@ outputs:
t.Fatalf("unexpected error: %v", err)
}
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.TimeWindowS != 1 {
t.Errorf("expected default time window value 1, got %d", cfg.Correlation.TimeWindowS)
}
if cfg.Correlation.OrphanPolicy.ApacheAlwaysEmit != true {
t.Error("expected default apache_always_emit to be true")
if cfg.Correlation.EmitOrphans != true {
t.Error("expected default emit_orphans to be true")
}
}
@ -131,7 +110,7 @@ func TestValidate_MinimumInputs(t *testing.T) {
},
},
Outputs: OutputsConfig{
File: FileOutputConfig{Enabled: true},
File: FileOutputConfig{Path: "/var/log/test.log"},
},
}
@ -150,9 +129,9 @@ func TestValidate_AtLeastOneOutput(t *testing.T) {
},
},
Outputs: OutputsConfig{
File: FileOutputConfig{Enabled: false},
File: FileOutputConfig{},
ClickHouse: ClickHouseOutputConfig{Enabled: false},
Stdout: StdoutOutputConfig{Enabled: false},
Stdout: false,
},
}
@ -169,30 +148,23 @@ func TestGetTimeWindow(t *testing.T) {
expected time.Duration
}{
{
name: "seconds",
name: "1 second",
config: CorrelationConfig{
TimeWindow: TimeWindowConfig{Value: 1, Unit: "s"},
TimeWindowS: 1,
},
expected: time.Second,
},
{
name: "milliseconds",
name: "5 seconds",
config: CorrelationConfig{
TimeWindow: TimeWindowConfig{Value: 500, Unit: "ms"},
TimeWindowS: 5,
},
expected: 500 * time.Millisecond,
},
{
name: "minutes",
config: CorrelationConfig{
TimeWindow: TimeWindowConfig{Value: 2, Unit: "m"},
},
expected: 2 * time.Minute,
expected: 5 * time.Second,
},
{
name: "default",
config: CorrelationConfig{
TimeWindow: TimeWindowConfig{},
TimeWindowS: 0,
},
expected: time.Second,
},
@ -217,7 +189,7 @@ func TestValidate_DuplicateNames(t *testing.T) {
},
},
Outputs: OutputsConfig{
File: FileOutputConfig{Enabled: true},
File: FileOutputConfig{Path: "/var/log/test.log"},
},
}
@ -236,7 +208,7 @@ func TestValidate_DuplicatePaths(t *testing.T) {
},
},
Outputs: OutputsConfig{
File: FileOutputConfig{Enabled: true},
File: FileOutputConfig{Path: "/var/log/test.log"},
},
}
@ -255,7 +227,7 @@ func TestValidate_EmptyName(t *testing.T) {
},
},
Outputs: OutputsConfig{
File: FileOutputConfig{Enabled: true},
File: FileOutputConfig{Path: "/var/log/test.log"},
},
}
@ -274,7 +246,7 @@ func TestValidate_EmptyPath(t *testing.T) {
},
},
Outputs: OutputsConfig{
File: FileOutputConfig{Enabled: true},
File: FileOutputConfig{Path: "/var/log/test.log"},
},
}
@ -293,7 +265,7 @@ func TestValidate_EmptyFilePath(t *testing.T) {
},
},
Outputs: OutputsConfig{
File: FileOutputConfig{Enabled: true, Path: ""},
File: FileOutputConfig{Path: ""},
},
}
@ -312,7 +284,7 @@ func TestValidate_ClickHouseMissingDSN(t *testing.T) {
},
},
Outputs: OutputsConfig{
File: FileOutputConfig{Enabled: false},
File: FileOutputConfig{Path: ""},
ClickHouse: ClickHouseOutputConfig{
Enabled: true,
DSN: "",
@ -336,7 +308,7 @@ func TestValidate_ClickHouseMissingTable(t *testing.T) {
},
},
Outputs: OutputsConfig{
File: FileOutputConfig{Enabled: false},
File: FileOutputConfig{Path: ""},
ClickHouse: ClickHouseOutputConfig{
Enabled: true,
DSN: "clickhouse://localhost:9000/db",
@ -360,7 +332,7 @@ func TestValidate_ClickHouseInvalidBatchSize(t *testing.T) {
},
},
Outputs: OutputsConfig{
File: FileOutputConfig{Enabled: false},
File: FileOutputConfig{Path: ""},
ClickHouse: ClickHouseOutputConfig{
Enabled: true,
DSN: "clickhouse://localhost:9000/db",
@ -385,7 +357,7 @@ func TestValidate_ClickHouseInvalidMaxBufferSize(t *testing.T) {
},
},
Outputs: OutputsConfig{
File: FileOutputConfig{Enabled: false},
File: FileOutputConfig{Path: ""},
ClickHouse: ClickHouseOutputConfig{
Enabled: true,
DSN: "clickhouse://localhost:9000/db",
@ -411,7 +383,7 @@ func TestValidate_ClickHouseInvalidTimeout(t *testing.T) {
},
},
Outputs: OutputsConfig{
File: FileOutputConfig{Enabled: false},
File: FileOutputConfig{Path: ""},
ClickHouse: ClickHouseOutputConfig{
Enabled: true,
DSN: "clickhouse://localhost:9000/db",
@ -437,33 +409,10 @@ func TestValidate_EmptyCorrelationKey(t *testing.T) {
},
},
Outputs: OutputsConfig{
File: FileOutputConfig{Enabled: true},
File: FileOutputConfig{Path: "/var/log/test.log"},
},
Correlation: CorrelationConfig{
Key: []string{},
},
}
err := cfg.Validate()
if err == nil {
t.Error("expected error for empty correlation key")
}
}
func TestValidate_InvalidTimeWindow(t *testing.T) {
cfg := &Config{
Inputs: InputsConfig{
UnixSockets: []UnixSocketConfig{
{Name: "a", Path: "/tmp/a.sock"},
{Name: "b", Path: "/tmp/b.sock"},
},
},
Outputs: OutputsConfig{
File: FileOutputConfig{Enabled: true},
},
Correlation: CorrelationConfig{
Key: []string{"src_ip", "src_port"},
TimeWindow: TimeWindowConfig{Value: 0},
TimeWindowS: 0,
},
}
@ -473,13 +422,55 @@ func TestValidate_InvalidTimeWindow(t *testing.T) {
}
}
func TestGetTimeWindow_UnknownUnit(t *testing.T) {
config := CorrelationConfig{
TimeWindow: TimeWindowConfig{Value: 5, Unit: "unknown"},
func TestGetSocketPermissions(t *testing.T) {
tests := []struct {
name string
config UnixSocketConfig
expected os.FileMode
}{
{
name: "default",
config: UnixSocketConfig{
SocketPermissions: "",
},
expected: 0660,
},
{
name: "explicit 0660",
config: UnixSocketConfig{
SocketPermissions: "0660",
},
expected: 0660,
},
{
name: "explicit 0666",
config: UnixSocketConfig{
SocketPermissions: "0666",
},
expected: 0666,
},
{
name: "without leading zero",
config: UnixSocketConfig{
SocketPermissions: "660",
},
expected: 0660,
},
{
name: "invalid value",
config: UnixSocketConfig{
SocketPermissions: "invalid",
},
expected: 0660,
},
}
result := config.GetTimeWindow()
expected := 5 * time.Second // Should default to seconds
if result != expected {
t.Errorf("expected %v, got %v", expected, result)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.config.GetSocketPermissions()
if result != tt.expected {
t.Errorf("expected %o, got %o", tt.expected, result)
}
})
}
}