mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-11-29 17:21:13 +01:00
A pool built from /dev/disk/by-id/nvme-MODEL_SERIAL_1-part1 references (systemd appends the _1 namespace token) made serialFromByID take the last underscore token, so it extracted the namespace digit instead of the serial and the disk fell back to the generic 'ZFS' usage string while an identically-built node without the suffix resolved its pool name. Reported with exact zpool output in issue #1540. Contract-Neutral: bugfix restoring intended disk-to-pool annotation (#1540); no contract surface changes
155 lines
4.1 KiB
Go
155 lines
4.1 KiB
Go
package monitoring
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/models"
|
|
"github.com/rcourtman/pulse-go-rewrite/pkg/proxmox"
|
|
)
|
|
|
|
func TestDiskPoolAssignmentLookup(t *testing.T) {
|
|
pools := []proxmox.ZFSPoolInfo{
|
|
{
|
|
Name: "rpool",
|
|
Devices: []proxmox.ZFSPoolDevice{
|
|
{
|
|
Name: "mirror-0",
|
|
Children: []proxmox.ZFSPoolDevice{
|
|
{Name: "/dev/disk/by-id/ata-Samsung_SSD_870_EVO_1TB_S5Y2NX0R500001Z-part3", Leaf: 1},
|
|
{Name: "/dev/disk/by-id/ata-Samsung_SSD_870_EVO_1TB_S5Y2NX0R500002Z-part3", Leaf: 1},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Name: "tank",
|
|
Devices: []proxmox.ZFSPoolDevice{
|
|
{Name: "sdc", Leaf: 1},
|
|
{Name: "/dev/nvme1n1p1", Leaf: 1},
|
|
},
|
|
},
|
|
{
|
|
Name: "scratch",
|
|
Devices: []proxmox.ZFSPoolDevice{
|
|
{Name: "wwn-0x50014ee2123456ab", Leaf: 1},
|
|
},
|
|
},
|
|
{
|
|
Name: "flash",
|
|
Devices: []proxmox.ZFSPoolDevice{
|
|
{Name: "/dev/disk/by-id/nvme-eui.0025385b91501234-part3", Leaf: 1},
|
|
},
|
|
},
|
|
{
|
|
Name: "local-zfs",
|
|
Devices: []proxmox.ZFSPoolDevice{
|
|
// systemd nvme by-id link with a trailing _1 namespace suffix,
|
|
// as reported on #1540's pve01.
|
|
{Name: "/dev/disk/by-id/nvme-Samsung_SSD_990_PRO_4TB_S7DPNF0Y316714T_1-part1", Leaf: 1},
|
|
},
|
|
},
|
|
}
|
|
|
|
assignment := buildDiskPoolAssignment(pools)
|
|
|
|
cases := []struct {
|
|
label string
|
|
disk models.PhysicalDisk
|
|
want string
|
|
}{
|
|
{
|
|
label: "by-id match via serial token",
|
|
disk: models.PhysicalDisk{DevPath: "/dev/sda", Serial: "S5Y2NX0R500001Z"},
|
|
want: "rpool",
|
|
},
|
|
{
|
|
label: "nvme-eui by-id leaf matches disk WWN in eui form",
|
|
disk: models.PhysicalDisk{DevPath: "/dev/nvme0n1", WWN: "eui.0025385B91501234"},
|
|
want: "flash",
|
|
},
|
|
{
|
|
label: "nvme-eui by-id leaf matches disk WWN in 0x form",
|
|
disk: models.PhysicalDisk{DevPath: "/dev/nvme0n1", WWN: "0x0025385b91501234"},
|
|
want: "flash",
|
|
},
|
|
{
|
|
label: "nvme by-id leaf with namespace suffix matches disk serial",
|
|
disk: models.PhysicalDisk{DevPath: "/dev/nvme2n1", Serial: "S7DPNF0Y316714T"},
|
|
want: "local-zfs",
|
|
},
|
|
{
|
|
label: "short leaf name matches partition-stripped devpath",
|
|
disk: models.PhysicalDisk{DevPath: "/dev/sdc"},
|
|
want: "tank",
|
|
},
|
|
{
|
|
label: "nvme partition leaf matches devpath with partition stripping",
|
|
disk: models.PhysicalDisk{DevPath: "/dev/nvme1n1"},
|
|
want: "tank",
|
|
},
|
|
{
|
|
label: "wwn leaf matches via disk WWN",
|
|
disk: models.PhysicalDisk{DevPath: "/dev/sdd", WWN: "0x50014ee2123456ab"},
|
|
want: "scratch",
|
|
},
|
|
{
|
|
label: "disk with no pool membership returns empty",
|
|
disk: models.PhysicalDisk{DevPath: "/dev/sde", Serial: "UNUSED1"},
|
|
want: "",
|
|
},
|
|
{
|
|
label: "blank devpath returns empty",
|
|
disk: models.PhysicalDisk{DevPath: ""},
|
|
want: "",
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.label, func(t *testing.T) {
|
|
got := assignment.lookup(tc.disk)
|
|
if got != tc.want {
|
|
t.Fatalf("lookup = %q, want %q", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNilAssignmentLookupReturnsEmpty(t *testing.T) {
|
|
var a *diskPoolAssignment
|
|
got := a.lookup(models.PhysicalDisk{DevPath: "/dev/sda"})
|
|
if got != "" {
|
|
t.Fatalf("nil lookup = %q, want empty", got)
|
|
}
|
|
}
|
|
|
|
func TestStripPartitionSuffix(t *testing.T) {
|
|
cases := map[string]string{
|
|
"sda": "sda",
|
|
"sda3": "sda",
|
|
"nvme0n1": "nvme0n1",
|
|
"nvme0n1p1": "nvme0n1",
|
|
"nvme10n1p3": "nvme10n1",
|
|
"": "",
|
|
}
|
|
for in, want := range cases {
|
|
if got := stripPartitionSuffix(in); got != want {
|
|
t.Fatalf("stripPartitionSuffix(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSerialFromByID(t *testing.T) {
|
|
cases := map[string]string{
|
|
"/dev/disk/by-id/ata-Samsung_SSD_870_EVO_1TB_S5Y2NX0R500001Z": "s5y2nx0r500001z",
|
|
"/dev/disk/by-id/ata-Samsung_SSD_870_EVO_1TB_S5Y2NX0R500001Z-part3": "s5y2nx0r500001z",
|
|
"nvme-INTEL_SSDPEKNW512G8_BTNH123456789": "btnh123456789",
|
|
"wwn-0x50014ee2123456ab": "50014ee2123456ab",
|
|
"/dev/sda": "",
|
|
}
|
|
for in, want := range cases {
|
|
if got := serialFromByID(in); got != want {
|
|
t.Fatalf("serialFromByID(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|