mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-09-10 10:35:51 +00:00
Hermeticize Linux SMART discovery tests
This commit is contained in:
@@ -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"},
|
||||
|
||||
@@ -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"},
|
||||
|
||||
Reference in New Issue
Block a user