mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-04-29 20:10:21 +00:00
Add unit tests for pulse-sensor-proxy utility functions
- hashIPToUID: 11 test cases covering IP hashing for rate limiting (determinism, range bounds, collision detection, boundary values) - extractNodesFromYAML: 17 test cases covering YAML node list parsing (map format, list format, mixed types, edge cases) First test files for config_cmd.go and http_server.go utilities.
This commit is contained in:
parent
4c39f2f58f
commit
48af5615b9
2 changed files with 303 additions and 0 deletions
160
cmd/pulse-sensor-proxy/config_cmd_test.go
Normal file
160
cmd/pulse-sensor-proxy/config_cmd_test.go
Normal file
|
|
@ -0,0 +1,160 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestExtractNodesFromYAML(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expected []string
|
||||
}{
|
||||
// Map format with allowed_nodes key
|
||||
{
|
||||
name: "map format with allowed_nodes key",
|
||||
input: `allowed_nodes:
|
||||
- node1
|
||||
- node2
|
||||
- node3`,
|
||||
expected: []string{"node1", "node2", "node3"},
|
||||
},
|
||||
{
|
||||
name: "map format with other keys",
|
||||
input: `metrics_address: 127.0.0.1:9127
|
||||
allowed_nodes:
|
||||
- host1
|
||||
- host2
|
||||
ssh_timeout: 30s`,
|
||||
expected: []string{"host1", "host2"},
|
||||
},
|
||||
{
|
||||
name: "map format empty allowed_nodes",
|
||||
input: `allowed_nodes: []
|
||||
other_key: value`,
|
||||
expected: nil,
|
||||
},
|
||||
|
||||
// List format (bare YAML list)
|
||||
{
|
||||
name: "list format bare",
|
||||
input: `- node1
|
||||
- node2
|
||||
- node3`,
|
||||
expected: []string{"node1", "node2", "node3"},
|
||||
},
|
||||
{
|
||||
name: "list format single item",
|
||||
input: `- single-node`,
|
||||
expected: []string{"single-node"},
|
||||
},
|
||||
|
||||
// Empty and edge cases
|
||||
{
|
||||
name: "empty input",
|
||||
input: ``,
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "only whitespace",
|
||||
input: ` `,
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "empty strings filtered",
|
||||
input: `allowed_nodes:
|
||||
- node1
|
||||
- ""
|
||||
- node2`,
|
||||
expected: []string{"node1", "node2"},
|
||||
},
|
||||
{
|
||||
name: "null value in list",
|
||||
input: `allowed_nodes:
|
||||
- node1
|
||||
- ~
|
||||
- node2`,
|
||||
expected: []string{"node1", "node2"},
|
||||
},
|
||||
{
|
||||
name: "invalid YAML",
|
||||
input: `{{{invalid`,
|
||||
expected: nil,
|
||||
},
|
||||
|
||||
// Mixed content
|
||||
{
|
||||
name: "map format with comments",
|
||||
input: `# This is a comment
|
||||
allowed_nodes:
|
||||
- node1
|
||||
# inline comment
|
||||
- node2`,
|
||||
expected: []string{"node1", "node2"},
|
||||
},
|
||||
{
|
||||
name: "map format allowed_nodes not a list",
|
||||
input: `allowed_nodes: not-a-list`,
|
||||
expected: nil,
|
||||
},
|
||||
|
||||
// Different YAML structures
|
||||
{
|
||||
name: "nested structure ignored",
|
||||
input: `config:
|
||||
allowed_nodes:
|
||||
- nested-node`,
|
||||
expected: nil, // only top-level allowed_nodes is recognized
|
||||
},
|
||||
{
|
||||
name: "just null",
|
||||
input: `~`,
|
||||
expected: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := extractNodesFromYAML([]byte(tc.input))
|
||||
|
||||
if !reflect.DeepEqual(result, tc.expected) {
|
||||
t.Errorf("extractNodesFromYAML() = %v, want %v", result, tc.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractNodesFromYAML_WithNumericValues(t *testing.T) {
|
||||
// YAML can have non-string values in lists
|
||||
input := `allowed_nodes:
|
||||
- node1
|
||||
- 12345
|
||||
- true
|
||||
- node2`
|
||||
|
||||
result := extractNodesFromYAML([]byte(input))
|
||||
|
||||
// Only string values should be extracted
|
||||
expected := []string{"node1", "node2"}
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("extractNodesFromYAML() = %v, want %v", result, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractNodesFromYAML_ListWithMixedTypes(t *testing.T) {
|
||||
// Bare list with mixed types
|
||||
input := `- host1
|
||||
- 42
|
||||
- host2
|
||||
- null
|
||||
- host3`
|
||||
|
||||
result := extractNodesFromYAML([]byte(input))
|
||||
|
||||
// Only non-empty strings should be extracted
|
||||
expected := []string{"host1", "host2", "host3"}
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("extractNodesFromYAML() = %v, want %v", result, expected)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue