test_ansiblegate.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Author: Bo Maryniuk <bo@suse.de>
  4. from __future__ import absolute_import, print_function, unicode_literals
  5. import os
  6. import pytest
  7. import salt.modules.ansiblegate as ansible
  8. import salt.utils.platform
  9. from salt.exceptions import LoaderError
  10. from tests.support.mock import MagicMock, patch
  11. pytestmark = pytest.mark.skipif(
  12. salt.utils.platform.is_windows(), reason="Not supported on Windows"
  13. )
  14. @pytest.fixture
  15. def resolver():
  16. _resolver = ansible.AnsibleModuleResolver({})
  17. _resolver._modules_map = {
  18. "one.two.three": os.sep + os.path.join("one", "two", "three.py"),
  19. "four.five.six": os.sep + os.path.join("four", "five", "six.py"),
  20. "three.six.one": os.sep + os.path.join("three", "six", "one.py"),
  21. }
  22. return _resolver
  23. @pytest.fixture(autouse=True)
  24. def setup_loader(request):
  25. setup_loader_modules = {ansible: {}}
  26. with pytest.helpers.loader_mock(request, setup_loader_modules) as loader_mock:
  27. yield loader_mock
  28. def test_ansible_module_help(resolver):
  29. """
  30. Test help extraction from the module
  31. :return:
  32. """
  33. class Module(object):
  34. """
  35. An ansible module mock.
  36. """
  37. __name__ = "foo"
  38. DOCUMENTATION = """
  39. ---
  40. one:
  41. text here
  42. ---
  43. two:
  44. text here
  45. description:
  46. describe the second part
  47. """
  48. with patch.object(ansible, "_resolver", resolver), patch.object(
  49. ansible._resolver, "load_module", MagicMock(return_value=Module())
  50. ):
  51. ret = ansible.help("dummy")
  52. assert sorted(
  53. ret.get('Available sections on module "{0}"'.format(Module().__name__))
  54. ) == ["one", "two"]
  55. assert ret.get("Description") == "describe the second part"
  56. def test_module_resolver_modlist(resolver):
  57. """
  58. Test Ansible resolver modules list.
  59. :return:
  60. """
  61. assert resolver.get_modules_list() == [
  62. "four.five.six",
  63. "one.two.three",
  64. "three.six.one",
  65. ]
  66. for ptr in ["five", "fi", "ve"]:
  67. assert resolver.get_modules_list(ptr) == ["four.five.six"]
  68. for ptr in ["si", "ix", "six"]:
  69. assert resolver.get_modules_list(ptr) == ["four.five.six", "three.six.one"]
  70. assert resolver.get_modules_list("one") == ["one.two.three", "three.six.one"]
  71. assert resolver.get_modules_list("one.two") == ["one.two.three"]
  72. assert resolver.get_modules_list("four") == ["four.five.six"]
  73. def test_resolver_module_loader_failure(resolver):
  74. """
  75. Test Ansible module loader.
  76. :return:
  77. """
  78. mod = "four.five.six"
  79. with pytest.raises(ImportError) as import_error:
  80. resolver.load_module(mod)
  81. mod = "i.even.do.not.exist.at.all"
  82. with pytest.raises(LoaderError) as loader_error:
  83. resolver.load_module(mod)
  84. def test_resolver_module_loader(resolver):
  85. """
  86. Test Ansible module loader.
  87. :return:
  88. """
  89. with patch("salt.modules.ansiblegate.importlib", MagicMock()), patch(
  90. "salt.modules.ansiblegate.importlib.import_module", lambda x: x
  91. ):
  92. assert resolver.load_module("four.five.six") == "ansible.modules.four.five.six"
  93. def test_resolver_module_loader_import_failure(resolver):
  94. """
  95. Test Ansible module loader failure.
  96. :return:
  97. """
  98. with patch("salt.modules.ansiblegate.importlib", MagicMock()), patch(
  99. "salt.modules.ansiblegate.importlib.import_module", lambda x: x
  100. ):
  101. with pytest.raises(LoaderError) as loader_error:
  102. resolver.load_module("something.strange")
  103. def test_virtual_function(resolver):
  104. """
  105. Test Ansible module __virtual__ when ansible is not installed on the minion.
  106. :return:
  107. """
  108. with patch("salt.modules.ansiblegate.ansible", None):
  109. assert ansible.__virtual__() == "ansible"