test_cronitor.py 3.4 KB

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