Debian Codegraph @

tests/test_runtime_disruption.py

raw symbols SCIP
1 """Runtime disruption tests -- simulating mid-run sensor/core changes.
2
3 These tests mock psutil to return normal data during __init__, then change
4 the mock before calling update() to simulate real-world events like CPU
5 hotplug, sensor disconnection, or permission changes.
6
7 All tests are marked @pytest.mark.xfail(strict=True) to document current
8 crashes without blocking CI. When each crash is fixed, remove the xfail
9 marker so the test becomes a regression guard.
10 """
11
12 from collections import OrderedDict
13 from unittest.mock import MagicMock
14
15 import pytest
16
17 from s_tui.sources.fan_source import FanSource
18 from s_tui.sources.freq_source import FreqSource
19 from s_tui.sources.rapl_power_source import RaplPowerSource
20 from s_tui.sources.temp_source import TempSource
21 from s_tui.sources.util_source import UtilSource
22 from tests.conftest import (
23 RaplStats,
24 SensorTemperature,
25 make_cpu_freq_list,
26 make_cpu_freq_overall,
27 make_fans_dict,
28 )
29
30 # =====================================================================
31 # UtilSource -- core count changes
32 # =====================================================================
33
34
35 class TestUtilCoreCountChanges:
36 def test_core_count_grows(self, mocker):
37 """Simulate a core coming online mid-run (e.g. CPU hotplug)."""
38 mocker.patch("psutil.cpu_count", return_value=4)
39 mocker.patch("psutil.cpu_percent", return_value=[25.0, 30.0, 20.0, 15.0])
40 mocker.patch(
41 "s_tui.sources.source.Source._get_total_core_count", return_value=5
42 )
43 mocker.patch(
44 "s_tui.sources.source.Source._get_online_cpu_ids",
45 return_value=[0, 1, 2, 3],
46 )
47 src = UtilSource()
48 assert len(src.get_sensor_list()) == 6 # Avg + 5 cores
49
50 # Core comes online: 5 values now returned
51 mocker.patch("psutil.cpu_percent", return_value=[25.0, 30.0, 20.0, 15.0, 10.0])
52 mocker.patch(
53 "s_tui.sources.source.Source._get_online_cpu_ids",
54 return_value=[0, 1, 2, 3, 4],
55 )
56 src.update()
57
58 # This should not crash
59 summary = src.get_sensors_summary()
60 assert summary is not None
61
62 def test_core_count_shrinks(self, mocker):
63 """Simulate a core going offline mid-run.
64
65 The code marks offline cores as N/A and keeps the sensor list
66 at its original length.
67 """
68 mocker.patch("psutil.cpu_count", return_value=4)
69 mocker.patch("psutil.cpu_percent", return_value=[25.0, 30.0, 20.0, 15.0])
70 mocker.patch(
71 "s_tui.sources.source.Source._get_total_core_count", return_value=4
72 )
73 mocker.patch(
74 "s_tui.sources.source.Source._get_online_cpu_ids",
75 return_value=[0, 1, 2, 3],
76 )
77 src = UtilSource()
78 assert len(src.get_sensor_list()) == 5
79
80 # Core goes offline: only 3 values, online_ids shrinks
81 mocker.patch("psutil.cpu_percent", return_value=[25.0, 30.0, 20.0])
82 mocker.patch(
83 "s_tui.sources.source.Source._get_online_cpu_ids",
84 return_value=[0, 1, 2],
85 )
86 src.update()
87
88 # Does not crash; sensor_list keeps original length
89 summary = src.get_sensors_summary()
90 assert summary is not None
91
92
93 # =====================================================================
94 # FreqSource -- core count changes
95 # =====================================================================
96
97
98 class TestFreqCoreCountChanges:
99 def test_freq_core_count_shrinks(self, mocker):
100 """cpu_freq(percpu=True) returns fewer cores after init.
101
102 The code marks disappeared cores as N/A without crashing.
103 """
104 per_cpu_4 = make_cpu_freq_list(4)
105 overall = make_cpu_freq_overall()
106
107 def _freq_init(percpu=False):
108 return per_cpu_4 if percpu else overall
109
110 mocker.patch("psutil.cpu_freq", side_effect=_freq_init)
111 mocker.patch(
112 "s_tui.sources.source.Source._get_total_core_count", return_value=4
113 )
114 mocker.patch(
115 "s_tui.sources.source.Source._get_online_cpu_ids",
116 return_value=[0, 1, 2, 3],
117 )
118 src = FreqSource()
119 assert len(src.get_sensor_list()) == 5
120
121 # Now fewer cores
122 per_cpu_2 = make_cpu_freq_list(2)
123
124 def _freq_shrunk(percpu=False):
125 return per_cpu_2 if percpu else overall
126
127 mocker.patch("psutil.cpu_freq", side_effect=_freq_shrunk)
128 mocker.patch(
129 "s_tui.sources.source.Source._get_online_cpu_ids",
130 return_value=[0, 1],
131 )
132 src.update()
133
134 summary = src.get_sensors_summary()
135 assert summary is not None
136
137
138 # =====================================================================
139 # TempSource -- sensor appear / disappear
140 # =====================================================================
141
142
143 class TestTempSensorChanges:
144 def test_temp_sensor_disappears(self, mocker):
145 """sensors_temperatures() returns fewer sensors mid-run.
146
147 Disappeared sensor is marked N/A via sensor_available.
148 """
149 sensors_2 = [
150 SensorTemperature(label="Core 0", current=55.0, high=80.0, critical=100.0),
151 SensorTemperature(label="Core 1", current=60.0, high=80.0, critical=100.0),
152 ]
153 temps_2 = OrderedDict([("coretemp", sensors_2)])
154 mocker.patch("psutil.sensors_temperatures", return_value=temps_2)
155 src = TempSource()
156 assert len(src.get_sensor_list()) == 2
157
158 # Sensor disappears
159 sensors_1 = [
160 SensorTemperature(label="Core 0", current=55.0, high=80.0, critical=100.0),
161 ]
162 temps_1 = OrderedDict([("coretemp", sensors_1)])
163 mocker.patch("psutil.sensors_temperatures", return_value=temps_1)
164 src.update()
165
166 summary = src.get_sensors_summary()
167 assert summary is not None
168 assert len(src.get_sensor_list()) == 2
169 # Core 0 still available, Core 1 disappeared
170 assert src.sensor_available[0] is True
171 assert src.sensor_available[1] is False
172 # Summary shows N/A for disappeared sensor
173 values = list(summary.values())
174 assert values[1] == "N/A"
175
176 def test_temp_sensor_appears(self, mocker):
177 """New sensor shows up in sensors_temperatures() mid-run."""
178 sensors_1 = [
179 SensorTemperature(label="Core 0", current=55.0, high=80.0, critical=100.0),
180 ]
181 temps_1 = OrderedDict([("coretemp", sensors_1)])
182 mocker.patch("psutil.sensors_temperatures", return_value=temps_1)
183 src = TempSource()
184 assert len(src.get_sensor_list()) == 1
185
186 # New sensor appears
187 sensors_2 = [
188 SensorTemperature(label="Core 0", current=55.0, high=80.0, critical=100.0),
189 SensorTemperature(label="Core 1", current=60.0, high=80.0, critical=100.0),
190 ]
191 temps_2 = OrderedDict([("coretemp", sensors_2)])
192 mocker.patch("psutil.sensors_temperatures", return_value=temps_2)
193 src.update()
194
195 summary = src.get_sensors_summary()
196 assert len(summary) == len(src.get_reading_list())
197
198
199 # =====================================================================
200 # FanSource -- sensor disappear / None
201 # =====================================================================
202
203
204 class TestFanSensorChanges:
205 def test_fan_returns_none_during_update(self, mocker):
206 """sensors_fans() returns None mid-run — keeps stale data (GH-256)."""
207 fans = make_fans_dict(count=1)
208 mocker.patch("psutil.sensors_fans", return_value=fans)
209 src = FanSource()
210 assert src.get_is_available() is True
211 src.update()
212 prev_measurement = list(src.last_measurement)
213
214 # Mid-run: returns None
215 mocker.patch("psutil.sensors_fans", return_value=None)
216 src.update() # should not crash
217 # Stale data preserved
218 assert src.last_measurement == prev_measurement
219
220 def test_fan_typeerror_during_update(self, mocker):
221 """sensors_fans() raises TypeError mid-run — keeps stale data (GH-256)."""
222 fans = make_fans_dict(count=1)
223 mocker.patch("psutil.sensors_fans", return_value=fans)
224 src = FanSource()
225 assert src.get_is_available() is True
226 src.update()
227 prev_measurement = list(src.last_measurement)
228
229 # Mid-run: psutil raises TypeError (sysfs None bug)
230 mocker.patch("psutil.sensors_fans", side_effect=TypeError)
231 src.update() # should not crash
232 # Stale data preserved
233 assert src.last_measurement == prev_measurement
234
235 def test_fan_sensor_disappears(self, mocker):
236 """sensors_fans() returns empty dict mid-run.
237
238 Disappeared fan sensor is marked N/A via sensor_available.
239 """
240 fans = make_fans_dict(count=1)
241 mocker.patch("psutil.sensors_fans", return_value=fans)
242 src = FanSource()
243 assert len(src.get_sensor_list()) == 1
244
245 # Sensor disappears
246 mocker.patch("psutil.sensors_fans", return_value={})
247 src.update()
248
249 summary = src.get_sensors_summary()
250 assert summary is not None
251 assert len(src.get_sensor_list()) == 1
252 assert src.sensor_available[0] is False
253 # Summary shows N/A for disappeared sensor
254 values = list(summary.values())
255 assert values[0] == "N/A"
256
257
258 # =====================================================================
259 # RaplPowerSource -- reader failure mid-run
260 # =====================================================================
261
262
263 class TestRaplReaderFailsMidRun:
264 def test_rapl_reader_fails_during_update(self, mocker):
265 """RAPL energy file becomes unreadable mid-run."""
266 reader = MagicMock()
267 reader.read_power.return_value = [
268 RaplStats(label="pkg", current=1000000.0, max=0.0),
269 ]
270 mocker.patch(
271 "s_tui.sources.rapl_power_source.get_power_reader", return_value=reader
272 )
273 src = RaplPowerSource()
274 assert src.get_is_available() is True
275
276 # File disappears
277 reader.read_power.side_effect = OSError("permission denied")
278 src.update() # should not crash
279 assert src.get_is_available() is True # or False, but no crash
280
281
282 # =====================================================================
283 # Source.update() exception propagation
284 # =====================================================================
285
286
287 class TestSourceUpdateExceptionPropagation:
288 def test_source_update_raises_oserror(self, mocker):
289 """Any psutil call raises OSError during update() — should not propagate."""
290 mocker.patch("psutil.cpu_count", return_value=4)
291 mocker.patch("psutil.cpu_percent", return_value=[25.0, 30.0, 20.0, 15.0])
292 mocker.patch(
293 "s_tui.sources.source.Source._get_total_core_count", return_value=4
294 )
295 mocker.patch(
296 "s_tui.sources.source.Source._get_online_cpu_ids",
297 return_value=[0, 1, 2, 3],
298 )
299 src = UtilSource()
300
301 mocker.patch("psutil.cpu_percent", side_effect=OSError("device gone"))
302 # In current code this propagates unhandled
303 # The test verifies we want it to NOT propagate
304 try:
305 src.update()
306 except OSError:
307 pytest.fail("update() should not propagate OSError to caller")