test_cronitor.py 3.2 KB

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