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 invalid value tests for MemoryStatus.UnmarshalJSON

rcourtman79afff8ba2d1
diff --git a/pkg/proxmox/client_test.go b/pkg/proxmox/client_test.go
index b78a23a888..9d876ce396 100644
--- a/pkg/proxmox/client_test.go
+++ b/pkg/proxmox/client_test.go
@@ -251,6 +251,58 @@ func TestMemoryStatusUnmarshalFlexibleValues(t *testing.T) {
}
}
+func TestMemoryStatusUnmarshalJSON_InvalidValues(t *testing.T) {
+ tests := []struct {
+ name string
+ payload string
+ wantErr bool
+ }{
+ {
+ name: "invalid JSON",
+ payload: `{invalid json}`,
+ wantErr: true,
+ },
+ {
+ name: "invalid total field type",
+ payload: `{"total":true,"used":1000,"free":500}`,
+ wantErr: true,
+ },
+ {
+ name: "invalid used field type",
+ payload: `{"total":1000,"used":[1,2,3],"free":500}`,
+ wantErr: true,
+ },
+ {
+ name: "invalid free field type",
+ payload: `{"total":1000,"used":500,"free":{"nested":"object"}}`,
+ wantErr: true,
+ },
+ {
+ name: "invalid available field type",
+ payload: `{"total":1000,"used":500,"free":300,"available":true}`,
+ wantErr: true,
+ },
+ {
+ name: "invalid string value for total",
+ payload: `{"total":"not-a-number","used":500}`,
+ wantErr: true,
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ var status MemoryStatus
+ err := json.Unmarshal([]byte(tc.payload), &status)
+ if tc.wantErr && err == nil {
+ t.Errorf("expected error for %s, got nil", tc.name)
+ }
+ if !tc.wantErr && err != nil {
+ t.Errorf("unexpected error for %s: %v", tc.name, err)
+ }
+ })
+ }
+}
+
func TestFlexIntUnmarshalJSON(t *testing.T) {
tests := []struct {
name string