package metrics import ( "testing" "lostak.dev/pve-exporter/proxmox" ) func TestFlattenZfsComponents(t *testing.T) { zero := uint64(0) status := &proxmox.PveZfsPoolStatus{ PveZfsComponent: proxmox.PveZfsComponent{ Name: "hdd", State: "ONLINE", Children: []proxmox.PveZfsComponent{ { Name: "hdd", Children: []proxmox.PveZfsComponent{ { Name: "raidz1-0", State: "ONLINE", Read: &zero, Write: &zero, Checksum: &zero, Children: []proxmox.PveZfsComponent{ { Name: "/dev/disk/by-id/disk-1-part1", State: "ONLINE", Leaf: 1, Read: &zero, Write: &zero, Checksum: &zero, }, }, }, }, }, }, }, } components := flattenZfsComponents(status) if len(components) != 4 { t.Fatalf("flattenZfsComponents() returned %d components, want 4", len(components)) } leaf := components[3] wantPath := "hdd > hdd > raidz1-0 > /dev/disk/by-id/disk-1-part1" if leaf.Path != wantPath { t.Fatalf("leaf path = %q, want %q", leaf.Path, wantPath) } if leaf.Leaf != 1 || leaf.Read == nil || leaf.Write == nil || leaf.Checksum == nil { t.Fatal("leaf component lost its type or error counters") } } func TestZfsStateNumeric(t *testing.T) { tests := []struct { state string want float64 }{ {state: "UNKNOWN", want: 0}, {state: "ONLINE", want: 1}, {state: "DEGRADED", want: 2}, {state: "FAULTED", want: 3}, {state: "OFFLINE", want: 4}, {state: "REMOVED", want: 5}, {state: "UNAVAIL", want: 6}, } for _, tt := range tests { t.Run(tt.state, func(t *testing.T) { if got := zfsStateNumeric(tt.state); got != tt.want { t.Fatalf("zfsStateNumeric(%q) = %v, want %v", tt.state, got, tt.want) } }) } }