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