fix: tolerate unavailable CPU sensor telemetry

This commit is contained in:
2026-09-08 09:49:40 +03:00
parent b8c45d54c1
commit 5190d50358
5 changed files with 78 additions and 4 deletions
+37
View File
@@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
@@ -719,6 +720,42 @@ func TestCPUSATJobsIncludeThrottleCheck(t *testing.T) {
if jobs[5].name != "05-thermal-throttle-check.log" || jobs[5].informational {
t.Fatalf("throttle-check job=%+v want non-informational so it can fail overall_status", jobs[5])
}
for _, index := range []int{1, 4} {
if jobs[index].cmd[0] != "sh" || jobs[index].cmd[1] != "-c" || !strings.Contains(jobs[index].cmd[2], "No sensors found!") {
t.Fatalf("sensors job=%+v want probe that tolerates unavailable local hwmon", jobs[index])
}
}
}
func TestCPUSensorsProbeScript(t *testing.T) {
run := func(t *testing.T, output string, rc int) ([]byte, error) {
t.Helper()
binDir := t.TempDir()
mockSensors := filepath.Join(binDir, "sensors")
script := "#!/bin/sh\nprintf '%s\\n' '" + output + "'\nexit " + strconv.Itoa(rc) + "\n"
if err := os.WriteFile(mockSensors, []byte(script), 0755); err != nil {
t.Fatalf("write mock sensors: %v", err)
}
t.Setenv("PATH", binDir+":"+os.Getenv("PATH"))
return exec.Command("sh", "-c", cpuSensorsProbeScript()).CombinedOutput()
}
t.Run("no hardware-monitoring device is non-fatal", func(t *testing.T) {
out, err := run(t, "No sensors found!", 1)
if err != nil {
t.Fatalf("probe failed: %v; output=%s", err, out)
}
if !strings.Contains(string(out), "telemetry is unavailable") {
t.Fatalf("output=%s want unavailable telemetry annotation", out)
}
})
t.Run("unexpected sensors failure remains fatal", func(t *testing.T) {
out, err := run(t, "i2c read failed", 1)
if err == nil {
t.Fatalf("probe unexpectedly succeeded; output=%s", out)
}
})
}
// TestCPUThrottleCheckDetectsIncreaseDuringRun runs the real before/check