2
0

test_dump.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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_path():
  29. flexmock(module.os.path).should_receive('expanduser').and_return('databases/localhost')
  30. flexmock(module.os.path).should_receive('exists').and_return(True)
  31. flexmock(module.shutil).should_receive('rmtree').with_args('databases/localhost').once()
  32. module.remove_database_dumps('databases', 'SuperDB', 'test.yaml', dry_run=False)
  33. def test_remove_database_dumps_with_dry_run_skips_removal():
  34. flexmock(module.os.path).should_receive('expanduser').and_return('databases/localhost')
  35. flexmock(module.os.path).should_receive('exists').never()
  36. flexmock(module.shutil).should_receive('rmtree').never()
  37. module.remove_database_dumps('databases', 'SuperDB', 'test.yaml', dry_run=True)
  38. def test_remove_database_dumps_without_dump_path_present_skips_removal():
  39. flexmock(module.os.path).should_receive('expanduser').and_return('databases/localhost')
  40. flexmock(module.os.path).should_receive('exists').and_return(False)
  41. flexmock(module.shutil).should_receive('rmtree').never()
  42. module.remove_database_dumps('databases', 'SuperDB', 'test.yaml', dry_run=False)
  43. def test_convert_glob_patterns_to_borg_patterns_removes_leading_slash():
  44. assert module.convert_glob_patterns_to_borg_patterns(('/etc/foo/bar',)) == ['sh:etc/foo/bar']