test_init.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_does_not_raise_for_borg_init_warning():
  24. insert_info_command_not_found_mock()
  25. flexmock(module.subprocess).should_receive('check_call').and_raise(
  26. module.subprocess.CalledProcessError(1, 'borg init')
  27. )
  28. module.initialize_repository(repository='repo', encryption_mode='repokey')
  29. def test_initialize_repository_raises_for_borg_init_error():
  30. insert_info_command_not_found_mock()
  31. flexmock(module.subprocess).should_receive('check_call').and_raise(
  32. module.subprocess.CalledProcessError(2, 'borg init')
  33. )
  34. with pytest.raises(subprocess.CalledProcessError):
  35. module.initialize_repository(repository='repo', encryption_mode='repokey')
  36. def test_initialize_repository_skips_initialization_when_repository_already_exists():
  37. insert_info_command_found_mock()
  38. flexmock(module.subprocess).should_receive('check_call').never()
  39. module.initialize_repository(repository='repo', encryption_mode='repokey')
  40. def test_initialize_repository_raises_for_unknown_info_command_error():
  41. flexmock(module).should_receive('execute_command').and_raise(
  42. subprocess.CalledProcessError(INFO_SOME_UNKNOWN_EXIT_CODE, [])
  43. )
  44. with pytest.raises(subprocess.CalledProcessError):
  45. module.initialize_repository(repository='repo', encryption_mode='repokey')
  46. def test_initialize_repository_with_append_only_calls_borg_with_append_only_parameter():
  47. insert_info_command_not_found_mock()
  48. insert_init_command_mock(INIT_COMMAND + ('--append-only',))
  49. module.initialize_repository(repository='repo', encryption_mode='repokey', append_only=True)
  50. def test_initialize_repository_with_storage_quota_calls_borg_with_storage_quota_parameter():
  51. insert_info_command_not_found_mock()
  52. insert_init_command_mock(INIT_COMMAND + ('--storage-quota', '5G'))
  53. module.initialize_repository(repository='repo', encryption_mode='repokey', storage_quota='5G')
  54. def test_initialize_repository_with_log_info_calls_borg_with_info_parameter():
  55. insert_info_command_not_found_mock()
  56. insert_init_command_mock(INIT_COMMAND + ('--info',))
  57. insert_logging_mock(logging.INFO)
  58. module.initialize_repository(repository='repo', encryption_mode='repokey')
  59. def test_initialize_repository_with_log_debug_calls_borg_with_debug_parameter():
  60. insert_info_command_not_found_mock()
  61. insert_init_command_mock(INIT_COMMAND + ('--debug',))
  62. insert_logging_mock(logging.DEBUG)
  63. module.initialize_repository(repository='repo', encryption_mode='repokey')
  64. def test_initialize_repository_with_local_path_calls_borg_via_local_path():
  65. insert_info_command_not_found_mock()
  66. insert_init_command_mock(('borg1',) + INIT_COMMAND[1:])
  67. module.initialize_repository(repository='repo', encryption_mode='repokey', local_path='borg1')
  68. def test_initialize_repository_with_remote_path_calls_borg_with_remote_path_parameter():
  69. insert_info_command_not_found_mock()
  70. insert_init_command_mock(INIT_COMMAND + ('--remote-path', 'borg1'))
  71. module.initialize_repository(repository='repo', encryption_mode='repokey', remote_path='borg1')