*: Use action_utils.run instead of subprocess.run

- This is to capture stdout and stderr and transmit that from privileged daemon
back to the service to be displayed in HTML.

Tests:

- Unit tests and code checks pass.

- Some of the modified actions work as expected.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Veiko Aasa <veiko17@disroot.org>
This commit is contained in:
Sunil Mohan Adapa authored and Veiko Aasa committed 2025-09-29 16:58:53 +03:00
1 parent 355812c9f2
commit 61ff15a04f
36 files changed
+257 -244

No files matched your search

+33 -26
View File
@@ -81,54 +81,59 @@ def test_is_enabled(service_is_enabled, daemon):
@patch('subprocess.run')
def test_enable(subprocess_run, apps_init, app_list, mock_privileged, daemon):
"""Test that enabling the daemon works."""
common_args1 = dict(stdout=subprocess.PIPE, stderr=subprocess.PIPE,
check=False)
common_args2 = dict(stdout=subprocess.DEVNULL, stderr=subprocess.PIPE,
check=False)
daemon.enable()
subprocess_run.assert_has_calls(
[call(['systemctl', 'enable', 'test-unit'], check=False)])
[call(['systemctl', 'enable', 'test-unit'], **common_args1)])
subprocess_run.assert_any_call(['systemctl', 'start', 'test-unit'],
stdout=subprocess.DEVNULL, check=False)
**common_args2)
subprocess_run.reset_mock()
daemon.alias = 'test-unit-2'
daemon.enable()
subprocess_run.assert_has_calls([
call(['systemctl', 'enable', 'test-unit'], check=False),
call(['systemctl', 'start', 'test-unit'], stdout=subprocess.DEVNULL,
check=False),
call(['systemctl', 'enable', 'test-unit-2'], check=False),
call(['systemctl', 'start', 'test-unit-2'], stdout=subprocess.DEVNULL,
check=False),
call(['systemctl', 'enable', 'test-unit'], **common_args1),
call(['systemctl', 'start', 'test-unit'], **common_args2),
call(['systemctl', 'enable', 'test-unit-2'], **common_args1),
call(['systemctl', 'start', 'test-unit-2'], **common_args2),
])
subprocess_run.assert_any_call(['systemctl', 'start', 'test-unit'],
stdout=subprocess.DEVNULL, check=False)
**common_args2)
subprocess_run.assert_any_call(['systemctl', 'start', 'test-unit-2'],
stdout=subprocess.DEVNULL, check=False)
**common_args2)
@patch('plinth.app.apps_init')
@patch('subprocess.run')
def test_disable(subprocess_run, apps_init, app_list, mock_privileged, daemon):
"""Test that disabling the daemon works."""
common_args1 = dict(stdout=subprocess.PIPE, stderr=subprocess.PIPE,
check=False)
common_args2 = dict(stdout=subprocess.DEVNULL, stderr=subprocess.PIPE,
check=False)
daemon.disable()
subprocess_run.assert_has_calls(
[call(['systemctl', 'disable', 'test-unit'], check=False)])
[call(['systemctl', 'disable', 'test-unit'], **common_args1)])
subprocess_run.assert_any_call(['systemctl', 'stop', 'test-unit'],
stdout=subprocess.DEVNULL, check=False)
**common_args2)
subprocess_run.reset_mock()
daemon.alias = 'test-unit-2'
daemon.disable()
subprocess_run.assert_has_calls([
call(['systemctl', 'disable', 'test-unit'], check=False),
call(['systemctl', 'stop', 'test-unit'], stdout=subprocess.DEVNULL,
check=False),
call(['systemctl', 'disable', 'test-unit-2'], check=False),
call(['systemctl', 'stop', 'test-unit-2'], stdout=subprocess.DEVNULL,
check=False),
call(['systemctl', 'disable', 'test-unit'], **common_args1),
call(['systemctl', 'stop', 'test-unit'], **common_args2),
call(['systemctl', 'disable', 'test-unit-2'], **common_args1),
call(['systemctl', 'stop', 'test-unit-2'], **common_args2),
])
subprocess_run.assert_any_call(['systemctl', 'stop', 'test-unit'],
stdout=subprocess.DEVNULL, check=False)
**common_args2)
subprocess_run.assert_any_call(['systemctl', 'stop', 'test-unit-2'],
stdout=subprocess.DEVNULL, check=False)
**common_args2)
@patch('plinth.action_utils.service_is_running')
@@ -148,6 +153,10 @@ def test_is_running(service_is_running, daemon):
def test_ensure_running(subprocess_run, service_is_running, apps_init,
app_list, mock_privileged, daemon):
"""Test that checking that the daemon is running works."""
common_args1 = dict(stdout=subprocess.PIPE, stderr=subprocess.PIPE,
check=False)
common_args2 = dict(stdout=subprocess.DEVNULL, stderr=subprocess.PIPE,
check=False)
service_is_running.return_value = True
with daemon.ensure_running() as starting_state:
assert starting_state
@@ -159,16 +168,14 @@ def test_ensure_running(subprocess_run, service_is_running, apps_init,
with daemon.ensure_running() as starting_state:
assert not starting_state
assert subprocess_run.mock_calls == [
call(['systemctl', 'enable', 'test-unit'], check=False),
call(['systemctl', 'start', 'test-unit'],
stdout=subprocess.DEVNULL, check=False),
call(['systemctl', 'enable', 'test-unit'], **common_args1),
call(['systemctl', 'start', 'test-unit'], **common_args2),
]
subprocess_run.reset_mock()
assert subprocess_run.mock_calls == [
call(['systemctl', 'disable', 'test-unit'], check=False),
call(['systemctl', 'stop', 'test-unit'], stdout=subprocess.DEVNULL,
check=False),
call(['systemctl', 'disable', 'test-unit'], **common_args1),
call(['systemctl', 'stop', 'test-unit'], **common_args2),
]