test_init.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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').with_args(
  17. init_command, output_file=module.DO_NOT_CAPTURE, borg_local_path=init_command[0]
  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').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. flexmock(module).should_receive('execute_command').once()
  34. module.initialize_repository(repository='repo', storage_config={}, encryption_mode='repokey')
  35. def test_initialize_repository_raises_for_unknown_info_command_error():
  36. flexmock(module).should_receive('execute_command').and_raise(
  37. subprocess.CalledProcessError(INFO_SOME_UNKNOWN_EXIT_CODE, [])
  38. )
  39. with pytest.raises(subprocess.CalledProcessError):
  40. module.initialize_repository(
  41. repository='repo', storage_config={}, encryption_mode='repokey'
  42. )
  43. def test_initialize_repository_with_append_only_calls_borg_with_append_only_parameter():
  44. insert_info_command_not_found_mock()
  45. insert_init_command_mock(INIT_COMMAND + ('--append-only', 'repo'))
  46. module.initialize_repository(
  47. repository='repo', storage_config={}, encryption_mode='repokey', append_only=True
  48. )
  49. def test_initialize_repository_with_storage_quota_calls_borg_with_storage_quota_parameter():
  50. insert_info_command_not_found_mock()
  51. insert_init_command_mock(INIT_COMMAND + ('--storage-quota', '5G', 'repo'))
  52. module.initialize_repository(
  53. repository='repo', storage_config={}, encryption_mode='repokey', storage_quota='5G'
  54. )
  55. def test_initialize_repository_with_log_info_calls_borg_with_info_parameter():
  56. insert_info_command_not_found_mock()
  57. insert_init_command_mock(INIT_COMMAND + ('--info', 'repo'))
  58. insert_logging_mock(logging.INFO)
  59. module.initialize_repository(repository='repo', storage_config={}, encryption_mode='repokey')
  60. def test_initialize_repository_with_log_debug_calls_borg_with_debug_parameter():
  61. insert_info_command_not_found_mock()
  62. insert_init_command_mock(INIT_COMMAND + ('--debug', 'repo'))
  63. insert_logging_mock(logging.DEBUG)
  64. module.initialize_repository(repository='repo', storage_config={}, encryption_mode='repokey')
  65. def test_initialize_repository_with_local_path_calls_borg_via_local_path():
  66. insert_info_command_not_found_mock()
  67. insert_init_command_mock(('borg1',) + INIT_COMMAND[1:] + ('repo',))
  68. module.initialize_repository(
  69. repository='repo', storage_config={}, encryption_mode='repokey', local_path='borg1'
  70. )
  71. def test_initialize_repository_with_remote_path_calls_borg_with_remote_path_parameter():
  72. insert_info_command_not_found_mock()
  73. insert_init_command_mock(INIT_COMMAND + ('--remote-path', 'borg1', 'repo'))
  74. module.initialize_repository(
  75. repository='repo', storage_config={}, encryption_mode='repokey', remote_path='borg1'
  76. )
  77. def test_initialize_repository_with_extra_borg_options_calls_borg_with_extra_options():
  78. insert_info_command_not_found_mock()
  79. insert_init_command_mock(INIT_COMMAND + ('--extra', '--options', 'repo'))
  80. module.initialize_repository(
  81. repository='repo',
  82. storage_config={'extra_borg_options': {'init': '--extra --options'}},
  83. encryption_mode='repokey',
  84. )