test_ansible.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import os
  5. # Import Salt Libs
  6. import salt.config
  7. import salt.loader
  8. import salt.roster.ansible as ansible
  9. from tests.support import mixins
  10. # Import Salt Testing Libs
  11. from tests.support.mock import patch
  12. from tests.support.runtests import RUNTIME_VARS
  13. from tests.support.unit import TestCase, skipIf
  14. EXPECTED = {
  15. "host1": {
  16. "host": "host1",
  17. "passwd": "test123",
  18. "minion_opts": {
  19. "escape_pods": 2,
  20. "halon_system_timeout": 30,
  21. "self_destruct_countdown": 60,
  22. "some_server": "foo.southeast.example.com",
  23. },
  24. },
  25. "host2": {
  26. "host": "host2",
  27. "passwd": "test123",
  28. "minion_opts": {
  29. "escape_pods": 2,
  30. "halon_system_timeout": 30,
  31. "self_destruct_countdown": 60,
  32. "some_server": "foo.southeast.example.com",
  33. },
  34. },
  35. "host3": {
  36. "host": "host3",
  37. "passwd": "test123",
  38. "minion_opts": {
  39. "escape_pods": 2,
  40. "halon_system_timeout": 30,
  41. "self_destruct_countdown": 60,
  42. "some_server": "foo.southeast.example.com",
  43. },
  44. },
  45. }
  46. @skipIf(
  47. not salt.utils.path.which("ansible-inventory"),
  48. "Skipping because ansible-inventory is not available",
  49. )
  50. class AnsibleRosterTestCase(TestCase, mixins.LoaderModuleMockMixin):
  51. @classmethod
  52. def setUpClass(cls):
  53. cls.roster_dir = os.path.join(
  54. RUNTIME_VARS.TESTS_DIR, "unit/files/rosters/ansible/"
  55. )
  56. cls.opts = {"roster_defaults": {"passwd": "test123"}}
  57. @classmethod
  58. def tearDownClass(cls):
  59. delattr(cls, "roster_dir")
  60. delattr(cls, "opts")
  61. def setup_loader_modules(self):
  62. opts = salt.config.master_config(
  63. os.path.join(RUNTIME_VARS.TMP_CONF_DIR, "master")
  64. )
  65. utils = salt.loader.utils(opts, whitelist=["json", "stringutils"])
  66. runner = salt.loader.runner(opts, utils=utils, whitelist=["salt"])
  67. return {ansible: {"__utils__": utils, "__opts__": {}, "__runner__": runner}}
  68. def test_ini(self):
  69. self.opts["roster_file"] = os.path.join(self.roster_dir, "roster.ini")
  70. with patch.dict(ansible.__opts__, self.opts):
  71. ret = ansible.targets("*")
  72. assert ret == EXPECTED
  73. def test_yml(self):
  74. self.opts["roster_file"] = os.path.join(self.roster_dir, "roster.yml")
  75. with patch.dict(ansible.__opts__, self.opts):
  76. ret = ansible.targets("*")
  77. assert ret == EXPECTED
  78. def test_script(self):
  79. self.opts["roster_file"] = os.path.join(self.roster_dir, "roster.py")
  80. with patch.dict(ansible.__opts__, self.opts):
  81. ret = ansible.targets("*")
  82. assert ret == EXPECTED