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) <noreply@anthropic.com>
This commit is contained in:
Roland 2026-05-24 18:54:11 -06:00 • committed by lrnselfreliance
parent f54b5f0dd8
commit e691f4db1e
2 changed files with 27 additions and 1 deletions

View file

@ -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:

View file

@ -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."""