test_recreate.py 6.3 KB

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