test_dump.py 3.5 KB

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