209 lines
7.8 KiB
Go
209 lines
7.8 KiB
Go
package ingest
|
|
|
|
import "testing"
|
|
|
|
func TestFlattenHardwareComponentsUsesCanonicalTypeKeys(t *testing.T) {
|
|
boardSerial := "BOARD-001"
|
|
cpuTemperature := 63.5
|
|
cpuPower := 185.0
|
|
cpuThrottled := true
|
|
cpuCorrectableErrors := 2
|
|
cpuLifeRemaining := 99.0
|
|
memoryTemperature := 44.25
|
|
memoryCorrectableECC := 7
|
|
memoryLifeRemaining := 98.5
|
|
memoryPerformanceDegraded := true
|
|
storageTemperature := 39.0
|
|
storagePowerOnHours := int64(12450)
|
|
storageWrittenBytes := int64(9876543210)
|
|
storageLifeRemaining := 91.0
|
|
numaNode := 1
|
|
pcieTemperature := 71.5
|
|
pciePower := 248.0
|
|
pcieLifeRemaining := 88.0
|
|
pcieECCCorrected := int64(14)
|
|
pcieHWSlowdown := true
|
|
psuTemperature := 42.25
|
|
psuLifeRemaining := 97.0
|
|
manufacturedYearWeek := "2024-W07"
|
|
input := HardwareSnapshot{
|
|
Board: HardwareBoard{SerialNumber: boardSerial},
|
|
CPUs: []HardwareCPU{
|
|
{
|
|
Socket: intPtr(0),
|
|
TemperatureC: &cpuTemperature,
|
|
PowerW: &cpuPower,
|
|
Throttled: &cpuThrottled,
|
|
CorrectableErrorCount: &cpuCorrectableErrors,
|
|
LifeRemainingPct: &cpuLifeRemaining,
|
|
ManufacturedYearWeek: &manufacturedYearWeek,
|
|
Status: strPtr("OK"),
|
|
},
|
|
},
|
|
Memory: []HardwareMemory{
|
|
{
|
|
Slot: strPtr("DIMM_A1"),
|
|
SerialNumber: strPtr("DIMM-SN-001"),
|
|
TemperatureC: &memoryTemperature,
|
|
CorrectableECCErrorCount: &memoryCorrectableECC,
|
|
LifeRemainingPct: &memoryLifeRemaining,
|
|
PerformanceDegraded: &memoryPerformanceDegraded,
|
|
Status: strPtr("OK"),
|
|
},
|
|
},
|
|
Storage: []HardwareStorage{
|
|
{
|
|
Slot: strPtr("NVME0"),
|
|
SerialNumber: strPtr("SSD-SN-001"),
|
|
TemperatureC: &storageTemperature,
|
|
PowerOnHours: &storagePowerOnHours,
|
|
WrittenBytes: &storageWrittenBytes,
|
|
LifeRemainingPct: &storageLifeRemaining,
|
|
Status: strPtr("OK"),
|
|
},
|
|
},
|
|
PCIeDevices: []HardwarePCIeDevice{
|
|
{
|
|
Slot: strPtr("PCIe.Slot.1"),
|
|
BDF: strPtr("0000:3b:00.0"),
|
|
NUMANode: &numaNode,
|
|
TemperatureC: &pcieTemperature,
|
|
PowerW: &pciePower,
|
|
LifeRemainingPct: &pcieLifeRemaining,
|
|
ECCCorrectedTotal: &pcieECCCorrected,
|
|
HWSlowdown: &pcieHWSlowdown,
|
|
Status: strPtr("OK"),
|
|
},
|
|
},
|
|
PowerSupplies: []HardwarePowerSupply{
|
|
{
|
|
Slot: strPtr("PSU1"),
|
|
SerialNumber: strPtr("PSU-SN-001"),
|
|
TemperatureC: &psuTemperature,
|
|
LifeRemainingPct: &psuLifeRemaining,
|
|
Status: strPtr("OK"),
|
|
},
|
|
},
|
|
}
|
|
|
|
components, _ := FlattenHardwareComponents(input)
|
|
if len(components) != 5 {
|
|
t.Fatalf("expected 5 components, got %d", len(components))
|
|
}
|
|
|
|
foundCPU := false
|
|
foundMemory := false
|
|
foundStorage := false
|
|
foundPCIe := false
|
|
foundPSU := false
|
|
for _, component := range components {
|
|
switch component.ComponentType {
|
|
case "cpu":
|
|
foundCPU = true
|
|
if got := component.Attributes["manufactured_year_week"]; got != manufacturedYearWeek {
|
|
t.Fatalf("expected cpu attributes.manufactured_year_week=%q, got %#v", manufacturedYearWeek, got)
|
|
}
|
|
if got := component.Telemetry["temperature_c"]; got != cpuTemperature {
|
|
t.Fatalf("expected cpu telemetry.temperature_c=%v, got %#v", cpuTemperature, got)
|
|
}
|
|
if got := component.Telemetry["power_w"]; got != cpuPower {
|
|
t.Fatalf("expected cpu telemetry.power_w=%v, got %#v", cpuPower, got)
|
|
}
|
|
if got := component.Telemetry["throttled"]; got != cpuThrottled {
|
|
t.Fatalf("expected cpu telemetry.throttled=%v, got %#v", cpuThrottled, got)
|
|
}
|
|
if got := component.Telemetry["correctable_error_count"]; got != float64(cpuCorrectableErrors) {
|
|
t.Fatalf("expected cpu telemetry.correctable_error_count=%d, got %#v", cpuCorrectableErrors, got)
|
|
}
|
|
if got := component.Telemetry["life_remaining_pct"]; got != cpuLifeRemaining {
|
|
t.Fatalf("expected cpu telemetry.life_remaining_pct=%v, got %#v", cpuLifeRemaining, got)
|
|
}
|
|
case "memory":
|
|
foundMemory = true
|
|
if got := component.Telemetry["temperature_c"]; got != memoryTemperature {
|
|
t.Fatalf("expected memory telemetry.temperature_c=%v, got %#v", memoryTemperature, got)
|
|
}
|
|
if got := component.Telemetry["correctable_ecc_error_count"]; got != float64(memoryCorrectableECC) {
|
|
t.Fatalf("expected memory telemetry.correctable_ecc_error_count=%d, got %#v", memoryCorrectableECC, got)
|
|
}
|
|
if got := component.Telemetry["life_remaining_pct"]; got != memoryLifeRemaining {
|
|
t.Fatalf("expected memory telemetry.life_remaining_pct=%v, got %#v", memoryLifeRemaining, got)
|
|
}
|
|
if got := component.Telemetry["performance_degraded"]; got != memoryPerformanceDegraded {
|
|
t.Fatalf("expected memory telemetry.performance_degraded=%v, got %#v", memoryPerformanceDegraded, got)
|
|
}
|
|
case "storage":
|
|
foundStorage = true
|
|
if got := component.Telemetry["temperature_c"]; got != storageTemperature {
|
|
t.Fatalf("expected storage telemetry.temperature_c=%v, got %#v", storageTemperature, got)
|
|
}
|
|
if got := component.Telemetry["power_on_hours"]; got != float64(storagePowerOnHours) {
|
|
t.Fatalf("expected storage telemetry.power_on_hours=%d, got %#v", storagePowerOnHours, got)
|
|
}
|
|
if got := component.Telemetry["written_bytes"]; got != float64(storageWrittenBytes) {
|
|
t.Fatalf("expected storage telemetry.written_bytes=%d, got %#v", storageWrittenBytes, got)
|
|
}
|
|
if got := component.Telemetry["life_remaining_pct"]; got != storageLifeRemaining {
|
|
t.Fatalf("expected storage telemetry.life_remaining_pct=%v, got %#v", storageLifeRemaining, got)
|
|
}
|
|
case "pcie_device":
|
|
foundPCIe = true
|
|
if component.Slot == nil || *component.Slot != "0000:3b:00.0" {
|
|
t.Fatalf("expected pcie_device slot to normalize from bdf, got %#v", component.Slot)
|
|
}
|
|
if component.VendorSerial != "BOARD-001-PCIE-0000:3b:00.0" {
|
|
t.Fatalf("expected pcie_device fallback serial to use canonical bdf slot, got %q", component.VendorSerial)
|
|
}
|
|
if got := component.Telemetry["numa_node"]; got != float64(numaNode) {
|
|
t.Fatalf("expected pcie_device telemetry.numa_node=%d, got %#v", numaNode, got)
|
|
}
|
|
if got := component.Telemetry["temperature_c"]; got != pcieTemperature {
|
|
t.Fatalf("expected pcie_device telemetry.temperature_c=%v, got %#v", pcieTemperature, got)
|
|
}
|
|
if got := component.Telemetry["power_w"]; got != pciePower {
|
|
t.Fatalf("expected pcie_device telemetry.power_w=%v, got %#v", pciePower, got)
|
|
}
|
|
if got := component.Telemetry["life_remaining_pct"]; got != pcieLifeRemaining {
|
|
t.Fatalf("expected pcie_device telemetry.life_remaining_pct=%v, got %#v", pcieLifeRemaining, got)
|
|
}
|
|
if got := component.Telemetry["ecc_corrected_total"]; got != float64(pcieECCCorrected) {
|
|
t.Fatalf("expected pcie_device telemetry.ecc_corrected_total=%d, got %#v", pcieECCCorrected, got)
|
|
}
|
|
if got := component.Telemetry["hw_slowdown"]; got != pcieHWSlowdown {
|
|
t.Fatalf("expected pcie_device telemetry.hw_slowdown=%v, got %#v", pcieHWSlowdown, got)
|
|
}
|
|
case "power_supply":
|
|
foundPSU = true
|
|
if got := component.Telemetry["temperature_c"]; got != psuTemperature {
|
|
t.Fatalf("expected power_supply telemetry.temperature_c=%v, got %#v", psuTemperature, got)
|
|
}
|
|
if got := component.Telemetry["life_remaining_pct"]; got != psuLifeRemaining {
|
|
t.Fatalf("expected power_supply telemetry.life_remaining_pct=%v, got %#v", psuLifeRemaining, got)
|
|
}
|
|
}
|
|
}
|
|
if !foundCPU {
|
|
t.Fatalf("expected canonical cpu component type")
|
|
}
|
|
if !foundMemory {
|
|
t.Fatalf("expected canonical memory component type")
|
|
}
|
|
if !foundStorage {
|
|
t.Fatalf("expected canonical storage component type")
|
|
}
|
|
if !foundPCIe {
|
|
t.Fatalf("expected canonical pcie_device component type")
|
|
}
|
|
if !foundPSU {
|
|
t.Fatalf("expected canonical power_supply component type")
|
|
}
|
|
}
|
|
|
|
func strPtr(value string) *string {
|
|
return &value
|
|
}
|
|
|
|
func intPtr(value int) *int {
|
|
return &value
|
|
}
|