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

+10 -8
View File
@@ -13,7 +13,8 @@ from plinth import app as app_module
from plinth import cfg, menu
from plinth.config import DropinConfigs
from plinth.daemon import Daemon
from plinth.modules.diagnostics.check import DiagnosticCheck, Result
from plinth.diagnostic_check import (DiagnosticCheck,
DiagnosticCheckParameters, Result)
from plinth.package import Packages
from plinth.privileged import service as service_privileged
@@ -85,7 +86,7 @@ class UsersApp(app_module.App):
groups=groups)
self.add(users_and_groups)
def diagnose(self):
def diagnose(self) -> list[DiagnosticCheck]:
"""Run diagnostics and return the results."""
results = super().diagnose()
@@ -112,7 +113,7 @@ class UsersApp(app_module.App):
privileged.create_group('freedombox-share')
def _diagnose_ldap_entry(search_item):
def _diagnose_ldap_entry(search_item: str) -> DiagnosticCheck:
"""Diagnose that an LDAP entry exists."""
check_id = f'users-ldap-entry-{search_item}'
result = Result.FAILED
@@ -125,12 +126,13 @@ def _diagnose_ldap_entry(search_item):
pass
description = gettext_noop('Check LDAP entry "{search_item}"')
parameters = {'search_item': search_item}
parameters: DiagnosticCheckParameters = {'search_item': search_item}
return DiagnosticCheck(check_id, description, result, parameters)
def _diagnose_nslcd_config(config, key, value):
def _diagnose_nslcd_config(config: dict[str, str], key: str,
value: str) -> DiagnosticCheck:
"""Diagnose that nslcd has a configuration."""
check_id = f'users-nslcd-config-{key}'
try:
@@ -139,12 +141,12 @@ def _diagnose_nslcd_config(config, key, value):
result = Result.FAILED
description = gettext_noop('Check nslcd config "{key} {value}"')
parameters = {'key': key, 'value': value}
parameters: DiagnosticCheckParameters = {'key': key, 'value': value}
return DiagnosticCheck(check_id, description, result, parameters)
def _diagnose_nsswitch_config():
def _diagnose_nsswitch_config() -> list[DiagnosticCheck]:
"""Diagnose that Name Service Switch is configured to use LDAP."""
nsswitch_conf = '/etc/nsswitch.conf'
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
@@ -169,7 +171,7 @@ def _diagnose_nsswitch_config():
break
description = gettext_noop('Check nsswitch config "{database}"')
parameters = {'database': database}
parameters: DiagnosticCheckParameters = {'database': database}
results.append(
DiagnosticCheck(check_id, description, result, parameters))
+1 -1
View File
@@ -178,7 +178,7 @@ def _configure_ldapscripts():
@privileged
def get_nslcd_config():
def get_nslcd_config() -> dict[str, str]:
"""Get nslcd configuration for diagnostics."""
nslcd_conf = '/etc/nslcd.conf'
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +