test_zabbix_valuemap.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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_valuemap as zabbix_valuemap
  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. "mappings": [
  14. {"newvalue": "OK", "value": "0h"},
  15. {"newvalue": "Failure", "value": "1"},
  16. ],
  17. "name": "Server HP Health",
  18. }
  19. EXISTING_OBJ = [
  20. {
  21. "valuemapid": "21",
  22. "name": "Server HP Health",
  23. "mappings": [
  24. {"newvalue": "OK", "value": "0h"},
  25. {"newvalue": "Failure", "value": "1"},
  26. ],
  27. }
  28. ]
  29. EXISTING_OBJ_DIFF = {
  30. "valuemapid": "21",
  31. "name": "Server HP Health",
  32. "mappings": [
  33. {"newvalue": "OK", "value": "0h"},
  34. {"newvalue": "Failure", "value": "1"},
  35. {"newvalue": "some", "value": "2"},
  36. ],
  37. }
  38. DIFF_PARAMS = {
  39. "valuemapid": "21",
  40. "mappings": [
  41. {"newvalue": "OK", "value": "0h"},
  42. {"newvalue": "Failure", "value": "1"},
  43. ],
  44. }
  45. class ZabbixActionTestCase(TestCase, LoaderModuleMockMixin):
  46. """
  47. Test cases for salt.modules.zabbix
  48. """
  49. def setup_loader_modules(self):
  50. return {zabbix_valuemap: {}}
  51. def test_present_create(self):
  52. """
  53. Test to ensure that named value map is created
  54. """
  55. name = "Server HP Health"
  56. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  57. def side_effect_run_query(*args):
  58. """
  59. Differentiate between __salt__ exec module function calls with different parameters.
  60. """
  61. if args[0] == "valuemap.get":
  62. return False
  63. elif args[0] == "valuemap.create":
  64. return True
  65. with patch.dict(zabbix_valuemap.__opts__, {"test": False}):
  66. with patch.dict(
  67. zabbix_valuemap.__salt__,
  68. {
  69. "zabbix.get_zabbix_id_mapper": MagicMock(
  70. return_value={"valuemap": "valuemapid"}
  71. ),
  72. "zabbix.substitute_params": MagicMock(
  73. side_effect=[INPUT_PARAMS, False]
  74. ),
  75. "zabbix.run_query": MagicMock(side_effect=side_effect_run_query),
  76. "zabbix.compare_params": MagicMock(return_value={}),
  77. },
  78. ):
  79. ret["result"] = True
  80. ret["comment"] = 'Zabbix Value map "{0}" created.'.format(name)
  81. ret["changes"] = {
  82. name: {
  83. "old": 'Zabbix Value map "{0}" did not exist.'.format(name),
  84. "new": 'Zabbix Value map "{0}" created according definition.'.format(
  85. name
  86. ),
  87. }
  88. }
  89. self.assertDictEqual(zabbix_valuemap.present(name, {}), ret)
  90. def test_present_exists(self):
  91. """
  92. Test to ensure that named value map is present and not changed
  93. """
  94. name = "Server HP Health"
  95. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  96. with patch.dict(zabbix_valuemap.__opts__, {"test": False}):
  97. with patch.dict(
  98. zabbix_valuemap.__salt__,
  99. {
  100. "zabbix.get_zabbix_id_mapper": MagicMock(
  101. return_value={"valuemap": "valuemapid"}
  102. ),
  103. "zabbix.substitute_params": MagicMock(
  104. side_effect=[INPUT_PARAMS, EXISTING_OBJ]
  105. ),
  106. "zabbix.run_query": MagicMock(
  107. return_value=["length of result is 1"]
  108. ),
  109. "zabbix.compare_params": MagicMock(return_value={}),
  110. },
  111. ):
  112. ret["result"] = True
  113. ret[
  114. "comment"
  115. ] = 'Zabbix Value map "{0}" already exists and corresponds to a definition.'.format(
  116. name
  117. )
  118. self.assertDictEqual(zabbix_valuemap.present(name, {}), ret)
  119. def test_present_update(self):
  120. """
  121. Test to ensure that named value map is present but must be updated
  122. """
  123. name = "Server HP Health"
  124. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  125. def side_effect_run_query(*args):
  126. """
  127. Differentiate between __salt__ exec module function calls with different parameters.
  128. """
  129. if args[0] == "valuemap.get":
  130. return ["length of result is 1 = valuemap exists"]
  131. elif args[0] == "valuemap.update":
  132. return DIFF_PARAMS
  133. with patch.dict(zabbix_valuemap.__opts__, {"test": False}):
  134. with patch.dict(
  135. zabbix_valuemap.__salt__,
  136. {
  137. "zabbix.get_zabbix_id_mapper": MagicMock(
  138. return_value={"valuemap": "valuemapid"}
  139. ),
  140. "zabbix.substitute_params": MagicMock(
  141. side_effect=[INPUT_PARAMS, EXISTING_OBJ_DIFF]
  142. ),
  143. "zabbix.run_query": MagicMock(side_effect=side_effect_run_query),
  144. "zabbix.compare_params": MagicMock(return_value=DIFF_PARAMS),
  145. },
  146. ):
  147. ret["result"] = True
  148. ret["comment"] = 'Zabbix Value map "{0}" updated.'.format(name)
  149. ret["changes"] = {
  150. name: {
  151. "old": 'Zabbix Value map "{0}" differed '
  152. "in following parameters: {1}".format(name, DIFF_PARAMS),
  153. "new": 'Zabbix Value map "{0}" fixed.'.format(name),
  154. }
  155. }
  156. self.assertDictEqual(zabbix_valuemap.present(name, {}), ret)
  157. def test_absent_test_mode(self):
  158. """
  159. Test to ensure that named value map is absent in test mode
  160. """
  161. name = "Server HP Health"
  162. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  163. with patch.dict(zabbix_valuemap.__opts__, {"test": True}):
  164. with patch.dict(
  165. zabbix_valuemap.__salt__,
  166. {"zabbix.get_object_id_by_params": MagicMock(return_value=11)},
  167. ):
  168. ret["result"] = True
  169. ret["comment"] = 'Zabbix Value map "{0}" would be deleted.'.format(name)
  170. ret["changes"] = {
  171. name: {
  172. "old": 'Zabbix Value map "{0}" exists.'.format(name),
  173. "new": 'Zabbix Value map "{0}" would be deleted.'.format(name),
  174. }
  175. }
  176. self.assertDictEqual(zabbix_valuemap.absent(name), ret)
  177. def test_absent(self):
  178. """
  179. Test to ensure that named value map is absent
  180. """
  181. name = "Server HP Health"
  182. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  183. with patch.dict(zabbix_valuemap.__opts__, {"test": False}):
  184. with patch.dict(
  185. zabbix_valuemap.__salt__,
  186. {"zabbix.get_object_id_by_params": MagicMock(return_value=False)},
  187. ):
  188. ret["result"] = True
  189. ret["comment"] = 'Zabbix Value map "{0}" does not exist.'.format(name)
  190. self.assertDictEqual(zabbix_valuemap.absent(name), ret)
  191. with patch.dict(
  192. zabbix_valuemap.__salt__,
  193. {"zabbix.get_object_id_by_params": MagicMock(return_value=11)},
  194. ):
  195. with patch.dict(
  196. zabbix_valuemap.__salt__,
  197. {"zabbix.run_query": MagicMock(return_value=True)},
  198. ):
  199. ret["result"] = True
  200. ret["comment"] = 'Zabbix Value map "{0}" deleted.'.format(name)
  201. ret["changes"] = {
  202. name: {
  203. "old": 'Zabbix Value map "{0}" existed.'.format(name),
  204. "new": 'Zabbix Value map "{0}" deleted.'.format(name),
  205. }
  206. }
  207. self.assertDictEqual(zabbix_valuemap.absent(name), ret)