package proxmox import ( "encoding/json" "testing" "github.com/mitchellh/mapstructure" ) func TestPveClusterStatusGetClusterModeNumeric(t *testing.T) { tests := []struct { name string status PveClusterStatus want float64 }{ { name: "cluster mode", status: PveClusterStatus{Type: "cluster"}, want: 1, }, { name: "standalone node", status: PveClusterStatus{}, want: 0, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := tt.status.GetClusterModeNumeric(); got != tt.want { t.Fatalf("GetClusterModeNumeric() = %v, want %v", got, tt.want) } }) } } func TestPveSubscriptionGetStatusNumeric(t *testing.T) { tests := []struct { status string want float64 }{ {status: "notfound", want: 0}, {status: "active", want: 1}, {status: "expired", want: 2}, {status: "invalid", want: 2}, {status: "suspended", want: 2}, {status: "new", want: 2}, {status: "unknown", want: 2}, } for _, tt := range tests { t.Run(tt.status, func(t *testing.T) { subscription := PveSubscription{Status: tt.status} if got := subscription.GetStatusNumeric(); got != tt.want { t.Fatalf("GetStatusNumeric() = %v, want %v", got, tt.want) } }) } } func TestPveZfsPoolStatusUnmarshal(t *testing.T) { payload := []byte(`{ "errors": "No known data errors", "state": "ONLINE", "leaf": 0, "name": "hdd", "children": [ { "name": "hdd", "state": "ONLINE", "read": 0, "write": 0, "cksum": 0, "children": [ { "name": "/dev/disk/by-id/disk-1-part1", "state": "ONLINE", "leaf": 1, "read": 0, "write": 0, "cksum": 0 } ] }, { "name": "cache", "leaf": 0 } ] }`) var status PveZfsPoolStatus if err := json.Unmarshal(payload, &status); err != nil { t.Fatalf("unable to unmarshal ZFS pool status: %v", err) } if status.Name != "hdd" || status.State != "ONLINE" || status.Errors != "No known data errors" { t.Fatalf("unexpected pool status: %+v", status) } if len(status.Children) != 2 || len(status.Children[0].Children) != 1 { t.Fatalf("unexpected ZFS topology: %+v", status.Children) } leaf := status.Children[0].Children[0] if leaf.Leaf != 1 || leaf.Read == nil || leaf.Write == nil || leaf.Checksum == nil { t.Fatal("leaf component is missing its type or error counters") } if *leaf.Read != 0 || *leaf.Write != 0 || *leaf.Checksum != 0 { t.Fatalf("unexpected leaf error counters: read=%d write=%d checksum=%d", *leaf.Read, *leaf.Write, *leaf.Checksum) } cache := status.Children[1] if cache.Read != nil || cache.Write != nil || cache.Checksum != nil { t.Fatal("missing cache counters must stay absent instead of becoming zero metrics") } } func TestPveSdnResourceGetName(t *testing.T) { tests := []struct { name string resource PveSdnResource want string }{ { name: "sdn resource", resource: PveSdnResource{SDN: "localnetwork"}, want: "localnetwork", }, { name: "network resource", resource: PveSdnResource{Network: "localnetwork", NetworkType: "zone"}, want: "localnetwork", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := tt.resource.GetName(); got != tt.want { t.Fatalf("GetName() = %v, want %v", got, tt.want) } }) } } func TestPveSdnResourceDecodeNetwork(t *testing.T) { // Resource object as reported by PVE 9 for SDN entities. obj := map[string]interface{}{ "id": "network/pve1/zone/localnetwork", "type": "network", "node": "pve1", "status": "ok", "network": "localnetwork", "network-type": "zone", } var resource PveSdnResource if err := mapstructure.Decode(obj, &resource); err != nil { t.Fatalf("Decode() error = %v", err) } if err := mapstructure.Decode(obj, &resource.PveResource); err != nil { t.Fatalf("Decode() error = %v", err) } if got, want := resource.GetName(), "localnetwork"; got != want { t.Fatalf("GetName() = %v, want %v", got, want) } if got, want := resource.NetworkType, "zone"; got != want { t.Fatalf("NetworkType = %v, want %v", got, want) } if got, want := resource.ID, "network/pve1/zone/localnetwork"; got != want { t.Fatalf("ID = %v, want %v", got, want) } if got, want := resource.GetStatusNumeric(), float64(1); got != want { t.Fatalf("GetStatusNumeric() = %v, want %v", got, want) } }