From 7028c95ed0cb2ffeb08e14f18e6674fab19d034c Mon Sep 17 00:00:00 2001 From: rcourtman Date: Thu, 9 Apr 2026 21:28:37 +0100 Subject: [PATCH] Hermeticize Linux SMART discovery tests --- .../hostagent/collector_observability_test.go | 7 +++++++ internal/hostagent/smartctl_coverage_test.go | 17 +++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/internal/hostagent/collector_observability_test.go b/internal/hostagent/collector_observability_test.go index ed072c938..c0058d734 100644 --- a/internal/hostagent/collector_observability_test.go +++ b/internal/hostagent/collector_observability_test.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" "errors" + "os" "os/exec" "strings" "testing" @@ -17,13 +18,16 @@ func TestCollectLocal_LogsStructuredContextOnDeviceCollectionFailure(t *testing. origRun := smartRunCommandOutput origLook := execLookPath origGOOS := runtimeGOOS + origReadDir := readDir t.Cleanup(func() { smartRunCommandOutput = origRun execLookPath = origLook runtimeGOOS = origGOOS + readDir = origReadDir }) runtimeGOOS = "linux" + readDir = func(string) ([]os.DirEntry, error) { return nil, errors.New("sysfs unavailable") } execLookPath = func(string) (string, error) { return "smartctl", nil } smartRunCommandOutput = func(ctx context.Context, name string, args ...string) ([]byte, error) { if name == "lsblk" { @@ -62,10 +66,13 @@ func TestCollectLocal_LogsStructuredContextOnDeviceCollectionFailure(t *testing. func TestListBlockDevicesLinux_LogsStructuredContextForExcludedDevice(t *testing.T) { origRun := smartRunCommandOutput + origReadDir := readDir t.Cleanup(func() { smartRunCommandOutput = origRun + readDir = origReadDir }) + readDir = func(string) ([]os.DirEntry, error) { return nil, errors.New("sysfs unavailable") } smartRunCommandOutput = func(ctx context.Context, name string, args ...string) ([]byte, error) { data := lsblkJSON{Blockdevices: []lsblkDevice{ {Name: "sda", Type: "disk", Tran: "sata", Subsystems: "block:scsi:pci"}, diff --git a/internal/hostagent/smartctl_coverage_test.go b/internal/hostagent/smartctl_coverage_test.go index 8acd76c7d..62e2ea058 100644 --- a/internal/hostagent/smartctl_coverage_test.go +++ b/internal/hostagent/smartctl_coverage_test.go @@ -466,14 +466,17 @@ func TestCollectSMARTLocalSkipsErrors(t *testing.T) { origRun := smartRunCommandOutput origLook := execLookPath origNow := timeNow + origReadDir := readDir t.Cleanup(func() { smartRunCommandOutput = origRun execLookPath = origLook timeNow = origNow + readDir = origReadDir }) fixed := time.Date(2024, 1, 2, 3, 4, 5, 0, time.UTC) timeNow = func() time.Time { return fixed } + readDir = func(string) ([]os.DirEntry, error) { return nil, errors.New("sysfs unavailable") } execLookPath = func(string) (string, error) { return "smartctl", nil } smartRunCommandOutput = func(ctx context.Context, name string, args ...string) ([]byte, error) { @@ -926,8 +929,13 @@ func TestCollectDeviceSMARTWWN(t *testing.T) { func TestListBlockDevicesLinuxWithExcludes(t *testing.T) { origRun := smartRunCommandOutput - t.Cleanup(func() { smartRunCommandOutput = origRun }) + origReadDir := readDir + t.Cleanup(func() { + smartRunCommandOutput = origRun + readDir = origReadDir + }) + readDir = func(string) ([]os.DirEntry, error) { return nil, errors.New("sysfs unavailable") } smartRunCommandOutput = func(ctx context.Context, name string, args ...string) ([]byte, error) { if name != "lsblk" { return nil, errors.New("unexpected command") @@ -1045,8 +1053,13 @@ func TestLinuxSMARTSkipReason(t *testing.T) { func TestListBlockDevicesLinuxFiltersVirtualDevices(t *testing.T) { origRun := smartRunCommandOutput - t.Cleanup(func() { smartRunCommandOutput = origRun }) + origReadDir := readDir + t.Cleanup(func() { + smartRunCommandOutput = origRun + readDir = origReadDir + }) + readDir = func(string) ([]os.DirEntry, error) { return nil, errors.New("sysfs unavailable") } smartRunCommandOutput = func(ctx context.Context, name string, args ...string) ([]byte, error) { return mockLsblkJSON( lsblkDevice{Name: "sda", Type: "disk", Tran: "sata", Subsystems: "block:scsi:pci"},