test_zabbix_template.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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
  7. from __future__ import unicode_literals
  8. # Import Salt Testing Libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.unit import TestCase
  11. from tests.support.mock import (
  12. MagicMock,
  13. patch,
  14. )
  15. import salt.states.zabbix_template as zabbix_template
  16. INPUT_PARAMS = {"applications": [{"name": "Ceph OSD"}]}
  17. DEFINED_OBJ = {"macros": [{"macro": "{$CEPH_CLUSTER_NAME}", "value": "ceph"}], "host": "A Testing Template",
  18. "hosts": [{"hostid": "10112"}, {"hostid": "10113"}], "description": "Template for Ceph nodes",
  19. "groups": [{"groupid": "1"}]}
  20. DEFINED_C_LIST_SUBS = {"applications": [{"name": "Ceph OSD"}], 'graphs': [], 'triggers': [], 'items': [],
  21. 'httpTests': [], 'screens': [], 'gitems': [], 'discoveries': []}
  22. SUBSTITUTE_PARAMS_CREATE = [DEFINED_OBJ, [], DEFINED_C_LIST_SUBS['applications'], [], [], [], [], [], [], [], []]
  23. EXISTING_OBJ = [{"available": "0", "tls_connect": "1", "maintenance_type": "0", "groups": [{"groupid": "1"}],
  24. "macros": [{"macro": "{$CEPH_CLUSTER_NAME}", "hostmacroid": "60", "hostid": "10206", "value": "ceph"}],
  25. "hosts": [{"hostid": "10112"}, {"hostid": "10113"}], "status": "3",
  26. "description": "Template for Ceph nodes", "host": "A Testing Template", "disable_until": "0",
  27. "templateid": "10206", "name": "A Testing Template"}]
  28. SUBSTITUTE_PARAMS_EXISTS = [DEFINED_OBJ, EXISTING_OBJ[0], [], [], [], [], [], [], [], []]
  29. EXISTING_OBJ_DIFF = [{"groups": [{"groupid": "1"}], "macros": [{"macro": "{$CEPH_CLUSTER_NAME}", "hostmacroid": "60",
  30. "hostid": "10206", "value": "ceph"}],
  31. "hosts": [{"hostid": "10112"}, {"hostid": "10113"}], "status": "3", "templateid": "10206",
  32. "name": "A Testing Template"}]
  33. SUBSTITUTE_PARAMS_UPDATE = [DEFINED_OBJ, EXISTING_OBJ_DIFF[0], [], [], [], [], [], [], [], []]
  34. DIFF_PARAMS = {'old': {}, 'new': {'macros': [], 'templateid': '10206'}}
  35. class ZabbixTemplateTestCase(TestCase, LoaderModuleMockMixin):
  36. '''
  37. Test cases for salt.modules.zabbix
  38. '''
  39. def setup_loader_modules(self):
  40. return {zabbix_template: {}}
  41. @patch('salt.states.zabbix_template.CHANGE_STACK', [])
  42. def test_present_create(self):
  43. '''
  44. Test to ensure that named template is created
  45. '''
  46. name = 'A Testing Template'
  47. ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}
  48. def side_effect_run_query(*args):
  49. '''
  50. Differentiate between __salt__ exec module function calls with different parameters.
  51. '''
  52. if args[0] == 'template.get':
  53. return []
  54. elif args[0] == 'template.create':
  55. return {'templateids': ['10206']}
  56. elif args[0] == 'application.get':
  57. return []
  58. elif args[0] == 'application.create':
  59. return {"applicationids": ["701"]}
  60. with patch.dict(zabbix_template.__opts__, {'test': False}):
  61. with patch.dict(zabbix_template.__salt__,
  62. {'zabbix.get_zabbix_id_mapper': MagicMock(return_value={'template': 'templateid'}),
  63. 'zabbix.substitute_params': MagicMock(side_effect=SUBSTITUTE_PARAMS_CREATE),
  64. 'zabbix.run_query': MagicMock(side_effect=side_effect_run_query),
  65. 'zabbix.compare_params': MagicMock(return_value={})}):
  66. ret['result'] = True
  67. ret['comment'] = 'Zabbix Template "{0}" created.'.format(name)
  68. ret['changes'] = {name: {'old': 'Zabbix Template "{0}" did not exist.'.format(name),
  69. 'new': 'Zabbix Template "{0}" created according definition.'.format(name)}}
  70. self.assertDictEqual(zabbix_template.present(name, {}), ret)
  71. @patch('salt.states.zabbix_template.CHANGE_STACK', [])
  72. def test_present_exists(self):
  73. '''
  74. Test to ensure that named template is present and not changed
  75. '''
  76. name = 'A Testing Template'
  77. ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}
  78. def side_effect_run_query(*args):
  79. '''
  80. Differentiate between __salt__ exec module function calls with different parameters.
  81. '''
  82. if args[0] == 'template.get':
  83. return EXISTING_OBJ
  84. elif args[0] == 'application.get':
  85. return ['non-empty']
  86. with patch.dict(zabbix_template.__opts__, {'test': False}):
  87. with patch.dict(zabbix_template.__salt__,
  88. {'zabbix.get_zabbix_id_mapper': MagicMock(return_value={'template': 'templateid'}),
  89. 'zabbix.substitute_params': MagicMock(side_effect=SUBSTITUTE_PARAMS_EXISTS),
  90. 'zabbix.run_query': MagicMock(side_effect=side_effect_run_query),
  91. 'zabbix.compare_params': MagicMock(return_value={'new': {}, 'old': {}})}):
  92. ret['result'] = True
  93. ret['comment'] = 'Zabbix Template "{0}" already exists and corresponds to a definition.'.format(name)
  94. self.assertDictEqual(zabbix_template.present(name, {}), ret)
  95. @patch('salt.states.zabbix_template.CHANGE_STACK', [])
  96. def test_present_update(self):
  97. '''
  98. Test to ensure that named template is present but must be updated
  99. '''
  100. name = 'A Testing Template'
  101. ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}
  102. def side_effect_run_query(*args):
  103. '''
  104. Differentiate between __salt__ exec module function calls with different parameters.
  105. '''
  106. if args[0] == 'template.get':
  107. return ['length of result is 1 = template exists']
  108. elif args[0] == 'template.update':
  109. return DIFF_PARAMS
  110. with patch.dict(zabbix_template.__opts__, {'test': False}):
  111. with patch.dict(zabbix_template.__salt__,
  112. {'zabbix.get_zabbix_id_mapper': MagicMock(return_value={'template': 'templateid'}),
  113. 'zabbix.substitute_params': MagicMock(side_effect=SUBSTITUTE_PARAMS_UPDATE),
  114. 'zabbix.run_query': MagicMock(side_effect=side_effect_run_query),
  115. 'zabbix.compare_params': MagicMock(return_value=DIFF_PARAMS)}):
  116. ret['result'] = True
  117. ret['comment'] = 'Zabbix Template "{0}" updated.'.format(name)
  118. ret['changes'] = {name: {'old': 'Zabbix Template "{0}" differed.'.format(name),
  119. 'new': 'Zabbix Template "{0}" updated according definition.'.format(name)}}
  120. self.assertDictEqual(zabbix_template.present(name, {}), ret)
  121. def test_absent_test_mode(self):
  122. '''
  123. Test to ensure that named template is absent in test mode
  124. '''
  125. name = 'A Testing Template'
  126. ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}
  127. with patch.dict(zabbix_template.__opts__, {'test': True}):
  128. with patch.dict(zabbix_template.__salt__, {'zabbix.get_object_id_by_params': MagicMock(return_value=11)}):
  129. ret['result'] = True
  130. ret['comment'] = 'Zabbix Template "{0}" would be deleted.'.format(name)
  131. ret['changes'] = {name: {'old': 'Zabbix Template "{0}" exists.'.format(name),
  132. 'new': 'Zabbix Template "{0}" would be deleted.'.format(name)}}
  133. self.assertDictEqual(zabbix_template.absent(name), ret)
  134. def test_absent(self):
  135. '''
  136. Test to ensure that named template is absent
  137. '''
  138. name = 'A Testing Template'
  139. ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}
  140. with patch.dict(zabbix_template.__opts__, {'test': False}):
  141. with patch.dict(zabbix_template.__salt__,
  142. {'zabbix.get_object_id_by_params': MagicMock(return_value=False)}):
  143. ret['result'] = True
  144. ret['comment'] = 'Zabbix Template "{0}" does not exist.'.format(name)
  145. self.assertDictEqual(zabbix_template.absent(name), ret)
  146. with patch.dict(zabbix_template.__salt__, {'zabbix.get_object_id_by_params': MagicMock(return_value=11)}):
  147. with patch.dict(zabbix_template.__salt__, {'zabbix.run_query': MagicMock(return_value=True)}):
  148. ret['result'] = True
  149. ret['comment'] = 'Zabbix Template "{0}" deleted.'.format(name)
  150. ret['changes'] = {name: {'old': 'Zabbix Template "{0}" existed.'.format(name),
  151. 'new': 'Zabbix Template "{0}" deleted.'.format(name)}}
  152. self.assertDictEqual(zabbix_template.absent(name), ret)