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(coverage): unifiedresources adapters and agentexec server-receipt branches

rcourtmane18b28a78071
diff --git a/internal/agentexec/server_docker_receipt_coverage_test.go b/internal/agentexec/server_docker_receipt_coverage_test.go
new file mode 100644
index 0000000000..39be832674
--- /dev/null
+++ b/internal/agentexec/server_docker_receipt_coverage_test.go
@@ -0,0 +1,189 @@
+package agentexec
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/operationreceipt"
+)
+
+func TestMatchesPendingDockerUpdateOperation_Coverage(t *testing.T) {
+ const agentID = "a1"
+
+ baseResult := DockerContainerUpdateResultPayload{
+ RequestID: "req-1",
+ ActionID: "act-1",
+ Operation: "docker.container.update",
+ OperationVersion: 3,
+ RequestDigest: "sha256:abc",
+ ContainerID: "AbC123",
+ }
+ baseIdentity := operationreceipt.Identity{
+ AttemptID: baseResult.RequestID,
+ ActionID: baseResult.ActionID,
+ OperationKind: baseResult.Operation,
+ OperationVersion: baseResult.OperationVersion,
+ RequestDigest: baseResult.RequestDigest,
+ AgentID: strings.TrimSpace(agentID),
+ }
+ baseSubjectID := strings.ToLower(strings.TrimSpace(baseResult.ContainerID))
+
+ storePending := func(s *Server, identity operationreceipt.Identity, subjectID string) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ s.pendingHostOperations[pendingRequestKey(agentID, baseResult.RequestID)] = pendingHostOperation{
+ identity: identity,
+ subjectID: subjectID,
+ }
+ }
+
+ cases := []struct {
+ name string
+ setup func(s *Server)
+ result DockerContainerUpdateResultPayload
+ want bool
+ }{
+ {
+ name: "matching pending operation returns true",
+ setup: func(s *Server) {
+ storePending(s, baseIdentity, baseSubjectID)
+ },
+ result: baseResult,
+ want: true,
+ },
+ {
+ name: "absent pending key returns false",
+ setup: func(s *Server) {
+ },
+ result: baseResult,
+ want: false,
+ },
+ {
+ name: "mismatched identity returns false",
+ setup: func(s *Server) {
+ different := baseIdentity
+ different.ActionID = "different-action"
+ storePending(s, different, baseSubjectID)
+ },
+ result: baseResult,
+ want: false,
+ },
+ {
+ name: "mismatched container id returns false",
+ setup: func(s *Server) {
+ storePending(s, baseIdentity, "different-container")
+ },
+ result: baseResult,
+ want: false,
+ },
+ {
+ name: "normalized padded mixed case container id still matches",
+ setup: func(s *Server) {
+ storePending(s, baseIdentity, baseSubjectID)
+ },
+ result: DockerContainerUpdateResultPayload{
+ RequestID: baseResult.RequestID,
+ ActionID: baseResult.ActionID,
+ Operation: baseResult.Operation,
+ OperationVersion: baseResult.OperationVersion,
+ RequestDigest: baseResult.RequestDigest,
+ ContainerID: " AbC123 ",
+ },
+ want: true,
+ },
+ {
+ name: "mismatched request digest returns false",
+ setup: func(s *Server) {
+ different := baseIdentity
+ different.RequestDigest = "sha256:different"
+ storePending(s, different, baseSubjectID)
+ },
+ result: baseResult,
+ want: false,
+ },
+ }
+
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ s := NewServer(allowAllTestTokens)
+ tc.setup(s)
+ got := s.matchesPendingDockerUpdateOperation(agentID, tc.result)
+ if got != tc.want {
+ t.Fatalf("matchesPendingDockerUpdateOperation(%q, %+v) = %v, want %v", agentID, tc.result, got, tc.want)
+ }
+ })
+ }
+}
+
+func TestAgentOperationReceiptVersion_Coverage(t *testing.T) {
+ cases := []struct {
+ name string
+ build func() (*Server, string)
+ want int
+ }{
+ {
+ name: "nil receiver returns zero",
+ build: func() (*Server, string) { var s *Server; return s, "a1" },
+ want: 0,
+ },
+ {
+ name: "empty server returns zero",
+ build: func() (*Server, string) { return NewServer(allowAllTestTokens), "a1" },
+ want: 0,
+ },
+ {
+ name: "unknown agent returns zero",
+ build: func() (*Server, string) {
+ s := NewServer(allowAllTestTokens)
+ s.mu.Lock()
+ s.agents["a1"] = &agentConn{agent: ConnectedAgent{AgentID: "a1", OperationReceiptVersion: 7}}
+ s.mu.Unlock()
+ return s, "a2"
+ },
+ want: 0,
+ },
+ {
+ name: "known agent returns its receipt version",
+ build: func() (*Server, string) {
+ s := NewServer(allowAllTestTokens)
+ s.mu.Lock()
+ s.agents["a1"] = &agentConn{agent: ConnectedAgent{AgentID: "a1", OperationReceiptVersion: 7}}
+ s.mu.Unlock()
+ return s, "a1"
+ },
+ want: 7,
+ },
+ {
+ name: "whitespace padded agent id still resolves to known agent",
+ build: func() (*Server, string) {
+ s := NewServer(allowAllTestTokens)
+ s.mu.Lock()
+ s.agents["a1"] = &agentConn{agent: ConnectedAgent{AgentID: "a1", OperationReceiptVersion: 7}}
+ s.mu.Unlock()
+ return s, " a1 "
+ },
+ want: 7,
+ },
+ {
+ name: "known agent with zero receipt version returns zero",
+ build: func() (*Server, string) {
+ s := NewServer(allowAllTestTokens)
+ s.mu.Lock()
+ s.agents["a1"] = &agentConn{agent: ConnectedAgent{AgentID: "a1", OperationReceiptVersion: 0}}
+ s.mu.Unlock()
+ return s, "a1"
+ },
+ want: 0,
+ },
+ }
+
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ s, agentID := tc.build()
+ got := s.AgentOperationReceiptVersion(agentID)
+ if got != tc.want {
+ t.Fatalf("AgentOperationReceiptVersion(%q) = %d, want %d", agentID, got, tc.want)
+ }
+ })
+ }
+}
diff --git a/internal/unifiedresources/adapters_pure_coverage_test.go b/internal/unifiedresources/adapters_pure_coverage_test.go
new file mode 100644
index 0000000000..33d8a97b21
--- /dev/null
+++ b/internal/unifiedresources/adapters_pure_coverage_test.go
@@ -0,0 +1,144 @@
+package unifiedresources
+
+import (
+ "testing"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/models"
+)
+
+// TestUnraidDiskMountTotal covers the pure adapter unraidDiskMountTotal, which
+// resolves a disk's mount path via unraidDiskMountPath (driven by disk role and
+// name) and then selects the matching host disk's reported Total bytes.
+func TestUnraidDiskMountTotal(t *testing.T) {
+ tests := []struct {
+ name string
+ host models.Host
+ disk models.HostUnraidDisk
+ want int64
+ }{
+ {
+ name: "zero-value disk resolves to no mount path",
+ host: models.Host{},
+ disk: models.HostUnraidDisk{},
+ want: 0,
+ },
+ {
+ name: "explicit parity role yields no mount path",
+ host: models.Host{Disks: []models.Disk{{Mountpoint: "/mnt/disk1", Total: 1000}}},
+ disk: models.HostUnraidDisk{Name: "parity1", Role: "parity"},
+ want: 0,
+ },
+ {
+ name: "data role matches host disk total",
+ host: models.Host{Disks: []models.Disk{
+ {Mountpoint: "/mnt/cache", Total: 5000},
+ {Mountpoint: "/mnt/disk1", Total: 4000},
+ }},
+ disk: models.HostUnraidDisk{Name: "disk1", Role: "data"},
+ want: 4000,
+ },
+ {
+ name: "cache role matches host disk total",
+ host: models.Host{Disks: []models.Disk{{Mountpoint: "/mnt/cache", Total: 3000}}},
+ disk: models.HostUnraidDisk{Name: "cache", Role: "cache"},
+ want: 3000,
+ },
+ {
+ name: "data role inferred from disk name prefix without explicit role",
+ host: models.Host{Disks: []models.Disk{{Mountpoint: "/mnt/disk2", Total: 8000}}},
+ disk: models.HostUnraidDisk{Name: "disk2"},
+ want: 8000,
+ },
+ {
+ name: "matching mountpoint but zero total is skipped",
+ host: models.Host{Disks: []models.Disk{{Mountpoint: "/mnt/disk1", Total: 0}}},
+ disk: models.HostUnraidDisk{Name: "disk1", Role: "data"},
+ want: 0,
+ },
+ {
+ name: "matching mountpoint but negative total is skipped",
+ host: models.Host{Disks: []models.Disk{{Mountpoint: "/mnt/disk1", Total: -5}}},
+ disk: models.HostUnraidDisk{Name: "disk1", Role: "data"},
+ want: 0,
+ },
+ {
+ name: "non-matching host mountpoints yield zero",
+ host: models.Host{Disks: []models.Disk{
+ {Mountpoint: "/mnt/cache", Total: 9000},
+ {Mountpoint: "/mnt/user", Total: 7000},
+ }},
+ disk: models.HostUnraidDisk{Name: "disk1", Role: "data"},
+ want: 0,
+ },
+ {
+ name: "host mountpoint with surrounding whitespace is trimmed and matched",
+ host: models.Host{Disks: []models.Disk{{Mountpoint: " /mnt/disk1 ", Total: 6000}}},
+ disk: models.HostUnraidDisk{Name: "disk1", Role: "data"},
+ want: 6000,
+ },
+ {
+ name: "resolved mount path but empty host disks slice",
+ host: models.Host{Disks: []models.Disk{}},
+ disk: models.HostUnraidDisk{Name: "disk1", Role: "data"},
+ want: 0,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := unraidDiskMountTotal(tt.host, tt.disk)
+ if got != tt.want {
+ t.Fatalf("unraidDiskMountTotal() = %d, want %d", got, tt.want)
+ }
+ })
+ }
+}
+
+// TestDockerStorageUsageMeta covers the pure adapter dockerStorageUsageMeta,
+// which returns nil for an all-zero bucket and otherwise constructs a
+// DockerStorageUsageMeta copying all four bucket fields.
+func TestDockerStorageUsageMeta(t *testing.T) {
+ t.Run("zero-value bucket returns nil", func(t *testing.T) {
+ got := dockerStorageUsageMeta(models.DockerStorageUsageBucket{})
+ if got != nil {
+ t.Fatalf("expected nil for zero-value bucket, got %+v", got)
+ }
+ })
+
+ populated := []struct {
+ name string
+ bucket models.DockerStorageUsageBucket
+ }{
+ {name: "only total count set short-circuits nil arm", bucket: models.DockerStorageUsageBucket{TotalCount: 3}},
+ {name: "only active count set short-circuits nil arm", bucket: models.DockerStorageUsageBucket{ActiveCount: 2}},
+ {name: "only total size set short-circuits nil arm", bucket: models.DockerStorageUsageBucket{TotalSizeBytes: 4096}},
+ {name: "only reclaimable set short-circuits nil arm", bucket: models.DockerStorageUsageBucket{ReclaimableBytes: 2048}},
+ {name: "fully populated bucket", bucket: models.DockerStorageUsageBucket{
+ TotalCount: 12,
+ ActiveCount: 7,
+ TotalSizeBytes: 9999,
+ ReclaimableBytes: 1234,
+ }},
+ }
+
+ for _, tt := range populated {
+ t.Run(tt.name, func(t *testing.T) {
+ got := dockerStorageUsageMeta(tt.bucket)
+ if got == nil {
+ t.Fatalf("expected non-nil meta for non-zero bucket")
+ }
+ if got.TotalCount != tt.bucket.TotalCount {
+ t.Fatalf("TotalCount = %d, want %d", got.TotalCount, tt.bucket.TotalCount)
+ }
+ if got.ActiveCount != tt.bucket.ActiveCount {
+ t.Fatalf("ActiveCount = %d, want %d", got.ActiveCount, tt.bucket.ActiveCount)
+ }
+ if got.TotalSizeBytes != tt.bucket.TotalSizeBytes {
+ t.Fatalf("TotalSizeBytes = %d, want %d", got.TotalSizeBytes, tt.bucket.TotalSizeBytes)
+ }
+ if got.ReclaimableBytes != tt.bucket.ReclaimableBytes {
+ t.Fatalf("ReclaimableBytes = %d, want %d", got.ReclaimableBytes, tt.bucket.ReclaimableBytes)
+ }
+ })
+ }
+}