test_init.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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', '--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).should_receive('execute_command_without_capture').with_args(
  17. init_command, error_on_warnings=False
  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 + ('repo',))
  22. module.initialize_repository(repository='repo', storage_config={}, encryption_mode='repokey')
  23. def test_initialize_repository_raises_for_borg_init_error():
  24. insert_info_command_not_found_mock()
  25. flexmock(module).should_receive('execute_command_without_capture').and_raise(
  26. module.subprocess.CalledProcessError(2, 'borg init')
  27. )
  28. with pytest.raises(subprocess.CalledProcessError):
  29. module.initialize_repository(
  30. repository='repo', storage_config={}, encryption_mode='repokey'
  31. )
  32. def test_initialize_repository_skips_initialization_when_repository_already_exists():
  33. insert_info_command_found_mock()
  34. flexmock(module).should_receive('execute_command_without_capture').never()
  35. module.initialize_repository(repository='repo', storage_config={}, encryption_mode='repokey')
  36. def test_initialize_repository_raises_for_unknown_info_command_error():
  37. flexmock(module).should_receive('execute_command').and_raise(
  38. subprocess.CalledProcessError(INFO_SOME_UNKNOWN_EXIT_CODE, [])
  39. )
  40. with pytest.raises(subprocess.CalledProcessError):
  41. module.initialize_repository(
  42. repository='repo', storage_config={}, encryption_mode='repokey'
  43. )
  44. def test_initialize_repository_with_append_only_calls_borg_with_append_only_parameter():
  45. insert_info_command_not_found_mock()
  46. insert_init_command_mock(INIT_COMMAND + ('--append-only', 'repo'))
  47. module.initialize_repository(
  48. repository='repo', storage_config={}, encryption_mode='repokey', append_only=True
  49. )
  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', 'repo'))
  53. module.initialize_repository(
  54. repository='repo', storage_config={}, encryption_mode='repokey', storage_quota='5G'
  55. )
  56. def test_initialize_repository_with_log_info_calls_borg_with_info_parameter():
  57. insert_info_command_not_found_mock()
  58. insert_init_command_mock(INIT_COMMAND + ('--info', 'repo'))
  59. insert_logging_mock(logging.INFO)
  60. module.initialize_repository(repository='repo', storage_config={}, encryption_mode='repokey')
  61. def test_initialize_repository_with_log_debug_calls_borg_with_debug_parameter():
  62. insert_info_command_not_found_mock()
  63. insert_init_command_mock(INIT_COMMAND + ('--debug', 'repo'))
  64. insert_logging_mock(logging.DEBUG)
  65. module.initialize_repository(repository='repo', storage_config={}, encryption_mode='repokey')
  66. def test_initialize_repository_with_local_path_calls_borg_via_local_path():
  67. insert_info_command_not_found_mock()
  68. insert_init_command_mock(('borg1',) + INIT_COMMAND[1:] + ('repo',))
  69. module.initialize_repository(
  70. repository='repo', storage_config={}, encryption_mode='repokey', local_path='borg1'
  71. )
  72. def test_initialize_repository_with_remote_path_calls_borg_with_remote_path_parameter():
  73. insert_info_command_not_found_mock()
  74. insert_init_command_mock(INIT_COMMAND + ('--remote-path', 'borg1', 'repo'))
  75. module.initialize_repository(
  76. repository='repo', storage_config={}, encryption_mode='repokey', remote_path='borg1'
  77. )
  78. def test_initialize_repository_with_extra_borg_options_calls_borg_with_extra_options():
  79. insert_info_command_not_found_mock()
  80. insert_init_command_mock(INIT_COMMAND + ('--extra', '--options', 'repo'))
  81. module.initialize_repository(
  82. repository='repo',
  83. storage_config={'extra_borg_options': {'init': '--extra --options'}},
  84. encryption_mode='repokey',
  85. )