test_recreate.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.actions import recreate as module
  4. def test_run_recreate_does_not_raise():
  5. flexmock(module.logger).answer = lambda message: None
  6. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  7. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  8. flexmock()
  9. )
  10. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  11. None
  12. )
  13. flexmock(module.borgmatic.borg.recreate).should_receive('recreate_archive')
  14. module.run_recreate(
  15. repository={'path': 'repo'},
  16. config={},
  17. local_borg_version=None,
  18. recreate_arguments=flexmock(repository=flexmock(), archive=None),
  19. global_arguments=flexmock(),
  20. local_path=None,
  21. remote_path=None,
  22. )
  23. def test_run_recreate_with_archive_does_not_raise():
  24. flexmock(module.logger).answer = lambda message: None
  25. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  26. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  27. flexmock()
  28. )
  29. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  30. 'test-archive'
  31. )
  32. flexmock(module.borgmatic.borg.recreate).should_receive('recreate_archive')
  33. module.run_recreate(
  34. repository={'path': 'repo'},
  35. config={},
  36. local_borg_version=None,
  37. recreate_arguments=flexmock(repository=flexmock(), archive='test-archive'),
  38. global_arguments=flexmock(),
  39. local_path=None,
  40. remote_path=None,
  41. )
  42. def test_run_recreate_with_leftover_recreate_archive_raises():
  43. flexmock(module.logger).answer = lambda message: None
  44. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  45. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  46. flexmock()
  47. )
  48. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  49. 'test-archive.recreate'
  50. )
  51. flexmock(module.borgmatic.borg.recreate).should_receive('recreate_archive')
  52. with pytest.raises(ValueError):
  53. module.run_recreate(
  54. repository={'path': 'repo'},
  55. config={},
  56. local_borg_version=None,
  57. recreate_arguments=flexmock(repository=flexmock(), archive='test-archive.recreate'),
  58. global_arguments=flexmock(),
  59. local_path=None,
  60. remote_path=None,
  61. )
  62. def test_run_recreate_with_latest_archive_resolving_to_leftover_recreate_archive_raises():
  63. flexmock(module.logger).answer = lambda message: None
  64. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  65. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  66. flexmock()
  67. )
  68. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  69. 'test-archive.recreate'
  70. )
  71. flexmock(module.borgmatic.borg.recreate).should_receive('recreate_archive')
  72. with pytest.raises(ValueError):
  73. module.run_recreate(
  74. repository={'path': 'repo'},
  75. config={},
  76. local_borg_version=None,
  77. recreate_arguments=flexmock(repository=flexmock(), archive='latest'),
  78. global_arguments=flexmock(),
  79. local_path=None,
  80. remote_path=None,
  81. )
  82. def test_run_recreate_with_archive_already_exists_error_raises():
  83. flexmock(module.logger).answer = lambda message: None
  84. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  85. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  86. flexmock()
  87. )
  88. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  89. 'test-archive'
  90. )
  91. flexmock(module.borgmatic.borg.recreate).should_receive('recreate_archive').and_raise(
  92. module.subprocess.CalledProcessError(
  93. returncode=module.BORG_EXIT_CODE_ARCHIVE_ALREADY_EXISTS,
  94. cmd='borg recreate or whatever',
  95. )
  96. )
  97. with pytest.raises(ValueError):
  98. module.run_recreate(
  99. repository={'path': 'repo'},
  100. config={},
  101. local_borg_version=None,
  102. recreate_arguments=flexmock(repository=flexmock(), archive='test-archive', target=None),
  103. global_arguments=flexmock(),
  104. local_path=None,
  105. remote_path=None,
  106. )
  107. def test_run_recreate_with_target_and_archive_already_exists_error_raises():
  108. flexmock(module.logger).answer = lambda message: None
  109. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  110. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  111. flexmock()
  112. )
  113. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  114. 'test-archive'
  115. )
  116. flexmock(module.borgmatic.borg.recreate).should_receive('recreate_archive').and_raise(
  117. module.subprocess.CalledProcessError(
  118. returncode=module.BORG_EXIT_CODE_ARCHIVE_ALREADY_EXISTS,
  119. cmd='borg recreate or whatever',
  120. )
  121. )
  122. with pytest.raises(ValueError):
  123. module.run_recreate(
  124. repository={'path': 'repo'},
  125. config={},
  126. local_borg_version=None,
  127. recreate_arguments=flexmock(
  128. repository=flexmock(), archive='test-archive', target='target-archive'
  129. ),
  130. global_arguments=flexmock(),
  131. local_path=None,
  132. remote_path=None,
  133. )
  134. def test_run_recreate_with_other_called_process_error_passes_it_through():
  135. flexmock(module.logger).answer = lambda message: None
  136. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  137. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  138. flexmock()
  139. )
  140. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  141. 'test-archive'
  142. )
  143. flexmock(module.borgmatic.borg.recreate).should_receive('recreate_archive').and_raise(
  144. module.subprocess.CalledProcessError(
  145. returncode=1,
  146. cmd='borg recreate or whatever',
  147. )
  148. )
  149. with pytest.raises(module.subprocess.CalledProcessError):
  150. module.run_recreate(
  151. repository={'path': 'repo'},
  152. config={},
  153. local_borg_version=None,
  154. recreate_arguments=flexmock(
  155. repository=flexmock(), archive='test-archive', target='target-archive'
  156. ),
  157. global_arguments=flexmock(),
  158. local_path=None,
  159. remote_path=None,
  160. )