mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
diagnostics: Add DiagnosticCheck dataclass
- Set unique check_id for each diagnostic check. - Result is a string-based enumeration. The default value (NOT_DONE) can be used for diagnostic checks that have not been completed yet. - Result is StrEnum so that the return value of check_url can still be used directly as a diagnostic result. Closes: #2375 Signed-off-by: James Valleroy <jvalleroy@mailbox.org> Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
19 files changed
+284
-183
No files matched your search
@@ -10,6 +10,7 @@ import pytest
|
||||
|
||||
from plinth.app import (App, Component, EnableState, FollowerComponent, Info,
|
||||
LeaderComponent, apps_init)
|
||||
from plinth.modules.diagnostics.check import DiagnosticCheck, Result
|
||||
|
||||
# pylint: disable=protected-access
|
||||
|
||||
@@ -35,7 +36,10 @@ class LeaderTest(FollowerComponent):
|
||||
|
||||
def diagnose(self):
|
||||
"""Return diagnostic results."""
|
||||
return [('test-result-' + self.component_id, 'success')]
|
||||
return [
|
||||
DiagnosticCheck('test-result-' + self.component_id,
|
||||
'test-result-' + self.component_id, Result.PASSED)
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture(name='app_with_components')
|
||||
@@ -245,8 +249,12 @@ def test_app_set_enabled(app_with_components):
|
||||
def test_app_diagnose(app_with_components):
|
||||
"""Test running diagnostics on an app."""
|
||||
results = app_with_components.diagnose()
|
||||
assert results == [('test-result-test-leader-1', 'success'),
|
||||
('test-result-test-leader-2', 'success')]
|
||||
assert results[0].check_id == 'test-result-test-leader-1'
|
||||
assert results[0].description == 'test-result-test-leader-1'
|
||||
assert results[0].result == Result.PASSED
|
||||
assert results[1].check_id == 'test-result-test-leader-2'
|
||||
assert results[1].description == 'test-result-test-leader-2'
|
||||
assert results[1].result == Result.PASSED
|
||||
|
||||
|
||||
def test_app_has_diagnostics(app_with_components):
|
||||
@@ -262,7 +270,9 @@ def test_app_has_diagnostics(app_with_components):
|
||||
assert not app.has_diagnostics()
|
||||
|
||||
# App with app-level diagnostics
|
||||
with patch.object(AppTest, 'diagnose', return_value=[('test1', 'passed')]):
|
||||
with patch.object(
|
||||
AppTest, 'diagnose',
|
||||
return_value=[DiagnosticCheck('test1', 'test1', Result.PASSED)]):
|
||||
assert app.has_diagnostics()
|
||||
|
||||
|
||||
|
||||
+13
-13
@@ -160,32 +160,32 @@ def test_dropin_config_diagnose_symlinks(dropin_configs, tmp_path):
|
||||
with patch('plinth.config.DropinConfigs.ROOT', new=tmp_path):
|
||||
# Nothing exists
|
||||
results = dropin_configs.diagnose()
|
||||
assert results[0][1] == 'failed'
|
||||
assert results[1][1] == 'failed'
|
||||
assert results[0].result == 'failed'
|
||||
assert results[1].result == 'failed'
|
||||
|
||||
# Proper symlinks exist
|
||||
dropin_configs.enable()
|
||||
results = dropin_configs.diagnose()
|
||||
assert results[0][1] == 'passed'
|
||||
assert results[1][1] == 'passed'
|
||||
assert results[0].result == 'passed'
|
||||
assert results[1].result == 'passed'
|
||||
|
||||
# A file exists instead of symlink
|
||||
dropin_configs.disable()
|
||||
etc_path = DropinConfigs._get_etc_path('/etc/test/path1')
|
||||
etc_path.touch()
|
||||
results = dropin_configs.diagnose()
|
||||
assert results[0][1] == 'failed'
|
||||
assert results[0].result == 'failed'
|
||||
|
||||
# Symlink points to wrong location
|
||||
dropin_configs.disable()
|
||||
etc_path.symlink_to('/blah')
|
||||
results = dropin_configs.diagnose()
|
||||
assert results[0][1] == 'failed'
|
||||
assert results[0].result == 'failed'
|
||||
|
||||
# Symlink is recreated
|
||||
dropin_configs.enable()
|
||||
results = dropin_configs.diagnose()
|
||||
assert results[0][1] == 'passed'
|
||||
assert results[0].result == 'passed'
|
||||
|
||||
|
||||
def test_dropin_config_diagnose_copy_only(dropin_configs, tmp_path):
|
||||
@@ -199,24 +199,24 @@ def test_dropin_config_diagnose_copy_only(dropin_configs, tmp_path):
|
||||
|
||||
# Nothing exists
|
||||
results = dropin_configs.diagnose()
|
||||
assert results[0][1] == 'failed'
|
||||
assert results[1][1] == 'failed'
|
||||
assert results[0].result == 'failed'
|
||||
assert results[1].result == 'failed'
|
||||
|
||||
# Proper copies exist
|
||||
dropin_configs.enable()
|
||||
results = dropin_configs.diagnose()
|
||||
assert results[0][1] == 'passed'
|
||||
assert results[1][1] == 'passed'
|
||||
assert results[0].result == 'passed'
|
||||
assert results[1].result == 'passed'
|
||||
|
||||
# A symlink exists instead of a copied file
|
||||
dropin_configs.disable()
|
||||
etc_path = DropinConfigs._get_etc_path('/etc/test/path1')
|
||||
etc_path.symlink_to('/blah')
|
||||
results = dropin_configs.diagnose()
|
||||
assert results[0][1] == 'failed'
|
||||
assert results[0].result == 'failed'
|
||||
|
||||
# Copied file contains wrong contents
|
||||
dropin_configs.disable()
|
||||
etc_path.write_text('x-invalid-contents')
|
||||
results = dropin_configs.diagnose()
|
||||
assert results[0][1] == 'failed'
|
||||
assert results[0].result == 'failed'
|
||||
+44
-31
@@ -12,6 +12,7 @@ import pytest
|
||||
from plinth.app import App, FollowerComponent, Info
|
||||
from plinth.daemon import (Daemon, RelatedDaemon, app_is_running,
|
||||
diagnose_netcat, diagnose_port_listening)
|
||||
from plinth.modules.diagnostics.check import DiagnosticCheck
|
||||
|
||||
privileged_modules_to_mock = ['plinth.privileged.service']
|
||||
|
||||
@@ -140,22 +141,26 @@ def test_diagnose(port_listening, service_is_running, daemon):
|
||||
"""Test running diagnostics."""
|
||||
|
||||
def side_effect(port, kind):
|
||||
return [f'test-result-{port}-{kind}', 'passed']
|
||||
name = f'test-result-{port}-{kind}'
|
||||
return DiagnosticCheck(name, name, 'passed')
|
||||
|
||||
daemon = Daemon('test-daemon', 'test-unit', listen_ports=[(8273, 'tcp4'),
|
||||
(345, 'udp')])
|
||||
port_listening.side_effect = side_effect
|
||||
service_is_running.return_value = True
|
||||
results = daemon.diagnose()
|
||||
assert results == [['Service test-unit is running', 'passed'],
|
||||
['test-result-8273-tcp4', 'passed'],
|
||||
['test-result-345-udp', 'passed']]
|
||||
assert results[0].description == 'Service test-unit is running'
|
||||
assert results[0].result == 'passed'
|
||||
assert results[1].description == 'test-result-8273-tcp4'
|
||||
assert results[1].result == 'passed'
|
||||
assert results[2].description == 'test-result-345-udp'
|
||||
assert results[2].result == 'passed'
|
||||
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'
|
||||
assert results[0].result == 'failed'
|
||||
|
||||
|
||||
@patch('plinth.action_utils.service_is_running')
|
||||
@@ -207,15 +212,19 @@ def test_diagnose_port_listening(connections):
|
||||
|
||||
# Check that message is correct
|
||||
results = diagnose_port_listening(1234)
|
||||
assert results == ['Listening on tcp port 1234', 'passed']
|
||||
assert results.description == 'Listening on tcp port 1234'
|
||||
assert results.result == 'passed'
|
||||
results = diagnose_port_listening(1234, 'tcp', '0.0.0.0')
|
||||
assert results == ['Listening on tcp port 0.0.0.0:1234', 'passed']
|
||||
assert results.description == 'Listening on tcp port 0.0.0.0:1234'
|
||||
assert results.result == 'passed'
|
||||
|
||||
# Failed results
|
||||
results = diagnose_port_listening(4321)
|
||||
assert results == ['Listening on tcp port 4321', 'failed']
|
||||
assert results.description == 'Listening on tcp port 4321'
|
||||
assert results.result == 'failed'
|
||||
results = diagnose_port_listening(4321, 'tcp', '0.0.0.0')
|
||||
assert results == ['Listening on tcp port 0.0.0.0:4321', 'failed']
|
||||
assert results.description == 'Listening on tcp port 0.0.0.0:4321'
|
||||
assert results.result == 'failed'
|
||||
|
||||
# Check if psutil call is being made with right argument
|
||||
results = diagnose_port_listening(1234, 'tcp')
|
||||
@@ -232,26 +241,26 @@ def test_diagnose_port_listening(connections):
|
||||
connections.assert_called_with('udp6')
|
||||
|
||||
# TCP
|
||||
assert diagnose_port_listening(1234)[1] == 'passed'
|
||||
assert diagnose_port_listening(1000)[1] == 'failed'
|
||||
assert diagnose_port_listening(2345)[1] == 'failed'
|
||||
assert diagnose_port_listening(1234, 'tcp', '0.0.0.0')[1] == 'passed'
|
||||
assert diagnose_port_listening(1234, 'tcp', '1.1.1.1')[1] == 'failed'
|
||||
assert diagnose_port_listening(1234, 'tcp6')[1] == 'passed'
|
||||
assert diagnose_port_listening(1234, 'tcp4')[1] == 'passed'
|
||||
assert diagnose_port_listening(6789, 'tcp4')[1] == 'passed'
|
||||
assert diagnose_port_listening(5678, 'tcp4')[1] == 'failed'
|
||||
assert diagnose_port_listening(1234).result == 'passed'
|
||||
assert diagnose_port_listening(1000).result == 'failed'
|
||||
assert diagnose_port_listening(2345).result == 'failed'
|
||||
assert diagnose_port_listening(1234, 'tcp', '0.0.0.0').result == 'passed'
|
||||
assert diagnose_port_listening(1234, 'tcp', '1.1.1.1').result == 'failed'
|
||||
assert diagnose_port_listening(1234, 'tcp6').result == 'passed'
|
||||
assert diagnose_port_listening(1234, 'tcp4').result == 'passed'
|
||||
assert diagnose_port_listening(6789, 'tcp4').result == 'passed'
|
||||
assert diagnose_port_listening(5678, 'tcp4').result == 'failed'
|
||||
|
||||
# UDP
|
||||
assert diagnose_port_listening(3456, 'udp')[1] == 'passed'
|
||||
assert diagnose_port_listening(3000, 'udp')[1] == 'failed'
|
||||
assert diagnose_port_listening(4567, 'udp')[1] == 'failed'
|
||||
assert diagnose_port_listening(3456, 'udp', '0.0.0.0')[1] == 'passed'
|
||||
assert diagnose_port_listening(3456, 'udp', '1.1.1.1')[1] == 'failed'
|
||||
assert diagnose_port_listening(3456, 'udp6')[1] == 'passed'
|
||||
assert diagnose_port_listening(3456, 'udp4')[1] == 'passed'
|
||||
assert diagnose_port_listening(6789, 'udp4')[1] == 'passed'
|
||||
assert diagnose_port_listening(5678, 'udp4')[1] == 'failed'
|
||||
assert diagnose_port_listening(3456, 'udp').result == 'passed'
|
||||
assert diagnose_port_listening(3000, 'udp').result == 'failed'
|
||||
assert diagnose_port_listening(4567, 'udp').result == 'failed'
|
||||
assert diagnose_port_listening(3456, 'udp', '0.0.0.0').result == 'passed'
|
||||
assert diagnose_port_listening(3456, 'udp', '1.1.1.1').result == 'failed'
|
||||
assert diagnose_port_listening(3456, 'udp6').result == 'passed'
|
||||
assert diagnose_port_listening(3456, 'udp4').result == 'passed'
|
||||
assert diagnose_port_listening(6789, 'udp4').result == 'passed'
|
||||
assert diagnose_port_listening(5678, 'udp4').result == 'failed'
|
||||
|
||||
|
||||
@patch('subprocess.Popen')
|
||||
@@ -259,21 +268,25 @@ def test_diagnose_netcat(popen):
|
||||
"""Test running diagnostic test using netcat."""
|
||||
popen().returncode = 0
|
||||
result = diagnose_netcat('test-host', 3300, input='test-input')
|
||||
assert result == ['Connect to test-host:3300', 'passed']
|
||||
assert result.description == 'Connect to test-host:3300'
|
||||
assert result.result == 'passed'
|
||||
assert popen.mock_calls[1][1] == (['nc', 'test-host', '3300'], )
|
||||
assert popen.mock_calls[2] == call().communicate(input=b'test-input')
|
||||
|
||||
result = diagnose_netcat('test-host', 3300, input='test-input',
|
||||
negate=True)
|
||||
assert result == ['Cannot connect to test-host:3300', 'failed']
|
||||
assert result.description == 'Cannot connect to test-host:3300'
|
||||
assert result.result == 'failed'
|
||||
|
||||
popen().returncode = 1
|
||||
result = diagnose_netcat('test-host', 3300, input='test-input')
|
||||
assert result == ['Connect to test-host:3300', 'failed']
|
||||
assert result.description == 'Connect to test-host:3300'
|
||||
assert result.result == 'failed'
|
||||
|
||||
result = diagnose_netcat('test-host', 3300, input='test-input',
|
||||
negate=True)
|
||||
assert result == ['Cannot connect to test-host:3300', 'passed']
|
||||
assert result.description == 'Cannot connect to test-host:3300'
|
||||
assert result.result == 'passed'
|
||||
|
||||
|
||||
def test_related_daemon_initialization():
|
||||
|
||||
@@ -238,16 +238,16 @@ def test_diagnose(cache):
|
||||
Package('package6') | Package('package7')
|
||||
])
|
||||
results = component.diagnose()
|
||||
assert 'not available for install' in results[0][0]
|
||||
assert results[0][1] == 'failed'
|
||||
assert '(2.0)' in results[1][0]
|
||||
assert results[1][1] == 'passed'
|
||||
assert '(3.0)' in results[2][0]
|
||||
assert results[2][1] == 'warning'
|
||||
assert 'not available for install' in results[3][0]
|
||||
assert results[3][1] == 'failed'
|
||||
assert '(4.0)' in results[4][0]
|
||||
assert results[4][1] == 'passed'
|
||||
assert 'not available for install' in results[0].description
|
||||
assert results[0].result == 'failed'
|
||||
assert '(2.0)' in results[1].description
|
||||
assert results[1].result == 'passed'
|
||||
assert '(3.0)' in results[2].description
|
||||
assert results[2].result == 'warning'
|
||||
assert 'not available for install' in results[3].description
|
||||
assert results[3].result == 'failed'
|
||||
assert '(4.0)' in results[4].description
|
||||
assert results[4].result == 'passed'
|
||||
|
||||
|
||||
@patch('plinth.package.packages_installed')
|
||||
|
||||
Reference in new issue
Block a user