-
Notifications
You must be signed in to change notification settings - Fork 782
Expand file tree
/
Copy pathtest_systemd.py
More file actions
265 lines (197 loc) · 8.02 KB
/
test_systemd.py
File metadata and controls
265 lines (197 loc) · 8.02 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
"""Test hostname dbus interface."""
# pylint: disable=import-error
from dbus_fast import DBusError, Variant
from dbus_fast.aio.message_bus import MessageBus
import pytest
from supervisor.dbus.const import (
StartUnitMode,
StopUnitMode,
SystemState,
UnitActiveState,
)
from supervisor.dbus.systemd import Systemd
from supervisor.exceptions import DBusNotConnectedError, DBusSystemdNoSuchUnit
from tests.common import mock_dbus_services
from tests.dbus_service_mocks.systemd import Systemd as SystemdService
@pytest.fixture(name="systemd_service", autouse=True)
async def fixture_systemd_service(dbus_session_bus: MessageBus) -> SystemdService:
"""Mock systemd dbus service."""
yield (await mock_dbus_services({"systemd": None}, dbus_session_bus))["systemd"]
async def test_dbus_systemd_info(dbus_session_bus: MessageBus):
"""Test systemd properties."""
systemd = Systemd()
assert systemd.boot_timestamp is None
assert systemd.startup_time is None
await systemd.connect(dbus_session_bus)
assert systemd.boot_timestamp == 1632236713344227
assert systemd.startup_time == 45.304696
assert await systemd.get_system_state() == SystemState.RUNNING
async def test_subscribe_on_connect(
systemd_service: SystemdService, dbus_session_bus: MessageBus
):
"""Test that Subscribe is called on connect to enable signal emission."""
systemd_service.Subscribe.calls.clear()
systemd = Systemd()
await systemd.connect(dbus_session_bus)
assert systemd_service.Subscribe.calls == [()]
async def test_reboot(systemd_service: SystemdService, dbus_session_bus: MessageBus):
"""Test reboot."""
systemd_service.Reboot.calls.clear()
systemd = Systemd()
with pytest.raises(DBusNotConnectedError):
await systemd.reboot()
await systemd.connect(dbus_session_bus)
assert await systemd.reboot() is None
assert systemd_service.Reboot.calls == [()]
async def test_power_off(systemd_service: SystemdService, dbus_session_bus: MessageBus):
"""Test power off."""
systemd_service.PowerOff.calls.clear()
systemd = Systemd()
with pytest.raises(DBusNotConnectedError):
await systemd.power_off()
await systemd.connect(dbus_session_bus)
assert await systemd.power_off() is None
assert systemd_service.PowerOff.calls == [()]
async def test_start_unit(
systemd_service: SystemdService, dbus_session_bus: MessageBus
):
"""Test start unit."""
systemd_service.StartUnit.calls.clear()
systemd = Systemd()
with pytest.raises(DBusNotConnectedError):
await systemd.start_unit("test_unit", StartUnitMode.REPLACE)
await systemd.connect(dbus_session_bus)
assert (
await systemd.start_unit("test_unit", StartUnitMode.REPLACE)
== "/org/freedesktop/systemd1/job/7623"
)
assert systemd_service.StartUnit.calls == [("test_unit", "replace")]
async def test_stop_unit(systemd_service: SystemdService, dbus_session_bus: MessageBus):
"""Test stop unit."""
systemd_service.StopUnit.calls.clear()
systemd = Systemd()
with pytest.raises(DBusNotConnectedError):
await systemd.stop_unit("test_unit", StopUnitMode.REPLACE)
await systemd.connect(dbus_session_bus)
assert (
await systemd.stop_unit("test_unit", StopUnitMode.REPLACE)
== "/org/freedesktop/systemd1/job/7623"
)
assert systemd_service.StopUnit.calls == [("test_unit", "replace")]
async def test_restart_unit(
systemd_service: SystemdService, dbus_session_bus: MessageBus
):
"""Test restart unit."""
systemd_service.RestartUnit.calls.clear()
systemd = Systemd()
with pytest.raises(DBusNotConnectedError):
await systemd.restart_unit("test_unit", StartUnitMode.REPLACE)
await systemd.connect(dbus_session_bus)
assert (
await systemd.restart_unit("test_unit", StartUnitMode.REPLACE)
== "/org/freedesktop/systemd1/job/7623"
)
assert systemd_service.RestartUnit.calls == [("test_unit", "replace")]
async def test_reload_unit(
systemd_service: SystemdService, dbus_session_bus: MessageBus
):
"""Test reload unit."""
systemd_service.ReloadOrRestartUnit.calls.clear()
systemd = Systemd()
with pytest.raises(DBusNotConnectedError):
await systemd.reload_unit("test_unit", StartUnitMode.REPLACE)
await systemd.connect(dbus_session_bus)
assert (
await systemd.reload_unit("test_unit", StartUnitMode.REPLACE)
== "/org/freedesktop/systemd1/job/7623"
)
assert systemd_service.ReloadOrRestartUnit.calls == [("test_unit", "replace")]
async def test_list_units(dbus_session_bus: MessageBus):
"""Test list units."""
systemd = Systemd()
with pytest.raises(DBusNotConnectedError):
await systemd.list_units()
await systemd.connect(dbus_session_bus)
units = await systemd.list_units()
assert len(units) == 4
assert units[1][0] == "firewalld.service"
assert units[1][2] == "not-found"
assert units[3][0] == "zram-swap.service"
assert units[3][2] == "loaded"
async def test_start_transient_unit(
systemd_service: SystemdService, dbus_session_bus: MessageBus
):
"""Test start transient unit."""
systemd_service.StartTransientUnit.calls.clear()
systemd = Systemd()
with pytest.raises(DBusNotConnectedError):
await systemd.start_transient_unit(
"tmp-test.mount",
StartUnitMode.FAIL,
[],
)
await systemd.connect(dbus_session_bus)
assert (
await systemd.start_transient_unit(
"tmp-test.mount",
StartUnitMode.FAIL,
[
("Description", Variant("s", "Test")),
("What", Variant("s", "//homeassistant/config")),
("Type", Variant("s", "cifs")),
("Options", Variant("s", "username=homeassistant,password=password")),
],
)
== "/org/freedesktop/systemd1/job/7623"
)
assert systemd_service.StartTransientUnit.calls == [
(
"tmp-test.mount",
"fail",
[
("Description", Variant("s", "Test")),
("What", Variant("s", "//homeassistant/config")),
("Type", Variant("s", "cifs")),
("Options", Variant("s", "username=homeassistant,password=password")),
],
[],
)
]
async def test_reset_failed_unit(
systemd_service: SystemdService, dbus_session_bus: MessageBus
):
"""Test resetting a failed unit."""
systemd_service.ResetFailedUnit.calls.clear()
systemd = Systemd()
with pytest.raises(DBusNotConnectedError):
await systemd.reset_failed_unit("tmp-test.mount")
await systemd.connect(dbus_session_bus)
assert await systemd.reset_failed_unit("tmp-test.mount") is None
assert systemd_service.ResetFailedUnit.calls == [("tmp-test.mount",)]
async def test_get_unit(systemd_service: SystemdService, dbus_session_bus: MessageBus):
"""Test getting job ID for unit."""
await mock_dbus_services(
{"systemd_unit": "/org/freedesktop/systemd1/unit/tmp_2dyellow_2emount"},
dbus_session_bus,
)
systemd_service.GetUnit.calls.clear()
systemd = Systemd()
with pytest.raises(DBusNotConnectedError):
await systemd.get_unit("tmp-test.mount")
await systemd.connect(dbus_session_bus)
unit = await systemd.get_unit("tmp-test.mount")
assert unit.bus_name == "org.freedesktop.systemd1"
assert unit.object_path == "/org/freedesktop/systemd1/unit/tmp_2dyellow_2emount"
assert await unit.get_active_state() == UnitActiveState.ACTIVE
assert systemd_service.GetUnit.calls == [("tmp-test.mount",)]
async def test_get_unit_not_found(
systemd_service: SystemdService, dbus_session_bus: MessageBus
):
"""Test error for non-existent unit name."""
systemd_service.response_get_unit = DBusError(
"org.freedesktop.systemd1.NoSuchUnit", "error"
)
systemd = Systemd()
await systemd.connect(dbus_session_bus)
with pytest.raises(DBusSystemdNoSuchUnit):
await systemd.get_unit("error.mount")