test_dump.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.hooks import dump as module
  4. def test_make_database_dump_path_joins_arguments():
  5. assert module.make_database_dump_path('/tmp', 'super_databases') == '/tmp/super_databases'
  6. def test_make_database_dump_path_defaults_without_source_directory():
  7. assert module.make_database_dump_path(None, 'super_databases') == '~/.borgmatic/super_databases'
  8. def test_make_database_dump_filename_uses_name_and_hostname():
  9. flexmock(module.os.path).should_receive('expanduser').and_return('databases')
  10. assert (
  11. module.make_database_dump_filename('databases', 'test', 'hostname')
  12. == 'databases/hostname/test'
  13. )
  14. def test_make_database_dump_filename_without_hostname_defaults_to_localhost():
  15. flexmock(module.os.path).should_receive('expanduser').and_return('databases')
  16. assert module.make_database_dump_filename('databases', 'test') == 'databases/localhost/test'
  17. def test_make_database_dump_filename_with_invalid_name_raises():
  18. flexmock(module.os.path).should_receive('expanduser').and_return('databases')
  19. with pytest.raises(ValueError):
  20. module.make_database_dump_filename('databases', 'invalid/name')
  21. def test_create_parent_directory_for_dump_does_not_raise():
  22. flexmock(module.os).should_receive('makedirs')
  23. module.create_parent_directory_for_dump('/path/to/parent')
  24. def test_create_named_pipe_for_dump_does_not_raise():
  25. flexmock(module).should_receive('create_parent_directory_for_dump')
  26. flexmock(module.os).should_receive('mkfifo')
  27. module.create_named_pipe_for_dump('/path/to/pipe')
  28. def test_remove_database_dumps_removes_dump_for_each_database():
  29. databases = [{'name': 'foo'}, {'name': 'bar'}]
  30. flexmock(module).should_receive('make_database_dump_filename').with_args(
  31. 'databases', 'foo', None
  32. ).and_return('databases/localhost/foo')
  33. flexmock(module).should_receive('make_database_dump_filename').with_args(
  34. 'databases', 'bar', None
  35. ).and_return('databases/localhost/bar')
  36. flexmock(module.os.path).should_receive('exists').and_return(True)
  37. flexmock(module.os.path).should_receive('isdir').and_return(False)
  38. flexmock(module.os).should_receive('remove').with_args('databases/localhost/foo').once()
  39. flexmock(module.os).should_receive('remove').with_args('databases/localhost/bar').once()
  40. flexmock(module.os).should_receive('listdir').with_args('databases/localhost').and_return(
  41. ['bar']
  42. ).and_return([])
  43. flexmock(module.os).should_receive('rmdir').with_args('databases/localhost').once()
  44. module.remove_database_dumps('databases', databases, 'SuperDB', 'test.yaml', dry_run=False)
  45. def test_remove_database_dumps_removes_dump_in_directory_format():
  46. databases = [{'name': 'foo'}]
  47. flexmock(module).should_receive('make_database_dump_filename').with_args(
  48. 'databases', 'foo', None
  49. ).and_return('databases/localhost/foo')
  50. flexmock(module.os.path).should_receive('exists').and_return(True)
  51. flexmock(module.os.path).should_receive('isdir').and_return(True)
  52. flexmock(module.os).should_receive('remove').never()
  53. flexmock(module.shutil).should_receive('rmtree').with_args('databases/localhost/foo').once()
  54. flexmock(module.os).should_receive('listdir').with_args('databases/localhost').and_return([])
  55. flexmock(module.os).should_receive('rmdir').with_args('databases/localhost').once()
  56. module.remove_database_dumps('databases', databases, 'SuperDB', 'test.yaml', dry_run=False)
  57. def test_remove_database_dumps_with_dry_run_skips_removal():
  58. databases = [{'name': 'foo'}, {'name': 'bar'}]
  59. flexmock(module.os).should_receive('rmdir').never()
  60. flexmock(module.os).should_receive('remove').never()
  61. module.remove_database_dumps('databases', databases, 'SuperDB', 'test.yaml', dry_run=True)
  62. def test_remove_database_dumps_without_dump_present_skips_removal():
  63. databases = [{'name': 'foo'}]
  64. flexmock(module).should_receive('make_database_dump_filename').with_args(
  65. 'databases', 'foo', None
  66. ).and_return('databases/localhost/foo')
  67. flexmock(module.os.path).should_receive('exists').and_return(False)
  68. flexmock(module.os.path).should_receive('isdir').never()
  69. flexmock(module.os).should_receive('remove').never()
  70. flexmock(module.shutil).should_receive('rmtree').never()
  71. flexmock(module.os).should_receive('rmdir').never()
  72. module.remove_database_dumps('databases', databases, 'SuperDB', 'test.yaml', dry_run=False)
  73. def test_remove_database_dumps_without_databases_does_not_raise():
  74. module.remove_database_dumps('databases', [], 'SuperDB', 'test.yaml', dry_run=False)
  75. def test_convert_glob_patterns_to_borg_patterns_removes_leading_slash():
  76. assert module.convert_glob_patterns_to_borg_patterns(('/etc/foo/bar',)) == ['sh:etc/foo/bar']