Skip to content

Commit

Permalink
Add unit tests for existing health checks
Browse files Browse the repository at this point in the history
  • Loading branch information
rhcarvalho committed Mar 17, 2017
1 parent c73bb3a commit 8cfdd96
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ python_files =
# is Python unittest's default, while pytest discovers both "test_*.py" and
# "*_test.py" by default.
test_*.py
*_test.py
*_tests.py
addopts =
--cov=.
Expand Down
3 changes: 2 additions & 1 deletion roles/openshift_health_checker/openshift_checks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions roles/openshift_health_checker/test/conftest.py
Original file line number Diff line number Diff line change
@@ -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__)))
40 changes: 40 additions & 0 deletions roles/openshift_health_checker/test/openshift_check_test.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8cfdd96

Please sign in to comment.