test_cronhub.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from flexmock import flexmock
  2. from borgmatic.hooks import cronhub as module
  3. def test_ping_monitor_rewrites_ping_url_for_start_state():
  4. hook_config = {'ping_url': 'https://example.com/start/abcdef'}
  5. flexmock(module.requests).should_receive('get').with_args(
  6. 'https://example.com/start/abcdef'
  7. ).and_return(flexmock(ok=True))
  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_rewrites_ping_url_and_state_for_start_state():
  16. hook_config = {'ping_url': 'https://example.com/ping/abcdef'}
  17. flexmock(module.requests).should_receive('get').with_args(
  18. 'https://example.com/start/abcdef'
  19. ).and_return(flexmock(ok=True))
  20. module.ping_monitor(
  21. hook_config,
  22. 'config.yaml',
  23. module.monitor.State.START,
  24. monitoring_log_level=1,
  25. dry_run=False,
  26. )
  27. def test_ping_monitor_rewrites_ping_url_for_finish_state():
  28. hook_config = {'ping_url': 'https://example.com/start/abcdef'}
  29. flexmock(module.requests).should_receive('get').with_args(
  30. 'https://example.com/finish/abcdef'
  31. ).and_return(flexmock(ok=True))
  32. module.ping_monitor(
  33. hook_config,
  34. 'config.yaml',
  35. module.monitor.State.FINISH,
  36. monitoring_log_level=1,
  37. dry_run=False,
  38. )
  39. def test_ping_monitor_rewrites_ping_url_for_fail_state():
  40. hook_config = {'ping_url': 'https://example.com/start/abcdef'}
  41. flexmock(module.requests).should_receive('get').with_args(
  42. 'https://example.com/fail/abcdef'
  43. ).and_return(flexmock(ok=True))
  44. module.ping_monitor(
  45. hook_config, 'config.yaml', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=False
  46. )
  47. def test_ping_monitor_dry_run_does_not_hit_ping_url():
  48. hook_config = {'ping_url': 'https://example.com'}
  49. flexmock(module.requests).should_receive('get').never()
  50. module.ping_monitor(
  51. hook_config, 'config.yaml', module.monitor.State.START, monitoring_log_level=1, dry_run=True
  52. )
  53. def test_ping_monitor_with_connection_error_logs_warning():
  54. hook_config = {'ping_url': 'https://example.com/start/abcdef'}
  55. flexmock(module.requests).should_receive('get').and_raise(
  56. module.requests.exceptions.ConnectionError
  57. )
  58. flexmock(module.logger).should_receive('warning').once()
  59. module.ping_monitor(
  60. hook_config,
  61. 'config.yaml',
  62. module.monitor.State.START,
  63. monitoring_log_level=1,
  64. dry_run=False,
  65. )
  66. def test_ping_monitor_with_other_error_logs_warning():
  67. hook_config = {'ping_url': 'https://example.com/start/abcdef'}
  68. response = flexmock(ok=False)
  69. response.should_receive('raise_for_status').and_raise(
  70. module.requests.exceptions.RequestException
  71. )
  72. flexmock(module.requests).should_receive('get').with_args(
  73. 'https://example.com/start/abcdef'
  74. ).and_return(response)
  75. flexmock(module.logger).should_receive('warning').once()
  76. module.ping_monitor(
  77. hook_config,
  78. 'config.yaml',
  79. module.monitor.State.START,
  80. monitoring_log_level=1,
  81. dry_run=False,
  82. )
  83. def test_ping_monitor_with_unsupported_monitoring_state():
  84. hook_config = {'ping_url': 'https://example.com'}
  85. flexmock(module.requests).should_receive('get').never()
  86. module.ping_monitor(
  87. hook_config, 'config.yaml', module.monitor.State.LOG, monitoring_log_level=1, dry_run=False,
  88. )