test: lmsensors extend unit tests, vendored framework [#769]
Moved from KNEL/PFVCluster tests/unit with repo-relative paths. https://projects.knownelement.com/issues/769#note-4152
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
#!/bin/bash
|
||||
# Unit tests for lmsensors-extend.sh -n numeric mode [#618]
|
||||
set -euo pipefail
|
||||
|
||||
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
|
||||
SCRIPT="$PROJECT_ROOT/sensors/temper/lmsensors-extend.sh"
|
||||
|
||||
failed=0
|
||||
if [[ -f "$SCRIPT" ]] && (source "$SCRIPT"); then
|
||||
echo "✅ script exists + sources cleanly"
|
||||
else
|
||||
echo "❌ script missing or fails to source: $SCRIPT"
|
||||
exit 1
|
||||
fi
|
||||
# shellcheck disable=SC1091
|
||||
source "$SCRIPT"
|
||||
|
||||
# numeric_pick: TEMPer external > TEMPer internal > first native/sysfs value
|
||||
test_numeric_prefers_external() {
|
||||
if [[ "$(numeric_pick "22.5" "18.2" "" )" == "18.2" ]]; then
|
||||
echo "✅ numeric_pick prefers external probe"
|
||||
else
|
||||
echo "❌ numeric_pick external preference broken"
|
||||
((++failed))
|
||||
fi
|
||||
}
|
||||
|
||||
test_numeric_falls_back_internal() {
|
||||
if [[ "$(numeric_pick "22.5" "" "")" == "22.5" ]]; then
|
||||
echo "✅ numeric_pick falls back to internal"
|
||||
else
|
||||
echo "❌ numeric_pick internal fallback broken"
|
||||
((++failed))
|
||||
fi
|
||||
}
|
||||
|
||||
test_numeric_uses_first_line_value() {
|
||||
if [[ "$(numeric_pick "" "" "temp1: +27.5°C (high = +84.0°C)")" == "27.5" ]]; then
|
||||
echo "✅ numeric_pick parses first native temp"
|
||||
else
|
||||
echo "❌ numeric_pick native parse broken: got '$(numeric_pick "" "" "+27.5°C")'"
|
||||
((++failed))
|
||||
fi
|
||||
}
|
||||
|
||||
test_numeric_empty_is_error() {
|
||||
if numeric_pick "" "" "" >/dev/null 2>&1; then
|
||||
echo "❌ numeric_pick should fail on no data"
|
||||
((++failed))
|
||||
else
|
||||
echo "✅ numeric_pick errors on no data"
|
||||
fi
|
||||
}
|
||||
|
||||
test_numeric_prefers_external
|
||||
test_numeric_falls_back_internal
|
||||
test_numeric_uses_first_line_value
|
||||
test_numeric_empty_is_error
|
||||
|
||||
if ((failed > 0)); then
|
||||
echo "❌ $failed numeric-mode test(s) failed"
|
||||
exit 1
|
||||
fi
|
||||
echo "✅ all numeric-mode tests passed"
|
||||
@@ -0,0 +1,212 @@
|
||||
#!/bin/bash
|
||||
# Unit tests for the lm-sensors extend wrapper (dcinfra/sensors/temper) [#341/#439]
|
||||
set -euo pipefail
|
||||
|
||||
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
|
||||
SCRIPT="$PROJECT_ROOT/sensors/temper/lmsensors-extend.sh"
|
||||
|
||||
failed=0
|
||||
|
||||
# Script must exist and source cleanly (main guarded for sourcing)
|
||||
if [[ -f "$SCRIPT" ]] && (source "$SCRIPT"); then
|
||||
echo "✅ script exists + sources cleanly"
|
||||
else
|
||||
echo "❌ script missing or fails to source: $SCRIPT"
|
||||
exit 1
|
||||
fi
|
||||
# shellcheck disable=SC1091
|
||||
source "$SCRIPT"
|
||||
|
||||
# lm-sensors format block: chip, adapter, one line per sensor, +X.X°C
|
||||
test_format_two_sensors() {
|
||||
local got expected
|
||||
expected=$(printf 'temper-usb-1\nAdapter: USB adapter\nInternal: +23.5°C\nExternal: +18.2°C')
|
||||
got="$(format_block "23.5" "18.2")"
|
||||
if [[ "$got" == "$expected" ]]; then
|
||||
echo "✅ format_block renders two-sensor lm-sensors output"
|
||||
else
|
||||
echo "❌ format_block two-sensor output mismatch"
|
||||
printf 'got:\n%s\nexpected:\n%s\n' "$got" "$expected"
|
||||
((++failed))
|
||||
fi
|
||||
}
|
||||
|
||||
test_format_single_sensor() {
|
||||
local got expected
|
||||
expected=$(printf 'temper-usb-1\nAdapter: USB adapter\nInternal: +23.5°C')
|
||||
got="$(format_block "23.5" "")"
|
||||
if [[ "$got" == "$expected" ]]; then
|
||||
echo "✅ format_block omits absent external sensor"
|
||||
else
|
||||
echo "❌ format_block single-sensor output mismatch"
|
||||
printf 'got:\n%s\nexpected:\n%s\n' "$got" "$expected"
|
||||
((++failed))
|
||||
fi
|
||||
}
|
||||
|
||||
test_format_negative() {
|
||||
if format_block "-5.0" | grep -q 'Internal: -5.0°C'; then
|
||||
echo "✅ format_block handles negative temps"
|
||||
else
|
||||
echo "❌ format_block mangles negative temps"
|
||||
((++failed))
|
||||
fi
|
||||
}
|
||||
|
||||
test_format_garbage_rejected() {
|
||||
if format_block "not-a-number" >/dev/null 2>&1; then
|
||||
echo "❌ format_block accepted non-numeric input"
|
||||
((++failed))
|
||||
else
|
||||
echo "✅ format_block rejects non-numeric input"
|
||||
fi
|
||||
}
|
||||
|
||||
# main() composition with stubbed binaries via PATH + env overrides
|
||||
STUB_DIR="$(mktemp -d "/tmp/lmsensors-test-$$-XXXXXX")"
|
||||
trap 'rm -rf "$STUB_DIR"' EXIT
|
||||
|
||||
make_stub_lsusb() {
|
||||
if [[ "${1:-}" == "probe" ]]; then
|
||||
printf '#!/bin/bash\necho "Bus 001 Device 007: ID 0c45:7401 Microdia TEMPer Temperature Sensor"\n' >"$STUB_DIR/lsusb"
|
||||
else
|
||||
printf '#!/bin/bash\nexit 0\n' >"$STUB_DIR/lsusb"
|
||||
fi
|
||||
chmod +x "$STUB_DIR/lsusb"
|
||||
}
|
||||
|
||||
make_stub_sensors() {
|
||||
printf '#!/bin/bash\nprintf "coretemp-isa-0000\\nAdapter: ISA adapter\\nPackage id 0: +45.0°C\\nCore 0: +41.0°C\\n"\n' >"$STUB_DIR/sensors"
|
||||
chmod +x "$STUB_DIR/sensors"
|
||||
}
|
||||
|
||||
make_stub_python() {
|
||||
printf '#!/bin/bash\necho "23.5 18.2"\n' >"$STUB_DIR/fakepython"
|
||||
chmod +x "$STUB_DIR/fakepython"
|
||||
}
|
||||
|
||||
test_main_native_only() {
|
||||
make_stub_lsusb noprobe
|
||||
make_stub_sensors
|
||||
local got
|
||||
got="$(PATH="$STUB_DIR:$PATH" NATIVE_SENSORS_BIN="$STUB_DIR/sensors" \
|
||||
VENV_PY=/nonexistent main)"
|
||||
if grep -q 'Package id 0' <<<"$got" && ! grep -q 'temper-usb-1' <<<"$got"; then
|
||||
echo "✅ main: native sensors only (no probe)"
|
||||
else
|
||||
echo "❌ main: native-only composition wrong"
|
||||
((++failed))
|
||||
fi
|
||||
}
|
||||
|
||||
test_main_temper_only() {
|
||||
make_stub_lsusb probe
|
||||
make_stub_python
|
||||
local got
|
||||
got="$(PATH="$STUB_DIR:$PATH" NATIVE_SENSORS_BIN=/nonexistent \
|
||||
VENV_PY="$STUB_DIR/fakepython" main)"
|
||||
if grep -q 'Internal: +23.5°C' <<<"$got" && ! grep -q 'Package id 0' <<<"$got"; then
|
||||
echo "✅ main: TEMPer only (no lm-sensors bin)"
|
||||
else
|
||||
echo "❌ main: temper-only composition wrong"
|
||||
((++failed))
|
||||
fi
|
||||
}
|
||||
|
||||
test_main_merge() {
|
||||
make_stub_lsusb probe
|
||||
make_stub_sensors
|
||||
make_stub_python
|
||||
local got
|
||||
got="$(PATH="$STUB_DIR:$PATH" NATIVE_SENSORS_BIN="$STUB_DIR/sensors" \
|
||||
VENV_PY="$STUB_DIR/fakepython" main)"
|
||||
if grep -q 'Package id 0' <<<"$got" && grep -q 'Internal: +23.5°C' <<<"$got"; then
|
||||
echo "✅ main: native + TEMPer merged"
|
||||
else
|
||||
echo "❌ main: merge composition wrong"
|
||||
((++failed))
|
||||
fi
|
||||
}
|
||||
|
||||
test_main_neither_fails() {
|
||||
make_stub_lsusb noprobe
|
||||
if PATH="$STUB_DIR:$PATH" NATIVE_SENSORS_BIN=/nonexistent \
|
||||
VENV_PY=/nonexistent SYSFS_THERMAL_ROOT=/nonexistent main >/dev/null 2>&1; then
|
||||
echo "❌ main: succeeded with no sensor sources"
|
||||
((++failed))
|
||||
else
|
||||
echo "✅ main: fails cleanly with no sensor sources"
|
||||
fi
|
||||
}
|
||||
|
||||
# sysfs thermal-zone fallback (SBCs: Pis, Jetson-class, /sys/class/thermal)
|
||||
make_sysfs_fixture() {
|
||||
SYSFS_FIXTURE="$STUB_DIR/sysfs/class-thermal"
|
||||
mkdir -p "$SYSFS_FIXTURE/thermal_zone0" "$SYSFS_FIXTURE/thermal_zone1"
|
||||
printf 'cpu-thermal\n' >"$SYSFS_FIXTURE/thermal_zone0/type"
|
||||
printf '45200\n' >"$SYSFS_FIXTURE/thermal_zone0/temp"
|
||||
printf 'gpu-thermal\n' >"$SYSFS_FIXTURE/thermal_zone1/type"
|
||||
printf '52350\n' >"$SYSFS_FIXTURE/thermal_zone1/temp"
|
||||
}
|
||||
|
||||
test_format_sysfs_block() {
|
||||
local got expected
|
||||
expected=$(printf 'soc-thermal-virtual-0\nAdapter: Virtual device\ntemp1: +45.2°C\ntemp2: +52.4°C')
|
||||
got="$(printf 'cpu-thermal 45200\ngpu-thermal 52350\n' | format_sysfs_block "$(printf 'cpu-thermal 45200\ngpu-thermal 52350\n')")"
|
||||
if [[ "$got" == "$expected" ]]; then
|
||||
echo "✅ format_sysfs_block renders lm-sensors format from millicelsius"
|
||||
else
|
||||
echo "❌ format_sysfs_block output mismatch"
|
||||
printf 'got:\n%s\nexpected:\n%s\n' "$got" "$expected"
|
||||
((++failed))
|
||||
fi
|
||||
}
|
||||
|
||||
test_main_sysfs_fallback() {
|
||||
make_stub_lsusb noprobe
|
||||
make_sysfs_fixture
|
||||
local got
|
||||
got="$(PATH="$STUB_DIR:$PATH" NATIVE_SENSORS_BIN=/nonexistent VENV_PY=/nonexistent \
|
||||
SYSFS_THERMAL_ROOT="$SYSFS_FIXTURE" main)"
|
||||
if grep -q 'temp1: *+45.2°C' <<<"$got" && grep -q 'soc-thermal-virtual-0' <<<"$got"; then
|
||||
echo "✅ main: sysfs fallback when lm-sensors silent (SBC)"
|
||||
else
|
||||
echo "❌ main: sysfs fallback composition wrong"
|
||||
printf 'got:\n%s\n' "$got"
|
||||
((++failed))
|
||||
fi
|
||||
}
|
||||
|
||||
test_main_native_suppresses_sysfs() {
|
||||
make_stub_lsusb noprobe
|
||||
make_stub_sensors
|
||||
make_sysfs_fixture
|
||||
local got
|
||||
got="$(PATH="$STUB_DIR:$PATH" NATIVE_SENSORS_BIN="$STUB_DIR/sensors" VENV_PY=/nonexistent \
|
||||
SYSFS_THERMAL_ROOT="$SYSFS_FIXTURE" main)"
|
||||
if grep -q 'Package id 0' <<<"$got" && ! grep -q 'soc-thermal-virtual-0' <<<"$got"; then
|
||||
echo "✅ main: native output present, sysfs fallback suppressed (no duplicates)"
|
||||
else
|
||||
echo "❌ main: sysfs fallback leaked alongside native output"
|
||||
printf 'got:\n%s\n' "$got"
|
||||
((++failed))
|
||||
fi
|
||||
}
|
||||
|
||||
test_format_two_sensors
|
||||
test_format_single_sensor
|
||||
test_format_negative
|
||||
test_format_garbage_rejected
|
||||
test_main_native_only
|
||||
test_main_temper_only
|
||||
test_main_merge
|
||||
test_main_neither_fails
|
||||
test_format_sysfs_block
|
||||
test_main_sysfs_fallback
|
||||
test_main_native_suppresses_sysfs
|
||||
|
||||
if ((failed > 0)); then
|
||||
echo "❌ lm-sensors extend: $failed test(s) failed"
|
||||
exit 1
|
||||
fi
|
||||
echo "PASS: lm-sensors extend unit tests"
|
||||
Reference in New Issue
Block a user