test_cronhub.py 3.8 KB

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