2
0

test_uptimekuma.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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', verify=True
  10. ).and_return(flexmock(ok=True)).once()
  11. module.ping_monitor(
  12. hook_config,
  13. {},
  14. 'config.yaml',
  15. borgmatic.hooks.monitoring.monitor.State.FAIL,
  16. monitoring_log_level=1,
  17. dry_run=False,
  18. )
  19. def test_ping_monitor_hits_custom_uptimekuma_on_fail():
  20. hook_config = {'push_url': CUSTOM_PUSH_URL}
  21. flexmock(module.requests).should_receive('get').with_args(
  22. f'{CUSTOM_PUSH_URL}?status=down&msg=fail', verify=True
  23. ).and_return(flexmock(ok=True)).once()
  24. module.ping_monitor(
  25. hook_config,
  26. {},
  27. 'config.yaml',
  28. borgmatic.hooks.monitoring.monitor.State.FAIL,
  29. monitoring_log_level=1,
  30. dry_run=False,
  31. )
  32. def test_ping_monitor_custom_uptimekuma_on_start():
  33. hook_config = {'push_url': CUSTOM_PUSH_URL}
  34. flexmock(module.requests).should_receive('get').with_args(
  35. f'{CUSTOM_PUSH_URL}?status=up&msg=start', verify=True
  36. ).and_return(flexmock(ok=True)).once()
  37. module.ping_monitor(
  38. hook_config,
  39. {},
  40. 'config.yaml',
  41. borgmatic.hooks.monitoring.monitor.State.START,
  42. monitoring_log_level=1,
  43. dry_run=False,
  44. )
  45. def test_ping_monitor_custom_uptimekuma_on_finish():
  46. hook_config = {'push_url': CUSTOM_PUSH_URL}
  47. flexmock(module.requests).should_receive('get').with_args(
  48. f'{CUSTOM_PUSH_URL}?status=up&msg=finish', verify=True
  49. ).and_return(flexmock(ok=True)).once()
  50. module.ping_monitor(
  51. hook_config,
  52. {},
  53. 'config.yaml',
  54. borgmatic.hooks.monitoring.monitor.State.FINISH,
  55. monitoring_log_level=1,
  56. dry_run=False,
  57. )
  58. def test_ping_monitor_does_not_hit_custom_uptimekuma_on_fail_dry_run():
  59. hook_config = {'push_url': CUSTOM_PUSH_URL}
  60. flexmock(module.requests).should_receive('get').never()
  61. module.ping_monitor(
  62. hook_config,
  63. {},
  64. 'config.yaml',
  65. borgmatic.hooks.monitoring.monitor.State.FAIL,
  66. monitoring_log_level=1,
  67. dry_run=True,
  68. )
  69. def test_ping_monitor_does_not_hit_custom_uptimekuma_on_start_dry_run():
  70. hook_config = {'push_url': CUSTOM_PUSH_URL}
  71. flexmock(module.requests).should_receive('get').never()
  72. module.ping_monitor(
  73. hook_config,
  74. {},
  75. 'config.yaml',
  76. borgmatic.hooks.monitoring.monitor.State.START,
  77. monitoring_log_level=1,
  78. dry_run=True,
  79. )
  80. def test_ping_monitor_does_not_hit_custom_uptimekuma_on_finish_dry_run():
  81. hook_config = {'push_url': CUSTOM_PUSH_URL}
  82. flexmock(module.requests).should_receive('get').never()
  83. module.ping_monitor(
  84. hook_config,
  85. {},
  86. 'config.yaml',
  87. borgmatic.hooks.monitoring.monitor.State.FINISH,
  88. monitoring_log_level=1,
  89. dry_run=True,
  90. )
  91. def test_ping_monitor_with_connection_error_logs_warning():
  92. hook_config = {'push_url': CUSTOM_PUSH_URL}
  93. flexmock(module.requests).should_receive('get').with_args(
  94. f'{CUSTOM_PUSH_URL}?status=down&msg=fail', verify=True
  95. ).and_raise(module.requests.exceptions.ConnectionError)
  96. flexmock(module.logger).should_receive('warning').once()
  97. module.ping_monitor(
  98. hook_config,
  99. {},
  100. 'config.yaml',
  101. borgmatic.hooks.monitoring.monitor.State.FAIL,
  102. monitoring_log_level=1,
  103. dry_run=False,
  104. )
  105. def test_ping_monitor_with_other_error_logs_warning():
  106. hook_config = {'push_url': CUSTOM_PUSH_URL}
  107. response = flexmock(ok=False)
  108. response.should_receive('raise_for_status').and_raise(
  109. module.requests.exceptions.RequestException
  110. )
  111. flexmock(module.requests).should_receive('get').with_args(
  112. f'{CUSTOM_PUSH_URL}?status=down&msg=fail', verify=True
  113. ).and_return(response)
  114. flexmock(module.logger).should_receive('warning').once()
  115. module.ping_monitor(
  116. hook_config,
  117. {},
  118. 'config.yaml',
  119. borgmatic.hooks.monitoring.monitor.State.FAIL,
  120. monitoring_log_level=1,
  121. dry_run=False,
  122. )
  123. def test_ping_monitor_with_invalid_run_state():
  124. hook_config = {'push_url': CUSTOM_PUSH_URL}
  125. flexmock(module.requests).should_receive('get').never()
  126. module.ping_monitor(
  127. hook_config,
  128. {},
  129. 'config.yaml',
  130. borgmatic.hooks.monitoring.monitor.State.LOG,
  131. monitoring_log_level=1,
  132. dry_run=True,
  133. )
  134. def test_ping_monitor_skips_ssl_verification_when_verify_tls_false():
  135. hook_config = {'push_url': CUSTOM_PUSH_URL, 'verify_tls': False}
  136. flexmock(module.requests).should_receive('get').with_args(
  137. f'{CUSTOM_PUSH_URL}?status=down&msg=fail', verify=False
  138. ).and_return(flexmock(ok=True)).once()
  139. module.ping_monitor(
  140. hook_config,
  141. {},
  142. 'config.yaml',
  143. borgmatic.hooks.monitoring.monitor.State.FAIL,
  144. monitoring_log_level=1,
  145. dry_run=False,
  146. )
  147. def test_ping_monitor_executes_ssl_verification_when_verify_tls_true():
  148. hook_config = {'push_url': CUSTOM_PUSH_URL, 'verify_tls': True}
  149. flexmock(module.requests).should_receive('get').with_args(
  150. f'{CUSTOM_PUSH_URL}?status=down&msg=fail', verify=True
  151. ).and_return(flexmock(ok=True)).once()
  152. module.ping_monitor(
  153. hook_config,
  154. {},
  155. 'config.yaml',
  156. borgmatic.hooks.monitoring.monitor.State.FAIL,
  157. monitoring_log_level=1,
  158. dry_run=False,
  159. )