test_ansible.py 3.0 KB

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