| 1 |
"""Hardware integration tests -- run only on real machines with sensors. |
| 2 |
|
| 3 |
All tests are marked ``@pytest.mark.hardware`` and will be automatically |
| 4 |
skipped when CI=true or GITHUB_ACTIONS=true (see conftest.py). |
| 5 |
|
| 6 |
Run locally with:: |
| 7 |
|
| 8 |
pytest -m hardware -v |
| 9 |
""" |
| 10 |
|
| 11 |
import psutil |
| 12 |
import pytest |
| 13 |
|
| 14 |
from s_tui.sources.fan_source import FanSource |
| 15 |
from s_tui.sources.freq_source import FreqSource |
| 16 |
from s_tui.sources.rapl_power_source import RaplPowerSource |
| 17 |
from s_tui.sources.rapl_read import get_power_reader |
| 18 |
from s_tui.sources.temp_source import TempSource |
| 19 |
from s_tui.sources.util_source import UtilSource |
| 20 |
|
| 21 |
pytestmark = pytest.mark.hardware |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class TestUtilSourceHW: |
| 30 |
def test_init_and_available(self): |
| 31 |
"""UtilSource should always be available on a Linux machine.""" |
| 32 |
src = UtilSource() |
| 33 |
assert src.get_is_available() is True |
| 34 |
|
| 35 |
def test_sensor_list_matches_cpu_count(self): |
| 36 |
"""Sensor list should cover all present cores (including offline) + Avg.""" |
| 37 |
from s_tui.sources.source import Source |
| 38 |
|
| 39 |
src = UtilSource() |
| 40 |
expected = Source._get_total_core_count() + 1 |
| 41 |
assert len(src.get_sensor_list()) == expected |
| 42 |
|
| 43 |
def test_update_produces_readings(self): |
| 44 |
"""After update(), readings should be populated.""" |
| 45 |
src = UtilSource() |
| 46 |
src.update() |
| 47 |
readings = src.get_reading_list() |
| 48 |
assert len(readings) > 0 |
| 49 |
|
| 50 |
for v in readings: |
| 51 |
assert 0.0 <= v <= 100.0 |
| 52 |
|
| 53 |
def test_summary(self): |
| 54 |
"""get_summary() should return a non-empty dict.""" |
| 55 |
src = UtilSource() |
| 56 |
src.update() |
| 57 |
summary = src.get_summary() |
| 58 |
assert len(summary) > 0 |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
class TestFreqSourceHW: |
| 67 |
def test_init_and_available(self): |
| 68 |
"""FreqSource should be available when cpu_freq works.""" |
| 69 |
src = FreqSource() |
| 70 |
if psutil.cpu_freq() is not None: |
| 71 |
assert src.get_is_available() is True |
| 72 |
|
| 73 |
def test_update(self): |
| 74 |
"""After update(), readings should contain positive values.""" |
| 75 |
src = FreqSource() |
| 76 |
if not src.get_is_available(): |
| 77 |
pytest.skip("FreqSource not available on this hardware") |
| 78 |
src.update() |
| 79 |
readings = src.get_reading_list() |
| 80 |
assert len(readings) > 0 |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
class TestTempSourceHW: |
| 89 |
def test_init(self): |
| 90 |
"""TempSource should init without crashing.""" |
| 91 |
TempSource() |
| 92 |
|
| 93 |
def test_update_if_available(self): |
| 94 |
"""If available, update produces temperature readings.""" |
| 95 |
src = TempSource() |
| 96 |
if not src.get_is_available(): |
| 97 |
pytest.skip("TempSource not available on this hardware") |
| 98 |
src.update() |
| 99 |
readings = src.get_reading_list() |
| 100 |
assert len(readings) > 0 |
| 101 |
|
| 102 |
for v in readings: |
| 103 |
assert -40.0 < v < 200.0 |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
class TestFanSourceHW: |
| 112 |
def test_init(self): |
| 113 |
"""FanSource should init without crashing.""" |
| 114 |
FanSource() |
| 115 |
|
| 116 |
def test_update_if_available(self): |
| 117 |
"""If available, update produces fan readings.""" |
| 118 |
src = FanSource() |
| 119 |
if not src.get_is_available(): |
| 120 |
pytest.skip("FanSource not available on this hardware") |
| 121 |
src.update() |
| 122 |
readings = src.get_reading_list() |
| 123 |
assert len(readings) > 0 |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
class TestRaplPowerSourceHW: |
| 132 |
def test_init(self): |
| 133 |
"""RaplPowerSource should init without crashing.""" |
| 134 |
RaplPowerSource() |
| 135 |
|
| 136 |
def test_update_if_available(self): |
| 137 |
"""If available, update produces power readings. |
| 138 |
|
| 139 |
Note: RAPL sysfs files may require root permissions, so this test |
| 140 |
skips when readings are empty due to PermissionError. |
| 141 |
""" |
| 142 |
src = RaplPowerSource() |
| 143 |
if not src.get_is_available(): |
| 144 |
pytest.skip("RaplPowerSource not available on this hardware") |
| 145 |
src.update() |
| 146 |
readings = src.get_reading_list() |
| 147 |
if len(readings) == 0: |
| 148 |
pytest.skip("RAPL readings empty (likely permission denied)") |
| 149 |
assert len(readings) > 0 |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
class TestGetPowerReaderHW: |
| 158 |
def test_reader_type(self): |
| 159 |
"""get_power_reader returns a reader or None.""" |
| 160 |
reader = get_power_reader() |
| 161 |
if reader is not None: |
| 162 |
|
| 163 |
assert hasattr(reader, "read_power") |
| 164 |
result = reader.read_power() |
| 165 |
assert isinstance(result, list) |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
class TestMultiUpdateStability: |
| 174 |
def test_util_multiple_updates(self): |
| 175 |
"""UtilSource survives multiple update() calls.""" |
| 176 |
src = UtilSource() |
| 177 |
for _ in range(10): |
| 178 |
src.update() |
| 179 |
assert len(src.get_reading_list()) > 0 |
| 180 |
|
| 181 |
def test_freq_multiple_updates(self): |
| 182 |
"""FreqSource survives multiple update() calls.""" |
| 183 |
src = FreqSource() |
| 184 |
if not src.get_is_available(): |
| 185 |
pytest.skip("FreqSource not available") |
| 186 |
for _ in range(10): |
| 187 |
src.update() |
| 188 |
assert len(src.get_reading_list()) > 0 |