From 4b657031ff309cb8b004cd7fbd44ae479ce09432 Mon Sep 17 00:00:00 2001 From: Rodolfo Carvalho Date: Wed, 22 Mar 2017 16:51:25 +0100 Subject: Test OpenShift health check loader --- .../action_plugins/openshift_health_check.py | 4 +++- .../openshift_checks/__init__.py | 27 ++++++++++++---------- .../test/openshift_check_test.py | 9 +++++++- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/roles/openshift_health_checker/action_plugins/openshift_health_check.py b/roles/openshift_health_checker/action_plugins/openshift_health_check.py index 027abf398..cf0fe19f1 100644 --- a/roles/openshift_health_checker/action_plugins/openshift_health_check.py +++ b/roles/openshift_health_checker/action_plugins/openshift_health_check.py @@ -17,7 +17,7 @@ from ansible.plugins.action import ActionBase # this callback plugin. sys.path.insert(1, os.path.dirname(os.path.dirname(__file__))) -from openshift_checks import OpenShiftCheck, OpenShiftCheckException # noqa: E402 +from openshift_checks import OpenShiftCheck, OpenShiftCheckException, load_checks # noqa: E402 class ActionModule(ActionBase): @@ -78,6 +78,8 @@ class ActionModule(ActionBase): return result def load_known_checks(self): + load_checks() + known_checks = {} known_check_classes = set(cls for cls in OpenShiftCheck.subclasses()) diff --git a/roles/openshift_health_checker/openshift_checks/__init__.py b/roles/openshift_health_checker/openshift_checks/__init__.py index 72d0b26df..be63d864a 100644 --- a/roles/openshift_health_checker/openshift_checks/__init__.py +++ b/roles/openshift_health_checker/openshift_checks/__init__.py @@ -63,6 +63,21 @@ class OpenShiftCheck(object): yield subclass +LOADER_EXCLUDES = ( + "__init__.py", + "mixins.py", +) + + +def load_checks(): + """Dynamically import all check modules for the side effect of registering checks.""" + return [ + import_module(__package__ + "." + name[:-3]) + for name in os.listdir(os.path.dirname(__file__)) + if name.endswith(".py") and name not in LOADER_EXCLUDES + ] + + def get_var(task_vars, *keys, **kwargs): """Helper function to get deeply nested values from task_vars. @@ -78,15 +93,3 @@ def get_var(task_vars, *keys, **kwargs): return kwargs["default"] raise OpenShiftCheckException("'{}' is undefined".format(".".join(map(str, keys)))) return value - - -# Dynamically import all submodules for the side effect of loading checks. - -EXCLUDES = ( - "__init__.py", - "mixins.py", -) - -for name in os.listdir(os.path.dirname(__file__)): - if name.endswith(".py") and name not in EXCLUDES: - import_module(__package__ + "." + name[:-3]) diff --git a/roles/openshift_health_checker/test/openshift_check_test.py b/roles/openshift_health_checker/test/openshift_check_test.py index 9cbd5b11e..03465a7c3 100644 --- a/roles/openshift_health_checker/test/openshift_check_test.py +++ b/roles/openshift_health_checker/test/openshift_check_test.py @@ -1,6 +1,7 @@ import pytest -from openshift_checks import OpenShiftCheck, get_var, OpenShiftCheckException +from openshift_checks import OpenShiftCheck, OpenShiftCheckException +from openshift_checks import load_checks, get_var # Fixtures @@ -57,6 +58,12 @@ def test_OpenShiftCheck_init(): assert check.module_executor == execute_module +def test_load_checks(): + """Loading checks should load and return Python modules.""" + modules = load_checks() + assert modules + + @pytest.mark.parametrize("keys,expected", [ (("foo",), 42), (("bar", "baz"), "openshift"), -- cgit v1.2.3