From e691f4db1ef2d83f256da095e409a41d10408225 Mon Sep 17 00:00:00 2001 From: Roland Date: Sun, 24 May 2026 18:54:11 -0600 Subject: [PATCH] Fixing kiwix restart cmd: missing comma silently concatenated two argv entries. `cmd = ('sudo', '/usr/bin/systemctl', 'restart' 'wrolpi-kiwix.service')` ^ missing comma here Python implicit string-literal concatenation joined the two strings into 'restartwrolpi-kiwix.service', so systemctl saw one argument it didn't recognise and exited 2 (INVALIDARGUMENT). Net effect: kiwix never restarted after a Zim download, the user still had to manually `sudo systemctl restart wrolpi-kiwix` to see the new Zim. Adds a unit test that pins the argv shape so an adjacent-literal typo here surfaces in CI. Co-Authored-By: Claude Opus 4.7 (1M context) --- modules/zim/lib.py | 2 +- modules/zim/test/test_restart_kiwix.py | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/modules/zim/lib.py b/modules/zim/lib.py index 2e791410..b0e8b196 100644 --- a/modules/zim/lib.py +++ b/modules/zim/lib.py @@ -638,7 +638,7 @@ async def restart_kiwix(): logger.info('Restarting Kiwix serve') - cmd = ('sudo', '/usr/bin/systemctl', 'restart' 'wrolpi-kiwix.service') + cmd = ('sudo', '/usr/bin/systemctl', 'restart', 'wrolpi-kiwix.service') result = await run_command(cmd) logger.debug(f'systemctl returned {result.return_code}') if result.return_code != 0 and result.stderr: diff --git a/modules/zim/test/test_restart_kiwix.py b/modules/zim/test/test_restart_kiwix.py index f9b68065..ad310e60 100644 --- a/modules/zim/test/test_restart_kiwix.py +++ b/modules/zim/test/test_restart_kiwix.py @@ -1,4 +1,6 @@ """Tests for the restart_kiwix switch handler triggered by model_zim().""" +from unittest import mock + import pytest from modules.zim import lib @@ -7,6 +9,30 @@ from wrolpi.api_utils import api_app from wrolpi.files.models import FileGroup +@pytest.mark.asyncio +async def test_restart_kiwix_systemctl_command_shape(): + """restart_kiwix() must call systemctl with `restart` and the unit name as + separate argv entries. Adjacent-string-literal typos (missing comma in + a Python tuple) silently produce 'restartwrolpi-kiwix.service' as a single + argument, which systemctl rejects.""" + captured = {} + + async def fake_run_command(cmd): + captured['cmd'] = cmd + return mock.MagicMock(return_code=0, stderr=b'') + + with mock.patch.object(lib, 'PYTEST', False), \ + mock.patch.object(lib, 'DOCKERIZED', False), \ + mock.patch.object(lib, 'run_command', side_effect=fake_run_command): + await lib.restart_kiwix() + + cmd = captured['cmd'] + assert 'restart' in cmd + assert 'wrolpi-kiwix.service' in cmd + # 'restart' and the unit must NOT be concatenated into one argv entry. + assert 'restartwrolpi-kiwix.service' not in cmd + + @pytest.mark.asyncio async def test_model_zim_activates_restart_switch(async_client, test_session, zim_path_factory): """model_zim() should activate the restart_kiwix switch when creating a new Zim."""