test_cronhub.py 3.6 KB

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