test_dump.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.hooks import dump as module
  4. def test_make_data_source_dump_path_joins_arguments():
  5. assert module.make_data_source_dump_path('/tmp', 'super_databases') == '/tmp/super_databases'
  6. def test_make_data_source_dump_path_defaults_without_source_directory():
  7. assert (
  8. module.make_data_source_dump_path(None, 'super_databases') == '~/.borgmatic/super_databases'
  9. )
  10. def test_make_data_source_dump_filename_uses_name_and_hostname():
  11. flexmock(module.os.path).should_receive('expanduser').and_return('databases')
  12. assert (
  13. module.make_data_source_dump_filename('databases', 'test', 'hostname')
  14. == 'databases/hostname/test'
  15. )
  16. def test_make_data_source_dump_filename_without_hostname_defaults_to_localhost():
  17. flexmock(module.os.path).should_receive('expanduser').and_return('databases')
  18. assert module.make_data_source_dump_filename('databases', 'test') == 'databases/localhost/test'
  19. def test_make_data_source_dump_filename_with_invalid_name_raises():
  20. flexmock(module.os.path).should_receive('expanduser').and_return('databases')
  21. with pytest.raises(ValueError):
  22. module.make_data_source_dump_filename('databases', 'invalid/name')
  23. def test_create_parent_directory_for_dump_does_not_raise():
  24. flexmock(module.os).should_receive('makedirs')
  25. module.create_parent_directory_for_dump('/path/to/parent')
  26. def test_create_named_pipe_for_dump_does_not_raise():
  27. flexmock(module).should_receive('create_parent_directory_for_dump')
  28. flexmock(module.os).should_receive('mkfifo')
  29. module.create_named_pipe_for_dump('/path/to/pipe')
  30. def test_remove_data_source_dumps_removes_dump_path():
  31. flexmock(module.os.path).should_receive('expanduser').and_return('databases/localhost')
  32. flexmock(module.os.path).should_receive('exists').and_return(True)
  33. flexmock(module.shutil).should_receive('rmtree').with_args('databases/localhost').once()
  34. module.remove_data_source_dumps('databases', 'SuperDB', 'test.yaml', dry_run=False)
  35. def test_remove_data_source_dumps_with_dry_run_skips_removal():
  36. flexmock(module.os.path).should_receive('expanduser').and_return('databases/localhost')
  37. flexmock(module.os.path).should_receive('exists').never()
  38. flexmock(module.shutil).should_receive('rmtree').never()
  39. module.remove_data_source_dumps('databases', 'SuperDB', 'test.yaml', dry_run=True)
  40. def test_remove_data_source_dumps_without_dump_path_present_skips_removal():
  41. flexmock(module.os.path).should_receive('expanduser').and_return('databases/localhost')
  42. flexmock(module.os.path).should_receive('exists').and_return(False)
  43. flexmock(module.shutil).should_receive('rmtree').never()
  44. module.remove_data_source_dumps('databases', 'SuperDB', 'test.yaml', dry_run=False)
  45. def test_convert_glob_patterns_to_borg_patterns_removes_leading_slash():
  46. assert module.convert_glob_patterns_to_borg_patterns(('/etc/foo/bar',)) == ['sh:etc/foo/bar']