openshift_check_test.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import pytest
  2. from openshift_checks import OpenShiftCheck, OpenShiftCheckException
  3. from openshift_checks import load_checks, get_var
  4. # Fixtures
  5. @pytest.fixture()
  6. def task_vars():
  7. return dict(foo=42, bar=dict(baz="openshift"))
  8. @pytest.fixture(params=[
  9. ("notfound",),
  10. ("multiple", "keys", "not", "in", "task_vars"),
  11. ])
  12. def missing_keys(request):
  13. return request.param
  14. # Tests
  15. def test_OpenShiftCheck_init():
  16. class TestCheck(OpenShiftCheck):
  17. name = "test_check"
  18. run = NotImplemented
  19. # initialization requires at least one argument (apart from self)
  20. with pytest.raises(TypeError) as excinfo:
  21. TestCheck()
  22. assert 'execute_module' in str(excinfo.value)
  23. assert 'module_executor' in str(excinfo.value)
  24. execute_module = object()
  25. # initialize with positional argument
  26. check = TestCheck(execute_module)
  27. # new recommended name
  28. assert check.execute_module == execute_module
  29. # deprecated attribute name
  30. assert check.module_executor == execute_module
  31. # initialize with keyword argument, recommended name
  32. check = TestCheck(execute_module=execute_module)
  33. # new recommended name
  34. assert check.execute_module == execute_module
  35. # deprecated attribute name
  36. assert check.module_executor == execute_module
  37. # initialize with keyword argument, deprecated name
  38. check = TestCheck(module_executor=execute_module)
  39. # new recommended name
  40. assert check.execute_module == execute_module
  41. # deprecated attribute name
  42. assert check.module_executor == execute_module
  43. def test_subclasses():
  44. """OpenShiftCheck.subclasses should find all subclasses recursively."""
  45. class TestCheck1(OpenShiftCheck):
  46. pass
  47. class TestCheck2(OpenShiftCheck):
  48. pass
  49. class TestCheck1A(TestCheck1):
  50. pass
  51. local_subclasses = set([TestCheck1, TestCheck1A, TestCheck2])
  52. known_subclasses = set(OpenShiftCheck.subclasses())
  53. assert local_subclasses - known_subclasses == set(), "local_subclasses should be a subset of known_subclasses"
  54. def test_load_checks():
  55. """Loading checks should load and return Python modules."""
  56. modules = load_checks()
  57. assert modules
  58. @pytest.mark.parametrize("keys,expected", [
  59. (("foo",), 42),
  60. (("bar", "baz"), "openshift"),
  61. ])
  62. def test_get_var_ok(task_vars, keys, expected):
  63. assert get_var(task_vars, *keys) == expected
  64. def test_get_var_error(task_vars, missing_keys):
  65. with pytest.raises(OpenShiftCheckException):
  66. get_var(task_vars, *missing_keys)
  67. def test_get_var_default(task_vars, missing_keys):
  68. default = object()
  69. assert get_var(task_vars, *missing_keys, default=default) == default