test_cronitor.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from flexmock import flexmock
  2. from borgmatic.hooks import cronitor as module
  3. def test_ping_monitor_hits_ping_url_for_start_state():
  4. hook_config = {'ping_url': 'https://example.com'}
  5. flexmock(module.requests).should_receive('get').with_args('https://example.com/run').and_return(
  6. flexmock(ok=True)
  7. )
  8. module.ping_monitor(
  9. hook_config,
  10. 'config.yaml',
  11. module.monitor.State.START,
  12. monitoring_log_level=1,
  13. dry_run=False,
  14. )
  15. def test_ping_monitor_hits_ping_url_for_finish_state():
  16. hook_config = {'ping_url': 'https://example.com'}
  17. flexmock(module.requests).should_receive('get').with_args(
  18. 'https://example.com/complete'
  19. ).and_return(flexmock(ok=True))
  20. module.ping_monitor(
  21. hook_config,
  22. 'config.yaml',
  23. module.monitor.State.FINISH,
  24. monitoring_log_level=1,
  25. dry_run=False,
  26. )
  27. def test_ping_monitor_hits_ping_url_for_fail_state():
  28. hook_config = {'ping_url': 'https://example.com'}
  29. flexmock(module.requests).should_receive('get').with_args(
  30. 'https://example.com/fail'
  31. ).and_return(flexmock(ok=True))
  32. module.ping_monitor(
  33. hook_config, 'config.yaml', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=False
  34. )
  35. def test_ping_monitor_dry_run_does_not_hit_ping_url():
  36. hook_config = {'ping_url': 'https://example.com'}
  37. flexmock(module.requests).should_receive('get').never()
  38. module.ping_monitor(
  39. hook_config, 'config.yaml', module.monitor.State.START, monitoring_log_level=1, dry_run=True
  40. )
  41. def test_ping_monitor_with_connection_error_logs_warning():
  42. hook_config = {'ping_url': 'https://example.com'}
  43. flexmock(module.requests).should_receive('get').and_raise(
  44. module.requests.exceptions.ConnectionError
  45. )
  46. flexmock(module.logger).should_receive('warning').once()
  47. module.ping_monitor(
  48. hook_config,
  49. 'config.yaml',
  50. module.monitor.State.START,
  51. monitoring_log_level=1,
  52. dry_run=False,
  53. )
  54. def test_ping_monitor_with_other_error_logs_warning():
  55. hook_config = {'ping_url': 'https://example.com'}
  56. response = flexmock(ok=False)
  57. response.should_receive('raise_for_status').and_raise(
  58. module.requests.exceptions.RequestException
  59. )
  60. flexmock(module.requests).should_receive('get').with_args('https://example.com/run').and_return(
  61. response
  62. )
  63. flexmock(module.logger).should_receive('warning').once()
  64. module.ping_monitor(
  65. hook_config,
  66. 'config.yaml',
  67. module.monitor.State.START,
  68. monitoring_log_level=1,
  69. dry_run=False,
  70. )
  71. def test_ping_monitor_with_unsupported_monitoring_state():
  72. hook_config = {'ping_url': 'https://example.com'}
  73. flexmock(module.requests).should_receive('get').never()
  74. module.ping_monitor(
  75. hook_config, 'config.yaml', module.monitor.State.LOG, monitoring_log_level=1, dry_run=False,
  76. )