72 lines
2.4 KiB
Go
72 lines
2.4 KiB
Go
package collector
|
|
|
|
import (
|
|
"bee/audit/internal/schema"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestBuildHealthSummary(t *testing.T) {
|
|
warning := statusWarning
|
|
critical := statusCritical
|
|
empty := statusEmpty
|
|
present := true
|
|
absent := false
|
|
diskModel := "NVMe A"
|
|
pcieSerial := "GPU-1"
|
|
psuSlot := "PSU1"
|
|
dimmSlot := "DIMM_A1"
|
|
|
|
summary := BuildHealthSummary(schema.HardwareSnapshot{
|
|
Memory: []schema.HardwareMemory{
|
|
{HardwareComponentStatus: schema.HardwareComponentStatus{Status: &warning}, Slot: &dimmSlot},
|
|
{HardwareComponentStatus: schema.HardwareComponentStatus{Status: &empty}},
|
|
},
|
|
Storage: []schema.HardwareStorage{{HardwareComponentStatus: schema.HardwareComponentStatus{Status: &critical}, Model: &diskModel}},
|
|
PCIeDevices: []schema.HardwarePCIeDevice{{HardwareComponentStatus: schema.HardwareComponentStatus{Status: &warning}, SerialNumber: &pcieSerial}},
|
|
PowerSupplies: []schema.HardwarePowerSupply{
|
|
{HardwareComponentStatus: schema.HardwareComponentStatus{Status: &critical}, Slot: &psuSlot, Present: &present},
|
|
{Present: &absent},
|
|
},
|
|
})
|
|
|
|
if summary.Status != statusCritical || summary.MemoryWarn != 1 || summary.EmptyDIMMs != 1 ||
|
|
summary.StorageFail != 1 || summary.PCIeWarn != 1 || summary.PSUFail != 1 || summary.MissingPSUs != 1 {
|
|
t.Fatalf("summary = %#v", summary)
|
|
}
|
|
if len(summary.Warnings) != 2 || len(summary.Failures) != 2 {
|
|
t.Fatalf("warnings/failures = %#v", summary)
|
|
}
|
|
if _, err := time.Parse(time.RFC3339, summary.CollectedAt); err != nil {
|
|
t.Fatalf("collected_at = %q: %v", summary.CollectedAt, err)
|
|
}
|
|
}
|
|
|
|
func TestBuildHealthSummaryEmptySnapshot(t *testing.T) {
|
|
summary := BuildHealthSummary(schema.HardwareSnapshot{})
|
|
if summary.Status != statusOK || summary.Warnings != nil || summary.Failures != nil {
|
|
t.Fatalf("summary = %#v", summary)
|
|
}
|
|
}
|
|
|
|
func TestPreferredName(t *testing.T) {
|
|
model, serial, slot := "model", "serial", "slot"
|
|
tests := []struct {
|
|
name string
|
|
model *string
|
|
serial *string
|
|
slot *string
|
|
want string
|
|
}{
|
|
{name: "model", model: &model, serial: &serial, slot: &slot, want: "model"},
|
|
{name: "serial", serial: &serial, slot: &slot, want: "serial"},
|
|
{name: "slot", slot: &slot, want: "slot"},
|
|
{name: "fallback", want: "unknown"},
|
|
}
|
|
for _, tt := range tests {
|
|
if got := preferredName(tt.model, tt.serial, tt.slot); got != tt.want {
|
|
t.Errorf("%s: preferredName() = %q, want %q", tt.name, got, tt.want)
|
|
}
|
|
}
|
|
}
|