summaryrefslogtreecommitdiffstats
path: root/roles
diff options
context:
space:
mode:
authorOpenShift Bot <eparis+openshiftbot@redhat.com>2017-03-17 14:54:58 -0500
committerGitHub <noreply@github.com>2017-03-17 14:54:58 -0500
commit31ac96c8d3b467e24ab4c60364cb1eb04132c348 (patch)
tree859a08f87612d29515e97e75b39cd59e709b78b0 /roles
parent516ac1d09df53a2230cda420aae259f4af35a34d (diff)
parent8cfdd96ffa1c5b35455751aa8be5a5704d2550da (diff)
downloadopenshift-31ac96c8d3b467e24ab4c60364cb1eb04132c348.tar.gz
openshift-31ac96c8d3b467e24ab4c60364cb1eb04132c348.tar.bz2
openshift-31ac96c8d3b467e24ab4c60364cb1eb04132c348.tar.xz
openshift-31ac96c8d3b467e24ab4c60364cb1eb04132c348.zip
Merge pull request #3695 from rhcarvalho/check-unit-tests
Merged by openshift-bot
Diffstat (limited to 'roles')
-rw-r--r--roles/openshift_health_checker/openshift_checks/__init__.py3
-rw-r--r--roles/openshift_health_checker/test/conftest.py5
-rw-r--r--roles/openshift_health_checker/test/openshift_check_test.py40
3 files changed, 47 insertions, 1 deletions
diff --git a/roles/openshift_health_checker/openshift_checks/__init__.py b/roles/openshift_health_checker/openshift_checks/__init__.py
index 8433923ed..f66d19fbf 100644
--- a/roles/openshift_health_checker/openshift_checks/__init__.py
+++ b/roles/openshift_health_checker/openshift_checks/__init__.py
@@ -66,7 +66,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