test_hook.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import logging
  2. from flexmock import flexmock
  3. from borgmatic import hook as module
  4. def test_interpolate_context_passes_through_command_without_variable():
  5. assert module.interpolate_context('ls', {'foo': 'bar'}) == 'ls'
  6. def test_interpolate_context_passes_through_command_with_unknown_variable():
  7. assert module.interpolate_context('ls {baz}', {'foo': 'bar'}) == 'ls {baz}'
  8. def test_interpolate_context_interpolates_variables():
  9. context = {'foo': 'bar', 'baz': 'quux'}
  10. assert module.interpolate_context('ls {foo}{baz} {baz}', context) == 'ls barquux quux'
  11. def test_execute_hook_invokes_each_command():
  12. flexmock(module).should_receive('interpolate_context').replace_with(
  13. lambda command, context: command
  14. )
  15. flexmock(module.execute).should_receive('execute_command').with_args(
  16. [':'], output_log_level=logging.WARNING, shell=True
  17. ).once()
  18. module.execute_hook([':'], None, 'config.yaml', 'pre-backup', dry_run=False)
  19. def test_execute_hook_with_multiple_commands_invokes_each_command():
  20. flexmock(module).should_receive('interpolate_context').replace_with(
  21. lambda command, context: command
  22. )
  23. flexmock(module.execute).should_receive('execute_command').with_args(
  24. [':'], output_log_level=logging.WARNING, shell=True
  25. ).once()
  26. flexmock(module.execute).should_receive('execute_command').with_args(
  27. ['true'], output_log_level=logging.WARNING, shell=True
  28. ).once()
  29. module.execute_hook([':', 'true'], None, 'config.yaml', 'pre-backup', dry_run=False)
  30. def test_execute_hook_with_umask_sets_that_umask():
  31. flexmock(module).should_receive('interpolate_context').replace_with(
  32. lambda command, context: command
  33. )
  34. flexmock(module.os).should_receive('umask').with_args(0o77).and_return(0o22).once()
  35. flexmock(module.os).should_receive('umask').with_args(0o22).once()
  36. flexmock(module.execute).should_receive('execute_command').with_args(
  37. [':'], output_log_level=logging.WARNING, shell=True
  38. )
  39. module.execute_hook([':'], 77, 'config.yaml', 'pre-backup', dry_run=False)
  40. def test_execute_hook_with_dry_run_skips_commands():
  41. flexmock(module).should_receive('interpolate_context').replace_with(
  42. lambda command, context: command
  43. )
  44. flexmock(module.execute).should_receive('execute_command').never()
  45. module.execute_hook([':', 'true'], None, 'config.yaml', 'pre-backup', dry_run=True)
  46. def test_execute_hook_with_empty_commands_does_not_raise():
  47. module.execute_hook([], None, 'config.yaml', 'post-backup', dry_run=False)
  48. def test_execute_hook_on_error_logs_as_error():
  49. flexmock(module).should_receive('interpolate_context').replace_with(
  50. lambda command, context: command
  51. )
  52. flexmock(module.execute).should_receive('execute_command').with_args(
  53. [':'], output_log_level=logging.ERROR, shell=True
  54. ).once()
  55. module.execute_hook([':'], None, 'config.yaml', 'on-error', dry_run=False)
  56. def test_ping_healthchecks_hits_ping_url():
  57. ping_url = 'https://example.com'
  58. flexmock(module.requests).should_receive('get').with_args(ping_url)
  59. module.ping_healthchecks(ping_url, 'config.yaml', dry_run=False)
  60. def test_ping_healthchecks_without_ping_url_does_not_raise():
  61. flexmock(module.requests).should_receive('get').never()
  62. module.ping_healthchecks(ping_url_or_uuid=None, config_filename='config.yaml', dry_run=False)
  63. def test_ping_healthchecks_with_ping_uuid_hits_corresponding_url():
  64. ping_uuid = 'abcd-efgh-ijkl-mnop'
  65. flexmock(module.requests).should_receive('get').with_args(
  66. 'https://hc-ping.com/{}'.format(ping_uuid)
  67. )
  68. module.ping_healthchecks(ping_uuid, 'config.yaml', dry_run=False)
  69. def test_ping_healthchecks_hits_ping_url_with_append():
  70. ping_url = 'https://example.com'
  71. append = 'failed-so-hard'
  72. flexmock(module.requests).should_receive('get').with_args('{}/{}'.format(ping_url, append))
  73. module.ping_healthchecks(ping_url, 'config.yaml', dry_run=False, append=append)