mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
*: 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:
1 parent
f9b186e14f
commit
4b09d91f93
28 files changed
+161
-117
No files matched your search
@@ -17,11 +17,12 @@ from django.utils.translation import gettext_noop
|
||||
from plinth import app as app_module
|
||||
from plinth import daemon, glib, kvstore, menu
|
||||
from plinth import operation as operation_module
|
||||
from plinth.diagnostic_check import (CheckJSONDecoder, CheckJSONEncoder,
|
||||
DiagnosticCheck, Result)
|
||||
from plinth.modules.apache.components import diagnose_url_on_all
|
||||
from plinth.modules.backups.components import BackupRestore
|
||||
|
||||
from . import manifest
|
||||
from .check import CheckJSONDecoder, CheckJSONEncoder, Result
|
||||
|
||||
_description = [
|
||||
_('The system diagnostic test will run a number of checks on your '
|
||||
@@ -75,7 +76,7 @@ class DiagnosticsApp(app_module.App):
|
||||
super().setup(old_version)
|
||||
self.enable()
|
||||
|
||||
def diagnose(self):
|
||||
def diagnose(self) -> list[DiagnosticCheck]:
|
||||
"""Run diagnostics and return the results."""
|
||||
results = super().diagnose()
|
||||
results.append(daemon.diagnose_port_listening(8000, 'tcp4'))
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""Diagnostic check data type."""
|
||||
|
||||
import dataclasses
|
||||
import json
|
||||
from dataclasses import dataclass, field
|
||||
from enum import StrEnum
|
||||
|
||||
from django.utils.translation import gettext
|
||||
|
||||
from plinth.utils import SafeFormatter
|
||||
|
||||
|
||||
class Result(StrEnum):
|
||||
"""The result of a diagnostic check."""
|
||||
NOT_DONE = 'not_done'
|
||||
PASSED = 'passed'
|
||||
WARNING = 'warning'
|
||||
FAILED = 'failed'
|
||||
ERROR = 'error'
|
||||
|
||||
|
||||
@dataclass
|
||||
class DiagnosticCheck:
|
||||
"""A diagnostic check and optional result and parameters."""
|
||||
check_id: str
|
||||
description: str
|
||||
result: Result = Result.NOT_DONE
|
||||
parameters: dict = field(default_factory=dict)
|
||||
|
||||
@property
|
||||
def translated_description(self):
|
||||
"""Return translated string for description."""
|
||||
description = gettext(self.description)
|
||||
if self.parameters:
|
||||
return SafeFormatter().vformat(description, [], self.parameters)
|
||||
|
||||
return description
|
||||
|
||||
|
||||
class CheckJSONEncoder(json.JSONEncoder):
|
||||
"""Encode objects that include DiagnosticChecks."""
|
||||
|
||||
def default(self, o):
|
||||
"""Add class tag to DiagnosticChecks."""
|
||||
if isinstance(o, DiagnosticCheck):
|
||||
o = dataclasses.asdict(o)
|
||||
o.update({'__class__': 'DiagnosticCheck'})
|
||||
return o
|
||||
|
||||
return super().default(o)
|
||||
|
||||
|
||||
class CheckJSONDecoder(json.JSONDecoder):
|
||||
"""Decode objects that include DiagnosticChecks."""
|
||||
|
||||
def __init__(self):
|
||||
json.JSONDecoder.__init__(self, object_hook=CheckJSONDecoder.from_dict)
|
||||
|
||||
@staticmethod
|
||||
def from_dict(data):
|
||||
"""Convert tagged data to DiagnosticCheck."""
|
||||
if data.get('__class__') == 'DiagnosticCheck':
|
||||
return DiagnosticCheck(data['check_id'], data['description'],
|
||||
data['result'], data['parameters'])
|
||||
|
||||
return data
|
||||
@@ -1,68 +0,0 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""Tests for diagnostic check data type."""
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from plinth.modules.diagnostics.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
|
||||
@@ -14,10 +14,10 @@ from django.views.generic import TemplateView
|
||||
|
||||
from plinth import operation
|
||||
from plinth.app import App
|
||||
from plinth.diagnostic_check import Result
|
||||
from plinth.modules import diagnostics
|
||||
from plinth.views import AppView
|
||||
|
||||
from .check import Result
|
||||
from .forms import ConfigureForm
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
Reference in new issue
Block a user