test_init.py 4.6 KB

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