| 1 |
"""Tests verifying graceful behavior when psutil functions fail or are unavailable. |
| 2 |
|
| 3 |
These tests ensure that s-tui source classes handle situations where psutil |
| 4 |
functions raise AttributeError (missing on platform), return None / empty, |
| 5 |
or raise IOError/OSError during sensor reads. None of these should crash. |
| 6 |
""" |
| 7 |
|
| 8 |
from collections import OrderedDict |
| 9 |
from unittest.mock import MagicMock |
| 10 |
|
| 11 |
from s_tui.sources.fan_source import FanSource |
| 12 |
from s_tui.sources.freq_source import FreqSource |
| 13 |
from s_tui.sources.rapl_power_source import RaplPowerSource |
| 14 |
from s_tui.sources.temp_source import TempSource |
| 15 |
from s_tui.sources.util_source import UtilSource |
| 16 |
from tests.conftest import SensorTemperature |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class TestUtilSourceFailures: |
| 24 |
def test_cpu_percent_missing_attribute(self, mocker): |
| 25 |
"""If psutil has no cpu_percent, UtilSource should be unavailable.""" |
| 26 |
mocker.patch("psutil.cpu_count", return_value=4) |
| 27 |
mocker.patch("psutil.cpu_percent", side_effect=AttributeError) |
| 28 |
|
| 29 |
|
| 30 |
src = UtilSource() |
| 31 |
|
| 32 |
|
| 33 |
assert isinstance(src, UtilSource) |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class TestFreqSourceFailures: |
| 42 |
def test_cpu_freq_returns_none(self, mocker): |
| 43 |
"""If cpu_freq returns None (no frequency info), handle gracefully.""" |
| 44 |
import contextlib |
| 45 |
|
| 46 |
mocker.patch("psutil.cpu_freq", return_value=None) |
| 47 |
|
| 48 |
with contextlib.suppress(TypeError, AttributeError): |
| 49 |
FreqSource() |
| 50 |
|
| 51 |
def test_cpu_freq_empty_percpu(self, mocker): |
| 52 |
"""If cpu_freq(percpu=True) returns empty list.""" |
| 53 |
|
| 54 |
def _cpu_freq(percpu=False): |
| 55 |
if percpu: |
| 56 |
return [] |
| 57 |
return None |
| 58 |
|
| 59 |
import contextlib |
| 60 |
|
| 61 |
mocker.patch("psutil.cpu_freq", side_effect=_cpu_freq) |
| 62 |
with contextlib.suppress(TypeError, ValueError, ZeroDivisionError): |
| 63 |
FreqSource() |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
class TestTempSourceFailures: |
| 72 |
def test_sensors_temperatures_returns_none(self, mocker): |
| 73 |
"""sensors_temperatures() returning None should mark unavailable.""" |
| 74 |
mocker.patch("psutil.sensors_temperatures", return_value=None) |
| 75 |
src = TempSource() |
| 76 |
assert src.get_is_available() is False |
| 77 |
|
| 78 |
def test_sensors_temperatures_returns_empty(self, mocker): |
| 79 |
"""sensors_temperatures() returning {} should mark unavailable.""" |
| 80 |
mocker.patch("psutil.sensors_temperatures", return_value={}) |
| 81 |
src = TempSource() |
| 82 |
assert src.get_is_available() is False |
| 83 |
|
| 84 |
def test_sensors_temperatures_attribute_error(self, mocker): |
| 85 |
"""sensors_temperatures not available on platform.""" |
| 86 |
mocker.patch("psutil.sensors_temperatures", side_effect=AttributeError) |
| 87 |
src = TempSource() |
| 88 |
assert src.get_is_available() is False |
| 89 |
|
| 90 |
def test_sensors_temperatures_ioerror_on_init(self, mocker): |
| 91 |
"""First call raises IOError — should mark unavailable.""" |
| 92 |
mocker.patch("psutil.sensors_temperatures", side_effect=IOError) |
| 93 |
src = TempSource() |
| 94 |
assert src.get_is_available() is False |
| 95 |
|
| 96 |
def test_sensors_temperatures_ioerror_on_update(self, mocker): |
| 97 |
"""sensors_temperatures works during init but fails during update. |
| 98 |
|
| 99 |
The implementation catches the error and keeps stale data. |
| 100 |
""" |
| 101 |
sensors = [ |
| 102 |
SensorTemperature(label="Core 0", current=55.0, high=80.0, critical=100.0), |
| 103 |
] |
| 104 |
temps = OrderedDict([("coretemp", sensors)]) |
| 105 |
|
| 106 |
call_count = [0] |
| 107 |
|
| 108 |
def _temps_side_effect(): |
| 109 |
call_count[0] += 1 |
| 110 |
if call_count[0] <= 2: |
| 111 |
return temps |
| 112 |
raise OSError("sensor read failed") |
| 113 |
|
| 114 |
mocker.patch("psutil.sensors_temperatures", side_effect=_temps_side_effect) |
| 115 |
src = TempSource() |
| 116 |
assert src.get_is_available() is True |
| 117 |
|
| 118 |
|
| 119 |
old_measurement = list(src.last_measurement) |
| 120 |
|
| 121 |
ta |
| 122 |
src.update() |
| 123 |
assert src.last_measurement == old_measurement |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
class TestFanSourceFailures: |
| 132 |
def test_sensors_fans_returns_none(self, mocker): |
| 133 |
"""sensors_fans() returning None should mark unavailable.""" |
| 134 |
mocker.patch("psutil.sensors_fans", return_value=None) |
| 135 |
src = FanSource() |
| 136 |
assert src.get_is_available() is False |
| 137 |
|
| 138 |
def test_sensors_fans_attribute_error(self, mocker): |
| 139 |
"""sensors_fans not available on platform.""" |
| 140 |
mocker.patch("psutil.sensors_fans", side_effect=AttributeError) |
| 141 |
src = FanSource() |
| 142 |
assert src.get_is_available() is False |
| 143 |
|
| 144 |
def test_sensors_fans_ioerror_on_init(self, mocker): |
| 145 |
"""IOError during fan dict creation.""" |
| 146 |
|
| 147 |
call_count = [0] |
| 148 |
|
| 149 |
def _fans(): |
| 150 |
call_count[0] += 1 |
| 151 |
if call_count[0] == 1: |
| 152 |
return {"hw": []} |
| 153 |
raise OSError("read failed") |
| 154 |
|
| 155 |
mocker.patch("psutil.sensors_fans", side_effect=_fans) |
| 156 |
src = FanSource() |
| 157 |
assert src.get_is_available() is False |
| 158 |
|
| 159 |
def test_sensors_fans_typeerror_on_init(self, mocker): |
| 160 |
"""TypeError from psutil on init should mark unavailable (GH-255). |
| 161 |
|
| 162 |
psutil may raise TypeError internally when sysfs fan sensor files |
| 163 |
contain None (e.g. Intel xe GPU fans). |
| 164 |
""" |
| 165 |
mocker.patch("psutil.sensors_fans", side_effect=TypeError) |
| 166 |
src = FanSource() |
| 167 |
assert src.get_is_available() is False |
| 168 |
|
| 169 |
def test_sensors_fans_typeerror_on_update(self, mocker): |
| 170 |
"""TypeError from psutil during update keeps stale data (GH-256). |
| 171 |
|
| 172 |
When psutil.sensors_fans() raises TypeError mid-run, the source |
| 173 |
should keep its previous measurement rather than crashing. |
| 174 |
""" |
| 175 |
from tests.conftest import make_fans_dict |
| 176 |
|
| 177 |
fans = make_fans_dict(count=1, current=1200) |
| 178 |
mocker.patch("psutil.sensors_fans", return_value=fans) |
| 179 |
src = FanSource() |
| 180 |
assert src.get_is_available() is True |
| 181 |
src.update() |
| 182 |
assert src.last_measurement == [1200] |
| 183 |
|
| 184 |
|
| 185 |
mocker.patch("psutil.sensors_fans", side_effect=TypeError) |
| 186 |
src.update() |
| 187 |
|
| 188 |
assert src.last_measurement == [1200] |
| 189 |
|
| 190 |
def test_sensors_fans_none_on_update(self, mocker): |
| 191 |
"""sensors_fans() returning None mid-run keeps stale data. |
| 192 |
|
| 193 |
When psutil.sensors_fans() returns None during an update cycle, |
| 194 |
the source should keep its previous measurement. |
| 195 |
""" |
| 196 |
from tests.conftest import make_fans_dict |
| 197 |
|
| 198 |
fans = make_fans_dict(count=1, current=900) |
| 199 |
mocker.patch("psutil.sensors_fans", return_value=fans) |
| 200 |
src = FanSource() |
| 201 |
assert src.get_is_available() is True |
| 202 |
src.update() |
| 203 |
assert src.last_measurement == [900] |
| 204 |
|
| 205 |
|
| 206 |
mocker.patch("psutil.sensors_fans", return_value=None) |
| 207 |
src.update() |
| 208 |
|
| 209 |
assert src.last_measurement == [900] |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
class TestRaplPowerSourceFailures: |
| 218 |
def test_no_reader_available(self, mocker): |
| 219 |
"""No power reader available on platform.""" |
| 220 |
mocker.patch( |
| 221 |
"s_tui.sources.rapl_power_source.get_power_reader", return_value=None |
| 222 |
) |
| 223 |
src = RaplPowerSource() |
| 224 |
assert src.get_is_available() is False |
| 225 |
|
| 226 |
def test_reader_fails_on_read(self, mocker): |
| 227 |
"""Reader is available but fails during update — should not crash.""" |
| 228 |
from tests.conftest import RaplStats |
| 229 |
|
| 230 |
reader = MagicMock() |
| 231 |
reader.read_power.return_value = [ |
| 232 |
RaplStats(label="pkg", current=1000000.0, max=0.0), |
| 233 |
] |
| 234 |
mocker.patch( |
| 235 |
"s_tui.sources.rapl_power_source.get_power_reader", return_value=reader |
| 236 |
) |
| 237 |
src = RaplPowerSource() |
| 238 |
assert src.get_is_available() is True |
| 239 |
|
| 240 |
|
| 241 |
reader.read_power.side_effect = OSError("file disappeared") |
| 242 |
src.update() |
| 243 |
assert src.get_is_available() is True |
| 244 |
|
| 245 |
def test_reader_returns_none(self, mocker): |
| 246 |
"""Reader returns None during update — should not crash.""" |
| 247 |
from tests.conftest import RaplStats |
| 248 |
|
| 249 |
reader = MagicMock() |
| 250 |
reader.read_power.return_value = [ |
| 251 |
RaplStats(label="pkg", current=1000000.0, max=0.0), |
| 252 |
] |
| 253 |
mocker.patch( |
| 254 |
"s_tui.sources.rapl_power_source.get_power_reader", return_value=reader |
| 255 |
) |
| 256 |
src = RaplPowerSource() |
| 257 |
assert src.get_is_available() is True |
| 258 |
|
| 259 |
|
| 260 |
reader.read_power.return_value = None |
| 261 |
src.update() |
| 262 |
assert src.get_is_available() is True |
| 263 |
|
| 264 |
def test_update_skipped_when_unavailable(self, mocker): |
| 265 |
"""update() should be a no-op when source is not available.""" |
| 266 |
mocker.patch( |
| 267 |
"s_tui.sources.rapl_power_source.get_power_reader", return_value=None |
| 268 |
) |
| 269 |
src = RaplPowerSource() |
| 270 |
|
| 271 |
src.update() |