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

+12 -9
View File
@@ -12,6 +12,8 @@ from django.utils.translation import gettext_lazy, gettext_noop
import plinth.privileged.packages as privileged
from plinth import app as app_module
from plinth.diagnostic_check import (DiagnosticCheck,
DiagnosticCheckParameters, Result)
from plinth.errors import MissingPackageError
from plinth.utils import format_lazy
@@ -200,10 +202,8 @@ class Packages(app_module.FollowerComponent):
uninstall([package for package in packages if package in packages_set],
purge=True)
def diagnose(self):
def diagnose(self) -> list[DiagnosticCheck]:
"""Run diagnostics and return results."""
from plinth.modules.diagnostics.check import DiagnosticCheck, Result
results = super().diagnose()
cache = apt.Cache()
for package_expression in self.package_expressions:
@@ -213,7 +213,9 @@ class Packages(app_module.FollowerComponent):
check_id = f'package-available-{package_expression}'
description = gettext_noop('Package {package_expression} is '
'not available for install')
parameters = {'package_expression': str(package_expression)}
parameters: DiagnosticCheckParameters = {
'package_expression': str(package_expression)
}
results.append(
DiagnosticCheck(check_id, description, Result.FAILED,
parameters))
@@ -223,16 +225,17 @@ class Packages(app_module.FollowerComponent):
latest_version = '?'
if package_name in cache:
package = cache[package_name]
latest_version = package.candidate.version
if package.candidate.is_installed:
result = Result.PASSED
if package.candidate:
latest_version = package.candidate.version
if package.candidate.is_installed:
result = Result.PASSED
check_id = f'package-latest-{package_name}'
description = gettext_noop('Package {package_name} is the latest '
'version ({latest_version})')
parameters = {
'package_name': package_name,
'latest_version': latest_version,
'package_name': str(package_name),
'latest_version': str(latest_version)
}
results.append(
DiagnosticCheck(check_id, description, result, parameters))