From 5e71e43a2a2e9089185d34e5406ee212cc478a75 Mon Sep 17 00:00:00 2001 From: Rodolfo Carvalho Date: Wed, 22 Mar 2017 16:29:37 +0100 Subject: Rename module_executor -> execute_module It is a function/callable, the name should imply action, should be a verb and not a noun. Keep supporting the old name while we have PRs in-flight that use the old name. --- .../test/openshift_check_test.py | 37 +++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'roles/openshift_health_checker/test') diff --git a/roles/openshift_health_checker/test/openshift_check_test.py b/roles/openshift_health_checker/test/openshift_check_test.py index c4c8cd1c2..9cbd5b11e 100644 --- a/roles/openshift_health_checker/test/openshift_check_test.py +++ b/roles/openshift_health_checker/test/openshift_check_test.py @@ -1,6 +1,6 @@ import pytest -from openshift_checks import get_var, OpenShiftCheckException +from openshift_checks import OpenShiftCheck, get_var, OpenShiftCheckException # Fixtures @@ -22,6 +22,41 @@ def missing_keys(request): # Tests +def test_OpenShiftCheck_init(): + class TestCheck(OpenShiftCheck): + name = "test_check" + run = NotImplemented + + # initialization requires at least one argument (apart from self) + with pytest.raises(TypeError) as excinfo: + TestCheck() + assert 'execute_module' in str(excinfo.value) + assert 'module_executor' in str(excinfo.value) + + execute_module = object() + + # initialize with positional argument + check = TestCheck(execute_module) + # new recommended name + assert check.execute_module == execute_module + # deprecated attribute name + assert check.module_executor == execute_module + + # initialize with keyword argument, recommended name + check = TestCheck(execute_module=execute_module) + # new recommended name + assert check.execute_module == execute_module + # deprecated attribute name + assert check.module_executor == execute_module + + # initialize with keyword argument, deprecated name + check = TestCheck(module_executor=execute_module) + # new recommended name + assert check.execute_module == execute_module + # deprecated attribute name + assert check.module_executor == execute_module + + @pytest.mark.parametrize("keys,expected", [ (("foo",), 42), (("bar", "baz"), "openshift"), -- cgit v1.2.3 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 --- roles/openshift_health_checker/test/openshift_check_test.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'roles/openshift_health_checker/test') 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 From 0af4d72753fa411ddbd17e180aca2c3f4a4df9a6 Mon Sep 17 00:00:00 2001 From: Rodolfo Carvalho Date: Wed, 22 Mar 2017 16:52:37 +0100 Subject: Test recursively finding subclasses --- .../test/openshift_check_test.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'roles/openshift_health_checker/test') diff --git a/roles/openshift_health_checker/test/openshift_check_test.py b/roles/openshift_health_checker/test/openshift_check_test.py index 03465a7c3..e3153979c 100644 --- a/roles/openshift_health_checker/test/openshift_check_test.py +++ b/roles/openshift_health_checker/test/openshift_check_test.py @@ -58,6 +58,23 @@ def test_OpenShiftCheck_init(): assert check.module_executor == execute_module +def test_subclasses(): + """OpenShiftCheck.subclasses should find all subclasses recursively.""" + class TestCheck1(OpenShiftCheck): + pass + + class TestCheck2(OpenShiftCheck): + pass + + class TestCheck1A(TestCheck1): + pass + + local_subclasses = set([TestCheck1, TestCheck1A, TestCheck2]) + known_subclasses = set(OpenShiftCheck.subclasses()) + + assert local_subclasses - known_subclasses == set(), "local_subclasses should be a subset of known_subclasses" + + def test_load_checks(): """Loading checks should load and return Python modules.""" modules = load_checks() -- cgit v1.2.3 From 209810c9905c0ebf89b1e03ccb8593f78a6e5396 Mon Sep 17 00:00:00 2001 From: Rodolfo Carvalho Date: Wed, 22 Mar 2017 21:05:18 +0100 Subject: Add unit tests for mixins.py --- roles/openshift_health_checker/test/mixins_test.py | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 roles/openshift_health_checker/test/mixins_test.py (limited to 'roles/openshift_health_checker/test') diff --git a/roles/openshift_health_checker/test/mixins_test.py b/roles/openshift_health_checker/test/mixins_test.py new file mode 100644 index 000000000..2d83e207d --- /dev/null +++ b/roles/openshift_health_checker/test/mixins_test.py @@ -0,0 +1,23 @@ +import pytest + +from openshift_checks import OpenShiftCheck, OpenShiftCheckException +from openshift_checks.mixins import NotContainerizedMixin + + +class NotContainerizedCheck(NotContainerizedMixin, OpenShiftCheck): + name = "not_containerized" + run = NotImplemented + + +@pytest.mark.parametrize('task_vars,expected', [ + (dict(openshift=dict(common=dict(is_containerized=False))), True), + (dict(openshift=dict(common=dict(is_containerized=True))), False), +]) +def test_is_active(task_vars, expected): + assert NotContainerizedCheck.is_active(task_vars) == expected + + +def test_is_active_missing_task_vars(): + with pytest.raises(OpenShiftCheckException) as excinfo: + NotContainerizedCheck.is_active(task_vars={}) + assert 'is_containerized' in str(excinfo.value) -- cgit v1.2.3 From a39620a07602db95d0d59702a9a5466b270a2be8 Mon Sep 17 00:00:00 2001 From: Rodolfo Carvalho Date: Fri, 24 Mar 2017 22:09:00 +0100 Subject: Add unit tests for package_availability.py --- .../test/package_availability_test.py | 49 ++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 roles/openshift_health_checker/test/package_availability_test.py (limited to 'roles/openshift_health_checker/test') diff --git a/roles/openshift_health_checker/test/package_availability_test.py b/roles/openshift_health_checker/test/package_availability_test.py new file mode 100644 index 000000000..25385339a --- /dev/null +++ b/roles/openshift_health_checker/test/package_availability_test.py @@ -0,0 +1,49 @@ +import pytest + +from openshift_checks.package_availability import PackageAvailability + + +@pytest.mark.parametrize('task_vars,must_have_packages,must_not_have_packages', [ + ( + dict(openshift=dict(common=dict(service_type='openshift'))), + set(), + set(['openshift-master', 'openshift-node']), + ), + ( + dict( + openshift=dict(common=dict(service_type='origin')), + group_names=['masters'], + ), + set(['origin-master']), + set(['origin-node']), + ), + ( + dict( + openshift=dict(common=dict(service_type='atomic-openshift')), + group_names=['nodes'], + ), + set(['atomic-openshift-node']), + set(['atomic-openshift-master']), + ), + ( + dict( + openshift=dict(common=dict(service_type='atomic-openshift')), + group_names=['masters', 'nodes'], + ), + set(['atomic-openshift-master', 'atomic-openshift-node']), + set(), + ), +]) +def test_package_availability(task_vars, must_have_packages, must_not_have_packages): + return_value = object() + + def execute_module(module_name=None, module_args=None, tmp=None, task_vars=None): + assert module_name == 'check_yum_update' + assert 'packages' in module_args + assert set(module_args['packages']).issuperset(must_have_packages) + assert not set(module_args['packages']).intersection(must_not_have_packages) + return return_value + + check = PackageAvailability(execute_module=execute_module) + result = check.run(tmp=None, task_vars=task_vars) + assert result is return_value -- cgit v1.2.3 From 5acb711c3ebbb273351f1a1f3d418c85a6abf2c3 Mon Sep 17 00:00:00 2001 From: Rodolfo Carvalho Date: Mon, 27 Mar 2017 14:58:13 +0200 Subject: Add unit tests for package_update.py --- .../openshift_health_checker/test/package_update_test.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 roles/openshift_health_checker/test/package_update_test.py (limited to 'roles/openshift_health_checker/test') diff --git a/roles/openshift_health_checker/test/package_update_test.py b/roles/openshift_health_checker/test/package_update_test.py new file mode 100644 index 000000000..5e000cff5 --- /dev/null +++ b/roles/openshift_health_checker/test/package_update_test.py @@ -0,0 +1,16 @@ +from openshift_checks.package_update import PackageUpdate + + +def test_package_update(): + return_value = object() + + def execute_module(module_name=None, module_args=None, tmp=None, task_vars=None): + assert module_name == 'check_yum_update' + assert 'packages' in module_args + # empty list of packages means "generic check if 'yum update' will work" + assert module_args['packages'] == [] + return return_value + + check = PackageUpdate(execute_module=execute_module) + result = check.run(tmp=None, task_vars=None) + assert result is return_value -- cgit v1.2.3 From ca2eaab70716a23d1d8064d16d183cb7f2ffc898 Mon Sep 17 00:00:00 2001 From: Rodolfo Carvalho Date: Mon, 27 Mar 2017 15:08:10 +0200 Subject: Add unit tests for package_version.py --- .../test/package_version_test.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 roles/openshift_health_checker/test/package_version_test.py (limited to 'roles/openshift_health_checker/test') diff --git a/roles/openshift_health_checker/test/package_version_test.py b/roles/openshift_health_checker/test/package_version_test.py new file mode 100644 index 000000000..cc1d263bc --- /dev/null +++ b/roles/openshift_health_checker/test/package_version_test.py @@ -0,0 +1,21 @@ +from openshift_checks.package_version import PackageVersion + + +def test_package_version(): + task_vars = dict( + openshift=dict(common=dict(service_type='origin')), + openshift_release='v3.5', + ) + return_value = object() + + def execute_module(module_name=None, module_args=None, tmp=None, task_vars=None): + assert module_name == 'aos_version' + assert 'prefix' in module_args + assert 'version' in module_args + assert module_args['prefix'] == task_vars['openshift']['common']['service_type'] + assert module_args['version'] == task_vars['openshift_release'] + return return_value + + check = PackageVersion(execute_module=execute_module) + result = check.run(tmp=None, task_vars=task_vars) + assert result is return_value -- cgit v1.2.3 From d6cf72a4f488d8c275bfbaf27620ff716da1ae5d Mon Sep 17 00:00:00 2001 From: Rodolfo Carvalho Date: Mon, 27 Mar 2017 16:08:22 +0200 Subject: Add test scaffold for docker_image_availability.py The intention is to set a starting point and let another team member work on the code to gain experience with tests. --- .../test/docker_image_availability_test.py | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 roles/openshift_health_checker/test/docker_image_availability_test.py (limited to 'roles/openshift_health_checker/test') diff --git a/roles/openshift_health_checker/test/docker_image_availability_test.py b/roles/openshift_health_checker/test/docker_image_availability_test.py new file mode 100644 index 000000000..2a9c32f77 --- /dev/null +++ b/roles/openshift_health_checker/test/docker_image_availability_test.py @@ -0,0 +1,28 @@ +import pytest + +from openshift_checks.docker_image_availability import DockerImageAvailability + + +@pytest.mark.xfail(strict=True) # TODO: remove this once this test is fully implemented. +@pytest.mark.parametrize('task_vars,expected_result', [ + ( + dict( + openshift=dict(common=dict( + service_type='origin', + is_containerized=False, + )), + openshift_release='v3.5', + deployment_type='origin', + openshift_image_tag='', # FIXME: should not be required + ), + {'changed': False}, + ), + # TODO: add more parameters here to test the multiple possible inputs that affect behavior. +]) +def test_docker_image_availability(task_vars, expected_result): + def execute_module(module_name=None, module_args=None, tmp=None, task_vars=None): + return {'info': {}} # TODO: this will vary depending on input parameters. + + check = DockerImageAvailability(execute_module=execute_module) + result = check.run(tmp=None, task_vars=task_vars) + assert result == expected_result -- cgit v1.2.3