test_init.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import logging
  2. import subprocess
  3. import pytest
  4. from flexmock import flexmock
  5. from borgmatic.borg import init as module
  6. from ..test_verbosity import insert_logging_mock
  7. INFO_SOME_UNKNOWN_EXIT_CODE = -999
  8. INIT_COMMAND = ('borg', 'init', 'repo', '--encryption', 'repokey')
  9. def insert_info_command_found_mock():
  10. flexmock(module).should_receive('execute_command')
  11. def insert_info_command_not_found_mock():
  12. flexmock(module).should_receive('execute_command').and_raise(
  13. subprocess.CalledProcessError(module.INFO_REPOSITORY_NOT_FOUND_EXIT_CODE, [])
  14. )
  15. def insert_init_command_mock(init_command, **kwargs):
  16. flexmock(module.subprocess).should_receive('check_call').with_args(
  17. init_command, **kwargs
  18. ).once()
  19. def test_initialize_repository_calls_borg_with_parameters():
  20. insert_info_command_not_found_mock()
  21. insert_init_command_mock(INIT_COMMAND)
  22. module.initialize_repository(repository='repo', encryption_mode='repokey')
  23. def test_initialize_repository_skips_initialization_when_repository_already_exists():
  24. insert_info_command_found_mock()
  25. flexmock(module.subprocess).should_receive('check_call').never()
  26. module.initialize_repository(repository='repo', encryption_mode='repokey')
  27. def test_initialize_repository_raises_for_unknown_info_command_error():
  28. flexmock(module).should_receive('execute_command').and_raise(
  29. subprocess.CalledProcessError(INFO_SOME_UNKNOWN_EXIT_CODE, [])
  30. )
  31. with pytest.raises(subprocess.CalledProcessError):
  32. module.initialize_repository(repository='repo', encryption_mode='repokey')
  33. def test_initialize_repository_with_append_only_calls_borg_with_append_only_parameter():
  34. insert_info_command_not_found_mock()
  35. insert_init_command_mock(INIT_COMMAND + ('--append-only',))
  36. module.initialize_repository(repository='repo', encryption_mode='repokey', append_only=True)
  37. def test_initialize_repository_with_storage_quota_calls_borg_with_storage_quota_parameter():
  38. insert_info_command_not_found_mock()
  39. insert_init_command_mock(INIT_COMMAND + ('--storage-quota', '5G'))
  40. module.initialize_repository(repository='repo', encryption_mode='repokey', storage_quota='5G')
  41. def test_initialize_repository_with_log_info_calls_borg_with_info_parameter():
  42. insert_info_command_not_found_mock()
  43. insert_init_command_mock(INIT_COMMAND + ('--info',))
  44. insert_logging_mock(logging.INFO)
  45. module.initialize_repository(repository='repo', encryption_mode='repokey')
  46. def test_initialize_repository_with_log_debug_calls_borg_with_debug_parameter():
  47. insert_info_command_not_found_mock()
  48. insert_init_command_mock(INIT_COMMAND + ('--debug',))
  49. insert_logging_mock(logging.DEBUG)
  50. module.initialize_repository(repository='repo', encryption_mode='repokey')
  51. def test_initialize_repository_with_local_path_calls_borg_via_local_path():
  52. insert_info_command_not_found_mock()
  53. insert_init_command_mock(('borg1',) + INIT_COMMAND[1:])
  54. module.initialize_repository(repository='repo', encryption_mode='repokey', local_path='borg1')
  55. def test_initialize_repository_with_remote_path_calls_borg_with_remote_path_parameter():
  56. insert_info_command_not_found_mock()
  57. insert_init_command_mock(INIT_COMMAND + ('--remote-path', 'borg1'))
  58. module.initialize_repository(repository='repo', encryption_mode='repokey', remote_path='borg1')