test_uptimekuma.py 6.1 KB

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