test_dump.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_named_pipe_for_dump_does_not_raise():
  22. flexmock(module.os).should_receive('makedirs')
  23. flexmock(module.os.path).should_receive('exists').and_return(True)
  24. flexmock(module.os).should_receive('remove')
  25. flexmock(module.os).should_receive('mkfifo')
  26. module.create_named_pipe_for_dump('/path/to/pipe')
  27. def test_remove_database_dumps_removes_dump_for_each_database():
  28. databases = [{'name': 'foo'}, {'name': 'bar'}]
  29. flexmock(module).should_receive('make_database_dump_filename').with_args(
  30. 'databases', 'foo', None
  31. ).and_return('databases/localhost/foo')
  32. flexmock(module).should_receive('make_database_dump_filename').with_args(
  33. 'databases', 'bar', None
  34. ).and_return('databases/localhost/bar')
  35. flexmock(module.os.path).should_receive('isdir').and_return(False)
  36. flexmock(module.os).should_receive('remove').with_args('databases/localhost/foo').once()
  37. flexmock(module.os).should_receive('remove').with_args('databases/localhost/bar').once()
  38. flexmock(module.os).should_receive('listdir').with_args('databases/localhost').and_return(
  39. ['bar']
  40. ).and_return([])
  41. flexmock(module.os).should_receive('rmdir').with_args('databases/localhost').once()
  42. module.remove_database_dumps('databases', databases, 'SuperDB', 'test.yaml', dry_run=False)
  43. def test_remove_database_dumps_removes_dump_in_directory_format():
  44. databases = [{'name': 'foo'}]
  45. flexmock(module).should_receive('make_database_dump_filename').with_args(
  46. 'databases', 'foo', None
  47. ).and_return('databases/localhost/foo')
  48. flexmock(module.os.path).should_receive('isdir').and_return(True)
  49. flexmock(module.os).should_receive('remove').never()
  50. flexmock(module.shutil).should_receive('rmtree').with_args('databases/localhost/foo').once()
  51. flexmock(module.os).should_receive('listdir').with_args('databases/localhost').and_return([])
  52. flexmock(module.os).should_receive('rmdir').with_args('databases/localhost').once()
  53. module.remove_database_dumps('databases', databases, 'SuperDB', 'test.yaml', dry_run=False)
  54. def test_remove_database_dumps_with_dry_run_skips_removal():
  55. databases = [{'name': 'foo'}, {'name': 'bar'}]
  56. flexmock(module.os).should_receive('rmdir').never()
  57. flexmock(module.os).should_receive('remove').never()
  58. module.remove_database_dumps('databases', databases, 'SuperDB', 'test.yaml', dry_run=True)
  59. def test_remove_database_dumps_without_databases_does_not_raise():
  60. module.remove_database_dumps('databases', [], 'SuperDB', 'test.yaml', dry_run=False)
  61. def test_convert_glob_patterns_to_borg_patterns_removes_leading_slash():
  62. assert module.convert_glob_patterns_to_borg_patterns(('/etc/foo/bar',)) == ['sh:etc/foo/bar']