test_saltutil.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Integration tests for the saltutil module.
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import os
  8. import time
  9. import textwrap
  10. # Import Salt Testing libs
  11. from tests.support.case import ModuleCase
  12. from tests.support.helpers import flaky
  13. from tests.support.paths import TMP_PILLAR_TREE
  14. from tests.support.unit import skipIf
  15. # Import Salt Libs
  16. import salt.utils.files
  17. import salt.utils.stringutils
  18. class SaltUtilModuleTest(ModuleCase):
  19. '''
  20. Testcase for the saltutil execution module
  21. '''
  22. def setUp(self):
  23. self.run_function('saltutil.refresh_pillar')
  24. # Tests for the wheel function
  25. def test_wheel_just_function(self):
  26. '''
  27. Tests using the saltutil.wheel function when passing only a function.
  28. '''
  29. # Wait for the pillar refresh to kick in, so that grains are ready to go
  30. time.sleep(3)
  31. ret = self.run_function('saltutil.wheel', ['minions.connected'])
  32. self.assertIn('minion', ret['return'])
  33. self.assertIn('sub_minion', ret['return'])
  34. def test_wheel_with_arg(self):
  35. '''
  36. Tests using the saltutil.wheel function when passing a function and an arg.
  37. '''
  38. ret = self.run_function('saltutil.wheel', ['key.list', 'minion'])
  39. self.assertEqual(ret['return'], {})
  40. def test_wheel_no_arg_raise_error(self):
  41. '''
  42. Tests using the saltutil.wheel function when passing a function that requires
  43. an arg, but one isn't supplied.
  44. '''
  45. self.assertRaises(TypeError, 'saltutil.wheel', ['key.list'])
  46. def test_wheel_with_kwarg(self):
  47. '''
  48. Tests using the saltutil.wheel function when passing a function and a kwarg.
  49. This function just generates a key pair, but doesn't do anything with it. We
  50. just need this for testing purposes.
  51. '''
  52. ret = self.run_function('saltutil.wheel', ['key.gen'], keysize=1024)
  53. self.assertIn('pub', ret['return'])
  54. self.assertIn('priv', ret['return'])
  55. class SaltUtilSyncModuleTest(ModuleCase):
  56. '''
  57. Testcase for the saltutil sync execution module
  58. '''
  59. def setUp(self):
  60. whitelist = {'modules': [], }
  61. self.run_function('saltutil.sync_all', extmod_whitelist=whitelist)
  62. def tearDown(self):
  63. self.run_function('saltutil.sync_all')
  64. def test_sync_all(self):
  65. '''
  66. Test syncing all ModuleCase
  67. '''
  68. expected_return = {'engines': [],
  69. 'clouds': [],
  70. 'grains': [],
  71. 'beacons': [],
  72. 'utils': [],
  73. 'returners': [],
  74. 'modules': ['modules.mantest',
  75. 'modules.override_test',
  76. 'modules.runtests_decorators',
  77. 'modules.runtests_helpers',
  78. 'modules.salttest'],
  79. 'renderers': [],
  80. 'log_handlers': [],
  81. 'matchers': [],
  82. 'states': [],
  83. 'sdb': [],
  84. 'proxymodules': [],
  85. 'executors': [],
  86. 'output': [],
  87. 'thorium': [],
  88. 'serializers': []}
  89. ret = self.run_function('saltutil.sync_all')
  90. self.assertEqual(ret, expected_return)
  91. def test_sync_all_whitelist(self):
  92. '''
  93. Test syncing all ModuleCase with whitelist
  94. '''
  95. expected_return = {'engines': [],
  96. 'clouds': [],
  97. 'grains': [],
  98. 'beacons': [],
  99. 'utils': [],
  100. 'returners': [],
  101. 'modules': ['modules.salttest'],
  102. 'renderers': [],
  103. 'log_handlers': [],
  104. 'matchers': [],
  105. 'states': [],
  106. 'sdb': [],
  107. 'proxymodules': [],
  108. 'executors': [],
  109. 'output': [],
  110. 'thorium': [],
  111. 'serializers': []}
  112. ret = self.run_function('saltutil.sync_all', extmod_whitelist={'modules': ['salttest']})
  113. self.assertEqual(ret, expected_return)
  114. def test_sync_all_blacklist(self):
  115. '''
  116. Test syncing all ModuleCase with blacklist
  117. '''
  118. expected_return = {'engines': [],
  119. 'clouds': [],
  120. 'grains': [],
  121. 'beacons': [],
  122. 'utils': [],
  123. 'returners': [],
  124. 'modules': ['modules.mantest',
  125. 'modules.override_test',
  126. 'modules.runtests_helpers',
  127. 'modules.salttest'],
  128. 'renderers': [],
  129. 'log_handlers': [],
  130. 'matchers': [],
  131. 'states': [],
  132. 'sdb': [],
  133. 'proxymodules': [],
  134. 'executors': [],
  135. 'output': [],
  136. 'thorium': [],
  137. 'serializers': []}
  138. ret = self.run_function('saltutil.sync_all', extmod_blacklist={'modules': ['runtests_decorators']})
  139. self.assertEqual(ret, expected_return)
  140. def test_sync_all_blacklist_and_whitelist(self):
  141. '''
  142. Test syncing all ModuleCase with whitelist and blacklist
  143. '''
  144. expected_return = {'engines': [],
  145. 'clouds': [],
  146. 'grains': [],
  147. 'beacons': [],
  148. 'utils': [],
  149. 'returners': [],
  150. 'executors': [],
  151. 'modules': [],
  152. 'renderers': [],
  153. 'log_handlers': [],
  154. 'matchers': [],
  155. 'states': [],
  156. 'sdb': [],
  157. 'proxymodules': [],
  158. 'output': [],
  159. 'thorium': [],
  160. 'serializers': []}
  161. ret = self.run_function('saltutil.sync_all', extmod_whitelist={'modules': ['runtests_decorators']},
  162. extmod_blacklist={'modules': ['runtests_decorators']})
  163. self.assertEqual(ret, expected_return)
  164. @skipIf(True, 'Pillar refresh test is flaky. Skipping for now.')
  165. class SaltUtilSyncPillarTest(ModuleCase):
  166. '''
  167. Testcase for the saltutil sync pillar module
  168. '''
  169. @flaky
  170. def test_pillar_refresh(self):
  171. '''
  172. test pillar refresh module
  173. '''
  174. pillar_key = 'itworked'
  175. pre_pillar = self.run_function('pillar.raw')
  176. self.assertNotIn(pillar_key, pre_pillar.get(pillar_key, 'didnotwork'))
  177. with salt.utils.files.fopen(os.path.join(TMP_PILLAR_TREE, 'add_pillar.sls'), 'w') as fp:
  178. fp.write(salt.utils.stringutils.to_str(
  179. '{0}: itworked'.format(pillar_key)
  180. ))
  181. with salt.utils.files.fopen(os.path.join(TMP_PILLAR_TREE, 'top.sls'), 'w') as fp:
  182. fp.write(textwrap.dedent('''\
  183. base:
  184. '*':
  185. - add_pillar
  186. '''))
  187. self.run_function('saltutil.refresh_pillar')
  188. pillar = False
  189. timeout = time.time() + 30
  190. while not pillar:
  191. post_pillar = self.run_function('pillar.raw')
  192. try:
  193. self.assertIn(pillar_key, post_pillar.get(pillar_key, 'didnotwork'))
  194. pillar = True
  195. except AssertionError:
  196. if time.time() > timeout:
  197. self.assertIn(pillar_key, post_pillar.get(pillar_key, 'didnotwork'))
  198. continue
  199. post_pillar = self.run_function('pillar.raw')
  200. self.assertIn(pillar_key, post_pillar.get(pillar_key, 'didnotwork'))
  201. def tearDown(self):
  202. for filename in os.listdir(TMP_PILLAR_TREE):
  203. os.remove(os.path.join(TMP_PILLAR_TREE, filename))