test_recreate.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import logging
  2. from flexmock import flexmock
  3. from borgmatic.borg import recreate as module
  4. from ..test_verbosity import insert_logging_mock
  5. # from borgmatic.borg.pattern import Pattern, Pattern_type, Pattern_style, Pattern_source
  6. # from borgmatic.borg.create import make_exclude_flags, make_list_filter_flags
  7. def insert_execute_command_mock(command, working_directory=None, borg_exit_codes=None):
  8. flexmock(module.borgmatic.borg.environment).should_receive('make_environment')
  9. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  10. full_command=command,
  11. output_log_level=module.logging.INFO,
  12. environment=None,
  13. working_directory=working_directory,
  14. borg_local_path=command[0],
  15. borg_exit_codes=borg_exit_codes,
  16. ).once()
  17. def test_recreate_archive_dry_run_skips_execution():
  18. flexmock(module.borgmatic.borg.flags).should_receive(
  19. 'make_repository_archive_flags'
  20. ).and_return(('repo::archive',))
  21. flexmock(module.borgmatic.execute).should_receive('execute_command').never()
  22. recreate_arguments = flexmock(repository=flexmock(), list=None, path=None)
  23. result = module.recreate_archive(
  24. repository='repo',
  25. archive='archive',
  26. config={},
  27. local_borg_version='1.2.3',
  28. recreate_arguments=recreate_arguments,
  29. global_arguments=flexmock(log_json=False, dry_run=True),
  30. local_path='borg',
  31. )
  32. assert result is None
  33. def test_recreate_calls_borg_with_required_flags():
  34. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  35. ('repo::archive',)
  36. )
  37. flexmock(module).should_receive('write_patterns_file').and_return(None)
  38. flexmock(module).should_receive('make_list_filter_flags').and_return(None)
  39. insert_execute_command_mock(('borg', 'recreate', 'repo::archive'))
  40. module.recreate_archive(
  41. repository='repo',
  42. archive='archive',
  43. config={},
  44. local_borg_version='1.2.3',
  45. recreate_arguments=flexmock(path=None, list=None),
  46. global_arguments=flexmock(dry_run=False, log_json=False),
  47. local_path='borg',
  48. remote_path=None,
  49. patterns=None,
  50. )
  51. def test_recreate_with_remote_path():
  52. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  53. ('repo::archive',)
  54. )
  55. flexmock(module).should_receive('write_patterns_file').and_return(None)
  56. flexmock(module).should_receive('make_list_filter_flags').and_return(None)
  57. insert_execute_command_mock(('borg', 'recreate', '--remote-path', 'borg1', 'repo::archive'))
  58. module.recreate_archive(
  59. repository='repo',
  60. archive='archive',
  61. config={},
  62. local_borg_version='1.2.3',
  63. recreate_arguments=flexmock(path=None, list=None),
  64. global_arguments=flexmock(dry_run=False, log_json=False),
  65. local_path='borg',
  66. remote_path='borg1',
  67. patterns=None,
  68. )
  69. def test_recreate_with_lock_wait():
  70. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  71. ('repo::archive',)
  72. )
  73. flexmock(module).should_receive('write_patterns_file').and_return(None)
  74. flexmock(module).should_receive('make_list_filter_flags').and_return(None)
  75. insert_execute_command_mock(('borg', 'recreate', '--lock-wait', '5', 'repo::archive'))
  76. module.recreate_archive(
  77. repository='repo',
  78. archive='archive',
  79. config={'lock_wait': '5'},
  80. local_borg_version='1.2.3',
  81. recreate_arguments=flexmock(path=None, list=None),
  82. global_arguments=flexmock(dry_run=False, log_json=False),
  83. local_path='borg',
  84. patterns=None,
  85. )
  86. def test_recreate_with_log_info():
  87. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  88. ('repo::archive',)
  89. )
  90. flexmock(module).should_receive('write_patterns_file').and_return(None)
  91. flexmock(module).should_receive('make_list_filter_flags').and_return(None)
  92. insert_execute_command_mock(('borg', 'recreate', '--info', 'repo::archive'))
  93. insert_logging_mock(logging.INFO)
  94. module.recreate_archive(
  95. repository='repo',
  96. archive='archive',
  97. config={},
  98. local_borg_version='1.2.3',
  99. recreate_arguments=flexmock(path=None, list=None),
  100. global_arguments=flexmock(dry_run=False, log_json=False),
  101. local_path='borg',
  102. patterns=None,
  103. )
  104. def test_recreate_with_log_debug():
  105. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  106. ('repo::archive',)
  107. )
  108. flexmock(module).should_receive('write_patterns_file').and_return(None)
  109. flexmock(module).should_receive('make_list_filter_flags').and_return(None)
  110. insert_execute_command_mock(('borg', 'recreate', '--debug', '--show-rc', 'repo::archive'))
  111. insert_logging_mock(logging.DEBUG)
  112. module.recreate_archive(
  113. repository='repo',
  114. archive='archive',
  115. config={},
  116. local_borg_version='1.2.3',
  117. recreate_arguments=flexmock(path=None, list=None),
  118. global_arguments=flexmock(dry_run=False, log_json=False),
  119. local_path='borg',
  120. patterns=None,
  121. )
  122. def test_recreate_with_log_json():
  123. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  124. ('repo::archive',)
  125. )
  126. flexmock(module).should_receive('write_patterns_file').and_return(None)
  127. flexmock(module).should_receive('make_list_filter_flags').and_return(None)
  128. insert_execute_command_mock(('borg', 'recreate', '--log-json', 'repo::archive'))
  129. module.recreate_archive(
  130. repository='repo',
  131. archive='archive',
  132. config={},
  133. local_borg_version='1.2.3',
  134. recreate_arguments=flexmock(path=None, list=None),
  135. global_arguments=flexmock(dry_run=False, log_json=True),
  136. local_path='borg',
  137. patterns=None,
  138. )
  139. def test_recreate_with_path_flag():
  140. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  141. ('repo::archive',)
  142. )
  143. flexmock(module).should_receive('write_patterns_file').and_return(None)
  144. flexmock(module).should_receive('make_list_filter_flags').and_return(None)
  145. insert_execute_command_mock(('borg', 'recreate', '--path', '/some/path', 'repo::archive'))
  146. module.recreate_archive(
  147. repository='repo',
  148. archive='archive',
  149. config={},
  150. local_borg_version='1.2.3',
  151. recreate_arguments=flexmock(path='/some/path', list=None),
  152. global_arguments=flexmock(dry_run=False, log_json=False),
  153. local_path='borg',
  154. patterns=None,
  155. )
  156. def test_recreate_with_list_filter_flags():
  157. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  158. ('repo::archive',)
  159. )
  160. flexmock(module).should_receive('write_patterns_file').and_return(None)
  161. flexmock(module).should_receive('make_list_filter_flags').and_return('AME+-')
  162. insert_execute_command_mock(
  163. ('borg', 'recreate', '--list', '--filter', 'AME+-', 'repo::archive')
  164. )
  165. module.recreate_archive(
  166. repository='repo',
  167. archive='archive',
  168. config={},
  169. local_borg_version='1.2.3',
  170. recreate_arguments=flexmock(path=None, list=True),
  171. global_arguments=flexmock(dry_run=False, log_json=False),
  172. local_path='borg',
  173. patterns=None,
  174. )
  175. def test_recreate_with_patterns_from_flag():
  176. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  177. ('repo::archive',)
  178. )
  179. mock_patterns_file = flexmock(name='patterns_file')
  180. flexmock(module).should_receive('write_patterns_file').and_return(mock_patterns_file)
  181. flexmock(module).should_receive('make_list_filter_flags').and_return(None)
  182. insert_execute_command_mock(
  183. ('borg', 'recreate', '--patterns-from', 'patterns_file', 'repo::archive')
  184. )
  185. module.recreate_archive(
  186. repository='repo',
  187. archive='archive',
  188. config={},
  189. local_borg_version='1.2.3',
  190. recreate_arguments=flexmock(path=None, list=None),
  191. global_arguments=flexmock(dry_run=False, log_json=False),
  192. local_path='borg',
  193. patterns=['pattern1', 'pattern2'],
  194. )
  195. def test_recreate_with_exclude_flags():
  196. flexmock(module.borgmatic.borg.flags).should_receive(
  197. 'make_repository_archive_flags'
  198. ).and_return(('repo::archive',))
  199. flexmock(module).should_receive('write_patterns_file').and_return(None)
  200. flexmock(module).should_receive('make_list_filter_flags').and_return(None)
  201. # Mock the make_exclude_flags to return a sample exclude flag
  202. flexmock(module).should_receive('make_exclude_flags').and_return(('--exclude', 'pattern'))
  203. insert_execute_command_mock(('borg', 'recreate', '--exclude', 'pattern', 'repo::archive'))
  204. module.recreate_archive(
  205. repository='repo',
  206. archive='archive',
  207. config={'exclude_patterns': ['pattern']},
  208. local_borg_version='1.2.3',
  209. recreate_arguments=flexmock(path=None, list=None),
  210. global_arguments=flexmock(dry_run=False, log_json=False),
  211. local_path='borg',
  212. patterns=None,
  213. )