-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathtest_startup.py
More file actions
323 lines (277 loc) · 12.4 KB
/
test_startup.py
File metadata and controls
323 lines (277 loc) · 12.4 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import typing
import pytest
from pytest_mock import MockerFixture
from pytestqt.qtbot import QtBot
if typing.TYPE_CHECKING:
from PySide2 import QtCore
from dangerzone.gui import startup
from dangerzone.isolation_provider.qubes import is_qubes_native_conversion
from dangerzone.startup import MachineInitTask, MachineStartTask, Task
from dangerzone.updater import ErrorReport, InstallationStrategy, ReleaseReport
from dangerzone.updater import errors as update_errors
# It doesn't make sense to test the startup logic in a Qubes platform, since
# we don't make much use of it.
if is_qubes_native_conversion():
pytest.skip("Qubes native conversion is enabled", allow_module_level=True)
class StartupThreadMocker(startup.StartupThread):
def __init__(self, qtbot: QtBot, mocker: MockerFixture) -> None:
self.qtbot = qtbot
self.mocker = mocker
self.task_machine_init = startup.MachineInitTask()
self.task_machine_start = startup.MachineStartTask()
self.task_update_check = startup.UpdateCheckTask()
self.task_container_install = startup.ContainerInstallTask()
self.tasks = [
self.task_machine_init,
self.task_machine_start,
self.task_update_check,
self.task_container_install,
]
self.startup_thread = startup.StartupThread(self.tasks, raise_on_error=False)
self.expected_signals: list[tuple[QtCore.SignalInstance, str]] = []
self.not_expected_funcs: list[typing.Callable] = []
def make_machine_task_succeed(self) -> None:
self.mocker.patch("platform.system", return_value="Windows")
self.mocker.patch("dangerzone.startup.PodmanMachineManager")
def make_machine_task_skip(self) -> None:
self.mocker.patch("platform.system", return_value="Linux")
def make_machine_task_fail(self) -> None:
self.mocker.patch("platform.system", return_value="Windows")
self.mocker.patch(
"dangerzone.startup.PodmanMachineManager",
side_effect=Exception("Forcing task to fail"),
)
def make_update_task_succeed(self) -> None:
self.mocker.patch(
"dangerzone.updater.releases.should_check_for_updates", return_value=True
)
self.mocker.patch("dangerzone.updater.releases.check_for_updates")
def make_update_task_skip(self) -> None:
self.mocker.patch(
"dangerzone.updater.releases.should_check_for_updates", return_value=False
)
def make_update_task_fail(self) -> None:
self.mocker.patch(
"dangerzone.updater.releases.should_check_for_updates", return_value=True
)
self.mocker.patch(
"dangerzone.updater.releases.check_for_updates",
side_effect=Exception("Forcing task to fail"),
)
def make_install_task_succeed(self) -> None:
self.mocker.patch(
"dangerzone.updater.installer.get_installation_strategy",
return_value=InstallationStrategy.INSTALL_LOCAL_CONTAINER,
)
self.mocker.patch("dangerzone.updater.installer.install")
def make_install_task_skip(self) -> None:
self.mocker.patch(
"dangerzone.updater.installer.get_installation_strategy",
return_value=InstallationStrategy.DO_NOTHING,
)
def make_install_task_fail(self) -> None:
self.mocker.patch(
"dangerzone.updater.installer.get_installation_strategy",
return_value=InstallationStrategy.INSTALL_LOCAL_CONTAINER,
)
self.mocker.patch(
"dangerzone.updater.installer.install",
side_effect=Exception("Forcing task to fail"),
)
def expect_tasks_succeed(self, tasks: list[Task]) -> None:
for task in tasks:
if isinstance(task, (MachineInitTask, MachineStartTask)):
self.make_machine_task_succeed()
elif isinstance(task, startup.UpdateCheckTask):
self.make_update_task_succeed()
elif isinstance(task, startup.ContainerInstallTask):
self.make_install_task_succeed()
else:
raise RuntimeError(f"Unexpected task: {task}")
names = ["starting", "succeeded", "completed"]
for name in names:
self.expected_signals.append(
(getattr(task, name), f"{task.__class__.__name__}.{name}")
)
task.handle_skip = self.mocker.MagicMock() # type: ignore [method-assign]
self.not_expected_funcs.append(task.handle_skip)
task.handle_error = self.mocker.MagicMock() # type: ignore [method-assign]
self.not_expected_funcs.append(task.handle_error)
def expect_tasks_skip(self, tasks: list[Task]) -> None:
for task in tasks:
if isinstance(task, (MachineInitTask, MachineStartTask)):
self.make_machine_task_skip()
elif isinstance(task, startup.UpdateCheckTask):
self.make_update_task_skip()
elif isinstance(task, startup.ContainerInstallTask):
self.make_install_task_skip()
else:
raise RuntimeError(f"Unexpected task: {task}")
names = ["skipped", "completed"]
for name in names:
self.expected_signals.append(
(getattr(task, name), f"{task.__class__.__name__}.{name}")
)
task.handle_start = self.mocker.MagicMock() # type: ignore [method-assign]
self.not_expected_funcs.append(task.handle_start)
task.handle_error = self.mocker.MagicMock() # type: ignore [method-assign]
self.not_expected_funcs.append(task.handle_error)
def expect_tasks_fail(self, tasks: list[Task]) -> None:
for task in tasks:
if isinstance(task, (MachineInitTask, MachineStartTask)):
self.make_machine_task_fail()
elif isinstance(task, startup.UpdateCheckTask):
self.make_update_task_fail()
elif isinstance(task, startup.ContainerInstallTask):
self.make_install_task_fail()
else:
raise RuntimeError(f"Unexpected task: {task}")
names = ["starting", "failed"]
for name in names:
self.expected_signals.append(
(getattr(task, name), f"{task.__class__.__name__}.{name}")
)
task.handle_skip = self.mocker.MagicMock() # type: ignore [method-assign]
self.not_expected_funcs.append(task.handle_skip)
def expect_startup_succeed(self) -> None:
self.expected_signals += [
(self.startup_thread.starting, "StartupThread.starting"),
(self.startup_thread.succeeded, "StartupThread.succeeded"),
]
self.startup_thread.handle_error = self.mocker.MagicMock() # type: ignore [method-assign]
self.not_expected_funcs.append(self.startup_thread.handle_error)
def expect_startup_fail(self) -> None:
self.expected_signals += [
(self.startup_thread.starting, "StartupThread.starting"),
(self.startup_thread.failed, "StartupThread.failed"),
]
self.startup_thread.handle_success = self.mocker.MagicMock() # type: ignore [method-assign]
self.not_expected_funcs.append(self.startup_thread.handle_success)
def check_run(self) -> None:
with self.qtbot.waitSignals(self.expected_signals):
self.startup_thread.start()
self.startup_thread.wait()
for func in self.not_expected_funcs:
func.assert_not_called() # type: ignore [attr-defined]
def test_startup_all_success(qtbot: QtBot, mocker: MockerFixture) -> None:
startup_thread = StartupThreadMocker(qtbot, mocker)
startup_thread.expect_tasks_succeed(startup_thread.tasks)
startup_thread.expect_startup_succeed()
startup_thread.check_run()
def test_startup_all_skip(qtbot: QtBot, mocker: MockerFixture) -> None:
startup_thread = StartupThreadMocker(qtbot, mocker)
startup_thread.expect_tasks_skip(startup_thread.tasks)
startup_thread.expect_startup_succeed()
startup_thread.check_run()
def test_startup_machine_init_fail(qtbot: QtBot, mocker: MockerFixture) -> None:
startup_thread = StartupThreadMocker(qtbot, mocker)
startup_thread.expect_tasks_fail([startup_thread.task_machine_init])
startup_thread.expect_startup_fail()
startup_thread.check_run()
def test_startup_machine_start_fail(qtbot: QtBot, mocker: MockerFixture) -> None:
startup_thread = StartupThreadMocker(qtbot, mocker)
# NOTE: Make machine_init allowed to fail, so that we can proceed to the
# machine_start task.
startup_thread.task_machine_init.can_fail = True
startup_thread.expect_tasks_fail(
[startup_thread.task_machine_init, startup_thread.task_machine_start]
)
startup_thread.expect_startup_fail()
startup_thread.check_run()
def test_startup_update_check_fail(qtbot: QtBot, mocker: MockerFixture) -> None:
startup_thread = StartupThreadMocker(qtbot, mocker)
startup_thread.expect_tasks_succeed(
[startup_thread.task_machine_init, startup_thread.task_machine_start]
)
startup_thread.expect_tasks_fail([startup_thread.task_update_check])
startup_thread.expect_tasks_skip([startup_thread.task_container_install])
# NOTE: The update check task is a special case, where a failure does not mean that
# startup will fail as a whole.
startup_thread.expect_startup_succeed()
startup_thread.check_run()
def test_startup_update_check_needs_user_input(
qtbot: QtBot, mocker: MockerFixture
) -> None:
startup_thread = StartupThreadMocker(qtbot, mocker)
startup_thread.expect_tasks_succeed(
[
startup_thread.task_machine_init,
startup_thread.task_machine_start,
]
)
startup_thread.expect_tasks_skip(
[
startup_thread.task_update_check,
startup_thread.task_container_install,
]
)
startup_thread.expect_startup_succeed()
mocker.patch(
"dangerzone.updater.releases.should_check_for_updates",
side_effect=update_errors.NeedUserInput(),
)
startup_thread.expected_signals.append(
(
startup_thread.task_update_check.needs_user_input,
"UpdateCheckTask.needs_user_input",
)
)
startup_thread.check_run()
def test_startup_update_check_app_update(qtbot: QtBot, mocker: MockerFixture) -> None:
startup_thread = StartupThreadMocker(qtbot, mocker)
startup_thread.expect_tasks_succeed(
[
startup_thread.task_machine_init,
startup_thread.task_machine_start,
startup_thread.task_update_check,
]
)
startup_thread.expect_tasks_skip([startup_thread.task_container_install])
startup_thread.expect_startup_succeed()
mocker.patch(
"dangerzone.updater.releases.check_for_updates",
return_value=ReleaseReport(version="0.9.9"),
)
startup_thread.expected_signals.append(
(
startup_thread.task_update_check.app_update_available,
"UpdateCheckTask.app_update_available",
)
)
startup_thread.check_run()
def test_startup_update_check_container_update(
qtbot: QtBot, mocker: MockerFixture
) -> None:
startup_thread = StartupThreadMocker(qtbot, mocker)
startup_thread.expect_tasks_succeed(
[
startup_thread.task_machine_init,
startup_thread.task_machine_start,
startup_thread.task_update_check,
]
)
startup_thread.expect_tasks_skip([startup_thread.task_container_install])
startup_thread.expect_startup_succeed()
mocker.patch(
"dangerzone.updater.releases.check_for_updates",
return_value=ReleaseReport(container_image_bump=True),
)
startup_thread.expected_signals.append(
(
startup_thread.task_update_check.container_update_available,
"UpdateCheckTask.container_update_available",
)
)
startup_thread.check_run()
def test_startup_container_install_fail(qtbot: QtBot, mocker: MockerFixture) -> None:
startup_thread = StartupThreadMocker(qtbot, mocker)
startup_thread.expect_tasks_succeed(
[
startup_thread.task_machine_init,
startup_thread.task_machine_start,
startup_thread.task_update_check,
]
)
startup_thread.expect_tasks_fail([startup_thread.task_container_install])
startup_thread.expect_startup_fail()
startup_thread.check_run()