test_zabbix_template.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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_template as zabbix_template
  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 = {"applications": [{"name": "Ceph OSD"}]}
  13. DEFINED_OBJ = {
  14. "macros": [{"macro": "{$CEPH_CLUSTER_NAME}", "value": "ceph"}],
  15. "host": "A Testing Template",
  16. "hosts": [{"hostid": "10112"}, {"hostid": "10113"}],
  17. "description": "Template for Ceph nodes",
  18. "groups": [{"groupid": "1"}],
  19. }
  20. DEFINED_C_LIST_SUBS = {
  21. "applications": [{"name": "Ceph OSD"}],
  22. "graphs": [],
  23. "triggers": [],
  24. "items": [],
  25. "httpTests": [],
  26. "screens": [],
  27. "gitems": [],
  28. "discoveries": [],
  29. }
  30. SUBSTITUTE_PARAMS_CREATE = [
  31. DEFINED_OBJ,
  32. [],
  33. DEFINED_C_LIST_SUBS["applications"],
  34. [],
  35. [],
  36. [],
  37. [],
  38. [],
  39. [],
  40. [],
  41. [],
  42. ]
  43. EXISTING_OBJ = [
  44. {
  45. "available": "0",
  46. "tls_connect": "1",
  47. "maintenance_type": "0",
  48. "groups": [{"groupid": "1"}],
  49. "macros": [
  50. {
  51. "macro": "{$CEPH_CLUSTER_NAME}",
  52. "hostmacroid": "60",
  53. "hostid": "10206",
  54. "value": "ceph",
  55. }
  56. ],
  57. "hosts": [{"hostid": "10112"}, {"hostid": "10113"}],
  58. "status": "3",
  59. "description": "Template for Ceph nodes",
  60. "host": "A Testing Template",
  61. "disable_until": "0",
  62. "templateid": "10206",
  63. "name": "A Testing Template",
  64. }
  65. ]
  66. SUBSTITUTE_PARAMS_EXISTS = [
  67. DEFINED_OBJ,
  68. EXISTING_OBJ[0],
  69. [],
  70. [],
  71. [],
  72. [],
  73. [],
  74. [],
  75. [],
  76. [],
  77. ]
  78. EXISTING_OBJ_DIFF = [
  79. {
  80. "groups": [{"groupid": "1"}],
  81. "macros": [
  82. {
  83. "macro": "{$CEPH_CLUSTER_NAME}",
  84. "hostmacroid": "60",
  85. "hostid": "10206",
  86. "value": "ceph",
  87. }
  88. ],
  89. "hosts": [{"hostid": "10112"}, {"hostid": "10113"}],
  90. "status": "3",
  91. "templateid": "10206",
  92. "name": "A Testing Template",
  93. }
  94. ]
  95. SUBSTITUTE_PARAMS_UPDATE = [
  96. DEFINED_OBJ,
  97. EXISTING_OBJ_DIFF[0],
  98. [],
  99. [],
  100. [],
  101. [],
  102. [],
  103. [],
  104. [],
  105. [],
  106. ]
  107. DIFF_PARAMS = {"old": {}, "new": {"macros": [], "templateid": "10206"}}
  108. class ZabbixTemplateTestCase(TestCase, LoaderModuleMockMixin):
  109. """
  110. Test cases for salt.modules.zabbix
  111. """
  112. def setup_loader_modules(self):
  113. return {zabbix_template: {}}
  114. @patch("salt.states.zabbix_template.CHANGE_STACK", [])
  115. def test_present_create(self):
  116. """
  117. Test to ensure that named template is created
  118. """
  119. name = "A Testing Template"
  120. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  121. def side_effect_run_query(*args):
  122. """
  123. Differentiate between __salt__ exec module function calls with different parameters.
  124. """
  125. if args[0] == "template.get":
  126. return []
  127. elif args[0] == "template.create":
  128. return {"templateids": ["10206"]}
  129. elif args[0] == "application.get":
  130. return []
  131. elif args[0] == "application.create":
  132. return {"applicationids": ["701"]}
  133. with patch.dict(zabbix_template.__opts__, {"test": False}):
  134. with patch.dict(
  135. zabbix_template.__salt__,
  136. {
  137. "zabbix.get_zabbix_id_mapper": MagicMock(
  138. return_value={"template": "templateid"}
  139. ),
  140. "zabbix.substitute_params": MagicMock(
  141. side_effect=SUBSTITUTE_PARAMS_CREATE
  142. ),
  143. "zabbix.run_query": MagicMock(side_effect=side_effect_run_query),
  144. "zabbix.compare_params": MagicMock(return_value={}),
  145. },
  146. ):
  147. ret["result"] = True
  148. ret["comment"] = 'Zabbix Template "{0}" created.'.format(name)
  149. ret["changes"] = {
  150. name: {
  151. "old": 'Zabbix Template "{0}" did not exist.'.format(name),
  152. "new": 'Zabbix Template "{0}" created according definition.'.format(
  153. name
  154. ),
  155. }
  156. }
  157. self.assertDictEqual(zabbix_template.present(name, {}), ret)
  158. @patch("salt.states.zabbix_template.CHANGE_STACK", [])
  159. def test_present_exists(self):
  160. """
  161. Test to ensure that named template is present and not changed
  162. """
  163. name = "A Testing Template"
  164. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  165. def side_effect_run_query(*args):
  166. """
  167. Differentiate between __salt__ exec module function calls with different parameters.
  168. """
  169. if args[0] == "template.get":
  170. return EXISTING_OBJ
  171. elif args[0] == "application.get":
  172. return ["non-empty"]
  173. with patch.dict(zabbix_template.__opts__, {"test": False}):
  174. with patch.dict(
  175. zabbix_template.__salt__,
  176. {
  177. "zabbix.get_zabbix_id_mapper": MagicMock(
  178. return_value={"template": "templateid"}
  179. ),
  180. "zabbix.substitute_params": MagicMock(
  181. side_effect=SUBSTITUTE_PARAMS_EXISTS
  182. ),
  183. "zabbix.run_query": MagicMock(side_effect=side_effect_run_query),
  184. "zabbix.compare_params": MagicMock(
  185. return_value={"new": {}, "old": {}}
  186. ),
  187. },
  188. ):
  189. ret["result"] = True
  190. ret[
  191. "comment"
  192. ] = 'Zabbix Template "{0}" already exists and corresponds to a definition.'.format(
  193. name
  194. )
  195. self.assertDictEqual(zabbix_template.present(name, {}), ret)
  196. @patch("salt.states.zabbix_template.CHANGE_STACK", [])
  197. def test_present_update(self):
  198. """
  199. Test to ensure that named template is present but must be updated
  200. """
  201. name = "A Testing Template"
  202. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  203. def side_effect_run_query(*args):
  204. """
  205. Differentiate between __salt__ exec module function calls with different parameters.
  206. """
  207. if args[0] == "template.get":
  208. return ["length of result is 1 = template exists"]
  209. elif args[0] == "template.update":
  210. return DIFF_PARAMS
  211. with patch.dict(zabbix_template.__opts__, {"test": False}):
  212. with patch.dict(
  213. zabbix_template.__salt__,
  214. {
  215. "zabbix.get_zabbix_id_mapper": MagicMock(
  216. return_value={"template": "templateid"}
  217. ),
  218. "zabbix.substitute_params": MagicMock(
  219. side_effect=SUBSTITUTE_PARAMS_UPDATE
  220. ),
  221. "zabbix.run_query": MagicMock(side_effect=side_effect_run_query),
  222. "zabbix.compare_params": MagicMock(return_value=DIFF_PARAMS),
  223. },
  224. ):
  225. ret["result"] = True
  226. ret["comment"] = 'Zabbix Template "{0}" updated.'.format(name)
  227. ret["changes"] = {
  228. name: {
  229. "old": 'Zabbix Template "{0}" differed.'.format(name),
  230. "new": 'Zabbix Template "{0}" updated according definition.'.format(
  231. name
  232. ),
  233. }
  234. }
  235. self.assertDictEqual(zabbix_template.present(name, {}), ret)
  236. def test_absent_test_mode(self):
  237. """
  238. Test to ensure that named template is absent in test mode
  239. """
  240. name = "A Testing Template"
  241. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  242. with patch.dict(zabbix_template.__opts__, {"test": True}):
  243. with patch.dict(
  244. zabbix_template.__salt__,
  245. {"zabbix.get_object_id_by_params": MagicMock(return_value=11)},
  246. ):
  247. ret["result"] = True
  248. ret["comment"] = 'Zabbix Template "{0}" would be deleted.'.format(name)
  249. ret["changes"] = {
  250. name: {
  251. "old": 'Zabbix Template "{0}" exists.'.format(name),
  252. "new": 'Zabbix Template "{0}" would be deleted.'.format(name),
  253. }
  254. }
  255. self.assertDictEqual(zabbix_template.absent(name), ret)
  256. def test_absent(self):
  257. """
  258. Test to ensure that named template is absent
  259. """
  260. name = "A Testing Template"
  261. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  262. with patch.dict(zabbix_template.__opts__, {"test": False}):
  263. with patch.dict(
  264. zabbix_template.__salt__,
  265. {"zabbix.get_object_id_by_params": MagicMock(return_value=False)},
  266. ):
  267. ret["result"] = True
  268. ret["comment"] = 'Zabbix Template "{0}" does not exist.'.format(name)
  269. self.assertDictEqual(zabbix_template.absent(name), ret)
  270. with patch.dict(
  271. zabbix_template.__salt__,
  272. {"zabbix.get_object_id_by_params": MagicMock(return_value=11)},
  273. ):
  274. with patch.dict(
  275. zabbix_template.__salt__,
  276. {"zabbix.run_query": MagicMock(return_value=True)},
  277. ):
  278. ret["result"] = True
  279. ret["comment"] = 'Zabbix Template "{0}" deleted.'.format(name)
  280. ret["changes"] = {
  281. name: {
  282. "old": 'Zabbix Template "{0}" existed.'.format(name),
  283. "new": 'Zabbix Template "{0}" deleted.'.format(name),
  284. }
  285. }
  286. self.assertDictEqual(zabbix_template.absent(name), ret)