test_saltutil.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. # -*- coding: utf-8 -*-
  2. """
  3. Integration tests for the saltutil module.
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import os
  7. import shutil
  8. import textwrap
  9. import time
  10. import pytest
  11. import salt.utils.files
  12. import salt.utils.stringutils
  13. from tests.support.case import ModuleCase
  14. from tests.support.runtests import RUNTIME_VARS
  15. from tests.support.unit import skipIf
  16. @pytest.mark.windows_whitelisted
  17. @pytest.mark.usefixtures("salt_sub_minion")
  18. class SaltUtilModuleTest(ModuleCase):
  19. """
  20. Testcase for the saltutil execution module
  21. """
  22. @classmethod
  23. def setUpClass(cls):
  24. # Whell functions, on a minion, must run with the master running
  25. # along side the minion.
  26. # We copy the master config to the minion's configuration directory just
  27. # for this test since the test suite master and minion(s) do not share the
  28. # same configuration directory
  29. src = os.path.join(RUNTIME_VARS.TMP_CONF_DIR, "master")
  30. dst = os.path.join(RUNTIME_VARS.TMP_MINION_CONF_DIR, "master")
  31. shutil.copyfile(src, dst)
  32. cls.copied_master_config_file = dst
  33. @classmethod
  34. def tearDownClass(cls):
  35. if os.path.exists(cls.copied_master_config_file):
  36. os.unlink(cls.copied_master_config_file)
  37. cls.copied_master_config_file = None
  38. def setUp(self):
  39. self.run_function("saltutil.refresh_pillar")
  40. # Tests for the wheel function
  41. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  42. def test_wheel_just_function(self):
  43. """
  44. Tests using the saltutil.wheel function when passing only a function.
  45. """
  46. # Wait for the pillar refresh to kick in, so that grains are ready to go
  47. time.sleep(3)
  48. ret = self.run_function("saltutil.wheel", ["minions.connected"])
  49. self.assertIn("minion", ret["return"])
  50. self.assertIn("sub_minion", ret["return"])
  51. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  52. def test_wheel_with_arg(self):
  53. """
  54. Tests using the saltutil.wheel function when passing a function and an arg.
  55. """
  56. ret = self.run_function("saltutil.wheel", ["key.list", "minion"])
  57. self.assertEqual(ret["return"], {})
  58. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  59. def test_wheel_no_arg_raise_error(self):
  60. """
  61. Tests using the saltutil.wheel function when passing a function that requires
  62. an arg, but one isn't supplied.
  63. """
  64. self.assertRaises(TypeError, "saltutil.wheel", ["key.list"])
  65. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  66. def test_wheel_with_kwarg(self):
  67. """
  68. Tests using the saltutil.wheel function when passing a function and a kwarg.
  69. This function just generates a key pair, but doesn't do anything with it. We
  70. just need this for testing purposes.
  71. """
  72. ret = self.run_function("saltutil.wheel", ["key.gen"], keysize=1024)
  73. self.assertIn("pub", ret["return"])
  74. self.assertIn("priv", ret["return"])
  75. @pytest.mark.windows_whitelisted
  76. class SyncGrainsTest(ModuleCase):
  77. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  78. def test_sync_grains(self):
  79. ret = self.run_function("saltutil.sync_grains")
  80. self.assertEqual(ret, [])
  81. @pytest.mark.windows_whitelisted
  82. class SaltUtilSyncModuleTest(ModuleCase):
  83. """
  84. Testcase for the saltutil sync execution module
  85. """
  86. def setUp(self):
  87. whitelist = {
  88. "modules": [],
  89. }
  90. self.run_function("saltutil.sync_all", extmod_whitelist=whitelist)
  91. def tearDown(self):
  92. self.run_function("saltutil.sync_all")
  93. @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
  94. def test_sync_all(self):
  95. """
  96. Test syncing all ModuleCase
  97. """
  98. expected_return = {
  99. "engines": [],
  100. "clouds": [],
  101. "grains": [],
  102. "beacons": [],
  103. "utils": [],
  104. "returners": [],
  105. "modules": [
  106. "modules.depends_versioned",
  107. "modules.depends_versionless",
  108. "modules.mantest",
  109. "modules.override_test",
  110. "modules.runtests_decorators",
  111. "modules.runtests_helpers",
  112. "modules.salttest",
  113. ],
  114. "renderers": [],
  115. "log_handlers": [],
  116. "matchers": [],
  117. "states": [],
  118. "sdb": [],
  119. "proxymodules": [],
  120. "executors": [],
  121. "output": [],
  122. "thorium": [],
  123. "serializers": [],
  124. }
  125. ret = self.run_function("saltutil.sync_all")
  126. self.assertEqual(ret, expected_return)
  127. @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
  128. def test_sync_all_whitelist(self):
  129. """
  130. Test syncing all ModuleCase with whitelist
  131. """
  132. expected_return = {
  133. "engines": [],
  134. "clouds": [],
  135. "grains": [],
  136. "beacons": [],
  137. "utils": [],
  138. "returners": [],
  139. "modules": ["modules.salttest"],
  140. "renderers": [],
  141. "log_handlers": [],
  142. "matchers": [],
  143. "states": [],
  144. "sdb": [],
  145. "proxymodules": [],
  146. "executors": [],
  147. "output": [],
  148. "thorium": [],
  149. "serializers": [],
  150. }
  151. ret = self.run_function(
  152. "saltutil.sync_all", extmod_whitelist={"modules": ["salttest"]}
  153. )
  154. self.assertEqual(ret, expected_return)
  155. @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
  156. def test_sync_all_blacklist(self):
  157. """
  158. Test syncing all ModuleCase with blacklist
  159. """
  160. expected_return = {
  161. "engines": [],
  162. "clouds": [],
  163. "grains": [],
  164. "beacons": [],
  165. "utils": [],
  166. "returners": [],
  167. "modules": [
  168. "modules.mantest",
  169. "modules.override_test",
  170. "modules.runtests_helpers",
  171. "modules.salttest",
  172. ],
  173. "renderers": [],
  174. "log_handlers": [],
  175. "matchers": [],
  176. "states": [],
  177. "sdb": [],
  178. "proxymodules": [],
  179. "executors": [],
  180. "output": [],
  181. "thorium": [],
  182. "serializers": [],
  183. }
  184. ret = self.run_function(
  185. "saltutil.sync_all",
  186. extmod_blacklist={
  187. "modules": [
  188. "runtests_decorators",
  189. "depends_versioned",
  190. "depends_versionless",
  191. ]
  192. },
  193. )
  194. self.assertEqual(ret, expected_return)
  195. @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
  196. def test_sync_all_blacklist_and_whitelist(self):
  197. """
  198. Test syncing all ModuleCase with whitelist and blacklist
  199. """
  200. expected_return = {
  201. "engines": [],
  202. "clouds": [],
  203. "grains": [],
  204. "beacons": [],
  205. "utils": [],
  206. "returners": [],
  207. "executors": [],
  208. "modules": [],
  209. "renderers": [],
  210. "log_handlers": [],
  211. "matchers": [],
  212. "states": [],
  213. "sdb": [],
  214. "proxymodules": [],
  215. "output": [],
  216. "thorium": [],
  217. "serializers": [],
  218. }
  219. ret = self.run_function(
  220. "saltutil.sync_all",
  221. extmod_whitelist={"modules": ["runtests_decorators"]},
  222. extmod_blacklist={"modules": ["runtests_decorators"]},
  223. )
  224. self.assertEqual(ret, expected_return)
  225. @skipIf(True, "Pillar refresh test is flaky. Skipping for now.")
  226. @pytest.mark.windows_whitelisted
  227. class SaltUtilSyncPillarTest(ModuleCase):
  228. """
  229. Testcase for the saltutil sync pillar module
  230. """
  231. @pytest.mark.flaky(max_runs=4)
  232. def test_pillar_refresh(self):
  233. """
  234. test pillar refresh module
  235. """
  236. pillar_key = "itworked"
  237. pre_pillar = self.run_function("pillar.raw")
  238. self.assertNotIn(pillar_key, pre_pillar.get(pillar_key, "didnotwork"))
  239. with salt.utils.files.fopen(
  240. os.path.join(RUNTIME_VARS.TMP_PILLAR_TREE, "add_pillar.sls"), "w"
  241. ) as fp:
  242. fp.write(salt.utils.stringutils.to_str("{0}: itworked".format(pillar_key)))
  243. with salt.utils.files.fopen(
  244. os.path.join(RUNTIME_VARS.TMP_PILLAR_TREE, "top.sls"), "w"
  245. ) as fp:
  246. fp.write(
  247. textwrap.dedent(
  248. """\
  249. base:
  250. '*':
  251. - add_pillar
  252. """
  253. )
  254. )
  255. self.run_function("saltutil.refresh_pillar")
  256. pillar = False
  257. timeout = time.time() + 30
  258. while not pillar:
  259. post_pillar = self.run_function("pillar.raw")
  260. try:
  261. self.assertIn(pillar_key, post_pillar.get(pillar_key, "didnotwork"))
  262. pillar = True
  263. except AssertionError:
  264. if time.time() > timeout:
  265. self.assertIn(pillar_key, post_pillar.get(pillar_key, "didnotwork"))
  266. continue
  267. post_pillar = self.run_function("pillar.raw")
  268. self.assertIn(pillar_key, post_pillar.get(pillar_key, "didnotwork"))
  269. def tearDown(self):
  270. for filename in os.listdir(RUNTIME_VARS.TMP_PILLAR_TREE):
  271. os.remove(os.path.join(RUNTIME_VARS.TMP_PILLAR_TREE, filename))