daemon: Add new component to hold information about related daemons

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2021-11-17 10:44:27 -08:00 committed by James Valleroy
parent 9ec0c5f3db
commit 1c2a5f0825
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 39 additions and 2 deletions

View File

@ -107,6 +107,33 @@ class Daemon(app.LeaderComponent):
return [testname, result]
class RelatedDaemon(app.FollowerComponent):
"""Component to hold information about additional systemd units handled.
Unlike a daemon described by the Daemon component which is enabled/disabled
when the app is enabled/disabled, the daemon described by this component is
unaffected by the app's enabled/disabled status. The app only has an
indirect interest in this daemon.
This component primarily holds information about such daemon and does
nothing else. This information is used to check if the app is allowed to
perform operations on the daemon.
"""
def __init__(self, component_id, unit):
"""Initialize a new related daemon component.
'component_id' must be a unique string across all apps and components
of a app. Conventionally starts with 'related-daemon-'.
'unit' must the name of systemd unit.
"""
super().__init__(component_id)
self.unit = unit
def app_is_running(app_):
"""Return whether all the daemons in the app are running."""
for component in app_.components.values():

View File

@ -9,8 +9,8 @@ from unittest.mock import Mock, call, patch
import pytest
from plinth.app import App, FollowerComponent
from plinth.daemon import (Daemon, app_is_running, diagnose_netcat,
diagnose_port_listening)
from plinth.daemon import (Daemon, RelatedDaemon, app_is_running,
diagnose_netcat, diagnose_port_listening)
@pytest.fixture(name='daemon')
@ -228,3 +228,13 @@ def test_diagnose_netcat(popen):
result = diagnose_netcat('test-host', 3300, input='test-input',
negate=True)
assert result == ['Cannot connect to test-host:3300', 'passed']
def test_related_daemon_initialization():
"""Test that initializing related daemon works."""
component = RelatedDaemon('test-component', 'test-daemon')
assert component.component_id == 'test-component'
assert component.unit == 'test-daemon'
with pytest.raises(ValueError):
RelatedDaemon(None, 'test-daemon')