mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
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:
1 parent
a233bbfd9b
commit
465e452daf
17 files changed
+247
-197
No files matched your search
@@ -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
|
||||
@@ -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)
|
||||
Reference in new issue
Block a user