test_rcreate.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import logging
  2. import subprocess
  3. import pytest
  4. from flexmock import flexmock
  5. from borgmatic.borg import rcreate as module
  6. from ..test_verbosity import insert_logging_mock
  7. RINFO_SOME_UNKNOWN_EXIT_CODE = -999
  8. RCREATE_COMMAND = ('borg', 'rcreate', '--encryption', 'repokey')
  9. def insert_rinfo_command_found_mock():
  10. flexmock(module.rinfo).should_receive('display_repository_info')
  11. def insert_rinfo_command_not_found_mock():
  12. flexmock(module.rinfo).should_receive('display_repository_info').and_raise(
  13. subprocess.CalledProcessError(module.RINFO_REPOSITORY_NOT_FOUND_EXIT_CODE, [])
  14. )
  15. def insert_rcreate_command_mock(rcreate_command, **kwargs):
  16. flexmock(module.environment).should_receive('make_environment')
  17. flexmock(module).should_receive('execute_command').with_args(
  18. rcreate_command,
  19. output_file=module.DO_NOT_CAPTURE,
  20. borg_local_path=rcreate_command[0],
  21. extra_environment=None,
  22. ).once()
  23. def test_create_repository_calls_borg_with_parameters():
  24. insert_rinfo_command_not_found_mock()
  25. insert_rcreate_command_mock(RCREATE_COMMAND + ('--repo', 'repo'))
  26. flexmock(module.feature).should_receive('available').and_return(True)
  27. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo',))
  28. module.create_repository(
  29. repository='repo', storage_config={}, local_borg_version='2.3.4', encryption_mode='repokey'
  30. )
  31. def test_create_repository_raises_for_borg_rcreate_error():
  32. insert_rinfo_command_not_found_mock()
  33. flexmock(module.feature).should_receive('available').and_return(True)
  34. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo',))
  35. flexmock(module.environment).should_receive('make_environment')
  36. flexmock(module).should_receive('execute_command').and_raise(
  37. module.subprocess.CalledProcessError(2, 'borg rcreate')
  38. )
  39. with pytest.raises(subprocess.CalledProcessError):
  40. module.create_repository(
  41. repository='repo',
  42. storage_config={},
  43. local_borg_version='2.3.4',
  44. encryption_mode='repokey',
  45. )
  46. def test_create_repository_skips_creation_when_repository_already_exists():
  47. insert_rinfo_command_found_mock()
  48. flexmock(module.feature).should_receive('available').and_return(True)
  49. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo',))
  50. module.create_repository(
  51. repository='repo', storage_config={}, local_borg_version='2.3.4', encryption_mode='repokey'
  52. )
  53. def test_create_repository_raises_for_unknown_rinfo_command_error():
  54. flexmock(module.rinfo).should_receive('display_repository_info').and_raise(
  55. subprocess.CalledProcessError(RINFO_SOME_UNKNOWN_EXIT_CODE, [])
  56. )
  57. with pytest.raises(subprocess.CalledProcessError):
  58. module.create_repository(
  59. repository='repo',
  60. storage_config={},
  61. local_borg_version='2.3.4',
  62. encryption_mode='repokey',
  63. )
  64. def test_create_repository_with_append_only_calls_borg_with_append_only_parameter():
  65. insert_rinfo_command_not_found_mock()
  66. insert_rcreate_command_mock(RCREATE_COMMAND + ('--append-only', '--repo', 'repo'))
  67. flexmock(module.feature).should_receive('available').and_return(True)
  68. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo',))
  69. module.create_repository(
  70. repository='repo',
  71. storage_config={},
  72. local_borg_version='2.3.4',
  73. encryption_mode='repokey',
  74. append_only=True,
  75. )
  76. def test_create_repository_with_storage_quota_calls_borg_with_storage_quota_parameter():
  77. insert_rinfo_command_not_found_mock()
  78. insert_rcreate_command_mock(RCREATE_COMMAND + ('--storage-quota', '5G', '--repo', 'repo'))
  79. flexmock(module.feature).should_receive('available').and_return(True)
  80. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo',))
  81. module.create_repository(
  82. repository='repo',
  83. storage_config={},
  84. local_borg_version='2.3.4',
  85. encryption_mode='repokey',
  86. storage_quota='5G',
  87. )
  88. def test_create_repository_with_log_info_calls_borg_with_info_parameter():
  89. insert_rinfo_command_not_found_mock()
  90. insert_rcreate_command_mock(RCREATE_COMMAND + ('--info', '--repo', 'repo'))
  91. insert_logging_mock(logging.INFO)
  92. flexmock(module.feature).should_receive('available').and_return(True)
  93. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo',))
  94. module.create_repository(
  95. repository='repo', storage_config={}, local_borg_version='2.3.4', encryption_mode='repokey'
  96. )
  97. def test_create_repository_with_log_debug_calls_borg_with_debug_parameter():
  98. insert_rinfo_command_not_found_mock()
  99. insert_rcreate_command_mock(RCREATE_COMMAND + ('--debug', '--repo', 'repo'))
  100. insert_logging_mock(logging.DEBUG)
  101. flexmock(module.feature).should_receive('available').and_return(True)
  102. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo',))
  103. module.create_repository(
  104. repository='repo', storage_config={}, local_borg_version='2.3.4', encryption_mode='repokey'
  105. )
  106. def test_create_repository_with_local_path_calls_borg_via_local_path():
  107. insert_rinfo_command_not_found_mock()
  108. insert_rcreate_command_mock(('borg1',) + RCREATE_COMMAND[1:] + ('--repo', 'repo'))
  109. flexmock(module.feature).should_receive('available').and_return(True)
  110. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo',))
  111. module.create_repository(
  112. repository='repo',
  113. storage_config={},
  114. local_borg_version='2.3.4',
  115. encryption_mode='repokey',
  116. local_path='borg1',
  117. )
  118. def test_create_repository_with_remote_path_calls_borg_with_remote_path_parameter():
  119. insert_rinfo_command_not_found_mock()
  120. insert_rcreate_command_mock(RCREATE_COMMAND + ('--remote-path', 'borg1', '--repo', 'repo'))
  121. flexmock(module.feature).should_receive('available').and_return(True)
  122. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo',))
  123. module.create_repository(
  124. repository='repo',
  125. storage_config={},
  126. local_borg_version='2.3.4',
  127. encryption_mode='repokey',
  128. remote_path='borg1',
  129. )
  130. def test_create_repository_with_extra_borg_options_calls_borg_with_extra_options():
  131. insert_rinfo_command_not_found_mock()
  132. insert_rcreate_command_mock(RCREATE_COMMAND + ('--extra', '--options', '--repo', 'repo'))
  133. flexmock(module.feature).should_receive('available').and_return(True)
  134. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo',))
  135. module.create_repository(
  136. repository='repo',
  137. storage_config={'extra_borg_options': {'rcreate': '--extra --options'}},
  138. local_borg_version='2.3.4',
  139. encryption_mode='repokey',
  140. )