2
0

test_cronhub.py 4.0 KB

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