1
0

test_saltutil.py 8.8 KB

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