From 68bc2f45a598f34c96d0dd7018862ab14adb9e90 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Wed, 18 Dec 2019 12:25:49 -0800 Subject: [PATCH] daemon: Implement diagnostic test to check if a daemon is running Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/daemon.py | 14 +++++++++++++- plinth/tests/test_daemon.py | 12 ++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/plinth/daemon.py b/plinth/daemon.py index 352a75a30..4e0bece32 100644 --- a/plinth/daemon.py +++ b/plinth/daemon.py @@ -62,14 +62,26 @@ class Daemon(app.LeaderComponent): return action_utils.service_is_running(self.unit) def diagnose(self): - """Check if the daemon is listening on expected ports.""" + """Check if the daemon is running and listening on expected ports. + + See :py:meth:`plinth.app.Component.diagnose`. + + """ results = [] + results.append(self._diagnose_unit_is_running()) for port in self.listen_ports: results.append( action_utils.diagnose_port_listening(port[0], port[1])) return results + def _diagnose_unit_is_running(self): + """Check if a daemon is running.""" + message = _('Service {service_name} is running').format( + service_name=self.unit) + result = 'passed' if self.is_running() else 'failed' + return [message, result] + def app_is_running(app_): """Return whether all the daemons in the app are running.""" diff --git a/plinth/tests/test_daemon.py b/plinth/tests/test_daemon.py index 62c1f22d7..a2101a86c 100644 --- a/plinth/tests/test_daemon.py +++ b/plinth/tests/test_daemon.py @@ -92,8 +92,9 @@ def test_is_running(service_is_running, daemon): assert not daemon.is_running() +@patch('plinth.action_utils.service_is_running') @patch('plinth.action_utils.diagnose_port_listening') -def test_diagnose(diagnose_port_listening, daemon): +def test_diagnose(diagnose_port_listening, service_is_running, daemon): """Test running diagnostics.""" def side_effect(port, kind): return [f'test-result-{port}-{kind}', 'passed'] @@ -101,11 +102,18 @@ def test_diagnose(diagnose_port_listening, daemon): daemon = Daemon('test-daemon', 'test-unit', listen_ports=[(8273, 'tcp4'), (345, 'udp')]) diagnose_port_listening.side_effect = side_effect + service_is_running.return_value = True results = daemon.diagnose() - assert results == [['test-result-8273-tcp4', 'passed'], + assert results == [['Service test-unit is running', 'passed'], + ['test-result-8273-tcp4', 'passed'], ['test-result-345-udp', 'passed']] diagnose_port_listening.assert_has_calls( [call(8273, 'tcp4'), call(345, 'udp')]) + service_is_running.assert_has_calls([call('test-unit')]) + + service_is_running.return_value = False + results = daemon.diagnose() + assert results[0][1] == 'failed' @patch('plinth.action_utils.service_is_running')