Unverified Commit 4c81ef40 authored by Mark Vander Stel's avatar Mark Vander Stel
Browse files

Fix temperature erroring if tool returns no data

Rarely, sensors or acpi can return no temperature data. In this case,
the data function would return false and 0, but the detect function
would error if set -u was enabled.

Harden all of the four temperature functions against this, and greatly
increase the test coverage to cover these four functions, as well as
adding a specific case for the tools returning no data.

This also allows for the temperature functions to return values less
than 0, if somehow a device has all temp sensors returning less than
that.

Mention in the function documentation that it is possible for the
function to return a not set string.

Fixes 670
parent 964a4600
......@@ -398,6 +398,9 @@ Temperature
If the threshold is not surpassed, the highest temperature is still returned.
If no temperature data is found, returns ``false`` and *lp_temperature* will
not be set.
The threshold is configured with :attr:`LP_TEMP_THRESHOLD`.
Can be disabled by :attr:`LP_ENABLE_TEMP`.
......
......@@ -2545,7 +2545,9 @@ __lp_temp_sensors() {
local IFS=$' \t\n'
for i in $(LC_ALL=C \sensors -u 2>/dev/null |
sed -n 's/^ temp[0-9][0-9]*_input: \([0-9]*\)\..*$/\1/p'); do
(( $i > ${lp_temperature:-0} )) && lp_temperature=$i
if [[ -z ${lp_temperature-} ]] || (( $i > ${lp_temperature:-0} )); then
lp_temperature=$i
fi
done
}
......@@ -2556,7 +2558,9 @@ __lp_temp_acpi() {
# Only the integer part is retained
for i in $(LC_ALL=C \acpi -t |
sed -n 's/.* \(-\{0,1\}[0-9]*\)\.[0-9]* degrees C$/\1/p'); do
(( $i > ${lp_temperature:-0} )) && lp_temperature=$i
if [[ -z ${lp_temperature-} ]] || (( $i > ${lp_temperature:-0} )); then
lp_temperature=$i
fi
done
}
......@@ -2575,7 +2579,7 @@ __lp_temp_detect() {
# Check that we can retrieve temperature at least once
"$_LP_TEMP_FUNCTION" 2>/dev/null
# If $lp_temperature is set, success!
[[ -n "$lp_temperature" ]] && return 0
[[ -n "${lp_temperature-}" ]] && return 0
unset _LP_TEMP_FUNCTION
done
return 1
......@@ -2585,9 +2589,9 @@ __lp_temp_detect() {
_lp_temperature() {
(( LP_ENABLE_TEMP )) || return 2
lp_temperature=0
"$_LP_TEMP_FUNCTION"
[[ -z ${lp_temperature-} ]] && return 1
(( lp_temperature >= LP_TEMP_THRESHOLD ))
}
......
......@@ -34,8 +34,13 @@ battery_values+=(55)
temp_outputs+=(
"Thermal 0: ok, -267.8 degrees C"
)
temp_values+=(0)
temp_values+=(-267)
# VPS at OVH
temp_outputs+=(
""
)
temp_values+=("")
function test_acpi_battery {
......@@ -63,15 +68,38 @@ function test_acpi_battery {
function test_acpi_temperature {
LP_ENABLE_TEMP=1
LP_TEMP_THRESHOLD=-1000000
acpi() {
printf '%s\n' "$__temp_output"
}
local valid
for (( index=0; index < ${#temp_values[@]}; index++ )); do
__temp_output=${temp_outputs[$index]}
local lp_temperature=0
unset lp_temperature
__lp_temp_acpi
assertEquals "ACPI temperature output at index ${index}" "${temp_values[$index]}" "$lp_temperature"
assertEquals "ACPI temperature output at index ${index}" "${temp_values[$index]}" "${lp_temperature-}"
if [[ -n ${temp_values[$index]} ]]; then
valid=0
else
valid=1
fi
__lp_temp_detect acpi
assertEquals "ACPI temperature detect at index ${index}" "$valid" "$?"
# Set the temp function in case the above detect said it was invalid.
# While we should never be in this situation, might as well make sure
# it doesn't crash.
_LP_TEMP_FUNCTION=__lp_temp_acpi
unset lp_temperature
_lp_temperature
assertEquals "ACPI temperature return at index ${index}" "$valid" "$?"
assertEquals "ACPI temperature return output at index ${index}" "${temp_values[$index]}" "${lp_temperature-}"
done
}
......
......@@ -8,6 +8,10 @@ typeset -a outputs values
# Add test cases to these arrays like below
# No output
outputs+=("")
values+=("")
# Linux 4.18.0-193.19.1.el8_2.x86_64 #1 SMP Mon Sep 14 14:37:00 UTC 2020 x86_64 GNU/Linux CentOS Linux release 8.2.2004 (Core)
outputs+=(
"k10temp-pci-00c3
......@@ -239,15 +243,36 @@ values+=(48)
function test_sensors {
LP_ENABLE_TEMP=1
LP_TEMP_THRESHOLD=-1000000
sensors() {
printf '%s\n' "$__output"
}
for (( index=0; index < ${#values[@]}; index++ )); do
__output=${outputs[$index]}
local lp_temperature=0
unset lp_temperature
__lp_temp_sensors
assertEquals "Sensors temperature output at index ${index}" "${values[$index]}" "$lp_temperature"
assertEquals "Sensors temperature output at index ${index}" "${values[$index]}" "${lp_temperature-}"
if [[ -n ${values[$index]} ]]; then
valid=0
else
valid=1
fi
__lp_temp_detect sensors
assertEquals "Sensors temperature detect at index ${index}" "$valid" "$?"
# Set the temp function in case the above detect said it was invalid.
# While we should never be in this situation, might as well make sure
# it doesn't crash.
_LP_TEMP_FUNCTION=__lp_temp_sensors
unset lp_temperature
_lp_temperature
assertEquals "Sensors temperature return at index ${index}" "$valid" "$?"
assertEquals "Sensors temperature return output at index ${index}" "${values[$index]}" "${lp_temperature-}"
done
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment