app: Introduce API to run diagnostics on an 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 2019-12-11 17:57:00 -05:00 committed by James Valleroy
parent d9f6928001
commit 50644b02a4
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 46 additions and 0 deletions

View File

@ -104,6 +104,24 @@ class App:
if not component.is_leader: if not component.is_leader:
component.set_enabled(enabled) component.set_enabled(enabled)
def diagnose(self):
"""Run diagnostics and return results.
Return value must be a list of results. Each result is a two-tuple with
first value as user visible description of the test followed by the
result. The test result is a string enumeration from 'failed', 'passed'
and 'error'.
Results are typically collected by diagnosing each component of the app
and then supplementing the results with any app level diagnostic tests.
"""
results = []
for component in self.components.values():
results.extend(component.diagnose())
return results
class Component: class Component:
"""Interface for an app component.""" """Interface for an app component."""
@ -121,6 +139,17 @@ class Component:
"""Run operations to enable the component.""" """Run operations to enable the component."""
def disable(self): def disable(self):
"""Run operations to disable the component.""" """Run operations to disable the component."""
@staticmethod
def diagnose():
"""Run diagnostics and return results.
Return value must be a list of results. Each result is a two-tuple with
first value as user visible description of the test followed by the
result. The test result is a string enumeration from 'failed', 'passed'
and 'error'.
"""
return []
class FollowerComponent(Component): class FollowerComponent(Component):

View File

@ -34,6 +34,10 @@ class LeaderTest(FollowerComponent):
"""Test class for using LeaderComponent in tests.""" """Test class for using LeaderComponent in tests."""
is_leader = True is_leader = True
def diagnose(self):
"""Return diagnostic results."""
return [('test-result-' + self.component_id, 'success')]
@pytest.fixture(name='app_with_components') @pytest.fixture(name='app_with_components')
def fixture_app_with_components(): def fixture_app_with_components():
@ -182,6 +186,13 @@ def test_app_set_enabled(app_with_components):
assert app.components['test-leader-1'].is_enabled() assert app.components['test-leader-1'].is_enabled()
def test_app_diagnose(app_with_components):
"""Test running diagnostics on an app."""
results = app_with_components.diagnose()
assert results == [('test-result-test-leader-1', 'success'),
('test-result-test-leader-2', 'success')]
def test_component_initialization(): def test_component_initialization():
"""Test that component is initialized properly.""" """Test that component is initialized properly."""
with pytest.raises(ValueError): with pytest.raises(ValueError):
@ -192,6 +203,12 @@ def test_component_initialization():
assert not component.is_leader assert not component.is_leader
def test_component_diagnose():
"""Test running diagnostics on component."""
component = Component('test-component')
assert component.diagnose() == []
def test_follower_component_initialization(): def test_follower_component_initialization():
"""Test that follower component is initialized properly.""" """Test that follower component is initialized properly."""
component = FollowerComponent('test-follower-1') component = FollowerComponent('test-follower-1')