upgrades: Don't ignore apt error during distribution upgrade

- This is important because only if all the command succeed, the changes to
/etc/apt/sources.list file are committed.

Tests:

- Set the time to 2025-09-20. Distribution updates are triggered. 'apt update'
fails due an mismatch with release file's timestamp. Instead of proceeding, the
distribution upgrade is halted.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2025-04-03 16:15:50 -07:00 committed by James Valleroy
parent 44b4c38d8a
commit b6f0e7f323
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 11 additions and 4 deletions

View File

@ -74,8 +74,11 @@ distribution_info: dict = {
def _apt_run(arguments: list[str], enable_triggers: bool = False):
"""Run an apt command and ensure that output is written to stdout."""
return action_utils.run_apt_command(arguments, stdout=None,
enable_triggers=enable_triggers)
returncode = action_utils.run_apt_command(arguments, stdout=None,
enable_triggers=enable_triggers)
if returncode:
raise RuntimeError(
f'Apt command failed with return code: {returncode}')
def _sources_list_update(old_codename: str, new_codename: str):

View File

@ -19,13 +19,17 @@ from plinth.modules.upgrades import distupgrade
@patch('subprocess.run')
def test_apt_run(run):
"""Test that running apt command logs properly."""
run.return_value.returncode = 10
run.return_value.returncode = 0
args = ['command', 'arg1', 'arg2']
assert distupgrade._apt_run(args) == 10
distupgrade._apt_run(args)
assert run.call_args.args == \
(['apt-get', '--assume-yes', '--quiet=2'] + args,)
assert not run.call_args.kwargs['stdout']
run.return_value.returncode = 10
with pytest.raises(RuntimeError):
distupgrade._apt_run(args)
def test_sources_list_update(tmp_path):
"""Test that updating a sources file works."""