Files
ITWorx-Pulse-Public/internal/host/hardware_test.go
T
ITWorx Pulse release export bd774932d5
Public source validation / validate (push) Failing after 3m8s
Publish ITWorx Pulse source
2026-09-03 02:09:19 +02:00

71 lines
2.6 KiB
Go

package host
import (
"context"
"errors"
"testing"
)
func TestNormalizeHardwareAbsentCapabilitiesAreDisabled(t *testing.T) {
snapshot, err := NormalizeHardware(RawHardware{}, "agent-1", HardwareLimits{}, ThermalPolicy{})
if err != nil {
t.Fatal(err)
}
if len(snapshot.Capabilities) != 3 || snapshot.Status.State != StatusUnknown || snapshot.Capabilities[0].State != "disabled" {
t.Fatalf("absent hardware was treated as failure: %+v", snapshot)
}
if snapshot.Capabilities[0].Reason != "capability_absent" {
t.Fatalf("capabilities=%+v", snapshot.Capabilities)
}
}
func TestNormalizeHardwareStableIDsAndThermalReason(t *testing.T) {
hot := 91.5
snapshot, err := NormalizeHardware(RawHardware{
Capabilities: []Capability{{ID: CapabilityThermal, Version: ContractVersion, State: "enabled"}, {ID: CapabilityGPU, Version: ContractVersion, State: "enabled"}},
Temperatures: []RawTemperature{{ID: "core:0", Name: "CPU package", Celsius: 91.5}},
GPUs: []RawGPU{{ID: "pci/0000:01:00.0", Name: "RTX", Vendor: "nvidia", TemperatureCelsius: &hot, Utilization: floatPtr(55)}},
}, "agent-1", HardwareLimits{}, ThermalPolicy{})
if err != nil {
t.Fatal(err)
}
if snapshot.Status.State != "critical" || snapshot.Status.Reasons[0].Code != "thermal_critical" {
t.Fatalf("status=%+v", snapshot.Status)
}
if snapshot.Temperatures[0].ID != "agent-1/temperature/core-0" || snapshot.GPUs[0].ID != "agent-1/gpu/pci-0000-01-00.0" {
t.Fatalf("unstable IDs: temperatures=%+v gpus=%+v", snapshot.Temperatures, snapshot.GPUs)
}
if snapshot.GPUs[0].MemoryUtilizationPercent != 0 {
t.Fatalf("unexpected GPU memory ratio: %+v", snapshot.GPUs[0])
}
}
func TestNormalizeHardwarePreservesUnsupportedCapability(t *testing.T) {
snapshot, err := NormalizeHardware(RawHardware{Capabilities: []Capability{{ID: CapabilityGPU, Version: ContractVersion, State: "unsupported", Reason: "no_driver"}}}, "agent-1", HardwareLimits{}, ThermalPolicy{})
if err != nil {
t.Fatal(err)
}
capability, ok := capabilityByID(snapshot.Capabilities, CapabilityGPU)
if !ok || capability.State != "unsupported" || capability.Reason != "no_driver" {
t.Fatalf("capability=%+v", capability)
}
}
type hardwareSource struct{ value RawHardware }
func (s hardwareSource) Hardware(ctx context.Context) (RawHardware, error) {
if err := ctx.Err(); err != nil {
return RawHardware{}, err
}
return s.value, nil
}
func TestHardwareAdapterHonorsCancellation(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := (HardwareAdapter{Source: hardwareSource{}}).Snapshot(ctx, "agent-1")
if !errors.Is(err, context.Canceled) {
t.Fatalf("err=%v", err)
}
}