Skip to content

Commit 634aaed

Browse files
committed
updater: update tests and code refformating
1 parent 242746b commit 634aaed

8 files changed

Lines changed: 78 additions & 50 deletions

File tree

qui/updater/intro_page.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,15 @@ def select_rows(self):
211211
for row in self.list_store:
212212
row.selected = row.updates_available in self.head_checkbox.allowed
213213

214-
def select_rows_ignoring_conditions(self, cliargs, dom0):
214+
def select_rows_ignoring_conditions(self, cliargs):
215215
cmd = ["qubes-vm-update", "--dry-run", "--quiet"]
216216

217217
args = [a for a in dir(cliargs) if not a.startswith("_")]
218218
for arg in args:
219219
if arg in (
220220
"non_interactive",
221221
"non_default_select",
222+
"dom0",
222223
"restart",
223224
"apply_to_sys",
224225
"apply_to_all",
@@ -235,10 +236,12 @@ def select_rows_ignoring_conditions(self, cliargs, dom0):
235236
cmd.append(f"--{arg.replace('_', '-')}")
236237
if not isinstance(value, bool):
237238
cmd.append(str(value))
239+
if cliargs.dom0:
240+
cmd.extend(["--targets", "dom0"])
238241

239242
to_update = set()
240243
non_default_select = [
241-
"--" + arg for arg in cliargs.non_default_select if arg != "dom0" # TODO remove dom0 flag
244+
"--" + arg for arg in cliargs.non_default_select if arg != "dom0"
242245
]
243246
non_default = [a for a in cmd if a in non_default_select]
244247
if non_default or cliargs.non_interactive:
@@ -257,15 +260,19 @@ def _get_stale_qubes(self, cmd):
257260
result = set()
258261
if "dom0" in output_lines[0]:
259262
result.add("dom0")
260-
if ":" not in output_lines[1]:
263+
264+
second_line = output_lines[1] if len(output_lines) > 1 else ""
265+
if ":" not in second_line:
261266
return result
262267

263-
return result.union({
264-
vm_name.strip()
265-
for vm_name in output_lines[1]
266-
.split(":", maxsplit=1)[1]
267-
.split(",")
268-
})
268+
return result.union(
269+
{
270+
vm_name.strip()
271+
for vm_name in second_line.split(":", maxsplit=1)[1].split(
272+
","
273+
)
274+
}
275+
)
269276
except subprocess.CalledProcessError as err:
270277
if err.returncode != 100:
271278
raise err

qui/updater/progress_page.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import threading
2525
import time
2626
import gi
27-
from typing import Dict, List
27+
from typing import Dict
2828

2929
gi.require_version("Gtk", "3.0") # isort:skip
3030
from gi.repository import Gtk, Gdk, GLib, GObject # isort:skip
@@ -165,7 +165,7 @@ def update_selected(self, to_update, settings):
165165
self.update_details.update_buffer()
166166

167167
def do_update_selected(
168-
self, rows: Dict[str, RowWrapper], settings: Settings
168+
self, rows: Dict[str, RowWrapper], settings: Settings
169169
):
170170
"""Runs `qubes-vm-update` command."""
171171
targets = ",".join((name for name in rows.keys()))
@@ -245,7 +245,9 @@ def handle_err_line(self, untrusted_line, rows):
245245

246246
def read_stdouts(self, proc, rows):
247247
curr_name_out = ""
248-
for untrusted_line in iter(proc.stdout.readline, "__COMMUNICATION_IS_DONE__"):
248+
for untrusted_line in iter(
249+
proc.stdout.readline, "__COMMUNICATION_IS_DONE__"
250+
):
249251
if untrusted_line != "__COMMUNICATION_IS_DONE__":
250252
line = self._sanitize_line(untrusted_line)
251253
maybe_name, text = line.split(" ", 1)

qui/updater/tests/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# You should have received a copy of the GNU Lesser General Public License along
1919
# with this program; if not, see <http://www.gnu.org/licenses/>.
2020
"""Conftest helper pytest file: fixtures container here are
21-
reachable by all tests"""
21+
reachable by all tests"""
2222
import pytest
2323
import importlib.resources
2424

@@ -87,7 +87,7 @@ def test_qapp_impl():
8787
qapp,
8888
"dom0",
8989
"AdminVM",
90-
{},
90+
{"updateable": ("bool", True, "True")},
9191
{
9292
"service.qubes-update-check": 1,
9393
"config.default.qubes-update-check": None,
@@ -337,7 +337,7 @@ def all_vms_list(test_qapp, mock_list_store):
337337

338338

339339
@pytest.fixture
340-
def updatable_vms_list(test_qapp, mock_list_store):
340+
def updateable_vms_list(test_qapp, mock_list_store):
341341
result = ListWrapper(UpdateRowWrapper, mock_list_store)
342342
for vm in test_qapp.domains:
343343
if vm.klass in ("AdminVM", "TemplateVM", "StandaloneVM"):

qui/updater/tests/test_intro_page.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ def test_populate_vm_list(
4040
("test-standalone", "admin.vm.feature.Get", "updates-available", None)
4141
] = (b"0\x00" + str(1).encode())
4242
# inconsistent output of qubes-vm-update, but it does not matter
43-
mock_subprocess.return_value = (
44-
b"Following templates will be updated:test-standalone"
45-
)
43+
mock_subprocess.return_value = b"The admin VM will not be updated.\nFollowing templates will be updated:test-standalone"
4644

4745
assert not sut.is_populated
4846

@@ -56,12 +54,13 @@ def test_populate_vm_list(
5654
("fedora-36", "admin.vm.feature.Get", "updates-available", None)
5755
] = (b"0\x00" + str(1).encode())
5856
mock_subprocess.return_value = (
57+
b"The admin VM (dom0) will be updated.\n"
5958
b"Following templates will be updated:test-standalone,fedora-36"
6059
)
6160

6261
sut.populate_vm_list(test_qapp, mock_settings)
6362
assert len(sut.list_store) == 4
64-
assert len(sut.get_vms_to_update()) == 2
63+
assert len(sut.get_vms_to_update()) == 3
6564

6665

6766
@pytest.mark.parametrize(
@@ -340,35 +339,52 @@ def test_prohibit_start_rationale_tooltip(
340339
# Comma separated list of VMs to target
341340
pytest.param(
342341
("--targets", "dom0,fedora-36"),
343-
b"fedora-36",
342+
b"dom0,fedora-36",
344343
b"",
345344
{"dom0", "fedora-36"},
346-
("--targets", "fedora-36"),
345+
("--targets", "dom0,fedora-36"),
347346
id="targets",
348347
),
349348
# `qubes-update-gui --standalones`
350349
# Target all StandaloneVMs
351350
pytest.param(
352351
("--standalones",),
353-
b"",
354352
",".join(_standalones).encode(),
353+
b"",
355354
_standalones,
356355
("--standalones",),
357356
id="standalones",
358357
),
359358
# `qubes-update-gui --dom0`
360359
# Target dom0
361360
pytest.param(
362-
("--dom0", "--force-update"), b"", b"", {"dom0"}, None, id="dom0"
361+
("--dom0", "--force-update"),
362+
b"",
363+
b"",
364+
{"dom0"},
365+
("--force-update", "--targets", "dom0"),
366+
id="dom0",
363367
),
364368
# `qubes-update-gui --dom0 --skip dom0`
365369
# Comma separated list of VMs to be skipped,
366370
# works with all other options.
367371
pytest.param(
368-
("--dom0", "--skip", "dom0"), b"", b"", set(), None, id="skip all"
372+
("--dom0", "--skip", "dom0"),
373+
b"",
374+
b"",
375+
set(),
376+
("--skip", "dom0", "--targets", "dom0"),
377+
id="skip all",
369378
),
370379
# `qubes-update-gui --skip dom0`
371-
pytest.param(("--skip", "dom0"), b"", b"", set(), None, id="skip dom0"),
380+
pytest.param(
381+
("--skip", "dom0"),
382+
b"",
383+
b"",
384+
set(),
385+
("--skip", "dom0"),
386+
id="skip dom0",
387+
),
372388
# `qubes-update-gui --skip garbage-name`
373389
pytest.param(
374390
("--skip", "garbage-name"),
@@ -385,7 +401,7 @@ def test_prohibit_start_rationale_tooltip(
385401
b"",
386402
b"",
387403
set(),
388-
None,
404+
("--skip", "dom0", "--targets", "dom0"),
389405
id="skip all targets",
390406
),
391407
# `qubes-update-gui --templates dom0 --skip fedora-36,garbage-name`
@@ -426,7 +442,11 @@ def test_select_rows_ignoring_conditions(
426442

427443
assert len(sut.list_store) == 12
428444

429-
result = b""
445+
if "dom0" in expected_selection:
446+
result = b"The admin VM (dom0) will be updated.\n"
447+
else:
448+
result = b"The admin VM will not be updated.\n"
449+
430450
if tmpls_and_stndas:
431451
result += (
432452
b"Following templates and standalones will be updated: "
@@ -446,7 +466,7 @@ def test_select_rows_ignoring_conditions(
446466
] = (b"0\x00" + b"3020-01-01 00:00:00")
447467

448468
cliargs = parse_args(args, test_qapp)
449-
sut.select_rows_ignoring_conditions(cliargs, test_qapp.domains["dom0"])
469+
sut.select_rows_ignoring_conditions(cliargs)
450470
to_update = {row.name for row in sut.list_store if row.selected}
451471

452472
assert to_update == expected_selection

qui/updater/tests/test_progress_page.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_perform_update(
8484
mock_next_button,
8585
mock_cancel_button,
8686
mock_label,
87-
updatable_vms_list,
87+
updateable_vms_list,
8888
):
8989
mock_log = Mock()
9090
mock_callback = Mock()
@@ -97,7 +97,7 @@ def test_perform_update(
9797
mock_callback,
9898
)
9999

100-
sut.vms_to_update = updatable_vms_list
100+
sut.vms_to_update = updateable_vms_list
101101

102102
class VMConsumer:
103103
def __call__(self, vm_rows, *args, **kwargs):
@@ -130,7 +130,7 @@ def test_update_templates(
130130
idle_add,
131131
interrupted,
132132
real_builder,
133-
updatable_vms_list,
133+
updateable_vms_list,
134134
mock_next_button,
135135
mock_cancel_button,
136136
mock_label,
@@ -154,15 +154,15 @@ def test_update_templates(
154154

155155
sut.update_details.progress_textview = mock_text_view
156156
# chose vm to show details
157-
sut.update_details.active_row = updatable_vms_list[0]
158-
for i, row in enumerate(updatable_vms_list):
157+
sut.update_details.active_row = updateable_vms_list[0]
158+
for i, row in enumerate(updateable_vms_list):
159159
row.buffer = f"Details {i}"
160160

161161
if interrupted:
162162
sut.interrupt_update()
163-
sut.update_selected(updatable_vms_list, mock_settings)
163+
sut.update_selected(updateable_vms_list, mock_settings)
164164

165-
sut.update_details.set_active_row(updatable_vms_list[2])
165+
sut.update_details.set_active_row(updateable_vms_list[2])
166166

167167
calls = [
168168
call(sut.set_total_progress, 100),
@@ -252,7 +252,7 @@ def test_get_update_summary(
252252
mock_next_button,
253253
mock_cancel_button,
254254
mock_label,
255-
updatable_vms_list,
255+
updateable_vms_list,
256256
):
257257
mock_log = Mock()
258258
mock_callback = Mock()
@@ -265,12 +265,12 @@ def test_get_update_summary(
265265
mock_callback,
266266
)
267267

268-
updatable_vms_list[0].set_status(UpdateStatus.NoUpdatesFound)
269-
updatable_vms_list[1].set_status(UpdateStatus.Error)
270-
updatable_vms_list[2].set_status(UpdateStatus.Cancelled)
271-
updatable_vms_list[3].set_status(UpdateStatus.Success)
268+
updateable_vms_list[0].set_status(UpdateStatus.NoUpdatesFound)
269+
updateable_vms_list[1].set_status(UpdateStatus.Error)
270+
updateable_vms_list[2].set_status(UpdateStatus.Cancelled)
271+
updateable_vms_list[3].set_status(UpdateStatus.Success)
272272

273-
sut.vms_to_update = updatable_vms_list
273+
sut.vms_to_update = updateable_vms_list
274274

275275
updated, no_updates, failed, cancelled = sut.get_update_summary()
276276

@@ -281,9 +281,9 @@ def test_get_update_summary(
281281
mock_callback.assert_not_called()
282282

283283

284-
def test_set_active_row(real_builder, updatable_vms_list):
284+
def test_set_active_row(real_builder, updateable_vms_list):
285285
sut = QubeUpdateDetails(real_builder)
286-
row = updatable_vms_list[0]
286+
row = updateable_vms_list[0]
287287
sut.set_active_row(row)
288288

289289
assert sut.details_label.get_text().strip() == "Details for"

qui/updater/tests/test_summary_page.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def test_populate_restart_list(
201201
expected,
202202
real_builder,
203203
test_qapp,
204-
updatable_vms_list,
204+
updateable_vms_list,
205205
mock_next_button,
206206
mock_cancel_button,
207207
mock_settings,
@@ -224,7 +224,7 @@ def test_populate_restart_list(
224224
)
225225
sut.summary_list = mock_tree_view
226226

227-
for row in updatable_vms_list:
227+
for row in updateable_vms_list:
228228
row.set_status(UpdateStatus.Success)
229229
if row.vm.klass == "TemplateVM":
230230
for i, appvm in enumerate(row.vm.appvms):
@@ -233,7 +233,7 @@ def test_populate_restart_list(
233233
else:
234234
appvm.is_running = lambda *_args: False
235235

236-
sut.populate_restart_list(True, updatable_vms_list, mock_settings)
236+
sut.populate_restart_list(True, updateable_vms_list, mock_settings)
237237

238238
assert len(sut.list_store) == UP_VMS
239239
assert sum(row.selected for row in sut.list_store) == expected

qui/updater/tests/test_updater.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_setup_non_interactive_nothing_to_do(
4646
get_vms, select, subproc, _mock_logging, __mock_logging, test_qapp
4747
):
4848
sut = QubesUpdater(test_qapp, parse_args(("-n",), test_qapp))
49-
subproc.return_value = b""
49+
subproc.return_value = b"The admin VM will not be updated."
5050
get_vms.return_value = ()
5151
sut.perform_setup()
5252
select.assert_called_once()

qui/updater/updater.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def cell_data_func(_column, cell, model, it, data):
204204
if skip_intro_if_args(self.cliargs):
205205
self.log.info("Skipping intro page.")
206206
self.intro_page.select_rows_ignoring_conditions(
207-
cliargs=self.cliargs, dom0=self.qapp.domains["dom0"]
207+
cliargs=self.cliargs
208208
)
209209
if len(self.intro_page.get_vms_to_update()) == 0:
210210
self.do_nothing = True
@@ -492,8 +492,7 @@ def parse_args(args, app):
492492
parser.add_argument(
493493
"--dom0",
494494
action="store_true",
495-
help="Target dom0. "
496-
"If present, skip manual selection of qubes to update.",
495+
help="Target dom0. " "Short version of --targets dom0.",
497496
)
498497

499498
parser.add_argument(

0 commit comments

Comments
 (0)