diff options
Diffstat (limited to 'roles/openshift_health_checker')
6 files changed, 76 insertions, 17 deletions
diff --git a/roles/openshift_health_checker/callback_plugins/zz_failure_summary.py b/roles/openshift_health_checker/callback_plugins/zz_failure_summary.py index 8caefab15..208e81048 100644 --- a/roles/openshift_health_checker/callback_plugins/zz_failure_summary.py +++ b/roles/openshift_health_checker/callback_plugins/zz_failure_summary.py @@ -53,11 +53,11 @@ class CallbackModule(CallbackBase): subsequent_extra_indent = u' ' * (initial_indent_len + 10) for i, failure in enumerate(self.__failures, 1): - lines = _format_failure(failure) - self._display.display(u'\n{}{}'.format(initial_indent_format.format(i), lines[0])) - for line in lines[1:]: - line = line.replace(u'\n', u'\n' + subsequent_extra_indent) - indented = u'{}{}'.format(subsequent_indent, line) + entries = _format_failure(failure) + self._display.display(u'\n{}{}'.format(initial_indent_format.format(i), entries[0])) + for entry in entries[1:]: + entry = entry.replace(u'\n', u'\n' + subsequent_extra_indent) + indented = u'{}{}'.format(subsequent_indent, entry) self._display.display(indented) @@ -66,8 +66,9 @@ class CallbackModule(CallbackBase): # Status: permanently disabled unless Ansible's API changes. # pylint: disable=protected-access def _format_failure(failure): - '''Return a list of pretty-formatted lines describing a failure, including - relevant information about it. Line separators are not included.''' + '''Return a list of pretty-formatted text entries describing a failure, including + relevant information about it. Expect that the list of text entries will be joined + by a newline separator when output to the user.''' result = failure['result'] host = result._host.get_name() play = _get_play(result._task) @@ -75,16 +76,29 @@ def _format_failure(failure): play = play.get_name() task = result._task.get_name() msg = result._result.get('msg', u'???') - rows = ( + fields = ( (u'Host', host), (u'Play', play), (u'Task', task), (u'Message', stringc(msg, C.COLOR_ERROR)), ) if 'checks' in result._result: - rows += ((u'Details', stringc(pformat(result._result['checks']), C.COLOR_ERROR)),) + fields += ((u'Details', _format_failed_checks(result._result['checks'])),) row_format = '{:10}{}' - return [row_format.format(header + u':', body) for header, body in rows] + return [row_format.format(header + u':', body) for header, body in fields] + + +def _format_failed_checks(checks): + '''Return pretty-formatted text describing checks that failed.''' + failed_check_msgs = [] + for check, body in checks.items(): + if body.get('failed', False): # only show the failed checks + msg = body.get('msg', u"Failed without returning a message") + failed_check_msgs.append('check "%s":\n%s' % (check, msg)) + if failed_check_msgs: + return stringc("\n\n".join(failed_check_msgs), C.COLOR_ERROR) + else: # something failed but no checks will admit to it, so dump everything + return stringc(pformat(checks), C.COLOR_ERROR) # Reason: disable pylint protected-access because we need to access _* diff --git a/roles/openshift_health_checker/library/aos_version.py b/roles/openshift_health_checker/library/aos_version.py index 13b7d310b..191a4b107 100755 --- a/roles/openshift_health_checker/library/aos_version.py +++ b/roles/openshift_health_checker/library/aos_version.py @@ -32,6 +32,7 @@ def main(): # pylint: disable=missing-docstring,too-many-branches bail("prefix must not be empty") yb = yum.YumBase() # pylint: disable=invalid-name + yb.conf.disable_excludes = ["all"] # assume the openshift excluder will be managed, ignore current state # search for package versions available for aos pkgs expected_pkgs = [ diff --git a/roles/openshift_health_checker/library/check_yum_update.py b/roles/openshift_health_checker/library/check_yum_update.py index 9bc14fd47..630ebc848 100755 --- a/roles/openshift_health_checker/library/check_yum_update.py +++ b/roles/openshift_health_checker/library/check_yum_update.py @@ -27,6 +27,7 @@ def main(): # pylint: disable=missing-docstring,too-many-branches module.fail_json(msg=error) yb = yum.YumBase() # pylint: disable=invalid-name + yb.conf.disable_excludes = ["all"] # assume the openshift excluder will be managed, ignore current state # determine if the existing yum configuration is valid try: yb.repos.populateSack(mdtype='metadata', cacheonly=1) diff --git a/roles/openshift_health_checker/openshift_checks/__init__.py b/roles/openshift_health_checker/openshift_checks/__init__.py index 8433923ed..93547a2e0 100644 --- a/roles/openshift_health_checker/openshift_checks/__init__.py +++ b/roles/openshift_health_checker/openshift_checks/__init__.py @@ -8,11 +8,8 @@ import os from abc import ABCMeta, abstractmethod, abstractproperty from importlib import import_module -# add_metaclass is not available in the embedded six from module_utils in Ansible 2.2.1 -from six import add_metaclass -# pylint import-error disabled because pylint cannot find the package -# when installed in a virtualenv -from ansible.module_utils.six.moves import reduce # pylint: disable=import-error, redefined-builtin +from ansible.module_utils import six +from ansible.module_utils.six.moves import reduce # pylint: disable=import-error,redefined-builtin class OpenShiftCheckException(Exception): @@ -20,7 +17,7 @@ class OpenShiftCheckException(Exception): pass -@add_metaclass(ABCMeta) +@six.add_metaclass(ABCMeta) class OpenShiftCheck(object): """A base class for defining checks for an OpenShift cluster environment.""" @@ -66,7 +63,8 @@ def get_var(task_vars, *keys, **kwargs): Ansible task_vars structures are Python dicts, often mapping strings to other dicts. This helper makes it easier to get a nested value, raising - OpenShiftCheckException when a key is not found. + OpenShiftCheckException when a key is not found or returning a default value + provided as a keyword argument. """ try: value = reduce(operator.getitem, keys, task_vars) diff --git a/roles/openshift_health_checker/test/conftest.py b/roles/openshift_health_checker/test/conftest.py new file mode 100644 index 000000000..bf717ae85 --- /dev/null +++ b/roles/openshift_health_checker/test/conftest.py @@ -0,0 +1,5 @@ +import os +import sys + +# extend sys.path so that tests can import openshift_checks +sys.path.insert(1, os.path.dirname(os.path.dirname(__file__))) diff --git a/roles/openshift_health_checker/test/openshift_check_test.py b/roles/openshift_health_checker/test/openshift_check_test.py new file mode 100644 index 000000000..c4c8cd1c2 --- /dev/null +++ b/roles/openshift_health_checker/test/openshift_check_test.py @@ -0,0 +1,40 @@ +import pytest + +from openshift_checks import get_var, OpenShiftCheckException + + +# Fixtures + + +@pytest.fixture() +def task_vars(): + return dict(foo=42, bar=dict(baz="openshift")) + + +@pytest.fixture(params=[ + ("notfound",), + ("multiple", "keys", "not", "in", "task_vars"), +]) +def missing_keys(request): + return request.param + + +# Tests + + +@pytest.mark.parametrize("keys,expected", [ + (("foo",), 42), + (("bar", "baz"), "openshift"), +]) +def test_get_var_ok(task_vars, keys, expected): + assert get_var(task_vars, *keys) == expected + + +def test_get_var_error(task_vars, missing_keys): + with pytest.raises(OpenShiftCheckException): + get_var(task_vars, *missing_keys) + + +def test_get_var_default(task_vars, missing_keys): + default = object() + assert get_var(task_vars, *missing_keys, default=default) == default |