2
0

test_pagerduty.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from flexmock import flexmock
  2. from borgmatic.hooks import pagerduty as module
  3. def test_ping_monitor_ignores_start_state():
  4. flexmock(module.requests).should_receive('post').never()
  5. module.ping_monitor(
  6. {'integration_key': 'abc123'},
  7. 'config.yaml',
  8. module.monitor.State.START,
  9. monitoring_log_level=1,
  10. dry_run=False,
  11. )
  12. def test_ping_monitor_ignores_finish_state():
  13. flexmock(module.requests).should_receive('post').never()
  14. module.ping_monitor(
  15. {'integration_key': 'abc123'},
  16. 'config.yaml',
  17. module.monitor.State.FINISH,
  18. monitoring_log_level=1,
  19. dry_run=False,
  20. )
  21. def test_ping_monitor_calls_api_for_fail_state():
  22. flexmock(module.requests).should_receive('post').and_return(flexmock(ok=True))
  23. module.ping_monitor(
  24. {'integration_key': 'abc123'},
  25. 'config.yaml',
  26. module.monitor.State.FAIL,
  27. monitoring_log_level=1,
  28. dry_run=False,
  29. )
  30. def test_ping_monitor_dry_run_does_not_call_api():
  31. flexmock(module.requests).should_receive('post').never()
  32. module.ping_monitor(
  33. {'integration_key': 'abc123'},
  34. 'config.yaml',
  35. module.monitor.State.FAIL,
  36. monitoring_log_level=1,
  37. dry_run=True,
  38. )
  39. def test_ping_monitor_with_connection_error_logs_warning():
  40. flexmock(module.requests).should_receive('post').and_raise(
  41. module.requests.exceptions.ConnectionError
  42. )
  43. flexmock(module.logger).should_receive('warning').once()
  44. module.ping_monitor(
  45. {'integration_key': 'abc123'},
  46. 'config.yaml',
  47. module.monitor.State.FAIL,
  48. monitoring_log_level=1,
  49. dry_run=False,
  50. )
  51. def test_ping_monitor_with_other_error_logs_warning():
  52. response = flexmock(ok=False)
  53. response.should_receive('raise_for_status').and_raise(
  54. module.requests.exceptions.RequestException
  55. )
  56. flexmock(module.requests).should_receive('post').and_return(response)
  57. flexmock(module.logger).should_receive('warning')
  58. module.ping_monitor(
  59. {'integration_key': 'abc123'},
  60. 'config.yaml',
  61. module.monitor.State.FAIL,
  62. monitoring_log_level=1,
  63. dry_run=False,
  64. )