1
0

test_beacons.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Justin Anderson <janderson@saltstack.com>
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import os
  7. import pytest
  8. from salt.exceptions import CommandExecutionError
  9. from tests.support.case import ModuleCase
  10. from tests.support.helpers import slowTest
  11. from tests.support.runtests import RUNTIME_VARS
  12. from tests.support.unit import skipIf
  13. @pytest.mark.windows_whitelisted
  14. class BeaconsAddDeleteTest(ModuleCase):
  15. """
  16. Tests the add and delete functions
  17. """
  18. def setUp(self):
  19. self.minion_conf_d_dir = os.path.join(
  20. RUNTIME_VARS.TMP_CONF_DIR,
  21. os.path.dirname(self.minion_opts["default_include"]),
  22. )
  23. if not os.path.isdir(self.minion_conf_d_dir):
  24. os.makedirs(self.minion_conf_d_dir)
  25. self.beacons_config_file_path = os.path.join(
  26. self.minion_conf_d_dir, "beacons.conf"
  27. )
  28. self.run_function("beacons.reset", f_timeout=300)
  29. def tearDown(self):
  30. if os.path.isfile(self.beacons_config_file_path):
  31. os.unlink(self.beacons_config_file_path)
  32. # Reset beacons
  33. self.run_function("beacons.reset", f_timeout=300)
  34. @slowTest
  35. def test_add_and_delete(self):
  36. """
  37. Test adding and deleting a beacon
  38. """
  39. _add = self.run_function(
  40. "beacons.add",
  41. ["ps", [{"processes": {"apache2": "stopped"}}]],
  42. f_timeout=300,
  43. )
  44. self.assertTrue(_add["result"])
  45. # save added beacon
  46. _save = self.run_function("beacons.save", f_timeout=300)
  47. self.assertTrue(_save["result"])
  48. # delete the beacon
  49. _delete = self.run_function("beacons.delete", ["ps"], f_timeout=300)
  50. self.assertTrue(_delete["result"])
  51. # save the results
  52. self.run_function("beacons.save", f_timeout=300)
  53. @slowTest
  54. def test_add_and_delete_beacon_module(self):
  55. """
  56. Test adding and deleting a beacon
  57. """
  58. _add = self.run_function(
  59. "beacons.add",
  60. [
  61. "watch_apache",
  62. [{"processes": {"apache2": "stopped"}}, {"beacon_module": "ps"}],
  63. ],
  64. f_timeout=300,
  65. )
  66. self.assertTrue(_add["result"])
  67. # save added beacon
  68. _save = self.run_function("beacons.save", f_timeout=300)
  69. self.assertTrue(_save["result"])
  70. # delete the beacon
  71. _delete = self.run_function("beacons.delete", ["ps"], f_timeout=300)
  72. self.assertTrue(_delete["result"])
  73. # save the results
  74. self.run_function("beacons.save", f_timeout=300)
  75. @pytest.mark.windows_whitelisted
  76. class BeaconsTest(ModuleCase):
  77. """
  78. Tests the beacons execution module
  79. """
  80. beacons_config_file_path = minion_conf_d_dir = None
  81. @classmethod
  82. def tearDownClass(cls):
  83. if cls.beacons_config_file_path and os.path.isfile(
  84. cls.beacons_config_file_path
  85. ):
  86. os.unlink(cls.beacons_config_file_path)
  87. def setUp(self):
  88. if self.minion_conf_d_dir is None:
  89. self.minion_conf_d_dir = os.path.join(
  90. RUNTIME_VARS.TMP_CONF_DIR,
  91. os.path.dirname(self.minion_opts["default_include"]),
  92. )
  93. if not os.path.isdir(self.minion_conf_d_dir):
  94. os.makedirs(self.minion_conf_d_dir)
  95. self.__class__.beacons_config_file_path = os.path.join(
  96. self.minion_conf_d_dir, "beacons.conf"
  97. )
  98. self.run_function("beacons.reset", f_timeout=300)
  99. try:
  100. # Add beacon to disable
  101. self.run_function(
  102. "beacons.add",
  103. ["ps", [{"processes": {"apache2": "stopped"}}]],
  104. f_timeout=300,
  105. )
  106. self.run_function("beacons.save", f_timeout=300)
  107. except CommandExecutionError:
  108. self.skipTest("Unable to add beacon")
  109. def tearDown(self):
  110. # delete added beacon
  111. self.run_function("beacons.delete", ["ps"], f_timeout=300)
  112. self.run_function("beacons.save", f_timeout=300)
  113. # Reset beacons
  114. self.run_function("beacons.reset", f_timeout=300)
  115. @slowTest
  116. def test_disable(self):
  117. """
  118. Test disabling beacons
  119. """
  120. # assert beacon exists
  121. _list = self.run_function("beacons.list", return_yaml=False, f_timeout=300)
  122. self.assertIn("ps", _list)
  123. ret = self.run_function("beacons.disable", f_timeout=300)
  124. self.assertTrue(ret["result"])
  125. # assert beacons are disabled
  126. _list = self.run_function("beacons.list", return_yaml=False, f_timeout=300)
  127. self.assertFalse(_list["enabled"])
  128. # disable added beacon
  129. ret = self.run_function("beacons.disable_beacon", ["ps"], f_timeout=300)
  130. self.assertTrue(ret["result"])
  131. # assert beacon ps is disabled
  132. _list = self.run_function("beacons.list", return_yaml=False, f_timeout=300)
  133. for bdict in _list["ps"]:
  134. if "enabled" in bdict:
  135. self.assertFalse(bdict["enabled"])
  136. break
  137. @slowTest
  138. def test_enable(self):
  139. """
  140. Test enabling beacons
  141. """
  142. # assert beacon exists
  143. _list = self.run_function("beacons.list", return_yaml=False, f_timeout=300)
  144. self.assertIn("ps", _list)
  145. # enable beacons on minion
  146. ret = self.run_function("beacons.enable", f_timeout=300)
  147. self.assertTrue(ret["result"])
  148. # assert beacons are enabled
  149. _list = self.run_function("beacons.list", return_yaml=False, f_timeout=300)
  150. self.assertTrue(_list["enabled"])
  151. @skipIf(
  152. True,
  153. "Skip until https://github.com/saltstack/salt/issues/31516 "
  154. "problems are resolved.",
  155. )
  156. def test_enabled_beacons(self):
  157. """
  158. Test enabled specific beacon
  159. """
  160. # enable added beacon
  161. ret = self.run_function("beacons.enable_beacon", ["ps"], f_timeout=300)
  162. self.assertTrue(ret["result"])
  163. # assert beacon ps is enabled
  164. _list = self.run_function("beacons.list", return_yaml=False, f_timeout=300)
  165. self.assertTrue(_list["ps"]["enabled"])
  166. @slowTest
  167. def test_list(self):
  168. """
  169. Test listing the beacons
  170. """
  171. # list beacons
  172. ret = self.run_function("beacons.list", return_yaml=False, f_timeout=300)
  173. if "enabled" in ret:
  174. self.assertEqual(
  175. ret, {"ps": [{"processes": {"apache2": "stopped"}}], "enabled": True}
  176. )
  177. else:
  178. self.assertEqual(ret, {"ps": [{"processes": {"apache2": "stopped"}}]})
  179. @slowTest
  180. def test_list_available(self):
  181. """
  182. Test listing the beacons
  183. """
  184. # list beacons
  185. ret = self.run_function(
  186. "beacons.list_available", return_yaml=False, f_timeout=300
  187. )
  188. self.assertTrue(ret)
  189. class BeaconsWithBeaconTypeTest(ModuleCase):
  190. """
  191. Tests the beacons execution module
  192. """
  193. beacons_config_file_path = minion_conf_d_dir = None
  194. @classmethod
  195. def tearDownClass(cls):
  196. if cls.beacons_config_file_path and os.path.isfile(
  197. cls.beacons_config_file_path
  198. ):
  199. os.unlink(cls.beacons_config_file_path)
  200. def setUp(self):
  201. if self.minion_conf_d_dir is None:
  202. self.minion_conf_d_dir = os.path.join(
  203. RUNTIME_VARS.TMP_CONF_DIR,
  204. os.path.dirname(self.minion_opts["default_include"]),
  205. )
  206. if not os.path.isdir(self.minion_conf_d_dir):
  207. os.makedirs(self.minion_conf_d_dir)
  208. self.__class__.beacons_config_file_path = os.path.join(
  209. self.minion_conf_d_dir, "beacons.conf"
  210. )
  211. self.run_function("beacons.reset", f_timeout=300)
  212. try:
  213. # Add beacon to disable
  214. self.run_function(
  215. "beacons.add",
  216. [
  217. "watch_apache",
  218. [{"processes": {"apache2": "stopped"}}, {"beacon_module": "ps"}],
  219. ],
  220. f_timeout=300,
  221. )
  222. self.run_function("beacons.save", f_timeout=300)
  223. except CommandExecutionError:
  224. self.skipTest("Unable to add beacon")
  225. def tearDown(self):
  226. # delete added beacon
  227. self.run_function("beacons.delete", ["watch_apache"], f_timeout=300)
  228. self.run_function("beacons.save", f_timeout=300)
  229. @slowTest
  230. def test_disable(self):
  231. """
  232. Test disabling beacons
  233. """
  234. ret = self.run_function("beacons.enable", f_timeout=300)
  235. self.assertTrue(ret["result"])
  236. # assert beacon exists
  237. _list = self.run_function("beacons.list", return_yaml=False, f_timeout=300)
  238. self.assertIn("watch_apache", _list)
  239. ret = self.run_function("beacons.disable", f_timeout=300)
  240. self.assertTrue(ret["result"])
  241. # assert beacons are disabled
  242. _list = self.run_function("beacons.list", return_yaml=False, f_timeout=300)
  243. self.assertFalse(_list["enabled"])
  244. # disable added beacon
  245. ret = self.run_function(
  246. "beacons.disable_beacon", ["watch_apache"], f_timeout=300
  247. )
  248. self.assertTrue(ret["result"])
  249. # assert beacon ps is disabled
  250. _list = self.run_function("beacons.list", return_yaml=False, f_timeout=300)
  251. for bdict in _list["watch_apache"]:
  252. if "enabled" in bdict:
  253. self.assertFalse(bdict["enabled"])
  254. break
  255. @slowTest
  256. def test_enable(self):
  257. """
  258. Test enabling beacons
  259. """
  260. # assert beacon exists
  261. _list = self.run_function("beacons.list", return_yaml=False, f_timeout=300)
  262. self.assertIn("watch_apache", _list)
  263. # enable beacons on minion
  264. ret = self.run_function("beacons.enable", f_timeout=300)
  265. self.assertTrue(ret["result"])
  266. # assert beacons are enabled
  267. _list = self.run_function("beacons.list", return_yaml=False, f_timeout=300)
  268. self.assertTrue(_list["enabled"])
  269. @skipIf(
  270. True,
  271. "Skip until https://github.com/saltstack/salt/issues/31516 problems are resolved.",
  272. )
  273. def test_enabled_beacons(self):
  274. """
  275. Test enabled specific beacon
  276. """
  277. # enable added beacon
  278. ret = self.run_function(
  279. "beacons.enable_beacon", ["watch_apache"], f_timeout=300
  280. )
  281. self.assertTrue(ret["result"])
  282. # assert beacon ps is enabled
  283. _list = self.run_function("beacons.list", return_yaml=False, f_timeout=300)
  284. self.assertTrue(_list["watch_apache"]["enabled"])
  285. @slowTest
  286. def test_list(self):
  287. """
  288. Test listing the beacons
  289. """
  290. # list beacons
  291. ret = self.run_function("beacons.list", return_yaml=False, f_timeout=300)
  292. _expected = {
  293. "watch_apache": [
  294. {"processes": {"apache2": "stopped"}},
  295. {"beacon_module": "ps"},
  296. ]
  297. }
  298. _enabled_expected = {
  299. "watch_apache": [
  300. {"processes": {"apache2": "stopped"}},
  301. {"beacon_module": "ps"},
  302. ],
  303. "enabled": True,
  304. }
  305. if "enabled" in ret:
  306. self.assertEqual(ret, _enabled_expected)
  307. else:
  308. self.assertEqual(ret, _expected)