From 6d2f4b6cea368e51c5e996222b8df4380f18da6f Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Thu, 7 May 2020 20:15:50 -0700 Subject: [PATCH] daemon: Allow using an alias when enabling a daemon Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/daemon.py | 16 +++++++++++++++- plinth/tests/test_daemon.py | 12 +++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/plinth/daemon.py b/plinth/daemon.py index 4644d17b2..3b80d8a2a 100644 --- a/plinth/daemon.py +++ b/plinth/daemon.py @@ -15,7 +15,7 @@ from plinth import action_utils, actions, app class Daemon(app.LeaderComponent): """Component to manage a background daemon or any systemd unit.""" def __init__(self, component_id, unit, strict_check=False, - listen_ports=None): + listen_ports=None, alias=None): """Initialize a new daemon component. 'component_id' must be a unique string across all apps and components @@ -28,12 +28,24 @@ class Daemon(app.LeaderComponent): 'tcp', 'udp4', 'udp6', 'udp' indicating the protocol that the daemon listens on. This information is used to run diagnostic tests. + 'alias' is an alternate name for the same unit file. When a unit file + is renamed, the new unit file usually contains an Alias= setting in + [Install] section with value of old unit name. When the unit is + enabled, a symlink with the name of the alias is created. All + operations such as is-enabled, is-running and disable work with the + alias along with the primary unit name. However, for the case of + enabling the unit file, the alias does not work. To be able to provide + management for multiple versions of the unit file with different names, + specify an alias. Both the names are taken into consideration when + enabling the unit file. + """ super().__init__(component_id) self.unit = unit self.strict_check = strict_check self.listen_ports = listen_ports or [] + self.alias = alias def is_enabled(self): """Return if the daemon/unit is enabled.""" @@ -43,6 +55,8 @@ class Daemon(app.LeaderComponent): def enable(self): """Run operations to enable the daemon/unit.""" actions.superuser_run('service', ['enable', self.unit]) + if self.alias: + actions.superuser_run('service', ['enable', self.alias]) def disable(self): """Run operations to disable the daemon/unit.""" diff --git a/plinth/tests/test_daemon.py b/plinth/tests/test_daemon.py index e9e49edbc..8fc1018eb 100644 --- a/plinth/tests/test_daemon.py +++ b/plinth/tests/test_daemon.py @@ -29,12 +29,14 @@ def test_initialization(): assert daemon.unit == 'test-unit' assert not daemon.strict_check assert daemon.listen_ports == [] + assert daemon.alias is None listen_ports = [(345, 'tcp4'), (123, 'udp')] daemon = Daemon('test-daemon', 'test-unit', strict_check=True, - listen_ports=listen_ports) + listen_ports=listen_ports, alias='test-unit-2') assert daemon.strict_check assert daemon.listen_ports == listen_ports + assert daemon.alias == 'test-unit-2' @patch('plinth.action_utils.service_is_enabled') @@ -60,6 +62,13 @@ def test_enable(superuser_run, daemon): daemon.enable() superuser_run.assert_has_calls([call('service', ['enable', 'test-unit'])]) + daemon.alias = 'test-unit-2' + daemon.enable() + superuser_run.assert_has_calls([ + call('service', ['enable', 'test-unit']), + call('service', ['enable', 'test-unit-2']) + ]) + @patch('plinth.actions.superuser_run') def test_disable(superuser_run, daemon): @@ -83,6 +92,7 @@ def test_is_running(service_is_running, daemon): @patch('plinth.daemon.diagnose_port_listening') def test_diagnose(port_listening, service_is_running, daemon): """Test running diagnostics.""" + def side_effect(port, kind): return [f'test-result-{port}-{kind}', 'passed']