Pulse/cmd/pulse-sensor-proxy/config_test.go
2025-11-18 22:51:40 +00:00

84 lines
2.2 KiB
Go

package main
import (
"strings"
"testing"
)
func TestSanitizeDuplicateAllowedNodesBlocks_RemovesExtraBlocks(t *testing.T) {
raw := `
allowed_nodes:
- delly
- minipc
# Cluster nodes (auto-discovered during installation)
# These nodes are allowed to request temperature data when cluster IPC validation is unavailable
allowed_nodes:
- delly
- minipc
- extra
`
sanitized, out := sanitizeDuplicateAllowedNodesBlocks("", []byte(raw))
if !sanitized {
t.Fatalf("expected sanitization to occur")
}
result := string(out)
if strings.Count(result, "allowed_nodes:") != 1 {
t.Fatalf("expected only one allowed_nodes block, got %q", result)
}
if strings.Contains(result, "extra") {
t.Fatalf("duplicate entries should be removed, got %q", result)
}
if strings.Contains(result, "Cluster nodes (auto-discovered during installation)") {
t.Fatalf("duplicate comment block should be removed")
}
}
func TestSanitizeDuplicateAllowedNodesBlocks_NoChangeWhenUnique(t *testing.T) {
raw := `
metrics_address: 127.0.0.1:9127
allowed_nodes:
- delly
`
sanitized, out := sanitizeDuplicateAllowedNodesBlocks("", []byte(raw))
if sanitized {
t.Fatalf("unexpected sanitization for unique config")
}
if string(out) != raw {
t.Fatalf("expected config to remain unchanged")
}
}
func TestSanitizeDuplicateAllowedNodesBlocks_WithCommentBlocks(t *testing.T) {
raw := `
allowed_source_subnets:
- 192.168.1.0/24
# Cluster nodes (auto-discovered during installation)
# These nodes are allowed to request temperature data when cluster IPC validation is unavailable
allowed_nodes:
- delly
- minipc
# Cluster nodes (auto-discovered during installation)
# These nodes are allowed to request temperature data when cluster IPC validation is unavailable
allowed_nodes:
- delly
- minipc
`
sanitized, out := sanitizeDuplicateAllowedNodesBlocks("", []byte(raw))
if !sanitized {
t.Fatalf("expected sanitizer to run for duplicate comment blocks")
}
result := string(out)
if strings.Count(result, "allowed_nodes:") != 1 {
t.Fatalf("expected a single allowed_nodes block, got %q", result)
}
if strings.Count(result, "# Cluster nodes") != 1 {
t.Fatalf("expected duplicate comments to collapse, got %q", result)
}
}