test_cronitor.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from flexmock import flexmock
  2. from borgmatic.hooks.monitoring 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. {},
  11. 'config.yaml',
  12. module.monitor.State.START,
  13. monitoring_log_level=1,
  14. dry_run=False,
  15. )
  16. def test_ping_monitor_hits_ping_url_for_finish_state():
  17. hook_config = {'ping_url': 'https://example.com'}
  18. flexmock(module.requests).should_receive('get').with_args(
  19. 'https://example.com/complete'
  20. ).and_return(flexmock(ok=True))
  21. module.ping_monitor(
  22. hook_config,
  23. {},
  24. 'config.yaml',
  25. module.monitor.State.FINISH,
  26. monitoring_log_level=1,
  27. dry_run=False,
  28. )
  29. def test_ping_monitor_hits_ping_url_for_fail_state():
  30. hook_config = {'ping_url': 'https://example.com'}
  31. flexmock(module.requests).should_receive('get').with_args(
  32. 'https://example.com/fail'
  33. ).and_return(flexmock(ok=True))
  34. module.ping_monitor(
  35. hook_config,
  36. {},
  37. 'config.yaml',
  38. module.monitor.State.FAIL,
  39. monitoring_log_level=1,
  40. dry_run=False,
  41. )
  42. def test_ping_monitor_dry_run_does_not_hit_ping_url():
  43. hook_config = {'ping_url': 'https://example.com'}
  44. flexmock(module.requests).should_receive('get').never()
  45. module.ping_monitor(
  46. hook_config,
  47. {},
  48. 'config.yaml',
  49. module.monitor.State.START,
  50. monitoring_log_level=1,
  51. dry_run=True,
  52. )
  53. def test_ping_monitor_with_connection_error_logs_warning():
  54. hook_config = {'ping_url': 'https://example.com'}
  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. {},
  62. 'config.yaml',
  63. module.monitor.State.START,
  64. monitoring_log_level=1,
  65. dry_run=False,
  66. )
  67. def test_ping_monitor_with_other_error_logs_warning():
  68. hook_config = {'ping_url': 'https://example.com'}
  69. response = flexmock(ok=False)
  70. response.should_receive('raise_for_status').and_raise(
  71. module.requests.exceptions.RequestException
  72. )
  73. flexmock(module.requests).should_receive('get').with_args('https://example.com/run').and_return(
  74. response
  75. )
  76. flexmock(module.logger).should_receive('warning').once()
  77. module.ping_monitor(
  78. hook_config,
  79. {},
  80. 'config.yaml',
  81. module.monitor.State.START,
  82. monitoring_log_level=1,
  83. dry_run=False,
  84. )
  85. def test_ping_monitor_with_unsupported_monitoring_state_bails():
  86. hook_config = {'ping_url': 'https://example.com'}
  87. flexmock(module.requests).should_receive('get').never()
  88. module.ping_monitor(
  89. hook_config,
  90. {},
  91. 'config.yaml',
  92. module.monitor.State.LOG,
  93. monitoring_log_level=1,
  94. dry_run=False,
  95. )