daemon: When ensuring running state handle not-installed state

Tests:

- Uninstall miniflux and postgresql. Install freshly with all the patches in
this series. When installing miniflux freshly, postgresql is not disabled soon
after miniflux package is installed. Without this patch, postgresql is disabled
after packages are installed leading to a setup failure.

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-09-23 16:25:28 -07:00 committed by James Valleroy
parent 4ed2a25a8b
commit f9ca06dc5f
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 18 additions and 2 deletions

View File

@ -91,6 +91,13 @@ class Daemon(app.LeaderComponent, log.LogEmitter):
def ensure_running(self):
"""Ensure a service is running and return to previous state."""
from plinth.privileged import service as service_privileged
if action_utils.service_show(self.unit)['LoadState'] == 'not-found':
# The service's package not installed yet, don't try to start it
# and later stop it after it is installed.
yield False # Not running
return
starting_state = self.is_running()
if not starting_state:
service_privileged.enable(self.unit)

View File

@ -145,12 +145,21 @@ def test_is_running(service_is_running, daemon):
@patch('plinth.app.apps_init')
@patch('plinth.action_utils.service_is_running')
@patch('plinth.action_utils.service_show')
@patch('subprocess.run')
def test_ensure_running(subprocess_run, service_is_running, apps_init,
app_list, mock_privileged, daemon):
def test_ensure_running(subprocess_run, service_show, service_is_running,
apps_init, app_list, mock_privileged, daemon):
"""Test that checking that the daemon is running works."""
common_args = dict(stdout=subprocess.PIPE, stderr=subprocess.PIPE,
check=False)
service_show.return_value = {'LoadState': 'not-found'}
with daemon.ensure_running() as starting_state:
assert not starting_state
assert subprocess_run.mock_calls == []
service_show.return_value = {'LoadState': 'loaded'}
service_is_running.return_value = True
with daemon.ensure_running() as starting_state:
assert starting_state