apache: Move diagnostics for checking URLs into apache module

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 2019-12-30 11:37:57 -05:00
1 parent c617cecc02
commit e0dba2cc17
10 files changed
+217 -113

No files matched your search

+107 -3
View File
@@ -18,11 +18,14 @@
Test module for webserver components.
"""
import subprocess
from unittest.mock import call, patch
import pytest
from plinth.modules.apache.components import Uwsgi, Webserver
from plinth.modules.apache.components import (Uwsgi, Webserver, check_url,
diagnose_url,
diagnose_url_on_all)
def test_webserver_init():
@@ -80,8 +83,8 @@ def test_webserver_disable(superuser_run):
])
@patch('plinth.action_utils.diagnose_url')
@patch('plinth.action_utils.diagnose_url_on_all')
@patch('plinth.modules.apache.components.diagnose_url')
@patch('plinth.modules.apache.components.diagnose_url_on_all')
def test_webserver_diagnose(diagnose_url_on_all, diagnose_url):
"""Test running diagnostics."""
def on_all_side_effect(url, check_certificate):
@@ -177,3 +180,104 @@ def test_uwsgi_is_running(uwsgi_is_enabled, service_is_running):
uwsgi_is_enabled.return_value = False
service_is_running.return_value = False
assert not uwsgi.is_running()
@patch('plinth.modules.apache.components.check_url')
@patch('plinth.action_utils.get_addresses')
def test_diagnose_url(get_addresses, check):
"""Test diagnosing a URL."""
args = {
'url': 'https://localhost/test',
'kind': '4',
'env': {
'test': 'value'
},
'check_certificate': False,
'extra_options': {
'test-1': 'value-1'
},
'wrapper': 'test-wrapper',
'expected_output': 'test-expected'
}
check.return_value = 'passed'
result = diagnose_url(**args)
assert result == ['Access URL https://localhost/test on tcp4', 'passed']
check.return_value = 'failed'
result = diagnose_url(**args)
assert result == ['Access URL https://localhost/test on tcp4', 'failed']
del args['kind']
args['url'] = 'https://{host}/test'
check.return_value = 'passed'
get_addresses.return_value = [{
'kind': '4',
'address': 'test-host-1',
'numeric': False,
'url_address': 'test-host-1'
}, {
'kind': '6',
'address': 'test-host-2',
'numeric': False,
'url_address': 'test-host-2'
}]
result = diagnose_url_on_all(**args)
assert result == [
['Access URL https://test-host-1/test on tcp4', 'passed'],
['Access URL https://test-host-2/test on tcp6', 'passed'],
]
@patch('subprocess.run')
def test_check_url(run):
"""Test checking whether a URL is accessible."""
url = 'http://localhost/test'
basic_command = ['curl', '--location', '-f', '-w', '%{response_code}']
extra_args = {'env': None, 'check': True, 'stdout': -1, 'stderr': -1}
# Basic
assert check_url(url) == 'passed'
run.assert_called_with(basic_command + [url], **extra_args)
# Wrapper
check_url(url, wrapper='test-wrapper')
run.assert_called_with(['test-wrapper'] + basic_command + [url],
**extra_args)
# No certificate check
check_url(url, check_certificate=False)
run.assert_called_with(basic_command + [url, '-k'], **extra_args)
# Extra options
check_url(url, extra_options=['test-opt1', 'test-opt2'])
run.assert_called_with(basic_command + [url, 'test-opt1', 'test-opt2'],
**extra_args)
# TCP4/TCP6
check_url(url, kind='4')
run.assert_called_with(basic_command + [url, '-4'], **extra_args)
check_url(url, kind='6')
run.assert_called_with(basic_command + [url, '-6'], **extra_args)
# IPv6 Link Local URLs
check_url('https://[::2%eth0]/test', kind='6')
run.assert_called_with(
basic_command + ['--interface', 'eth0', 'https://[::2]/test', '-6'],
**extra_args)
# Failure
exception = subprocess.CalledProcessError(returncode=1, cmd=['curl'])
run.side_effect = exception
run.side_effect.stdout = b'500'
assert check_url(url) == 'failed'
# Return code 401, 405
run.side_effect = exception
run.side_effect.stdout = b' 401 '
assert check_url(url) == 'passed'
run.side_effect.stdout = b'405\n'
assert check_url(url) == 'passed'
# Error
run.side_effect = FileNotFoundError()
assert check_url(url) == 'error'