Files
felhom-agent/internal/localapi/disks_smart_test.go
T
admin 21fee69154 v0.94.0: serialize per-disk SMART into the /disks payload
Additive, backward-compatible (MinAgent floor unchanged). The SMART is already
computed on the request path (storage.Observe -> enrich); this copies the target's
Smart into localapi.DiskInfo (pointer, omitempty) only when Health != "", so an
unread/absent summary stays omitted and the controller renders "Nincs adat".
No new smartctl load, endpoint, or sudoers change.

Test TestDisks_SmartSerialized + red-proof (drop the copy -> fails).
2026-07-24 21:11:13 +02:00

65 lines
2.3 KiB
Go

package localapi
import (
"net/http"
"testing"
"gitea.dooplex.hu/admin/felhom-agent/internal/hub"
"gitea.dooplex.hu/admin/felhom-agent/internal/storage"
)
func smartIP(v int) *int { return &v }
// TestDisks_SmartSerialized (v0.94.0) — the already-computed SMART summary is copied into the /disks
// payload for a target that has it (Health set), including the SATA counters + temperature; a target
// whose SMART was never read (zero-value summary, Health "") omits the field entirely.
//
// Red-proof: drop the `di.Smart = &sm` copy in handleDisks → the "data" disk's Smart is nil → this fails.
func TestDisks_SmartSerialized(t *testing.T) {
d := &fakeDiskOps{probe: storage.DeviceProbe{Probed: true, HasFilesystem: true, FSType: "ext4"}}
sv := fakeStorage{targets: []hub.StorageTarget{
{
Name: "data", Type: hub.StorageTypeUSB, BackingDevice: "/dev/sdb1", MountPath: "/mnt/data",
Smart: hub.SmartSummary{
Health: hub.SmartPassed,
TemperatureC: smartIP(34),
ReallocatedSectors: smartIP(3),
PendingSectors: smartIP(0),
},
},
// Zero-value SMART (never read) — Health "" → must be omitted from the payload.
{Name: "nosmart", Type: hub.StorageTypeUSB, BackingDevice: "/dev/sdc1", MountPath: "/mnt/nosmart"},
}}
h := newDiskServer(t, d, &fakeGate{}, sv, nil)
w := do(t, h, "GET", "/disks", "A", "")
if w.Code != http.StatusOK {
t.Fatalf("GET /disks: %d (%s)", w.Code, w.Body.String())
}
byName := map[string]DiskInfo{}
for _, di := range decodeDisks(t, w.Body.Bytes()) {
byName[di.Name] = di
}
ds := byName["data"].Smart
if ds == nil {
t.Fatal("data disk: smart summary was not serialized")
}
if ds.Health != hub.SmartPassed {
t.Errorf("data smart Health = %q, want PASSED", ds.Health)
}
if ds.ReallocatedSectors == nil || *ds.ReallocatedSectors != 3 {
t.Errorf("data ReallocatedSectors = %v, want 3", ds.ReallocatedSectors)
}
if ds.PendingSectors == nil || *ds.PendingSectors != 0 {
t.Errorf("data PendingSectors = %v, want 0 (a real zero, not null)", ds.PendingSectors)
}
if ds.TemperatureC == nil || *ds.TemperatureC != 34 {
t.Errorf("data TemperatureC = %v, want 34", ds.TemperatureC)
}
if byName["nosmart"].Smart != nil {
t.Errorf("nosmart disk: smart must be omitted when Health is empty, got %+v", byName["nosmart"].Smart)
}
}