*: Add type hints for diagnose method

Helps: #2410.

- Ensure that diagnostics methods and parameters are type checked so that we can
catch any potential issues.

- Move plinth/modules/diagnostics/check.py to plinth/diagnostic_check.py to
avoid many circular dependencies created. This is due to
plinth.modules.diagnostics automatically imported when
plinth.modules.diagnostics.check is imported. Also app.py is already (type)
dependent on diagnostic_check due to diagnose() method. To make the Check
classes independent of diagnostic module is okay.

Tests:

- Run make check-type.

- Run full diagnostics with following apps installed: torproxy, tor.
  - Test to netcat to 9051 in tor works.
  - Test 'port available for internal/external networks' in firewall works.
  - Test 'Package is latest' works.
  - Test 'Access url with proxy' in privoxy works.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
[jvalleroy: Also move tests for diagnostic_check]
Signed-off-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa authored and James Valleroy committed 2024-03-09 14:23:33 -05:00
1 parent f9b186e14f
commit 4b09d91f93
28 files changed
+161 -117

No files matched your search

+2 -2
View File
@@ -10,7 +10,7 @@ import pytest
from plinth.app import (App, Component, EnableState, FollowerComponent, Info,
LeaderComponent, apps_init)
from plinth.modules.diagnostics.check import DiagnosticCheck, Result
from plinth.diagnostic_check import DiagnosticCheck, Result
# pylint: disable=protected-access
@@ -34,7 +34,7 @@ class LeaderTest(FollowerComponent):
"""Test class for using LeaderComponent in tests."""
is_leader = True
def diagnose(self):
def diagnose(self) -> list[DiagnosticCheck]:
"""Return diagnostic results."""
return [
DiagnosticCheck('test-result-' + self.component_id,
+1 -1
View File
@@ -9,7 +9,7 @@ import pytest
from plinth.app import App
from plinth.config import DropinConfigs
from plinth.modules.diagnostics.check import DiagnosticCheck, Result
from plinth.diagnostic_check import DiagnosticCheck, Result
pytestmark = pytest.mark.usefixtures('mock_privileged')
privileged_modules_to_mock = ['plinth.privileged.config']
+11 -11
View File
@@ -12,7 +12,7 @@ import pytest
from plinth.app import App, FollowerComponent, Info
from plinth.daemon import (Daemon, RelatedDaemon, SharedDaemon, app_is_running,
diagnose_netcat, diagnose_port_listening)
from plinth.modules.diagnostics.check import DiagnosticCheck, Result
from plinth.diagnostic_check import DiagnosticCheck, Result
privileged_modules_to_mock = ['plinth.privileged.service']
@@ -295,32 +295,32 @@ def test_diagnose_port_listening(connections):
def test_diagnose_netcat(popen):
"""Test running diagnostic test using netcat."""
popen().returncode = 0
result = diagnose_netcat('test-host', 3300, input='test-input')
result = diagnose_netcat('test-host', 3300, remote_input='test-input')
parameters = {'host': 'test-host', 'port': 3300, 'negate': False}
assert result == DiagnosticCheck('daemon-netcat-test-host-3300',
'Connect to test-host:3300',
Result.PASSED, parameters)
'Connect to {host}:{port}', Result.PASSED,
parameters)
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',
result = diagnose_netcat('test-host', 3300, remote_input='test-input',
negate=True)
parameters2 = parameters.copy()
parameters2['negate'] = True
assert result == DiagnosticCheck('daemon-netcat-negate-test-host-3300',
'Cannot connect to test-host:3300',
'Cannot connect to {host}:{port}',
Result.FAILED, parameters2)
popen().returncode = 1
result = diagnose_netcat('test-host', 3300, input='test-input')
result = diagnose_netcat('test-host', 3300, remote_input='test-input')
assert result == DiagnosticCheck('daemon-netcat-test-host-3300',
'Connect to test-host:3300',
Result.FAILED, parameters)
'Connect to {host}:{port}', Result.FAILED,
parameters)
result = diagnose_netcat('test-host', 3300, input='test-input',
result = diagnose_netcat('test-host', 3300, remote_input='test-input',
negate=True)
assert result == DiagnosticCheck('daemon-netcat-negate-test-host-3300',
'Cannot connect to test-host:3300',
'Cannot connect to {host}:{port}',
Result.PASSED, parameters2)
+67
View File
@@ -0,0 +1,67 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Tests for diagnostic check data type."""
import json
import pytest
from plinth.diagnostic_check import (CheckJSONDecoder, CheckJSONEncoder,
DiagnosticCheck, Result)
def test_result():
"""Test result enum type."""
assert Result.__members__['ERROR'].name == 'ERROR'
assert Result.__members__['ERROR'].value == 'error'
assert Result.NOT_DONE == 'not_done'
assert Result.PASSED == 'passed'
assert Result.WARNING == 'warning'
assert Result.FAILED == 'failed'
assert Result.ERROR == 'error'
def test_diagnostic_check():
"""Test the diagnostic check data class."""
with pytest.raises(TypeError):
DiagnosticCheck()
check = DiagnosticCheck('some-check-id', 'sample check')
assert check.check_id == 'some-check-id'
assert check.description == 'sample check'
assert check.translated_description == 'sample check'
assert check.result == Result.NOT_DONE
assert not check.parameters
check = DiagnosticCheck('some-check-id', 'sample check', Result.PASSED)
assert check.result == Result.PASSED
assert not check.parameters
check = DiagnosticCheck('some-check-id', 'sample check', Result.PASSED,
{'key': 'value'})
assert check.parameters['key'] == 'value'
def test_translate():
"""Test formatting the translated description."""
check = DiagnosticCheck('some-check-id', 'sample check {key}',
Result.FAILED, {'key': 'value'})
assert check.translated_description == 'sample check value'
check = DiagnosticCheck('some-check-id', 'sample check {missing}',
Result.PASSED, {'key': 'value'})
assert check.translated_description == 'sample check ?missing?'
def test_json_encoder_decoder():
"""Test encoding and decoding as JSON."""
check = DiagnosticCheck('some-check-id', 'sample check', Result.PASSED)
check_json = json.dumps(check, cls=CheckJSONEncoder)
for string in [
'"check_id": "some-check-id"', '"description": "sample check"',
'"result": "passed"', '"parameters": {}',
'"__class__": "DiagnosticCheck"'
]:
assert string in check_json
decoded_check = json.loads(check_json, cls=CheckJSONDecoder)
assert decoded_check == check
+1 -1
View File
@@ -9,8 +9,8 @@ from unittest.mock import Mock, call, patch
import pytest
from plinth.app import App
from plinth.diagnostic_check import DiagnosticCheck, Result
from plinth.errors import MissingPackageError
from plinth.modules.diagnostics.check import DiagnosticCheck, Result
from plinth.package import Package, Packages, packages_installed