sanity_check_test.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import os
  2. import pytest
  3. import sys
  4. from ansible.playbook.play_context import PlayContext
  5. from ansible.template import Templar
  6. from ansible import errors
  7. sys.path.insert(1, os.path.join(os.path.dirname(__file__), os.pardir, "action_plugins"))
  8. from sanity_checks import ActionModule # noqa: E402
  9. @pytest.mark.parametrize('hostvars, host, varname, result', [
  10. ({"example.com": {"param": 3.11}}, "example.com", "param", 3.11),
  11. ({"example.com": {"param": 3.11}}, "example.com", "another_param", None)
  12. ])
  13. def test_template_var(hostvars, host, varname, result):
  14. task = FakeTask('sanity_checks', {'checks': []})
  15. plugin = ActionModule(task, None, PlayContext(), None, Templar(None, None, None), None)
  16. check = plugin.template_var(hostvars, host, varname)
  17. assert check == result
  18. @pytest.mark.parametrize('hostvars, host, result', [
  19. ({"example.com": {"openshift_pkg_version": "-3.6.0"}}, "example.com", None),
  20. ({"example.com": {"openshift_pkg_version": "-3.7.0-0.126.0.git.0.9351aae.el7"}}, "example.com", None),
  21. ({"example.com": {"openshift_pkg_version": "-3.9.0-2.fc28"}}, "example.com", None),
  22. ({"example.com": {"openshift_pkg_version": "-3.11*"}}, "example.com", None),
  23. ({"example.com": {"openshift_pkg_version": "-3"}}, "example.com", None),
  24. ])
  25. def test_valid_check_pkg_version_format(hostvars, host, result):
  26. task = FakeTask('sanity_checks', {'checks': []})
  27. plugin = ActionModule(task, None, PlayContext(), None, Templar(None, None, None), None)
  28. check = plugin.check_pkg_version_format(hostvars, host)
  29. assert check == result
  30. @pytest.mark.parametrize('hostvars, host, result', [
  31. ({"example.com": {"openshift_pkg_version": "3.11.0"}}, "example.com", None),
  32. ({"example.com": {"openshift_pkg_version": "v3.11.0"}}, "example.com", None),
  33. ])
  34. def test_invalid_check_pkg_version_format(hostvars, host, result):
  35. with pytest.raises(errors.AnsibleModuleError):
  36. task = FakeTask('sanity_checks', {'checks': []})
  37. plugin = ActionModule(task, None, PlayContext(), None, Templar(None, None, None), None)
  38. plugin.check_pkg_version_format(hostvars, host)
  39. @pytest.mark.parametrize('hostvars, host, result', [
  40. ({"example.com": {"openshift_release": "v3"}}, "example.com", None),
  41. ({"example.com": {"openshift_release": "v3.11"}}, "example.com", None),
  42. ({"example.com": {"openshift_release": "v3.11.0"}}, "example.com", None),
  43. ({"example.com": {"openshift_release": "3.11"}}, "example.com", None),
  44. ])
  45. def test_valid_check_release_format(hostvars, host, result):
  46. task = FakeTask('sanity_checks', {'checks': []})
  47. plugin = ActionModule(task, None, PlayContext(), None, Templar(None, None, None), None)
  48. check = plugin.check_release_format(hostvars, host)
  49. assert check == result
  50. @pytest.mark.parametrize('hostvars, host, result', [
  51. ({"example.com": {"openshift_release": "-3.11.0"}}, "example.com", None),
  52. ({"example.com": {"openshift_release": "-3.7.0-0.126.0.git.0.9351aae.el7"}}, "example.com", None),
  53. ({"example.com": {"openshift_release": "3.1.2.3"}}, "example.com", None),
  54. ])
  55. def test_invalid_check_release_format(hostvars, host, result):
  56. with pytest.raises(errors.AnsibleModuleError):
  57. task = FakeTask('sanity_checks', {'checks': []})
  58. plugin = ActionModule(task, None, PlayContext(), None, Templar(None, None, None), None)
  59. plugin.check_release_format(hostvars, host)
  60. @pytest.mark.parametrize('hostvars, host, result', [
  61. ({"example.com": {"openshift_builddefaults_json": "{}"}}, "example.com", None),
  62. ({"example.com": {"openshift_builddefaults_json": '[]'}}, "example.com", None),
  63. ({"example.com": {"openshift_builddefaults_json": '{"a": []}'}}, "example.com", None),
  64. ({"example.com": {"openshift_builddefaults_json": '{"a": [], "b": "c"}'}}, "example.com", None),
  65. ({"example.com": {"openshift_builddefaults_json": '{"a": [], "b": {"c": "d"}}'}}, "example.com", None),
  66. ({"example.com": {"openshift_builddefaults_json": '["a", "b", "c"]'}}, "example.com", None),
  67. ({"example.com": {"NOT_IN_JSON_FORMAT_VARIABLES": '{"invalid"}'}}, "example.com", None),
  68. ])
  69. def test_valid_valid_json_format_vars(hostvars, host, result):
  70. task = FakeTask('sanity_checks', {'checks': []})
  71. plugin = ActionModule(task, None, PlayContext(), None, Templar(None, None, None), None)
  72. check = plugin.validate_json_format_vars(hostvars, host)
  73. assert check == result
  74. @pytest.mark.parametrize('hostvars, host, result', [
  75. ({"example.com": {"openshift_builddefaults_json": '{"a"}'}}, "example.com", None),
  76. ({"example.com": {"openshift_builddefaults_json": '{"a": { '}}, "example.com", None),
  77. ({"example.com": {"openshift_builddefaults_json": '{"a": [ }'}}, "example.com", None),
  78. ])
  79. def test_invalid_valid_json_format_vars(hostvars, host, result):
  80. with pytest.raises(errors.AnsibleModuleError):
  81. task = FakeTask('sanity_checks', {'checks': []})
  82. plugin = ActionModule(task, None, PlayContext(), None, Templar(None, None, None), None)
  83. plugin.validate_json_format_vars(hostvars, host)
  84. def fake_execute_module(*args):
  85. raise AssertionError('this function should not be called')
  86. class FakeTask(object):
  87. def __init__(self, action, args):
  88. self.action = action
  89. self.args = args
  90. self.async = 0