test_uptimekuma.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. from flexmock import flexmock
  2. import borgmatic.hooks.monitoring.monitor
  3. from borgmatic.hooks.monitoring import uptime_kuma as module
  4. DEFAULT_PUSH_URL = 'https://example.uptime.kuma/api/push/abcd1234'
  5. CUSTOM_PUSH_URL = 'https://uptime.example.com/api/push/efgh5678'
  6. def test_ping_monitor_hits_default_uptimekuma_on_fail():
  7. hook_config = {}
  8. flexmock(module.requests).should_receive('get').with_args(
  9. f'{DEFAULT_PUSH_URL}?status=down&msg=fail',
  10. verify=True,
  11. timeout=int,
  12. headers={'User-Agent': 'borgmatic'},
  13. ).and_return(flexmock(ok=True)).once()
  14. module.ping_monitor(
  15. hook_config,
  16. {},
  17. 'config.yaml',
  18. borgmatic.hooks.monitoring.monitor.State.FAIL,
  19. monitoring_log_level=1,
  20. dry_run=False,
  21. )
  22. def test_ping_monitor_hits_custom_uptimekuma_on_fail():
  23. hook_config = {'push_url': CUSTOM_PUSH_URL}
  24. flexmock(module.requests).should_receive('get').with_args(
  25. f'{CUSTOM_PUSH_URL}?status=down&msg=fail',
  26. verify=True,
  27. timeout=int,
  28. headers={'User-Agent': 'borgmatic'},
  29. ).and_return(flexmock(ok=True)).once()
  30. module.ping_monitor(
  31. hook_config,
  32. {},
  33. 'config.yaml',
  34. borgmatic.hooks.monitoring.monitor.State.FAIL,
  35. monitoring_log_level=1,
  36. dry_run=False,
  37. )
  38. def test_ping_monitor_custom_uptimekuma_on_start():
  39. hook_config = {'push_url': CUSTOM_PUSH_URL}
  40. flexmock(module.requests).should_receive('get').with_args(
  41. f'{CUSTOM_PUSH_URL}?status=up&msg=start',
  42. verify=True,
  43. timeout=int,
  44. headers={'User-Agent': 'borgmatic'},
  45. ).and_return(flexmock(ok=True)).once()
  46. module.ping_monitor(
  47. hook_config,
  48. {},
  49. 'config.yaml',
  50. borgmatic.hooks.monitoring.monitor.State.START,
  51. monitoring_log_level=1,
  52. dry_run=False,
  53. )
  54. def test_ping_monitor_custom_uptimekuma_on_finish():
  55. hook_config = {'push_url': CUSTOM_PUSH_URL}
  56. flexmock(module.requests).should_receive('get').with_args(
  57. f'{CUSTOM_PUSH_URL}?status=up&msg=finish',
  58. verify=True,
  59. timeout=int,
  60. headers={'User-Agent': 'borgmatic'},
  61. ).and_return(flexmock(ok=True)).once()
  62. module.ping_monitor(
  63. hook_config,
  64. {},
  65. 'config.yaml',
  66. borgmatic.hooks.monitoring.monitor.State.FINISH,
  67. monitoring_log_level=1,
  68. dry_run=False,
  69. )
  70. def test_ping_monitor_does_not_hit_custom_uptimekuma_on_fail_dry_run():
  71. hook_config = {'push_url': CUSTOM_PUSH_URL}
  72. flexmock(module.requests).should_receive('get').never()
  73. module.ping_monitor(
  74. hook_config,
  75. {},
  76. 'config.yaml',
  77. borgmatic.hooks.monitoring.monitor.State.FAIL,
  78. monitoring_log_level=1,
  79. dry_run=True,
  80. )
  81. def test_ping_monitor_does_not_hit_custom_uptimekuma_on_start_dry_run():
  82. hook_config = {'push_url': CUSTOM_PUSH_URL}
  83. flexmock(module.requests).should_receive('get').never()
  84. module.ping_monitor(
  85. hook_config,
  86. {},
  87. 'config.yaml',
  88. borgmatic.hooks.monitoring.monitor.State.START,
  89. monitoring_log_level=1,
  90. dry_run=True,
  91. )
  92. def test_ping_monitor_does_not_hit_custom_uptimekuma_on_finish_dry_run():
  93. hook_config = {'push_url': CUSTOM_PUSH_URL}
  94. flexmock(module.requests).should_receive('get').never()
  95. module.ping_monitor(
  96. hook_config,
  97. {},
  98. 'config.yaml',
  99. borgmatic.hooks.monitoring.monitor.State.FINISH,
  100. monitoring_log_level=1,
  101. dry_run=True,
  102. )
  103. def test_ping_monitor_with_connection_error_logs_warning():
  104. hook_config = {'push_url': CUSTOM_PUSH_URL}
  105. flexmock(module.requests).should_receive('get').with_args(
  106. f'{CUSTOM_PUSH_URL}?status=down&msg=fail',
  107. verify=True,
  108. timeout=int,
  109. headers={'User-Agent': 'borgmatic'},
  110. ).and_raise(module.requests.exceptions.ConnectionError)
  111. flexmock(module.logger).should_receive('warning').once()
  112. module.ping_monitor(
  113. hook_config,
  114. {},
  115. 'config.yaml',
  116. borgmatic.hooks.monitoring.monitor.State.FAIL,
  117. monitoring_log_level=1,
  118. dry_run=False,
  119. )
  120. def test_ping_monitor_with_other_error_logs_warning():
  121. hook_config = {'push_url': CUSTOM_PUSH_URL}
  122. response = flexmock(ok=False)
  123. response.should_receive('raise_for_status').and_raise(
  124. module.requests.exceptions.RequestException,
  125. )
  126. flexmock(module.requests).should_receive('get').with_args(
  127. f'{CUSTOM_PUSH_URL}?status=down&msg=fail',
  128. verify=True,
  129. timeout=int,
  130. headers={'User-Agent': 'borgmatic'},
  131. ).and_return(response)
  132. flexmock(module.logger).should_receive('warning').once()
  133. module.ping_monitor(
  134. hook_config,
  135. {},
  136. 'config.yaml',
  137. borgmatic.hooks.monitoring.monitor.State.FAIL,
  138. monitoring_log_level=1,
  139. dry_run=False,
  140. )
  141. def test_ping_monitor_with_invalid_run_state():
  142. hook_config = {'push_url': CUSTOM_PUSH_URL}
  143. flexmock(module.requests).should_receive('get').never()
  144. module.ping_monitor(
  145. hook_config,
  146. {},
  147. 'config.yaml',
  148. borgmatic.hooks.monitoring.monitor.State.LOG,
  149. monitoring_log_level=1,
  150. dry_run=True,
  151. )
  152. def test_ping_monitor_skips_ssl_verification_when_verify_tls_false():
  153. hook_config = {'push_url': CUSTOM_PUSH_URL, 'verify_tls': False}
  154. flexmock(module.requests).should_receive('get').with_args(
  155. f'{CUSTOM_PUSH_URL}?status=down&msg=fail',
  156. verify=False,
  157. timeout=int,
  158. headers={'User-Agent': 'borgmatic'},
  159. ).and_return(flexmock(ok=True)).once()
  160. module.ping_monitor(
  161. hook_config,
  162. {},
  163. 'config.yaml',
  164. borgmatic.hooks.monitoring.monitor.State.FAIL,
  165. monitoring_log_level=1,
  166. dry_run=False,
  167. )
  168. def test_ping_monitor_executes_ssl_verification_when_verify_tls_true():
  169. hook_config = {'push_url': CUSTOM_PUSH_URL, 'verify_tls': True}
  170. flexmock(module.requests).should_receive('get').with_args(
  171. f'{CUSTOM_PUSH_URL}?status=down&msg=fail',
  172. verify=True,
  173. timeout=int,
  174. headers={'User-Agent': 'borgmatic'},
  175. ).and_return(flexmock(ok=True)).once()
  176. module.ping_monitor(
  177. hook_config,
  178. {},
  179. 'config.yaml',
  180. borgmatic.hooks.monitoring.monitor.State.FAIL,
  181. monitoring_log_level=1,
  182. dry_run=False,
  183. )