| 1 |
"""Testcases for rpi bad power.""" |
| 2 |
|
| 3 |
from typing import Any, List, Mapping, Text |
| 4 |
from unittest.mock import MagicMock, patch |
| 5 |
|
| 6 |
from rpi_bad_power import ( |
| 7 |
SYSFILE_HWMON_DIR, |
| 8 |
SYSFILE_LEGACY, |
| 9 |
UnderVoltageLegacy, |
| 10 |
UnderVoltageNew, |
| 11 |
new_under_voltage, |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
class MockFile: |
| 16 |
"""Mocked file.""" |
| 17 |
|
| 18 |
def __init__(self, content: Text): |
| 19 |
"""Initialize the mocked file.""" |
| 20 |
self.read = MagicMock(return_value=f"{content}\n") |
| 21 |
self.close = MagicMock() |
| 22 |
|
| 23 |
|
| 24 |
class PatchFile: |
| 25 |
"""Patch a file.""" |
| 26 |
|
| 27 |
def __init__(self, content: Text): |
| 28 |
"""Initialize the mocked file.""" |
| 29 |
self.file = MockFile(content) |
| 30 |
|
| 31 |
def __enter__(self) -> MockFile: |
| 32 |
"""Enter a with statement doing nothing.""" |
| 33 |
return self.file |
| 34 |
|
| 35 |
def __exit__(self, *exc_info: Any) -> bool: |
| 36 |
"""Exit a with statement by closing the file.""" |
| 37 |
self.file.close() |
| 38 |
return True |
| 39 |
|
| 40 |
|
| 41 |
class MockSysFiles: |
| 42 |
"""Mocking the system files.""" |
| 43 |
|
| 44 |
def __init__( |
| 45 |
self, |
| 46 |
multi: bool, |
| 47 |
new: bool, |
| 48 |
legacy: bool, |
| 49 |
no_hwmon: bool, |
| 50 |
is_under_voltage: bool, |
| 51 |
): |
| 52 |
"""Initialize the mocked system files.""" |
| 53 |
self.multi = multi |
| 54 |
self.new = new |
| 55 |
self.legacy = legacy |
| 56 |
self.no_hwmon = no_hwmon |
| 57 |
self.is_under_voltage = is_under_voltage |
| 58 |
|
| 59 |
self.listdir = MagicMock(side_effect=self._listdir) |
| 60 |
self.open = MagicMock(side_effect=self._open) |
| 61 |
self.isfile = MagicMock(side_effect=self._isfile) |
| 62 |
|
| 63 |
self.opened_files: List[MockFile] = [] |
| 64 |
self.files: Mapping[Text, Text] = {} |
| 65 |
if multi: |
| 66 |
self.files[f"{SYSFILE_HWMON_DIR}/hwmon0/name"] = "cpu_thermal" |
| 67 |
self.files[f"{SYSFILE_HWMON_DIR}/hwmon2/name"] = "rpi_volt" |
| 68 |
self.files[f"{SYSFILE_HWMON_DIR}/hwmon2/in0_lcrit_alarm"] = ( |
| 69 |
"1" if is_under_voltage else "0" |
| 70 |
) |
| 71 |
elif new: |
| 72 |
self.files[f"{SYSFILE_HWMON_DIR}/hwmon0/name"] = "rpi_volt" |
| 73 |
self.files[f"{SYSFILE_HWMON_DIR}/hwmon0/in0_lcrit_alarm"] = ( |
| 74 |
"1" if is_under_voltage else "0" |
| 75 |
) |
| 76 |
elif legacy: |
| 77 |
self.files[SYSFILE_LEGACY] = "50005" if self.is_under_voltage else "0" |
| 78 |
|
| 79 |
def _listdir(self, path: Text) -> List[Text]: |
| 80 |
assert path == SYSFILE_HWMON_DIR |
| 81 |
if self.no_hwmon: |
| 82 |
raise FileNotFoundError() |
| 83 |
if self.multi: |
| 84 |
return ["hwmon0", "hwmon1", "hwmon2"] |
| 85 |
if self.new: |
| 86 |
return ["hwmon0"] |
| 87 |
return [] |
| 88 |
|
| 89 |
def _open(self, path: Text) -> PatchFile: |
| 90 |
try: |
| 91 |
patch_file = PatchFile(self.files[path]) |
| 92 |
except KeyError: |
| 93 |
raise FileNotFoundError() |
| 94 |
self.opened_files.append(patch_file.file) |
| 95 |
return patch_file |
| 96 |
|
| 97 |
def _isfile(self, path: Text) -> bool: |
| 98 |
return path in self.files |
| 99 |
|
| 100 |
def assert_all_files_closed(self) -> None: |
| 101 |
"""Assert all opened files are closed.""" |
| 102 |
for file in self.opened_files: |
| 103 |
file.close.assert_called_once() |
| 104 |
|
| 105 |
|
| 106 |
class PatchSysFiles: |
| 107 |
"""Patch the system files.""" |
| 108 |
|
| 109 |
def __init__( |
| 110 |
self, |
| 111 |
multi: bool = False, |
| 112 |
new: bool = False, |
| 113 |
legacy: bool = False, |
| 114 |
no_hwmon: bool = False, |
| 115 |
is_under_voltage: bool = False, |
| 116 |
): |
| 117 |
"""Initialize the patch helper class.""" |
| 118 |
self.mock = MockSysFiles(multi, new, legacy, no_hwmon, is_under_voltage) |
| 119 |
self.listdir_patch = patch("rpi_bad_power.os.listdir", self.mock.listdir) |
| 120 |
self.open_patch = patch("rpi_bad_power.open", self.mock.open) |
| 121 |
self.isfile_patch = patch("rpi_bad_power.os.path.isfile", self.mock.isfile) |
| 122 |
|
| 123 |
def __enter__(self) -> MockSysFiles: |
| 124 |
"""Enter the with statement by patching functions.""" |
| 125 |
self.listdir_patch.__enter__() |
| 126 |
self.open_patch.__enter__() |
| 127 |
self.isfile_patch.__enter__() |
| 128 |
return self.mock |
| 129 |
|
| 130 |
def __exit__(self, *exc_info: Any) -> bool: |
| 131 |
"""Exit the with statement by un-patching functions.""" |
| 132 |
if exc_info[0] is not None: |
| 133 |
return False |
| 134 |
self.listdir_patch.__exit__(*exc_info) |
| 135 |
self.open_patch.__exit__(*exc_info) |
| 136 |
self.isfile_patch.__exit__(*exc_info) |
| 137 |
self.mock.assert_all_files_closed() |
| 138 |
return True |
| 139 |
|
| 140 |
|
| 141 |
def test_non_rpi() -> None: |
| 142 |
"""Test running on non raspberry pi environment.""" |
| 143 |
with PatchSysFiles() as mock_sys_files: |
| 144 |
assert new_under_voltage() is None |
| 145 |
mock_sys_files.listdir.assert_called_once_with(SYSFILE_HWMON_DIR) |
| 146 |
|
| 147 |
|
| 148 |
def test_no_hwmon() -> None: |
| 149 |
"""Test running on a system without hwmon directory.""" |
| 150 |
with PatchSysFiles(no_hwmon=True): |
| 151 |
assert new_under_voltage() is None |
| 152 |
|
| 153 |
|
| 154 |
def test_multi() -> None: |
| 155 |
"""Test running on a rpi kernel with multiple hwmon entries.""" |
| 156 |
with PatchSysFiles(True): |
| 157 |
under_voltage = new_under_voltage() |
| 158 |
assert isinstance(under_voltage, UnderVoltageNew) |
| 159 |
assert under_voltage.get() is False |
| 160 |
|
| 161 |
with PatchSysFiles(True, is_under_voltage=True): |
| 162 |
assert new_under_voltage().get() is True |
| 163 |
|
| 164 |
|
| 165 |
def test_new() -> None: |
| 166 |
"""Test running on a rpi kernel with one new hwmon entry.""" |
| 167 |
with PatchSysFiles(new=True): |
| 168 |
under_voltage = new_under_voltage() |
| 169 |
assert isinstance(under_voltage, UnderVoltageNew) |
| 170 |
assert under_voltage.get() is False |
| 171 |
|
| 172 |
with PatchSysFiles(new=True, is_under_voltage=True): |
| 173 |
assert new_under_voltage().get() is True |
| 174 |
|
| 175 |
|
| 176 |
def test_legacy() -> None: |
| 177 |
"""Test running on a legacy rpi kernel.""" |
| 178 |
with PatchSysFiles(legacy=True) as mock_sys_files: |
| 179 |
under_voltage = new_under_voltage() |
| 180 |
assert isinstance(under_voltage, UnderVoltageLegacy) |
| 181 |
assert under_voltage.get() is False |
| 182 |
mock_sys_files.isfile.assert_called_once_with(SYSFILE_LEGACY) |
| 183 |
|
| 184 |
with PatchSysFiles(legacy=True, is_under_voltage=True): |
| 185 |
assert new_under_voltage().get() is True |