test_ansible.py 2.8 KB

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