apache: Implement diagnostic test for web server component

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2019-12-11 22:26:35 -05:00 committed by James Valleroy
parent 50644b02a4
commit 0d5493dae7
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
3 changed files with 55 additions and 4 deletions

View File

@ -57,14 +57,18 @@ app. Let us do that in our app's class.
def __init__(self):
...
webserver = Webserver('webserver-transmission', 'transmission-plinth')
webserver = Webserver('webserver-transmission', 'transmission-plinth'
urls=['https://{host}/transmission'])
self.add(webserver)
The first argument to instantiate the
:class:`~plinth.modules.apache.components.Webserver` class is a unique ID. The
second is the name of the Apache2 web server configuration snippet that contains
the directives to proxy Transmission web interface via Apache2. We then need to
create the configuration file itself in ``tranmission-freedombox.conf``.
create the configuration file itself in ``tranmission-freedombox.conf``. The
final argument is the list of URLs that the app exposes to the users of the app.
This information is used to check if the URLs are accessible as expected when
the user requests diagnostic tests on the app.
.. code-block:: apache

View File

@ -23,7 +23,7 @@ from plinth import action_utils, actions, app
class Webserver(app.LeaderComponent):
"""Component to enable/disable Apache configuration."""
def __init__(self, component_id, web_name, kind='config'):
def __init__(self, component_id, web_name, kind='config', urls=None):
"""Initialize the web server component.
component_id should be a unique ID across all components of an app and
@ -37,11 +37,15 @@ class Webserver(app.LeaderComponent):
'module' for configuration in /etc/apache2/mods-available/, 'site' for
configuration in /etc/apache2/sites-available/.
urls is a list of URLs over which a HTTP services will be available due
to this component. This list is only used for running diagnostics.
"""
super().__init__(component_id)
self.web_name = web_name
self.kind = kind
self.urls = urls or []
def is_enabled(self):
"""Return whether the Apache configuration is enabled."""
@ -58,6 +62,24 @@ class Webserver(app.LeaderComponent):
'apache',
['disable', '--name', self.web_name, '--kind', self.kind])
def diagnose(self):
"""Check if the web path is accessible by clients.
See :py:meth:`plinth.app.Component.diagnose`.
"""
results = []
for url in self.urls:
if '{host}' in url:
results.extend(
action_utils.diagnose_url_on_all(url,
check_certificate=False))
else:
results.append(
action_utils.diagnose_url(url, check_certificate=False))
return results
class Uwsgi(app.LeaderComponent):
"""Component to enable/disable uWSGI configuration."""

View File

@ -30,13 +30,16 @@ def test_webserver_init():
with pytest.raises(ValueError):
Webserver(None, None)
webserver = Webserver('test-webserver', 'test-config', kind='module')
webserver = Webserver('test-webserver', 'test-config', kind='module',
urls=['url1', 'url2'])
assert webserver.component_id == 'test-webserver'
assert webserver.web_name == 'test-config'
assert webserver.kind == 'module'
assert webserver.urls == ['url1', 'url2']
webserver = Webserver('test-webserver', None)
assert webserver.kind == 'config'
assert webserver.urls == []
@patch('plinth.action_utils.webserver_is_enabled')
@ -77,6 +80,28 @@ def test_webserver_disable(superuser_run):
])
@patch('plinth.action_utils.diagnose_url')
@patch('plinth.action_utils.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):
return [('test-result-' + url, 'success')]
def side_effect(url, check_certificate):
return ('test-result-' + url, 'success')
diagnose_url_on_all.side_effect = on_all_side_effect
diagnose_url.side_effect = side_effect
webserver = Webserver('test-webserver', 'test-config',
urls=['{host}url1', 'url2'])
results = webserver.diagnose()
assert results == [('test-result-{host}url1', 'success'),
('test-result-url2', 'success')]
diagnose_url_on_all.assert_has_calls(
[call('{host}url1', check_certificate=False)])
diagnose_url.assert_has_calls([call('url2', check_certificate=False)])
def test_uwsgi_init():
"""Test that uWSGI component can be initialized."""
with pytest.raises(ValueError):