test_zabbix_action.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: :email:`Jakub Sliva <jakub.sliva@ultimum.io>`
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import, unicode_literals
  7. import salt.states.zabbix_action as zabbix_action
  8. # Import Salt Testing Libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.mock import MagicMock, patch
  11. from tests.support.unit import TestCase
  12. INPUT_PARAMS = {
  13. "status": "0",
  14. "filter": {
  15. "evaltype": "2",
  16. "conditions": [{"operator": "2", "conditiontype": "24", "value": "database"}],
  17. },
  18. "eventsource": "2",
  19. "name": "Auto registration Databases",
  20. "operations": [{"opgroup": [{"groupid": "6"}], "operationtype": "4"}],
  21. }
  22. EXISTING_OBJ = [
  23. {
  24. "status": "0",
  25. "operations": [
  26. {
  27. "operationtype": "4",
  28. "esc_period": "0",
  29. "evaltype": "0",
  30. "opconditions": [],
  31. "esc_step_to": "1",
  32. "actionid": "28",
  33. "esc_step_from": "1",
  34. "opgroup": [{"groupid": "6", "operationid": "92"}],
  35. "operationid": "92",
  36. }
  37. ],
  38. "def_shortdata": "",
  39. "name": "Auto registration Databases",
  40. "esc_period": "0",
  41. "def_longdata": "",
  42. "filter": {
  43. "formula": "",
  44. "evaltype": "2",
  45. "conditions": [
  46. {
  47. "operator": "2",
  48. "conditiontype": "24",
  49. "formulaid": "A",
  50. "value": "database",
  51. }
  52. ],
  53. "eval_formula": "A",
  54. },
  55. "eventsource": "2",
  56. "actionid": "28",
  57. "r_shortdata": "",
  58. "r_longdata": "",
  59. "recovery_msg": "0",
  60. }
  61. ]
  62. EXISTING_OBJ_DIFF = {
  63. "status": "0",
  64. "operations": [
  65. {
  66. "operationtype": "4",
  67. "esc_period": "0",
  68. "evaltype": "0",
  69. "opconditions": [],
  70. "esc_step_to": "1",
  71. "actionid": "28",
  72. "esc_step_from": "1",
  73. "opgroup": [{"groupid": "6", "operationid": "92"}],
  74. "operationid": "92",
  75. }
  76. ],
  77. "def_shortdata": "",
  78. "name": "Auto registration Databases",
  79. "esc_period": "0",
  80. "def_longdata": "",
  81. "filter": {
  82. "formula": "",
  83. "evaltype": "2",
  84. "conditions": [
  85. {
  86. "operator": "2",
  87. "conditiontype": "24",
  88. "formulaid": "A",
  89. "value": "SOME OTHER VALUE",
  90. }
  91. ],
  92. "eval_formula": "A",
  93. },
  94. "eventsource": "2",
  95. "actionid": "28",
  96. "r_shortdata": "",
  97. "r_longdata": "",
  98. "recovery_msg": "0",
  99. }
  100. DIFF_PARAMS = {
  101. "filter": {
  102. "evaltype": "2",
  103. "conditions": [{"operator": "2", "conditiontype": "24", "value": "virtual"}],
  104. },
  105. "actionid": "28",
  106. }
  107. class ZabbixActionTestCase(TestCase, LoaderModuleMockMixin):
  108. """
  109. Test cases for salt.modules.zabbix
  110. """
  111. def setup_loader_modules(self):
  112. return {zabbix_action: {}}
  113. def test_present_create(self):
  114. """
  115. Test to ensure that named action is created
  116. """
  117. name = "Auto registration Databases"
  118. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  119. def side_effect_run_query(*args):
  120. """
  121. Differentiate between __salt__ exec module function calls with different parameters.
  122. """
  123. if args[0] == "action.get":
  124. return False
  125. elif args[0] == "action.create":
  126. return True
  127. with patch.dict(zabbix_action.__opts__, {"test": False}):
  128. with patch.dict(
  129. zabbix_action.__salt__,
  130. {
  131. "zabbix.get_zabbix_id_mapper": MagicMock(
  132. return_value={"action": "actionid"}
  133. ),
  134. "zabbix.substitute_params": MagicMock(
  135. side_effect=[INPUT_PARAMS, False]
  136. ),
  137. "zabbix.run_query": MagicMock(side_effect=side_effect_run_query),
  138. "zabbix.compare_params": MagicMock(return_value={}),
  139. },
  140. ):
  141. ret["result"] = True
  142. ret["comment"] = 'Zabbix Action "{0}" created.'.format(name)
  143. ret["changes"] = {
  144. name: {
  145. "old": 'Zabbix Action "{0}" did not exist.'.format(name),
  146. "new": 'Zabbix Action "{0}" created according definition.'.format(
  147. name
  148. ),
  149. }
  150. }
  151. self.assertDictEqual(zabbix_action.present(name, {}), ret)
  152. def test_present_exists(self):
  153. """
  154. Test to ensure that named action is present and not changed
  155. """
  156. name = "Auto registration Databases"
  157. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  158. with patch.dict(zabbix_action.__opts__, {"test": False}):
  159. with patch.dict(
  160. zabbix_action.__salt__,
  161. {
  162. "zabbix.get_zabbix_id_mapper": MagicMock(
  163. return_value={"action": "actionid"}
  164. ),
  165. "zabbix.substitute_params": MagicMock(
  166. side_effect=[INPUT_PARAMS, EXISTING_OBJ]
  167. ),
  168. "zabbix.run_query": MagicMock(
  169. return_value=["length of result is 1"]
  170. ),
  171. "zabbix.compare_params": MagicMock(return_value={}),
  172. },
  173. ):
  174. ret["result"] = True
  175. ret[
  176. "comment"
  177. ] = 'Zabbix Action "{0}" already exists and corresponds to a definition.'.format(
  178. name
  179. )
  180. self.assertDictEqual(zabbix_action.present(name, {}), ret)
  181. def test_present_update(self):
  182. """
  183. Test to ensure that named action is present but must be updated
  184. """
  185. name = "Auto registration Databases"
  186. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  187. def side_effect_run_query(*args):
  188. """
  189. Differentiate between __salt__ exec module function calls with different parameters.
  190. """
  191. if args[0] == "action.get":
  192. return ["length of result is 1 = action exists"]
  193. elif args[0] == "action.update":
  194. return DIFF_PARAMS
  195. with patch.dict(zabbix_action.__opts__, {"test": False}):
  196. with patch.dict(
  197. zabbix_action.__salt__,
  198. {
  199. "zabbix.get_zabbix_id_mapper": MagicMock(
  200. return_value={"action": "actionid"}
  201. ),
  202. "zabbix.substitute_params": MagicMock(
  203. side_effect=[INPUT_PARAMS, EXISTING_OBJ_DIFF]
  204. ),
  205. "zabbix.run_query": MagicMock(side_effect=side_effect_run_query),
  206. "zabbix.compare_params": MagicMock(return_value=DIFF_PARAMS),
  207. },
  208. ):
  209. ret["result"] = True
  210. ret["comment"] = 'Zabbix Action "{0}" updated.'.format(name)
  211. ret["changes"] = {
  212. name: {
  213. "old": 'Zabbix Action "{0}" differed '
  214. "in following parameters: {1}".format(name, DIFF_PARAMS),
  215. "new": 'Zabbix Action "{0}" fixed.'.format(name),
  216. }
  217. }
  218. self.assertDictEqual(zabbix_action.present(name, {}), ret)
  219. def test_absent_test_mode(self):
  220. """
  221. Test to ensure that named action is absent in test mode
  222. """
  223. name = "Auto registration Databases"
  224. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  225. with patch.dict(zabbix_action.__opts__, {"test": True}):
  226. with patch.dict(
  227. zabbix_action.__salt__,
  228. {"zabbix.get_object_id_by_params": MagicMock(return_value=11)},
  229. ):
  230. ret["result"] = True
  231. ret["comment"] = 'Zabbix Action "{0}" would be deleted.'.format(name)
  232. ret["changes"] = {
  233. name: {
  234. "old": 'Zabbix Action "{0}" exists.'.format(name),
  235. "new": 'Zabbix Action "{0}" would be deleted.'.format(name),
  236. }
  237. }
  238. self.assertDictEqual(zabbix_action.absent(name), ret)
  239. def test_absent(self):
  240. """
  241. Test to ensure that named action is absent
  242. """
  243. name = "Auto registration Databases"
  244. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  245. with patch.dict(zabbix_action.__opts__, {"test": False}):
  246. with patch.dict(
  247. zabbix_action.__salt__,
  248. {"zabbix.get_object_id_by_params": MagicMock(return_value=False)},
  249. ):
  250. ret["result"] = True
  251. ret["comment"] = 'Zabbix Action "{0}" does not exist.'.format(name)
  252. self.assertDictEqual(zabbix_action.absent(name), ret)
  253. with patch.dict(
  254. zabbix_action.__salt__,
  255. {"zabbix.get_object_id_by_params": MagicMock(return_value=11)},
  256. ):
  257. with patch.dict(
  258. zabbix_action.__salt__,
  259. {"zabbix.run_query": MagicMock(return_value=True)},
  260. ):
  261. ret["result"] = True
  262. ret["comment"] = 'Zabbix Action "{0}" deleted.'.format(name)
  263. ret["changes"] = {
  264. name: {
  265. "old": 'Zabbix Action "{0}" existed.'.format(name),
  266. "new": 'Zabbix Action "{0}" deleted.'.format(name),
  267. }
  268. }
  269. self.assertDictEqual(zabbix_action.absent(name), ret)