diagnostics: Refactor check IDs, tests and background checks

- Ensure that each diagnostic test category can be identified by easy prefix
matching on the test ID.

- Give a different unique IDs each different kind of test. More specific tests
of a type get a different kind of ID.

- Make comparison of diagnostic test results in test cases more comprehensive.

- Simplify code that shows the number if issues identified.

- In many languages, there is complex logic to write plural forms. Plurals
can't be handled by assuming singular = 1 item and plural is > 1. Translation of
messages in Notification does not support plurals properly. Avoid this for now
by using sometimes incorrect plural form.

- For i18n we should avoid joining phrases/words. Words don't always maintain
order after translation.

- Notify about the total number of issues in diagnostics and not just the most
severe category. This is likely to draw more attention and avoid i18n
complexity.

- Dismiss the diagnostic notification if the latest run succeeded completely.

Tests:

- Unit tests pass.

- Diagnostics for following apps works: networks (drop-in config),
apache (daemon, listen address, internal firewall, external firewall),
tor (netcat), torproxy (internal only firewall, torproxy url, torproxy using
tor), privoxy (privoxy url, package available, package latest),

- Untested: Is release file available method in upgrades app.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa authored and James Valleroy committed 2023-10-07 04:52:22 +09:00
1 parent a233bbfd9b
commit 465e452daf
17 files changed
+247 -197

No files matched your search

+29 -61
View File
@@ -263,11 +263,16 @@ def _warn_about_low_ram_space(request):
def _start_background_diagnostics(request):
"""Start daily diagnostics as a background operation."""
operation = operation_module.manager.new(
op_id='diagnostics-full', app_id='diagnostics',
name=gettext_noop('Running background diagnostics'),
target=_run_background_diagnostics, [], show_message=False,
show_notification=False)
try:
operation = operation_module.manager.new(
op_id='diagnostics-full', app_id='diagnostics',
name=gettext_noop('Running background diagnostics'),
target=_run_background_diagnostics, [], show_message=False,
show_notification=False)
except KeyError:
logger.warning('Diagnostics are already running')
return
operation.join()
@@ -294,71 +299,34 @@ def _run_background_diagnostics():
with running_task_lock:
running_task = None
exception_count = 0
error_count = 0
failure_count = 0
warning_count = 0
issue_count = 0
severity = 'warning'
for _app_id, app_data in results.items():
if app_data['exception']:
exception_count += 1
continue
for check in app_data['diagnosis']:
if check.result == Result.ERROR:
error_count += 1
elif check.result == Result.FAILED:
failure_count += 1
elif check.result == Result.WARNING:
warning_count += 1
notification_id = 'diagnostics-background'
if exception_count > 0:
severity = 'error'
issue_count = exception_count
if exception_count > 1:
issue_type = 'translate:exceptions'
issue_count += 1
severity = 'error'
else:
issue_type = 'translate:exception'
for check in app_data['diagnosis']:
if check.result != Result.PASSED:
if check.result != Result.WARNING:
severity = 'error'
elif error_count > 0:
severity = 'error'
issue_count = error_count
if error_count > 1:
issue_type = 'translate:errors'
else:
issue_type = 'translate:error'
issue_count += 1
elif failure_count > 0:
severity = 'error'
issue_count = failure_count
if failure_count > 1:
issue_type = 'translate:failures'
else:
issue_type = 'translate:failure'
if not issue_count:
# Remove any previous notifications if there are no issues.
try:
Notification.get('diagnostics-background').delete()
except KeyError:
pass
elif warning_count > 0:
severity = 'warning'
issue_count = warning_count
if warning_count > 1:
issue_type = 'translate:warnings'
else:
issue_type = 'translate:warning'
else:
# Don't display a notification if there are no issues.
return
message = gettext_noop(
# xgettext:no-python-format
'Background diagnostics completed with {issue_count} {issue_type}')
title = gettext_noop(
# xgettext:no-python-format
'Background diagnostics results')
data = {
'app_icon': 'fa-heartbeat',
'issue_count': issue_count,
'issue_type': issue_type,
}
'Found {issue_count} issues during routine tests.')
title = gettext_noop('Diagnostics results')
data = {'app_icon': 'fa-heartbeat', 'issue_count': issue_count}
actions = [{
'type': 'link',
'class': 'primary',
@@ -367,7 +335,7 @@ def _run_background_diagnostics():
}, {
'type': 'dismiss'
}]
note = Notification.update_or_create(id=notification_id,
note = Notification.update_or_create(id='diagnostics-background',
app_id='diagnostics',
severity=severity, title=title,
message=message, actions=actions,
+1 -4
View File
@@ -1,7 +1,5 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Diagnostic check data type
"""
"""Diagnostic check data type."""
from dataclasses import dataclass
from enum import StrEnum
@@ -16,7 +14,6 @@ class Result(StrEnum):
ERROR = 'error'
# TODO: Add parameters for test.
# TODO: Description should not be translated until we need to display it.
@@ -0,0 +1,30 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Tests for diagnostic check data type."""
import pytest
from plinth.modules.diagnostics.check import 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.result == Result.NOT_DONE
check = DiagnosticCheck('some-check-id', 'sample check', Result.PASSED)
assert check.result == Result.PASSED