| 1 |
"""Tests for PowerProfileMenu: UI construction, apply logic, and error handling.""" |
| 2 |
|
| 3 |
from unittest.mock import MagicMock, patch |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from s_tui.power_profile_menu import ( |
| 8 |
_EPP_TO_PROFILE, |
| 9 |
PowerProfileMenu, |
| 10 |
_read_current, |
| 11 |
_set_epp_via_powerprofilesctl, |
| 12 |
_write_all_cores, |
| 13 |
read_available, |
| 14 |
) |
| 15 |
|
| 16 |
GOVERNORS = ["performance", "powersave"] |
| 17 |
EPP_VALUES = ["default", "performance", "balance_performance", "balance_power", "power"] |
| 18 |
|
| 19 |
|
| 20 |
@pytest.fixture |
| 21 |
def menu_full(): |
| 22 |
"""Menu with both governor and EPP controllable (root + powerprofilesctl).""" |
| 23 |
with patch("s_tui.power_profile_menu._read_current", return_value="powersave"): |
| 24 |
return PowerProfileMenu( |
| 25 |
return_fn=MagicMock(), |
| 26 |
powerprofilesctl_exe="/usr/bin/powerprofilesctl", |
| 27 |
can_write_governor=True, |
| 28 |
can_write_epp=True, |
| 29 |
available_governors=GOVERNORS, |
| 30 |
available_epp=EPP_VALUES, |
| 31 |
) |
| 32 |
|
| 33 |
|
| 34 |
@pytest.fixture |
| 35 |
def menu_epp_only(): |
| 36 |
"""Menu with only EPP controllable via powerprofilesctl (non-root).""" |
| 37 |
with patch("s_tui.power_profile_menu._read_current", return_value="powersave"): |
| 38 |
return PowerProfileMenu( |
| 39 |
return_fn=MagicMock(), |
| 40 |
powerprofilesctl_exe="/usr/bin/powerprofilesctl", |
| 41 |
can_write_governor=False, |
| 42 |
can_write_epp=False, |
| 43 |
available_governors=GOVERNORS, |
| 44 |
available_epp=EPP_VALUES, |
| 45 |
) |
| 46 |
|
| 47 |
|
| 48 |
@pytest.fixture |
| 49 |
def menu_nothing(): |
| 50 |
"""Menu with nothing controllable (no root, no powerprofilesctl).""" |
| 51 |
with patch("s_tui.power_profile_menu._read_current", return_value="powersave"): |
| 52 |
return PowerProfileMenu( |
| 53 |
return_fn=MagicMock(), |
| 54 |
powerprofilesctl_exe=None, |
| 55 |
can_write_governor=False, |
| 56 |
can_write_epp=False, |
| 57 |
available_governors=GOVERNORS, |
| 58 |
available_epp=EPP_VALUES, |
| 59 |
) |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
class TestConstruction: |
| 68 |
def test_full_control(self, menu_full): |
| 69 |
assert menu_full.is_controllable() |
| 70 |
assert menu_full.governor_controllable |
| 71 |
assert menu_full.epp_controllable |
| 72 |
assert len(menu_full.governor_buttons) == 2 |
| 73 |
assert len(menu_full.epp_buttons) == 5 |
| 74 |
|
| 75 |
def test_epp_only(self, menu_epp_only): |
| 76 |
assert menu_epp_only.is_controllable() |
| 77 |
assert not menu_epp_only.governor_controllable |
| 78 |
assert menu_epp_only.epp_controllable |
| 79 |
|
| 80 |
assert len(menu_epp_only.governor_buttons) == 0 |
| 81 |
assert len(menu_epp_only.epp_buttons) == 5 |
| 82 |
|
| 83 |
def test_nothing_controllable(self, menu_nothing): |
| 84 |
assert not menu_nothing.is_controllable() |
| 85 |
|
| 86 |
def test_single_governor_hides_section(self): |
| 87 |
"""If only one governor, section is not controllable.""" |
| 88 |
with patch("s_tui.power_profile_menu._read_current", return_value="powersave"): |
| 89 |
m = PowerProfileMenu( |
| 90 |
return_fn=MagicMock(), |
| 91 |
powerprofilesctl_exe=None, |
| 92 |
can_write_governor=True, |
| 93 |
can_write_epp=False, |
| 94 |
available_governors=["powersave"], |
| 95 |
available_epp=[], |
| 96 |
) |
| 97 |
assert not m.governor_controllable |
| 98 |
assert not m.is_controllable() |
| 99 |
|
| 100 |
def test_no_epp_hides_section(self): |
| 101 |
"""If no EPP values, section is not controllable.""" |
| 102 |
with patch( |
| 103 |
"s_tui.power_profile_menu._read_current", return_value="performance" |
| 104 |
): |
| 105 |
m = PowerProfileMenu( |
| 106 |
return_fn=MagicMock(), |
| 107 |
powerprofilesctl_exe="/usr/bin/powerprofilesctl", |
| 108 |
can_write_governor=True, |
| 109 |
can_write_epp=False, |
| 110 |
available_governors=GOVERNORS, |
| 111 |
available_epp=[], |
| 112 |
) |
| 113 |
assert m.governor_controllable |
| 114 |
assert not m.epp_controllable |
| 115 |
assert m.is_controllable() |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
class TestGetSize: |
| 124 |
def test_returns_tuple(self, menu_full): |
| 125 |
size = menu_full.get_size() |
| 126 |
assert isinstance(size, tuple) |
| 127 |
assert len(size) == 2 |
| 128 |
assert size[1] == PowerProfileMenu.MAX_TITLE_LEN |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
class TestApplyGovernor: |
| 137 |
def test_apply_governor_writes_all_cores(self, menu_full): |
| 138 |
|
| 139 |
for rb in menu_full.governor_group: |
| 140 |
rb.set_state(rb.label == "performance", do_callback=False) |
| 141 |
|
| 142 |
with ( |
| 143 |
patch("s_tui.power_profile_menu._write_all_cores") as mock_write, |
| 144 |
patch("s_tui.power_profile_menu._set_epp_via_powerprofilesctl"), |
| 145 |
): |
| 146 |
menu_full.on_apply(None) |
| 147 |
|
| 148 |
mock_write.assert_any_call( |
| 149 |
"/sys/devices/system/cpu/cpu*/cpufreq/scaling_governor", "performance" |
| 150 |
) |
| 151 |
|
| 152 |
def test_apply_governor_error_shows_status(self, menu_full): |
| 153 |
for rb in menu_full.governor_group: |
| 154 |
rb.set_state(rb.label == "performance", do_callback=False) |
| 155 |
|
| 156 |
with patch( |
| 157 |
"s_tui.power_profile_menu._write_all_cores", |
| 158 |
side_effect=OSError("permission denied"), |
| 159 |
): |
| 160 |
menu_full.on_apply(None) |
| 161 |
|
| 162 |
status = menu_full.status_text.get_text()[0] |
| 163 |
assert "permission denied" in status |
| 164 |
|
| 165 |
menu_full.return_fn.assert_not_called() |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
class TestApplyEpp: |
| 174 |
def test_apply_epp_via_powerprofilesctl(self, menu_full): |
| 175 |
"""EPP values with a mapping should use powerprofilesctl.""" |
| 176 |
for rb in menu_full.epp_group: |
| 177 |
rb.set_state(rb.label == "balance_performance", do_callback=False) |
| 178 |
|
| 179 |
with ( |
| 180 |
patch("s_tui.power_profile_menu._write_all_cores"), |
| 181 |
patch( |
| 182 |
"s_tui.power_profile_menu._set_epp_via_powerprofilesctl" |
| 183 |
) as mock_pctl, |
| 184 |
): |
| 185 |
menu_full.on_apply(None) |
| 186 |
|
| 187 |
mock_pctl.assert_called_once_with( |
| 188 |
"/usr/bin/powerprofilesctl", "balance_performance" |
| 189 |
) |
| 190 |
|
| 191 |
def test_apply_epp_unmapped_falls_to_sysfs(self): |
| 192 |
"""EPP values without powerprofilesctl mapping fall back to sysfs.""" |
| 193 |
with patch( |
| 194 |
"s_tui.power_profile_menu._read_current", return_value="balance_power" |
| 195 |
): |
| 196 |
m = PowerProfileMenu( |
| 197 |
return_fn=MagicMock(), |
| 198 |
powerprofilesctl_exe="/usr/bin/powerprofilesctl", |
| 199 |
can_write_governor=False, |
| 200 |
can_write_epp=True, |
| 201 |
available_governors=["powersave"], |
| 202 |
available_epp=EPP_VALUES, |
| 203 |
) |
| 204 |
|
| 205 |
for rb in m.epp_group: |
| 206 |
rb.set_state(rb.label == "balance_power", do_callback=False) |
| 207 |
|
| 208 |
with ( |
| 209 |
patch( |
| 210 |
"s_tui.power_profile_menu._set_epp_via_powerprofilesctl", |
| 211 |
) as mock_pctl, |
| 212 |
patch("s_tui.power_profile_menu._write_all_cores") as mock_sysfs, |
| 213 |
): |
| 214 |
m.on_apply(None) |
| 215 |
|
| 216 |
mock_pctl.assert_not_called() |
| 217 |
mock_sysfs.assert_any_call( |
| 218 |
"/sys/devices/system/cpu/cpu*/cpufreq/energy_performance_preference", |
| 219 |
"balance_power", |
| 220 |
) |
| 221 |
|
| 222 |
def test_apply_epp_no_method_shows_error(self, menu_epp_only): |
| 223 |
"""If powerprofilesctl fails on unmapped value and no sysfs write, show error.""" |
| 224 |
for rb in menu_epp_only.epp_group: |
| 225 |
rb.set_state(rb.label == "default", do_callback=False) |
| 226 |
|
| 227 |
menu_epp_only.on_apply(None) |
| 228 |
status = menu_epp_only.status_text.get_text()[0] |
| 229 |
assert "EPP" in status |
| 230 |
|
| 231 |
def test_apply_epp_powerprofilesctl_busy_falls_back_to_sysfs(self): |
| 232 |
"""When powerprofilesctl returns 'busy', fall back to sysfs if writable.""" |
| 233 |
with patch( |
| 234 |
"s_tui.power_profile_menu._read_current", return_value="performance" |
| 235 |
): |
| 236 |
m = PowerProfileMenu( |
| 237 |
return_fn=MagicMock(), |
| 238 |
powerprofilesctl_exe="/usr/bin/powerprofilesctl", |
| 239 |
can_write_governor=True, |
| 240 |
can_write_epp=True, |
| 241 |
available_governors=GOVERNORS, |
| 242 |
available_epp=EPP_VALUES, |
| 243 |
) |
| 244 |
|
| 245 |
for rb in m.epp_group: |
| 246 |
rb.set_state(rb.label == "performance", do_callback=False) |
| 247 |
|
| 248 |
with ( |
| 249 |
patch( |
| 250 |
"s_tui.power_profile_menu._set_epp_via_powerprofilesctl", |
| 251 |
side_effect=OSError("Device busy"), |
| 252 |
), |
| 253 |
patch("s_tui.power_profile_menu._write_all_cores") as mock_sysfs, |
| 254 |
): |
| 255 |
m.on_apply(None) |
| 256 |
|
| 257 |
mock_sysfs.assert_any_call( |
| 258 |
"/sys/devices/system/cpu/cpu*/cpufreq/energy_performance_preference", |
| 259 |
"performance", |
| 260 |
) |
| 261 |
assert m.return_fn.call_count == 1 |
| 262 |
|
| 263 |
def test_apply_epp_busy_no_sysfs_shows_short_error(self, menu_epp_only): |
| 264 |
"""When powerprofilesctl returns 'busy' and no sysfs, show a short error.""" |
| 265 |
for rb in menu_epp_only.epp_group: |
| 266 |
rb.set_state(rb.label == "power", do_callback=False) |
| 267 |
|
| 268 |
with patch( |
| 269 |
"s_tui.power_profile_menu._set_epp_via_powerprofilesctl", |
| 270 |
side_effect=OSError("Cannot change EPP (governor: performance)"), |
| 271 |
): |
| 272 |
menu_epp_only.on_apply(None) |
| 273 |
|
| 274 |
status = menu_epp_only.status_text.get_text()[0] |
| 275 |
assert "Cannot change EPP" in status |
| 276 |
menu_epp_only.return_fn.assert_not_called() |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
class TestCancel: |
| 285 |
def test_cancel_calls_return_fn(self, menu_full): |
| 286 |
with patch.object(menu_full, "refresh_state"): |
| 287 |
menu_full.on_cancel(None) |
| 288 |
menu_full.return_fn.assert_called_once() |
| 289 |
|
| 290 |
def test_cancel_refreshes_state(self, menu_full): |
| 291 |
with patch.object(menu_full, "refresh_state") as mock_refresh: |
| 292 |
menu_full.on_cancel(None) |
| 293 |
mock_refresh.assert_called_once() |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
class TestRefreshState: |
| 302 |
def test_refresh_updates_radio_buttons(self, menu_full): |
| 303 |
with patch( |
| 304 |
"s_tui.power_profile_menu._read_current", return_value="performance" |
| 305 |
): |
| 306 |
menu_full.refresh_state() |
| 307 |
|
| 308 |
for rb in menu_full.governor_group: |
| 309 |
if rb.label == "performance": |
| 310 |
assert rb.state is True |
| 311 |
else: |
| 312 |
assert rb.state is False |
| 313 |
|
| 314 |
def test_refresh_clears_status(self, menu_full): |
| 315 |
menu_full.status_text.set_text("some error") |
| 316 |
with patch("s_tui.power_profile_menu._read_current", return_value="powersave"): |
| 317 |
menu_full.refresh_state() |
| 318 |
assert menu_full.status_text.get_text()[0] == "" |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
class TestHelpers: |
| 327 |
def test_read_available_success(self): |
| 328 |
with patch( |
| 329 |
"s_tui.power_profile_menu.cat", |
| 330 |
return_value="performance powersave", |
| 331 |
): |
| 332 |
result = read_available("/some/path") |
| 333 |
assert result == ["performance", "powersave"] |
| 334 |
|
| 335 |
def test_read_available_oserror(self): |
| 336 |
with patch("s_tui.power_profile_menu.cat", side_effect=OSError("no file")): |
| 337 |
result = read_available("/some/path") |
| 338 |
assert result == [] |
| 339 |
|
| 340 |
def test_read_current_success(self): |
| 341 |
with patch("s_tui.power_profile_menu.cat", return_value="powersave"): |
| 342 |
assert _read_current("/some/path") == "powersave" |
| 343 |
|
| 344 |
def test_read_current_oserror(self): |
| 345 |
with patch("s_tui.power_profile_menu.cat", side_effect=OSError("no file")): |
| 346 |
assert _read_current("/some/path") == "" |
| 347 |
|
| 348 |
def test_write_all_cores_success(self, tmp_path): |
| 349 |
|
| 350 |
for i in range(4): |
| 351 |
p = tmp_path / f"cpu{i}" |
| 352 |
p.mkdir() |
| 353 |
(p / "scaling_governor").write_text("powersave") |
| 354 |
|
| 355 |
_write_all_cores(str(tmp_path / "cpu*/scaling_governor"), "performance") |
| 356 |
|
| 357 |
for i in range(4): |
| 358 |
assert ( |
| 359 |
tmp_path / f"cpu{i}" / "scaling_governor" |
| 360 |
).read_text() == "performance" |
| 361 |
|
| 362 |
def test_write_all_cores_no_paths(self): |
| 363 |
with pytest.raises(OSError, match="No sysfs paths found"): |
| 364 |
_write_all_cores("/nonexistent/path/cpu*/gov", "performance") |
| 365 |
|
| 366 |
def test_write_all_cores_busy_gives_short_error(self, tmp_path): |
| 367 |
"""When all cores fail with 'busy', produce a short error message.""" |
| 368 |
for i in range(2): |
| 369 |
p = tmp_path / f"cpu{i}" |
| 370 |
p.mkdir() |
| 371 |
f = p / "epp" |
| 372 |
f.write_text("balance_performance") |
| 373 |
f.chmod(0o444) |
| 374 |
|
| 375 |
with pytest.raises(OSError, match=r"(?i)busy|permission"): |
| 376 |
_write_all_cores(str(tmp_path / "cpu*/epp"), "performance") |
| 377 |
|
| 378 |
def test_set_epp_via_powerprofilesctl_busy(self): |
| 379 |
"""Device busy stderr produces a clean short error with governor info.""" |
| 380 |
with ( |
| 381 |
patch("s_tui.power_profile_menu.subprocess.run") as mock_run, |
| 382 |
patch("s_tui.power_profile_menu._read_current", return_value="performance"), |
| 383 |
): |
| 384 |
mock_run.return_value = MagicMock( |
| 385 |
returncode=1, |
| 386 |
stderr="Device or resource busy", |
| 387 |
) |
| 388 |
with pytest.raises( |
| 389 |
OSError, match=r"Cannot change EPP.*governor.*performance" |
| 390 |
): |
| 391 |
_set_epp_via_powerprofilesctl("/usr/bin/powerprofilesctl", "power") |
| 392 |
|
| 393 |
def test_set_epp_via_powerprofilesctl_success(self): |
| 394 |
with patch("s_tui.power_profile_menu.subprocess.run") as mock_run: |
| 395 |
mock_run.return_value = MagicMock(returncode=0) |
| 396 |
_set_epp_via_powerprofilesctl("/usr/bin/powerprofilesctl", "performance") |
| 397 |
mock_run.assert_called_once_with( |
| 398 |
["/usr/bin/powerprofilesctl", "set", "performance"], |
| 399 |
capture_output=True, |
| 400 |
text=True, |
| 401 |
timeout=5, |
| 402 |
) |
| 403 |
|
| 404 |
def test_set_epp_via_powerprofilesctl_unmapped(self): |
| 405 |
with pytest.raises(OSError, match="No powerprofilesctl mapping"): |
| 406 |
_set_epp_via_powerprofilesctl("/usr/bin/powerprofilesctl", "default") |
| 407 |
|
| 408 |
def test_set_epp_via_powerprofilesctl_failure(self): |
| 409 |
with patch("s_tui.power_profile_menu.subprocess.run") as mock_run: |
| 410 |
mock_run.return_value = MagicMock(returncode=1, stderr="access denied") |
| 411 |
with pytest.raises(OSError, match="failed"): |
| 412 |
_set_epp_via_powerprofilesctl("/usr/bin/powerprofilesctl", "power") |
| 413 |
|
| 414 |
def test_epp_to_profile_mapping(self): |
| 415 |
assert _EPP_TO_PROFILE["performance"] == "performance" |
| 416 |
assert _EPP_TO_PROFILE["balance_performance"] == "balanced" |
| 417 |
assert _EPP_TO_PROFILE["power"] == "power-saver" |
| 418 |
assert "default" not in _EPP_TO_PROFILE |
| 419 |
assert "balance_power" not in _EPP_TO_PROFILE |