*: 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

+5 -4
View File
@@ -11,6 +11,7 @@ from plinth import action_utils
from plinth import app as app_module
from plinth import cfg, frontpage, menu
from plinth.daemon import Daemon
from plinth.diagnostic_check import DiagnosticCheck
from plinth.modules.apache.components import diagnose_url
from plinth.modules.backups.components import BackupRestore
from plinth.modules.firewall.components import Firewall
@@ -86,7 +87,7 @@ class PrivoxyApp(app_module.App):
**manifest.backup)
self.add(backup_restore)
def diagnose(self):
def diagnose(self) -> list[DiagnosticCheck]:
"""Run diagnostics and return the results."""
results = super().diagnose()
results.append(diagnose_url('https://www.debian.org'))
@@ -102,16 +103,16 @@ class PrivoxyApp(app_module.App):
self.enable()
def diagnose_url_with_proxy():
def diagnose_url_with_proxy() -> list[DiagnosticCheck]:
"""Run a diagnostic on a URL with a proxy."""
url = 'https://debian.org/' # Gives a simple redirect to www.
results = []
for address in action_utils.get_addresses():
proxy = 'http://{host}:8118/'.format(host=address['url_address'])
proxy = f'http://{address["url_address"]}:8118/'
env = {'https_proxy': proxy}
result = diagnose_url(url, kind=address['kind'], env=env)
result = diagnose_url(url, kind=str(address['kind']), env=env)
result.check_id = f'privoxy-url-proxy-kind-{url}-{address["kind"]}'
result.description = gettext_noop(
'Access {url} with proxy {proxy} on tcp{kind}')