test_zabbix.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. from enum import Enum
  2. from flexmock import flexmock
  3. import borgmatic.hooks.monitor
  4. from borgmatic.hooks import zabbix as module
  5. server = 'https://zabbix.com/zabbix/api_jsonrpc.php'
  6. itemid = 55105
  7. username = 'testuser'
  8. password = 'fakepassword'
  9. api_key = 'fakekey'
  10. host = 'borg-server'
  11. key = 'borg.status'
  12. value = 'fail'
  13. data_host_key = {
  14. "jsonrpc": "2.0",
  15. "method": "history.push",
  16. "params": {"host": host, "key": key, "value": value},
  17. "id": 1,
  18. }
  19. data_itemid = {
  20. "jsonrpc": "2.0",
  21. "method": "history.push",
  22. "params": {"itemid": itemid, "value": value},
  23. "id": 1,
  24. }
  25. data_user_login = {
  26. "jsonrpc": "2.0",
  27. "method": "user.login",
  28. "params": {"username": username, "password": password},
  29. "id": 1,
  30. }
  31. auth_headers_api_key = {
  32. 'Content-Type': 'application/json-rpc',
  33. 'Authorization': f'Bearer {api_key}'
  34. }
  35. auth_headers_username_password = {
  36. 'Content-Type': 'application/json-rpc'
  37. }
  38. def test_ping_monitor_config_with_api_key_only():
  39. hook_config = {
  40. 'api_key': api_key
  41. }
  42. flexmock(module.logger).should_receive('warning').once()
  43. module.ping_monitor(
  44. hook_config,
  45. {},
  46. 'config.yaml',
  47. borgmatic.hooks.monitor.State.FAIL,
  48. monitoring_log_level=1,
  49. dry_run=False,
  50. )
  51. def test_ping_monitor_config_with_host_only():
  52. hook_config = {
  53. 'host': host
  54. }
  55. flexmock(module.logger).should_receive('warning').once()
  56. module.ping_monitor(
  57. hook_config,
  58. {},
  59. 'config.yaml',
  60. borgmatic.hooks.monitor.State.FAIL,
  61. monitoring_log_level=1,
  62. dry_run=False,
  63. )
  64. def test_ping_monitor_config_with_key_only():
  65. hook_config = {
  66. 'key': key
  67. }
  68. flexmock(module.logger).should_receive('warning').once()
  69. module.ping_monitor(
  70. hook_config,
  71. {},
  72. 'config.yaml',
  73. borgmatic.hooks.monitor.State.FAIL,
  74. monitoring_log_level=1,
  75. dry_run=False,
  76. )
  77. def test_ping_monitor_config_with_server_only():
  78. hook_config = {
  79. 'server': server
  80. }
  81. flexmock(module.logger).should_receive('warning').once()
  82. module.ping_monitor(
  83. hook_config,
  84. {},
  85. 'config.yaml',
  86. borgmatic.hooks.monitor.State.FAIL,
  87. monitoring_log_level=1,
  88. dry_run=False,
  89. )
  90. def test_ping_monitor_config_user_password_no_zabbix_data():
  91. hook_config = {
  92. 'server': server,
  93. 'username': username,
  94. 'password': password
  95. }
  96. flexmock(module.logger).should_receive('warning').once()
  97. module.ping_monitor(
  98. hook_config,
  99. {},
  100. 'config.yaml',
  101. borgmatic.hooks.monitor.State.FAIL,
  102. monitoring_log_level=1,
  103. dry_run=False,
  104. )
  105. def test_ping_monitor_config_api_key_no_zabbix_data():
  106. hook_config = {
  107. 'server': server,
  108. 'api_key': api_key
  109. }
  110. flexmock(module.logger).should_receive('warning').once()
  111. module.ping_monitor(
  112. hook_config,
  113. {},
  114. 'config.yaml',
  115. borgmatic.hooks.monitor.State.FAIL,
  116. monitoring_log_level=1,
  117. dry_run=False,
  118. )
  119. def test_ping_monitor_config_itemid_no_auth_data():
  120. hook_config = {
  121. 'server': server,
  122. 'itemid': itemid
  123. }
  124. flexmock(module.logger).should_receive('warning').once()
  125. module.ping_monitor(
  126. hook_config,
  127. {},
  128. 'config.yaml',
  129. borgmatic.hooks.monitor.State.FAIL,
  130. monitoring_log_level=1,
  131. dry_run=False,
  132. )
  133. def test_ping_monitor_config_host_and_key_no_auth_data():
  134. hook_config = {
  135. 'server': server,
  136. 'host': host,
  137. 'key': key
  138. }
  139. flexmock(module.logger).should_receive('warning').once()
  140. module.ping_monitor(
  141. hook_config,
  142. {},
  143. 'config.yaml',
  144. borgmatic.hooks.monitor.State.FAIL,
  145. monitoring_log_level=1,
  146. dry_run=False,
  147. )
  148. def test_ping_monitor_config_host_and_key_with_api_key_auth_data():
  149. hook_config = {
  150. 'server': server,
  151. 'host': host,
  152. 'key': key,
  153. 'api_key': api_key
  154. }
  155. flexmock(module.requests).should_receive('post').with_args(
  156. f'{server}',
  157. headers=auth_headers_api_key,
  158. json=data_host_key,
  159. ).and_return(flexmock(ok=True)).once()
  160. flexmock(module.logger).should_receive('warning').never()
  161. module.ping_monitor(
  162. hook_config,
  163. {},
  164. 'config.yaml',
  165. borgmatic.hooks.monitor.State.FAIL,
  166. monitoring_log_level=1,
  167. dry_run=False,
  168. )