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

+12 -10
View File
@@ -8,7 +8,7 @@ from django.utils.text import format_lazy
from django.utils.translation import gettext_lazy
from plinth import action_utils, app
from plinth.modules.diagnostics.check import DiagnosticCheck
from plinth.modules.diagnostics.check import DiagnosticCheck, Result
from plinth.privileged import service as service_privileged
from . import privileged
@@ -142,11 +142,15 @@ def diagnose_url(url, kind=None, env=None, check_certificate=True,
Kind can be '4' for IPv4 or '6' for IPv6.
"""
result = check_url(url, kind, env, check_certificate, extra_options,
wrapper, expected_output)
try:
return_value = check_url(url, kind, env, check_certificate,
extra_options, wrapper, expected_output)
result = Result.PASSED if return_value else Result.FAILED
except FileNotFoundError:
result = Result.ERROR
if kind:
check_id = f'apache-url-{url}-{kind}'
check_id = f'apache-url-kind-{url}-{kind}'
template = gettext_lazy('Access URL {url} on tcp{kind}')
description = format_lazy(template, url=url, kind=kind)
else:
@@ -201,15 +205,13 @@ def check_url(url, kind=None, env=None, check_certificate=True,
process = subprocess.run(command, env=env, check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
result = 'passed'
result = True
if expected_output and expected_output not in process.stdout.decode():
result = 'failed'
result = False
except subprocess.CalledProcessError as exception:
result = 'failed'
result = False
# Authorization failed is a success
if exception.stdout.decode().strip() in ('401', '405'):
result = 'passed'
except FileNotFoundError:
result = 'error'
result = True
return result
+25 -19
View File
@@ -12,6 +12,7 @@ from plinth import app
from plinth.modules.apache.components import (Uwsgi, Webserver, check_url,
diagnose_url,
diagnose_url_on_all)
from plinth.modules.diagnostics.check import DiagnosticCheck, Result
def test_webserver_init():
@@ -241,19 +242,21 @@ def test_diagnose_url(get_addresses, check):
'wrapper': 'test-wrapper',
'expected_output': 'test-expected'
}
check.return_value = 'passed'
check.return_value = True
result = diagnose_url(**args)
assert result.description == 'Access URL https://localhost/test on tcp4'
assert result.result == 'passed'
assert result == DiagnosticCheck(
'apache-url-kind-https://localhost/test-4',
'Access URL https://localhost/test on tcp4', Result.PASSED)
check.return_value = 'failed'
check.return_value = False
result = diagnose_url(**args)
assert result.description == 'Access URL https://localhost/test on tcp4'
assert result.result == 'failed'
assert result == DiagnosticCheck(
'apache-url-kind-https://localhost/test-4',
'Access URL https://localhost/test on tcp4', Result.FAILED)
del args['kind']
args['url'] = 'https://{host}/test'
check.return_value = 'passed'
check.return_value = True
get_addresses.return_value = [{
'kind': '4',
'address': 'test-host-1',
@@ -265,13 +268,15 @@ def test_diagnose_url(get_addresses, check):
'numeric': False,
'url_address': 'test-host-2'
}]
result = diagnose_url_on_all(**args)
assert result[
0].description == 'Access URL https://test-host-1/test on tcp4'
assert result[0].result == 'passed'
assert result[
1].description == 'Access URL https://test-host-2/test on tcp6'
assert result[1].result == 'passed'
results = diagnose_url_on_all(**args)
assert results == [
DiagnosticCheck('apache-url-kind-https://test-host-1/test-4',
'Access URL https://test-host-1/test on tcp4',
Result.PASSED),
DiagnosticCheck('apache-url-kind-https://test-host-2/test-6',
'Access URL https://test-host-2/test on tcp6',
Result.PASSED),
]
@patch('subprocess.run')
@@ -282,7 +287,7 @@ def test_check_url(run):
extra_args = {'env': None, 'check': True, 'stdout': -1, 'stderr': -1}
# Basic
assert check_url(url) == 'passed'
assert check_url(url)
run.assert_called_with(basic_command + [url], **extra_args)
# Wrapper
@@ -315,15 +320,16 @@ def test_check_url(run):
exception = subprocess.CalledProcessError(returncode=1, cmd=['curl'])
run.side_effect = exception
run.side_effect.stdout = b'500'
assert check_url(url) == 'failed'
assert not check_url(url)
# Return code 401, 405
run.side_effect = exception
run.side_effect.stdout = b' 401 '
assert check_url(url) == 'passed'
assert check_url(url)
run.side_effect.stdout = b'405\n'
assert check_url(url) == 'passed'
assert check_url(url)
# Error
run.side_effect = FileNotFoundError()
assert check_url(url) == 'error'
with pytest.raises(FileNotFoundError):
assert check_url(url)
+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
+4 -2
View File
@@ -131,7 +131,7 @@ class Firewall(app.FollowerComponent):
for port_number, protocol in port_detail['details']))
# Internal zone
check_id = f'firewall-port-{port}-internal'
check_id = f'firewall-port-internal-{port}'
result = Result.PASSED if port in internal_ports else Result.FAILED
template = _(
'Port {name} ({details}) available for internal networks')
@@ -139,20 +139,22 @@ class Firewall(app.FollowerComponent):
results.append(DiagnosticCheck(check_id, description, result))
# External zone
check_id = f'firewall-port-{port}-external'
if self.is_external:
check_id = f'firewall-port-external-available-{port}'
result = Result.PASSED \
if port in external_ports else Result.FAILED
template = _(
'Port {name} ({details}) available for external networks')
description = format_lazy(template, name=port, details=details)
else:
check_id = f'firewall-port-external-unavailable-{port}'
result = Result.PASSED \
if port not in external_ports else Result.FAILED
template = _(
'Port {name} ({details}) unavailable for external networks'
)
description = format_lazy(template, name=port, details=details)
results.append(DiagnosticCheck(check_id, description, result))
return results
@@ -8,6 +8,7 @@ from unittest.mock import call, patch
import pytest
from plinth.app import App
from plinth.modules.diagnostics.check import DiagnosticCheck, Result
from plinth.modules.firewall.components import (Firewall,
FirewallLocalProtection)
@@ -153,34 +154,46 @@ def test_diagnose(get_enabled_services, get_port_details):
firewall = Firewall('test-firewall-1', ports=['test-port1', 'test-port2'],
is_external=False)
results = firewall.diagnose()
assert (results[0].description == 'Port test-port1 (1234/tcp, 1234/udp) '
'available for internal networks')
assert results[0].result == 'passed'
assert (results[1].description == 'Port test-port1 (1234/tcp, 1234/udp) '
'unavailable for external networks')
assert results[1].result == 'passed'
assert (results[2].description == 'Port test-port2 (2345/udp) available '
'for internal networks')
assert results[2].result == 'failed'
assert (results[3].description == 'Port test-port2 (2345/udp) unavailable '
'for external networks')
assert results[3].result == 'failed'
assert results == [
DiagnosticCheck(
'firewall-port-internal-test-port1',
'Port test-port1 (1234/tcp, 1234/udp) available for internal '
'networks', Result.PASSED),
DiagnosticCheck(
'firewall-port-external-unavailable-test-port1',
'Port test-port1 (1234/tcp, 1234/udp) unavailable for external '
'networks', Result.PASSED),
DiagnosticCheck(
'firewall-port-internal-test-port2',
'Port test-port2 (2345/udp) available for internal networks',
Result.FAILED),
DiagnosticCheck(
'firewall-port-external-unavailable-test-port2',
'Port test-port2 (2345/udp) unavailable for external networks',
Result.FAILED),
]
firewall = Firewall('test-firewall-1', ports=['test-port3', 'test-port4'],
is_external=True)
results = firewall.diagnose()
assert (results[0].description == 'Port test-port3 (3456/tcp) available '
'for internal networks')
assert results[0].result == 'passed'
assert (results[1].description == 'Port test-port3 (3456/tcp) available '
'for external networks')
assert results[1].result == 'passed'
assert (results[2].description == 'Port test-port4 (4567/udp) available '
'for internal networks')
assert results[2].result == 'failed'
assert (results[3].description == 'Port test-port4 (4567/udp) available '
'for external networks')
assert results[3].result == 'failed'
assert results == [
DiagnosticCheck(
'firewall-port-internal-test-port3',
'Port test-port3 (3456/tcp) available for internal networks',
Result.PASSED),
DiagnosticCheck(
'firewall-port-external-available-test-port3',
'Port test-port3 (3456/tcp) available for external networks',
Result.PASSED),
DiagnosticCheck(
'firewall-port-internal-test-port4',
'Port test-port4 (4567/udp) available for internal networks',
Result.FAILED),
DiagnosticCheck(
'firewall-port-external-available-test-port4',
'Port test-port4 (4567/udp) available for external networks',
Result.FAILED),
]
def test_local_protection_init():
+1
View File
@@ -111,6 +111,7 @@ def diagnose_url_with_proxy():
env = {'https_proxy': proxy}
result = diagnose_url(url, kind=address['kind'], env=env)
result.check_id = f'privoxy-url-proxy-kind-{url}-{address["kind"]}'
result.description = _(
'Access {url} with proxy {proxy} on tcp{kind}') \
.format(url=url, proxy=proxy, kind=address['kind'])
+2
View File
@@ -135,6 +135,7 @@ class TorProxyApp(app_module.App):
def _diagnose_url_via_tor(url, kind=None):
"""Diagnose whether a URL is reachable via Tor."""
result = diagnose_url(url, kind=kind, wrapper='torsocks')
result.check_id = 'torproxy-url'
result.description = _('Access URL {url} on tcp{kind} via Tor') \
.format(url=url, kind=kind)
@@ -146,6 +147,7 @@ def _diagnose_tor_use(url, kind=None):
expected_output = 'Congratulations. This browser is configured to use Tor.'
result = diagnose_url(url, kind=kind, wrapper='torsocks',
expected_output=expected_output)
result.check_id = 'torproxy-using-tor'
result.description = _('Confirm Tor usage at {url} on tcp{kind}') \
.format(url=url, kind=kind)
+4 -2
View File
@@ -216,8 +216,10 @@ def _is_release_file_available(protocol: str, dist: str,
if backports:
dist += '-backports'
result = check_url(RELEASE_FILE_URL.format(dist), wrapper=wrapper)
return result == 'passed'
try:
return check_url(RELEASE_FILE_URL.format(dist), wrapper=wrapper)
except FileNotFoundError:
return False
def _add_backports_sources(sources_list: str, protocol: str, dist: str):