test_reactor.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. """
  2. unit tests for the reactor runner
  3. """
  4. # Import Python Libs
  5. import logging
  6. import salt.runners.reactor as reactor
  7. from salt.exceptions import CommandExecutionError
  8. # Import Salt Libs
  9. from salt.utils.event import SaltEvent
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import MagicMock, patch
  13. from tests.support.unit import TestCase
  14. log = logging.getLogger(__name__)
  15. class MockEvent:
  16. """
  17. Mock event class
  18. """
  19. flag = None
  20. def __init__(self):
  21. self.full = None
  22. def get_event(self, wait, tag):
  23. """
  24. Mock get_event method
  25. """
  26. data = []
  27. return {"tag": tag, "data": data}
  28. def fire_event(self, data, tag):
  29. """
  30. Mock get_event method
  31. """
  32. return {"tag": tag, "data": data}
  33. class ReactorTest(TestCase, LoaderModuleMockMixin):
  34. """
  35. Validate the reactor runner
  36. """
  37. def setup_loader_modules(self):
  38. return {
  39. reactor: {
  40. "__opts__": {
  41. "reactor": [],
  42. "engines": [],
  43. "id": "master_id",
  44. "sock_dir": "/var/run/salt/master",
  45. "transport": "zeromq",
  46. },
  47. "__jid_event__": MockEvent(),
  48. }
  49. }
  50. def test_list(self):
  51. """
  52. test reactor.list runner
  53. """
  54. with self.assertRaises(CommandExecutionError) as excinfo:
  55. ret = reactor.list_()
  56. self.assertEqual(excinfo.exception.strerror, "Reactor system is not running.")
  57. mock_opts = {}
  58. mock_opts = {"engines": []}
  59. with patch.dict(reactor.__opts__, mock_opts):
  60. with self.assertRaises(CommandExecutionError) as excinfo:
  61. ret = reactor.list_()
  62. self.assertEqual(
  63. excinfo.exception.strerror, "Reactor system is not running."
  64. )
  65. mock_opts = {}
  66. mock_opts["engines"] = [
  67. {
  68. "reactor": {
  69. "refresh_interval": 60,
  70. "worker_threads": 10,
  71. "worker_hwm": 10000,
  72. }
  73. }
  74. ]
  75. event_returns = {
  76. "reactors": [{"test_event/*": ["/srv/reactors/reactor.sls"]}],
  77. "_stamp": "2020-09-04T16:51:52.577711",
  78. }
  79. with patch.dict(reactor.__opts__, mock_opts):
  80. with patch.object(SaltEvent, "get_event", return_value=event_returns):
  81. with patch("salt.utils.master.get_master_key") as get_master_key:
  82. get_master_key.retun_value = MagicMock(retun_value="master_key")
  83. ret = reactor.list_()
  84. self.assertIn({"test_event/*": ["/srv/reactors/reactor.sls"]}, ret)
  85. mock_opts = {}
  86. mock_opts["reactor"] = [{"test_event/*": ["/srv/reactors/reactor.sls"]}]
  87. with patch.dict(reactor.__opts__, mock_opts):
  88. with patch.dict(reactor.__opts__, mock_opts):
  89. event_returns = {
  90. "reactors": [{"test_event/*": ["/srv/reactors/reactor.sls"]}],
  91. "_stamp": "2020-09-04T16:51:52.577711",
  92. }
  93. with patch.object(SaltEvent, "get_event", return_value=event_returns):
  94. with patch("salt.utils.master.get_master_key") as get_master_key:
  95. get_master_key.retun_value = MagicMock(retun_value="master_key")
  96. ret = reactor.list_()
  97. self.assertIn({"test_event/*": ["/srv/reactors/reactor.sls"]}, ret)
  98. def test_add(self):
  99. """
  100. test reactor.add runner
  101. """
  102. with self.assertRaises(CommandExecutionError) as excinfo:
  103. ret = reactor.add(
  104. "salt/cloud/*/destroyed", reactors="/srv/reactor/destroy/*.sls"
  105. )
  106. self.assertEqual(excinfo.exception.strerror, "Reactor system is not running.")
  107. mock_opts = {}
  108. mock_opts = {"engines": []}
  109. with patch.dict(reactor.__opts__, mock_opts):
  110. with self.assertRaises(CommandExecutionError) as excinfo:
  111. ret = reactor.add(
  112. "salt/cloud/*/destroyed", reactors="/srv/reactor/destroy/*.sls"
  113. )
  114. self.assertEqual(
  115. excinfo.exception.strerror, "Reactor system is not running."
  116. )
  117. mock_opts = {}
  118. mock_opts["engines"] = [
  119. {
  120. "reactor": {
  121. "refresh_interval": 60,
  122. "worker_threads": 10,
  123. "worker_hwm": 10000,
  124. }
  125. }
  126. ]
  127. event_returns = {
  128. "reactors": [{"test_event/*": ["/srv/reactors/reactor.sls"]}],
  129. "result": {"status": True, "comment": "Reactor added."},
  130. "_stamp": "2020-09-04T17:45:33.206408",
  131. }
  132. with patch.dict(reactor.__opts__, mock_opts):
  133. with patch.object(SaltEvent, "get_event", return_value=event_returns):
  134. with patch("salt.utils.master.get_master_key") as get_master_key:
  135. get_master_key.retun_value = MagicMock(retun_value="master_key")
  136. ret = reactor.add("test_event/*", "/srv/reactor/reactor.sls")
  137. self.assertIn("status", ret)
  138. self.assertTrue(ret["status"])
  139. self.assertEqual("Reactor added.", ret["comment"])
  140. def test_delete(self):
  141. """
  142. test reactor.delete runner
  143. """
  144. with self.assertRaises(CommandExecutionError) as excinfo:
  145. ret = reactor.delete("salt/cloud/*/destroyed")
  146. self.assertEqual(excinfo.exception.strerror, "Reactor system is not running.")
  147. mock_opts = {}
  148. mock_opts = {"engines": []}
  149. with patch.dict(reactor.__opts__, mock_opts):
  150. with self.assertRaises(CommandExecutionError) as excinfo:
  151. ret = reactor.delete("salt/cloud/*/destroyed")
  152. self.assertEqual(
  153. excinfo.exception.strerror, "Reactor system is not running."
  154. )
  155. mock_opts = {}
  156. mock_opts["engines"] = [
  157. {
  158. "reactor": {
  159. "refresh_interval": 60,
  160. "worker_threads": 10,
  161. "worker_hwm": 10000,
  162. }
  163. }
  164. ]
  165. event_returns = {
  166. "reactors": [{"bot/*": ["/srv/reactors/bot.sls"]}],
  167. "result": {"status": True, "comment": "Reactor deleted."},
  168. "_stamp": "2020-09-04T18:15:41.586552",
  169. }
  170. with patch.dict(reactor.__opts__, mock_opts):
  171. with patch.object(SaltEvent, "get_event", return_value=event_returns):
  172. with patch("salt.utils.master.get_master_key") as get_master_key:
  173. get_master_key.retun_value = MagicMock(retun_value="master_key")
  174. ret = reactor.delete("test_event/*")
  175. self.assertIn("status", ret)
  176. self.assertTrue(ret["status"])
  177. self.assertEqual("Reactor deleted.", ret["comment"])
  178. def test_is_leader(self):
  179. """
  180. test reactor.is_leader runner
  181. """
  182. with self.assertRaises(CommandExecutionError) as excinfo:
  183. ret = reactor.is_leader()
  184. self.assertEqual(excinfo.exception.strerror, "Reactor system is not running.")
  185. mock_opts = {}
  186. mock_opts = {"engines": []}
  187. with patch.dict(reactor.__opts__, mock_opts):
  188. with self.assertRaises(CommandExecutionError) as excinfo:
  189. ret = reactor.is_leader()
  190. self.assertEqual(
  191. excinfo.exception.strerror, "Reactor system is not running."
  192. )
  193. mock_opts = {}
  194. mock_opts["engines"] = [
  195. {
  196. "reactor": {
  197. "refresh_interval": 60,
  198. "worker_threads": 10,
  199. "worker_hwm": 10000,
  200. }
  201. }
  202. ]
  203. event_returns = {"result": True, "_stamp": "2020-09-04T18:32:10.004490"}
  204. with patch.dict(reactor.__opts__, mock_opts):
  205. with patch.object(SaltEvent, "get_event", return_value=event_returns):
  206. with patch("salt.utils.master.get_master_key") as get_master_key:
  207. get_master_key.retun_value = MagicMock(retun_value="master_key")
  208. ret = reactor.is_leader()
  209. self.assertTrue(ret)
  210. def test_set_leader(self):
  211. """
  212. test reactor.set_leader runner
  213. """
  214. with self.assertRaises(CommandExecutionError) as excinfo:
  215. ret = reactor.set_leader()
  216. self.assertEqual(excinfo.exception.strerror, "Reactor system is not running.")
  217. mock_opts = {}
  218. mock_opts = {"engines": []}
  219. with patch.dict(reactor.__opts__, mock_opts):
  220. with self.assertRaises(CommandExecutionError) as excinfo:
  221. ret = reactor.set_leader()
  222. self.assertEqual(
  223. excinfo.exception.strerror, "Reactor system is not running."
  224. )
  225. mock_opts = {}
  226. mock_opts["engines"] = [
  227. {
  228. "reactor": {
  229. "refresh_interval": 60,
  230. "worker_threads": 10,
  231. "worker_hwm": 10000,
  232. }
  233. }
  234. ]
  235. event_returns = {"result": True, "_stamp": "2020-09-04T18:32:10.004490"}
  236. with patch.dict(reactor.__opts__, mock_opts):
  237. with patch.object(SaltEvent, "get_event", return_value=event_returns):
  238. with patch("salt.utils.master.get_master_key") as get_master_key:
  239. get_master_key.retun_value = MagicMock(retun_value="master_key")
  240. ret = reactor.set_leader()
  241. self.assertTrue(ret)