Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

test: add unit tests for hostagent package

rcourtmane4f4688fe400
diff --git a/internal/hostagent/agent_test.go b/internal/hostagent/agent_test.go
new file mode 100644
index 0000000000..6cf024e8e4
--- /dev/null
+++ b/internal/hostagent/agent_test.go
@@ -0,0 +1,140 @@
+package hostagent
+
+import (
+ "testing"
+ "time"
+
+ "github.com/rs/zerolog"
+)
+
+func TestNormalisePlatform(t *testing.T) {
+ tests := []struct {
+ input string
+ expected string
+ }{
+ {"darwin", "macos"},
+ {"Darwin", "macos"},
+ {"DARWIN", "macos"},
+ {" darwin ", "macos"},
+ {"linux", "linux"},
+ {"Linux", "linux"},
+ {"windows", "windows"},
+ {"freebsd", "freebsd"},
+ {"", ""},
+ {" ", ""},
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.input, func(t *testing.T) {
+ result := normalisePlatform(tc.input)
+ if result != tc.expected {
+ t.Errorf("normalisePlatform(%q) = %q, want %q", tc.input, result, tc.expected)
+ }
+ })
+ }
+}
+
+func TestIsLoopback(t *testing.T) {
+ tests := []struct {
+ name string
+ flags []string
+ expected bool
+ }{
+ {
+ name: "has loopback",
+ flags: []string{"up", "loopback", "running"},
+ expected: true,
+ },
+ {
+ name: "has LOOPBACK uppercase",
+ flags: []string{"UP", "LOOPBACK", "RUNNING"},
+ expected: true,
+ },
+ {
+ name: "has Loopback mixed case",
+ flags: []string{"up", "Loopback", "running"},
+ expected: true,
+ },
+ {
+ name: "no loopback",
+ flags: []string{"up", "broadcast", "running"},
+ expected: false,
+ },
+ {
+ name: "empty flags",
+ flags: []string{},
+ expected: false,
+ },
+ {
+ name: "nil flags",
+ flags: nil,
+ expected: false,
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ result := isLoopback(tc.flags)
+ if result != tc.expected {
+ t.Errorf("isLoopback(%v) = %v, want %v", tc.flags, result, tc.expected)
+ }
+ })
+ }
+}
+
+func TestNewAgent_MissingToken(t *testing.T) {
+ cfg := Config{
+ PulseURL: "http://localhost:7655",
+ APIToken: "",
+ }
+
+ _, err := New(cfg)
+ if err == nil {
+ t.Error("New() should fail when API token is missing")
+ }
+}
+
+func TestNewAgent_WhitespaceToken(t *testing.T) {
+ cfg := Config{
+ PulseURL: "http://localhost:7655",
+ APIToken: " ",
+ }
+
+ _, err := New(cfg)
+ if err == nil {
+ t.Error("New() should fail when API token is only whitespace")
+ }
+}
+
+func TestNewAgent_DefaultInterval(t *testing.T) {
+ // We can't easily test New() without mocking gohost.InfoWithContext
+ // but we can verify the default interval constant
+ if defaultInterval != 30*time.Second {
+ t.Errorf("defaultInterval = %v, want %v", defaultInterval, 30*time.Second)
+ }
+}
+
+func TestConfig_Defaults(t *testing.T) {
+ cfg := Config{}
+
+ // Verify zero values
+ if cfg.Interval != 0 {
+ t.Errorf("Config.Interval should be zero by default, got %v", cfg.Interval)
+ }
+ if cfg.InsecureSkipVerify {
+ t.Error("Config.InsecureSkipVerify should be false by default")
+ }
+ if cfg.RunOnce {
+ t.Error("Config.RunOnce should be false by default")
+ }
+ if cfg.LogLevel != zerolog.Level(0) {
+ t.Errorf("Config.LogLevel should be 0 (TraceLevel) by default, got %v", cfg.LogLevel)
+ }
+}
+
+func TestVersion(t *testing.T) {
+ // Verify Version is set (build-time override or default)
+ if Version == "" {
+ t.Error("Version should not be empty")
+ }
+}