2
0

test_create.py 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.borg import create as module
  5. from borgmatic.borg.pattern import Pattern, Pattern_source, Pattern_style, Pattern_type
  6. from ..test_verbosity import insert_logging_mock
  7. def test_write_patterns_file_writes_pattern_lines():
  8. temporary_file = flexmock(name='filename', flush=lambda: None)
  9. temporary_file.should_receive('write').with_args('R /foo\n+ sh:/foo/bar')
  10. flexmock(module.tempfile).should_receive('NamedTemporaryFile').and_return(temporary_file)
  11. module.write_patterns_file(
  12. [Pattern('/foo'), Pattern('/foo/bar', Pattern_type.INCLUDE, Pattern_style.SHELL)],
  13. borgmatic_runtime_directory='/run/user/0',
  14. )
  15. def test_write_patterns_file_with_empty_exclude_patterns_does_not_raise():
  16. module.write_patterns_file([], borgmatic_runtime_directory='/run/user/0')
  17. def test_write_patterns_file_appends_to_existing():
  18. patterns_file = flexmock(name='filename', flush=lambda: None)
  19. patterns_file.should_receive('write').with_args('\n')
  20. patterns_file.should_receive('write').with_args('R /foo\n+ /foo/bar')
  21. flexmock(module.tempfile).should_receive('NamedTemporaryFile').never()
  22. module.write_patterns_file(
  23. [Pattern('/foo'), Pattern('/foo/bar', Pattern_type.INCLUDE)],
  24. borgmatic_runtime_directory='/run/user/0',
  25. patterns_file=patterns_file,
  26. )
  27. def test_make_exclude_flags_includes_exclude_caches_when_true_in_config():
  28. exclude_flags = module.make_exclude_flags(config={'exclude_caches': True})
  29. assert exclude_flags == ('--exclude-caches',)
  30. def test_make_exclude_flags_does_not_include_exclude_caches_when_false_in_config():
  31. exclude_flags = module.make_exclude_flags(config={'exclude_caches': False})
  32. assert exclude_flags == ()
  33. def test_make_exclude_flags_includes_exclude_if_present_when_in_config():
  34. exclude_flags = module.make_exclude_flags(
  35. config={'exclude_if_present': ['exclude_me', 'also_me']}
  36. )
  37. assert exclude_flags == (
  38. '--exclude-if-present',
  39. 'exclude_me',
  40. '--exclude-if-present',
  41. 'also_me',
  42. )
  43. def test_make_exclude_flags_includes_keep_exclude_tags_when_true_in_config():
  44. exclude_flags = module.make_exclude_flags(config={'keep_exclude_tags': True})
  45. assert exclude_flags == ('--keep-exclude-tags',)
  46. def test_make_exclude_flags_does_not_include_keep_exclude_tags_when_false_in_config():
  47. exclude_flags = module.make_exclude_flags(config={'keep_exclude_tags': False})
  48. assert exclude_flags == ()
  49. def test_make_exclude_flags_includes_exclude_nodump_when_true_in_config():
  50. exclude_flags = module.make_exclude_flags(config={'exclude_nodump': True})
  51. assert exclude_flags == ('--exclude-nodump',)
  52. def test_make_exclude_flags_does_not_include_exclude_nodump_when_false_in_config():
  53. exclude_flags = module.make_exclude_flags(config={'exclude_nodump': False})
  54. assert exclude_flags == ()
  55. def test_make_exclude_flags_is_empty_when_config_has_no_excludes():
  56. exclude_flags = module.make_exclude_flags(config={})
  57. assert exclude_flags == ()
  58. def test_make_list_filter_flags_with_debug_and_feature_available_includes_plus_and_minus():
  59. flexmock(module.logger).should_receive('isEnabledFor').and_return(True)
  60. flexmock(module.feature).should_receive('available').and_return(True)
  61. assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=False) == 'AME+-'
  62. def test_make_list_filter_flags_with_info_and_feature_available_omits_plus_and_minus():
  63. flexmock(module.logger).should_receive('isEnabledFor').and_return(False)
  64. flexmock(module.feature).should_receive('available').and_return(True)
  65. assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=False) == 'AME'
  66. def test_make_list_filter_flags_with_debug_and_feature_available_and_dry_run_includes_plus_and_minus():
  67. flexmock(module.logger).should_receive('isEnabledFor').and_return(True)
  68. flexmock(module.feature).should_receive('available').and_return(True)
  69. assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=True) == 'AME+-'
  70. def test_make_list_filter_flags_with_info_and_feature_available_and_dry_run_includes_plus_and_minus():
  71. flexmock(module.logger).should_receive('isEnabledFor').and_return(False)
  72. flexmock(module.feature).should_receive('available').and_return(True)
  73. assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=True) == 'AME+-'
  74. def test_make_list_filter_flags_with_debug_and_feature_not_available_includes_x():
  75. flexmock(module.logger).should_receive('isEnabledFor').and_return(True)
  76. flexmock(module.feature).should_receive('available').and_return(False)
  77. assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=False) == 'AMEx-'
  78. def test_make_list_filter_flags_with_info_and_feature_not_available_omits_x():
  79. flexmock(module.logger).should_receive('isEnabledFor').and_return(False)
  80. flexmock(module.feature).should_receive('available').and_return(False)
  81. assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=False) == 'AME-'
  82. @pytest.mark.parametrize(
  83. 'character_device,block_device,fifo,expected_result',
  84. (
  85. (False, False, False, False),
  86. (True, False, False, True),
  87. (False, True, False, True),
  88. (True, True, False, True),
  89. (False, False, True, True),
  90. (False, True, True, True),
  91. (True, False, True, True),
  92. ),
  93. )
  94. def test_special_file_looks_at_file_type(character_device, block_device, fifo, expected_result):
  95. flexmock(module.os).should_receive('stat').and_return(flexmock(st_mode=flexmock()))
  96. flexmock(module.stat).should_receive('S_ISCHR').and_return(character_device)
  97. flexmock(module.stat).should_receive('S_ISBLK').and_return(block_device)
  98. flexmock(module.stat).should_receive('S_ISFIFO').and_return(fifo)
  99. assert module.special_file('/dev/special') == expected_result
  100. def test_special_file_treats_broken_symlink_as_non_special():
  101. flexmock(module.os).should_receive('stat').and_raise(FileNotFoundError)
  102. assert module.special_file('/broken/symlink') is False
  103. def test_special_file_prepends_relative_path_with_working_directory():
  104. flexmock(module.os).should_receive('stat').with_args('/working/dir/relative').and_return(
  105. flexmock(st_mode=flexmock())
  106. )
  107. flexmock(module.stat).should_receive('S_ISCHR').and_return(False)
  108. flexmock(module.stat).should_receive('S_ISBLK').and_return(False)
  109. flexmock(module.stat).should_receive('S_ISFIFO').and_return(False)
  110. assert module.special_file('relative', '/working/dir') is False
  111. def test_any_parent_directories_treats_parents_as_match():
  112. module.any_parent_directories('/foo/bar.txt', ('/foo', '/etc'))
  113. def test_any_parent_directories_treats_grandparents_as_match():
  114. module.any_parent_directories('/foo/bar/baz.txt', ('/foo', '/etc'))
  115. def test_any_parent_directories_treats_unrelated_paths_as_non_match():
  116. module.any_parent_directories('/foo/bar.txt', ('/usr', '/etc'))
  117. def test_collect_special_file_paths_parses_special_files_from_borg_dry_run_file_list():
  118. flexmock(module.flags).should_receive('omit_flag').replace_with(
  119. lambda arguments, flag: arguments
  120. )
  121. flexmock(module.flags).should_receive('omit_flag_and_value').replace_with(
  122. lambda arguments, flag: arguments
  123. )
  124. flexmock(module.environment).should_receive('make_environment').and_return(None)
  125. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  126. 'Processing files ...\n- /foo\n+ /bar\n- /baz'
  127. )
  128. flexmock(module).should_receive('special_file').and_return(True)
  129. flexmock(module.os.path).should_receive('exists').and_return(False)
  130. flexmock(module).should_receive('any_parent_directories').never()
  131. assert module.collect_special_file_paths(
  132. dry_run=False,
  133. create_command=('borg', 'create'),
  134. config={},
  135. local_path=None,
  136. working_directory=None,
  137. borgmatic_runtime_directory='/run/borgmatic',
  138. ) == ('/foo', '/bar', '/baz')
  139. def test_collect_special_file_paths_skips_borgmatic_runtime_directory():
  140. flexmock(module.flags).should_receive('omit_flag').replace_with(
  141. lambda arguments, flag: arguments
  142. )
  143. flexmock(module.flags).should_receive('omit_flag_and_value').replace_with(
  144. lambda arguments, flag: arguments
  145. )
  146. flexmock(module.environment).should_receive('make_environment').and_return(None)
  147. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  148. '+ /foo\n- /run/borgmatic/bar\n- /baz'
  149. )
  150. flexmock(module).should_receive('special_file').and_return(True)
  151. flexmock(module.os.path).should_receive('exists').and_return(True)
  152. flexmock(module).should_receive('any_parent_directories').with_args(
  153. '/foo', ('/run/borgmatic',)
  154. ).and_return(False)
  155. flexmock(module).should_receive('any_parent_directories').with_args(
  156. '/run/borgmatic/bar', ('/run/borgmatic',)
  157. ).and_return(True)
  158. flexmock(module).should_receive('any_parent_directories').with_args(
  159. '/baz', ('/run/borgmatic',)
  160. ).and_return(False)
  161. assert module.collect_special_file_paths(
  162. dry_run=False,
  163. create_command=('borg', 'create'),
  164. config={},
  165. local_path=None,
  166. working_directory=None,
  167. borgmatic_runtime_directory='/run/borgmatic',
  168. ) == ('/foo', '/baz')
  169. def test_collect_special_file_paths_with_borgmatic_runtime_directory_missing_from_paths_output_errors():
  170. flexmock(module.flags).should_receive('omit_flag').replace_with(
  171. lambda arguments, flag: arguments
  172. )
  173. flexmock(module.flags).should_receive('omit_flag_and_value').replace_with(
  174. lambda arguments, flag: arguments
  175. )
  176. flexmock(module.environment).should_receive('make_environment').and_return(None)
  177. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  178. '+ /foo\n- /bar\n- /baz'
  179. )
  180. flexmock(module).should_receive('special_file').and_return(True)
  181. flexmock(module.os.path).should_receive('exists').and_return(True)
  182. flexmock(module).should_receive('any_parent_directories').and_return(False)
  183. with pytest.raises(ValueError):
  184. module.collect_special_file_paths(
  185. dry_run=False,
  186. create_command=('borg', 'create'),
  187. config={},
  188. local_path=None,
  189. working_directory=None,
  190. borgmatic_runtime_directory='/run/borgmatic',
  191. )
  192. def test_collect_special_file_paths_with_dry_run_and_borgmatic_runtime_directory_missing_from_paths_output_does_not_raise():
  193. flexmock(module.flags).should_receive('omit_flag').replace_with(
  194. lambda arguments, flag: arguments
  195. )
  196. flexmock(module.flags).should_receive('omit_flag_and_value').replace_with(
  197. lambda arguments, flag: arguments
  198. )
  199. flexmock(module.environment).should_receive('make_environment').and_return(None)
  200. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  201. '+ /foo\n- /bar\n- /baz'
  202. )
  203. flexmock(module).should_receive('special_file').and_return(True)
  204. flexmock(module.os.path).should_receive('exists').and_return(True)
  205. flexmock(module).should_receive('any_parent_directories').and_return(False)
  206. assert module.collect_special_file_paths(
  207. dry_run=True,
  208. create_command=('borg', 'create'),
  209. config={},
  210. local_path=None,
  211. working_directory=None,
  212. borgmatic_runtime_directory='/run/borgmatic',
  213. ) == ('/foo', '/bar', '/baz')
  214. def test_collect_special_file_paths_excludes_non_special_files():
  215. flexmock(module.flags).should_receive('omit_flag').replace_with(
  216. lambda arguments, flag: arguments
  217. )
  218. flexmock(module.flags).should_receive('omit_flag_and_value').replace_with(
  219. lambda arguments, flag: arguments
  220. )
  221. flexmock(module.environment).should_receive('make_environment').and_return(None)
  222. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  223. '+ /foo\n+ /bar\n+ /baz'
  224. )
  225. flexmock(module).should_receive('special_file').and_return(True).and_return(False).and_return(
  226. True
  227. )
  228. flexmock(module.os.path).should_receive('exists').and_return(False)
  229. flexmock(module).should_receive('any_parent_directories').never()
  230. assert module.collect_special_file_paths(
  231. dry_run=False,
  232. create_command=('borg', 'create'),
  233. config={},
  234. local_path=None,
  235. working_directory=None,
  236. borgmatic_runtime_directory='/run/borgmatic',
  237. ) == ('/foo', '/baz')
  238. DEFAULT_ARCHIVE_NAME = '{hostname}-{now:%Y-%m-%dT%H:%M:%S.%f}' # noqa: FS003
  239. REPO_ARCHIVE = (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  240. def test_make_base_create_produces_borg_command():
  241. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  242. flexmock(module).should_receive('write_patterns_file').and_return(None)
  243. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  244. flexmock(module.feature).should_receive('available').and_return(True)
  245. flexmock(module).should_receive('make_exclude_flags').and_return(())
  246. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  247. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  248. )
  249. (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
  250. dry_run=False,
  251. repository_path='repo',
  252. config={
  253. 'source_directories': ['foo', 'bar'],
  254. 'repositories': ['repo'],
  255. },
  256. patterns=[Pattern('foo'), Pattern('bar')],
  257. local_borg_version='1.2.3',
  258. global_arguments=flexmock(log_json=False),
  259. borgmatic_runtime_directory='/run/borgmatic',
  260. )
  261. assert create_flags == ('borg', 'create')
  262. assert create_positional_arguments == REPO_ARCHIVE
  263. assert not pattern_file
  264. def test_make_base_create_command_includes_patterns_file_in_borg_command():
  265. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  266. mock_pattern_file = flexmock(name='/tmp/patterns')
  267. flexmock(module).should_receive('write_patterns_file').and_return(mock_pattern_file).and_return(
  268. None
  269. )
  270. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  271. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  272. '{hostname}'
  273. )
  274. flexmock(module.feature).should_receive('available').and_return(True)
  275. pattern_flags = ('--patterns-from', mock_pattern_file.name)
  276. flexmock(module).should_receive('make_exclude_flags').and_return(())
  277. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  278. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  279. )
  280. (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
  281. dry_run=False,
  282. repository_path='repo',
  283. config={
  284. 'source_directories': ['foo', 'bar'],
  285. 'repositories': ['repo'],
  286. 'patterns': ['pattern'],
  287. },
  288. patterns=[Pattern('foo'), Pattern('bar')],
  289. local_borg_version='1.2.3',
  290. global_arguments=flexmock(log_json=False),
  291. borgmatic_runtime_directory='/run/borgmatic',
  292. )
  293. assert create_flags == ('borg', 'create') + pattern_flags
  294. assert create_positional_arguments == (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  295. assert pattern_file == mock_pattern_file
  296. def test_make_base_create_command_with_store_config_false_omits_config_files():
  297. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  298. flexmock(module).should_receive('write_patterns_file').and_return(None)
  299. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  300. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  301. '{hostname}'
  302. )
  303. flexmock(module.feature).should_receive('available').and_return(True)
  304. flexmock(module).should_receive('make_exclude_flags').and_return(())
  305. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  306. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  307. )
  308. (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
  309. dry_run=False,
  310. repository_path='repo',
  311. config={
  312. 'source_directories': ['foo', 'bar'],
  313. 'repositories': ['repo'],
  314. 'store_config_files': False,
  315. },
  316. patterns=[Pattern('foo'), Pattern('bar')],
  317. local_borg_version='1.2.3',
  318. global_arguments=flexmock(log_json=False),
  319. borgmatic_runtime_directory='/run/borgmatic',
  320. )
  321. assert create_flags == ('borg', 'create')
  322. assert create_positional_arguments == REPO_ARCHIVE
  323. assert not pattern_file
  324. @pytest.mark.parametrize(
  325. 'option_name,option_value,feature_available,option_flags',
  326. (
  327. ('checkpoint_interval', 600, True, ('--checkpoint-interval', '600')),
  328. ('checkpoint_volume', 1024, True, ('--checkpoint-volume', '1024')),
  329. ('chunker_params', '1,2,3,4', True, ('--chunker-params', '1,2,3,4')),
  330. ('compression', 'rle', True, ('--compression', 'rle')),
  331. ('one_file_system', True, True, ('--one-file-system',)),
  332. ('upload_rate_limit', 100, True, ('--upload-ratelimit', '100')),
  333. ('upload_rate_limit', 100, False, ('--remote-ratelimit', '100')),
  334. ('upload_buffer_size', 160, True, ('--upload-buffer', '160')),
  335. ('numeric_ids', True, True, ('--numeric-ids',)),
  336. ('numeric_ids', True, False, ('--numeric-owner',)),
  337. ('read_special', True, True, ('--read-special',)),
  338. ('ctime', True, True, ()),
  339. ('ctime', False, True, ('--noctime',)),
  340. ('birthtime', True, True, ()),
  341. ('birthtime', False, True, ('--nobirthtime',)),
  342. ('atime', True, True, ('--atime',)),
  343. ('atime', True, False, ()),
  344. ('atime', False, True, ()),
  345. ('atime', False, False, ('--noatime',)),
  346. ('flags', True, True, ()),
  347. ('flags', True, False, ()),
  348. ('flags', False, True, ('--noflags',)),
  349. ('flags', False, False, ('--nobsdflags',)),
  350. ('files_cache', 'ctime,size', True, ('--files-cache', 'ctime,size')),
  351. ('umask', 740, True, ('--umask', '740')),
  352. ('lock_wait', 5, True, ('--lock-wait', '5')),
  353. ),
  354. )
  355. def test_make_base_create_command_includes_configuration_option_as_command_flag(
  356. option_name, option_value, feature_available, option_flags
  357. ):
  358. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  359. flexmock(module).should_receive('write_patterns_file').and_return(None)
  360. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  361. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  362. '{hostname}'
  363. )
  364. flexmock(module.feature).should_receive('available').and_return(feature_available)
  365. flexmock(module).should_receive('make_exclude_flags').and_return(())
  366. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  367. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  368. )
  369. (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
  370. dry_run=False,
  371. repository_path='repo',
  372. config={
  373. 'source_directories': ['foo', 'bar'],
  374. 'repositories': ['repo'],
  375. option_name: option_value,
  376. },
  377. patterns=[Pattern('foo'), Pattern('bar')],
  378. local_borg_version='1.2.3',
  379. global_arguments=flexmock(log_json=False),
  380. borgmatic_runtime_directory='/run/borgmatic',
  381. )
  382. assert create_flags == ('borg', 'create') + option_flags
  383. assert create_positional_arguments == REPO_ARCHIVE
  384. assert not pattern_file
  385. def test_make_base_create_command_includes_dry_run_in_borg_command():
  386. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  387. flexmock(module).should_receive('write_patterns_file').and_return(None)
  388. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  389. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  390. '{hostname}'
  391. )
  392. flexmock(module.feature).should_receive('available').and_return(True)
  393. flexmock(module).should_receive('make_exclude_flags').and_return(())
  394. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  395. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  396. )
  397. (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
  398. dry_run=True,
  399. repository_path='repo',
  400. config={
  401. 'source_directories': ['foo', 'bar'],
  402. 'repositories': ['repo'],
  403. 'exclude_patterns': ['exclude'],
  404. },
  405. patterns=[Pattern('foo'), Pattern('bar')],
  406. local_borg_version='1.2.3',
  407. global_arguments=flexmock(log_json=False),
  408. borgmatic_runtime_directory='/run/borgmatic',
  409. )
  410. assert create_flags == ('borg', 'create', '--dry-run')
  411. assert create_positional_arguments == REPO_ARCHIVE
  412. assert not pattern_file
  413. def test_make_base_create_command_includes_local_path_in_borg_command():
  414. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  415. flexmock(module).should_receive('write_patterns_file').and_return(None)
  416. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  417. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  418. '{hostname}'
  419. )
  420. flexmock(module.feature).should_receive('available').and_return(True)
  421. flexmock(module).should_receive('make_exclude_flags').and_return(())
  422. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  423. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  424. )
  425. (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
  426. dry_run=False,
  427. repository_path='repo',
  428. config={
  429. 'source_directories': ['foo', 'bar'],
  430. 'repositories': ['repo'],
  431. },
  432. patterns=[Pattern('foo'), Pattern('bar')],
  433. local_borg_version='1.2.3',
  434. global_arguments=flexmock(log_json=False),
  435. borgmatic_runtime_directory='/run/borgmatic',
  436. local_path='borg1',
  437. )
  438. assert create_flags == ('borg1', 'create')
  439. assert create_positional_arguments == REPO_ARCHIVE
  440. assert not pattern_file
  441. def test_make_base_create_command_includes_remote_path_in_borg_command():
  442. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  443. flexmock(module).should_receive('write_patterns_file').and_return(None)
  444. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  445. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  446. '{hostname}'
  447. )
  448. flexmock(module.feature).should_receive('available').and_return(True)
  449. flexmock(module).should_receive('make_exclude_flags').and_return(())
  450. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  451. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  452. )
  453. (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
  454. dry_run=False,
  455. repository_path='repo',
  456. config={
  457. 'source_directories': ['foo', 'bar'],
  458. 'repositories': ['repo'],
  459. },
  460. patterns=[Pattern('foo'), Pattern('bar')],
  461. local_borg_version='1.2.3',
  462. global_arguments=flexmock(log_json=False),
  463. borgmatic_runtime_directory='/run/borgmatic',
  464. remote_path='borg1',
  465. )
  466. assert create_flags == ('borg', 'create', '--remote-path', 'borg1')
  467. assert create_positional_arguments == REPO_ARCHIVE
  468. assert not pattern_file
  469. def test_make_base_create_command_includes_log_json_in_borg_command():
  470. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  471. flexmock(module).should_receive('write_patterns_file').and_return(None)
  472. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  473. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  474. '{hostname}'
  475. )
  476. flexmock(module.feature).should_receive('available').and_return(True)
  477. flexmock(module).should_receive('make_exclude_flags').and_return(())
  478. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  479. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  480. )
  481. (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
  482. dry_run=False,
  483. repository_path='repo',
  484. config={
  485. 'source_directories': ['foo', 'bar'],
  486. 'repositories': ['repo'],
  487. },
  488. patterns=[Pattern('foo'), Pattern('bar')],
  489. local_borg_version='1.2.3',
  490. global_arguments=flexmock(log_json=True),
  491. borgmatic_runtime_directory='/run/borgmatic',
  492. )
  493. assert create_flags == ('borg', 'create', '--log-json')
  494. assert create_positional_arguments == REPO_ARCHIVE
  495. assert not pattern_file
  496. def test_make_base_create_command_includes_list_flags_in_borg_command():
  497. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  498. flexmock(module).should_receive('write_patterns_file').and_return(None)
  499. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  500. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  501. '{hostname}'
  502. )
  503. flexmock(module.feature).should_receive('available').and_return(True)
  504. flexmock(module).should_receive('make_exclude_flags').and_return(())
  505. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  506. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  507. )
  508. (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
  509. dry_run=False,
  510. repository_path='repo',
  511. config={
  512. 'source_directories': ['foo', 'bar'],
  513. 'repositories': ['repo'],
  514. },
  515. patterns=[Pattern('foo'), Pattern('bar')],
  516. local_borg_version='1.2.3',
  517. global_arguments=flexmock(log_json=False),
  518. borgmatic_runtime_directory='/run/borgmatic',
  519. list_files=True,
  520. )
  521. assert create_flags == ('borg', 'create', '--list', '--filter', 'FOO')
  522. assert create_positional_arguments == REPO_ARCHIVE
  523. assert not pattern_file
  524. def test_make_base_create_command_with_stream_processes_ignores_read_special_false_and_excludes_special_files():
  525. patterns = [Pattern('foo'), Pattern('bar')]
  526. patterns_file = flexmock(name='patterns')
  527. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  528. flexmock(module).should_receive('write_patterns_file').with_args(
  529. patterns, '/run/borgmatic'
  530. ).and_return(patterns_file)
  531. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  532. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  533. '{hostname}'
  534. )
  535. flexmock(module.feature).should_receive('available').and_return(True)
  536. flexmock(module).should_receive('make_exclude_flags').and_return(())
  537. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  538. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  539. )
  540. flexmock(module.logger).should_receive('warning').twice()
  541. flexmock(module.environment).should_receive('make_environment')
  542. flexmock(module).should_receive('collect_special_file_paths').and_return(('/dev/null',)).once()
  543. flexmock(module).should_receive('write_patterns_file').with_args(
  544. (
  545. Pattern(
  546. '/dev/null',
  547. Pattern_type.NO_RECURSE,
  548. Pattern_style.FNMATCH,
  549. source=Pattern_source.INTERNAL,
  550. ),
  551. ),
  552. '/run/borgmatic',
  553. patterns_file=patterns_file,
  554. ).and_return(patterns_file).once()
  555. flexmock(module).should_receive('make_exclude_flags').and_return(())
  556. (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
  557. dry_run=False,
  558. repository_path='repo',
  559. config={
  560. 'source_directories': ['foo', 'bar'],
  561. 'repositories': ['repo'],
  562. 'read_special': False,
  563. },
  564. patterns=patterns,
  565. local_borg_version='1.2.3',
  566. global_arguments=flexmock(log_json=False),
  567. borgmatic_runtime_directory='/run/borgmatic',
  568. stream_processes=flexmock(),
  569. )
  570. assert create_flags == ('borg', 'create', '--patterns-from', 'patterns', '--read-special')
  571. assert create_positional_arguments == REPO_ARCHIVE
  572. assert pattern_file
  573. def test_make_base_create_command_without_patterns_and_with_stream_processes_ignores_read_special_false_and_excludes_special_files():
  574. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  575. flexmock(module).should_receive('write_patterns_file').with_args(
  576. [], '/run/borgmatic'
  577. ).and_return(None)
  578. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  579. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  580. '{hostname}'
  581. )
  582. flexmock(module.feature).should_receive('available').and_return(True)
  583. flexmock(module).should_receive('make_exclude_flags').and_return(())
  584. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  585. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  586. )
  587. flexmock(module.logger).should_receive('warning').twice()
  588. flexmock(module.environment).should_receive('make_environment')
  589. flexmock(module).should_receive('collect_special_file_paths').and_return(('/dev/null',)).once()
  590. flexmock(module).should_receive('write_patterns_file').with_args(
  591. (
  592. Pattern(
  593. '/dev/null',
  594. Pattern_type.NO_RECURSE,
  595. Pattern_style.FNMATCH,
  596. source=Pattern_source.INTERNAL,
  597. ),
  598. ),
  599. '/run/borgmatic',
  600. patterns_file=None,
  601. ).and_return(flexmock(name='patterns')).once()
  602. flexmock(module).should_receive('make_exclude_flags').and_return(())
  603. (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
  604. dry_run=False,
  605. repository_path='repo',
  606. config={
  607. 'source_directories': [],
  608. 'repositories': ['repo'],
  609. 'read_special': False,
  610. },
  611. patterns=[],
  612. local_borg_version='1.2.3',
  613. global_arguments=flexmock(log_json=False),
  614. borgmatic_runtime_directory='/run/borgmatic',
  615. stream_processes=flexmock(),
  616. )
  617. assert create_flags == ('borg', 'create', '--read-special', '--patterns-from', 'patterns')
  618. assert create_positional_arguments == REPO_ARCHIVE
  619. assert pattern_file
  620. def test_make_base_create_command_with_stream_processes_and_read_special_true_skips_special_files_excludes():
  621. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  622. flexmock(module).should_receive('write_patterns_file').and_return(None)
  623. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  624. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  625. '{hostname}'
  626. )
  627. flexmock(module.feature).should_receive('available').and_return(True)
  628. flexmock(module).should_receive('make_exclude_flags').and_return(())
  629. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  630. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  631. )
  632. flexmock(module.logger).should_receive('warning').never()
  633. flexmock(module).should_receive('collect_special_file_paths').never()
  634. (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
  635. dry_run=False,
  636. repository_path='repo',
  637. config={
  638. 'source_directories': ['foo', 'bar'],
  639. 'repositories': ['repo'],
  640. 'read_special': True,
  641. },
  642. patterns=[Pattern('foo'), Pattern('bar')],
  643. local_borg_version='1.2.3',
  644. global_arguments=flexmock(log_json=False),
  645. borgmatic_runtime_directory='/run/borgmatic',
  646. stream_processes=flexmock(),
  647. )
  648. assert create_flags == ('borg', 'create', '--read-special')
  649. assert create_positional_arguments == REPO_ARCHIVE
  650. assert not pattern_file
  651. def test_make_base_create_command_includes_archive_name_format_in_borg_command():
  652. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  653. flexmock(module).should_receive('write_patterns_file').and_return(None)
  654. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  655. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  656. '{hostname}'
  657. )
  658. flexmock(module.feature).should_receive('available').and_return(True)
  659. flexmock(module).should_receive('make_exclude_flags').and_return(())
  660. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  661. ('repo::ARCHIVE_NAME',)
  662. )
  663. (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
  664. dry_run=False,
  665. repository_path='repo',
  666. config={
  667. 'source_directories': ['foo', 'bar'],
  668. 'repositories': ['repo'],
  669. 'archive_name_format': 'ARCHIVE_NAME',
  670. },
  671. patterns=[Pattern('foo'), Pattern('bar')],
  672. local_borg_version='1.2.3',
  673. global_arguments=flexmock(log_json=False),
  674. borgmatic_runtime_directory='/run/borgmatic',
  675. )
  676. assert create_flags == ('borg', 'create')
  677. assert create_positional_arguments == ('repo::ARCHIVE_NAME',)
  678. assert not pattern_file
  679. def test_make_base_create_command_includes_default_archive_name_format_in_borg_command():
  680. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  681. flexmock(module).should_receive('write_patterns_file').and_return(None)
  682. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  683. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  684. '{hostname}'
  685. )
  686. flexmock(module.feature).should_receive('available').and_return(True)
  687. flexmock(module).should_receive('make_exclude_flags').and_return(())
  688. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  689. ('repo::{hostname}',)
  690. )
  691. (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
  692. dry_run=False,
  693. repository_path='repo',
  694. config={
  695. 'source_directories': ['foo', 'bar'],
  696. 'repositories': ['repo'],
  697. },
  698. patterns=[Pattern('foo'), Pattern('bar')],
  699. local_borg_version='1.2.3',
  700. global_arguments=flexmock(log_json=False),
  701. borgmatic_runtime_directory='/run/borgmatic',
  702. )
  703. assert create_flags == ('borg', 'create')
  704. assert create_positional_arguments == ('repo::{hostname}',)
  705. assert not pattern_file
  706. def test_make_base_create_command_includes_archive_name_format_with_placeholders_in_borg_command():
  707. repository_archive_pattern = 'repo::Documents_{hostname}-{now}' # noqa: FS003
  708. flexmock(module).should_receive('write_patterns_file').and_return(None)
  709. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  710. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  711. '{hostname}'
  712. )
  713. flexmock(module.feature).should_receive('available').and_return(True)
  714. flexmock(module).should_receive('make_exclude_flags').and_return(())
  715. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  716. (repository_archive_pattern,)
  717. )
  718. (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
  719. dry_run=False,
  720. repository_path='repo',
  721. config={
  722. 'source_directories': ['foo', 'bar'],
  723. 'repositories': ['repo'],
  724. 'archive_name_format': 'Documents_{hostname}-{now}', # noqa: FS003
  725. },
  726. patterns=[Pattern('foo'), Pattern('bar')],
  727. local_borg_version='1.2.3',
  728. global_arguments=flexmock(log_json=False),
  729. borgmatic_runtime_directory='/run/borgmatic',
  730. )
  731. assert create_flags == ('borg', 'create')
  732. assert create_positional_arguments == (repository_archive_pattern,)
  733. assert not pattern_file
  734. def test_make_base_create_command_includes_repository_and_archive_name_format_with_placeholders_in_borg_command():
  735. repository_archive_pattern = '{fqdn}::Documents_{hostname}-{now}' # noqa: FS003
  736. flexmock(module).should_receive('write_patterns_file').and_return(None)
  737. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  738. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  739. '{hostname}'
  740. )
  741. flexmock(module.feature).should_receive('available').and_return(True)
  742. flexmock(module).should_receive('make_exclude_flags').and_return(())
  743. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  744. (repository_archive_pattern,)
  745. )
  746. (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
  747. dry_run=False,
  748. repository_path='{fqdn}', # noqa: FS003
  749. config={
  750. 'source_directories': ['foo', 'bar'],
  751. 'repositories': ['{fqdn}'], # noqa: FS003
  752. 'archive_name_format': 'Documents_{hostname}-{now}', # noqa: FS003
  753. },
  754. patterns=[Pattern('foo'), Pattern('bar')],
  755. local_borg_version='1.2.3',
  756. global_arguments=flexmock(log_json=False),
  757. borgmatic_runtime_directory='/run/borgmatic',
  758. )
  759. assert create_flags == ('borg', 'create')
  760. assert create_positional_arguments == (repository_archive_pattern,)
  761. assert not pattern_file
  762. def test_make_base_create_command_includes_extra_borg_options_in_borg_command():
  763. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  764. flexmock(module).should_receive('write_patterns_file').and_return(None)
  765. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  766. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  767. '{hostname}'
  768. )
  769. flexmock(module.feature).should_receive('available').and_return(True)
  770. flexmock(module).should_receive('make_exclude_flags').and_return(())
  771. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  772. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  773. )
  774. (create_flags, create_positional_arguments, pattern_file) = module.make_base_create_command(
  775. dry_run=False,
  776. repository_path='repo',
  777. config={
  778. 'source_directories': ['foo', 'bar'],
  779. 'repositories': ['repo'],
  780. 'extra_borg_options': {'create': '--extra --options'},
  781. },
  782. patterns=[Pattern('foo'), Pattern('bar')],
  783. local_borg_version='1.2.3',
  784. global_arguments=flexmock(log_json=False),
  785. borgmatic_runtime_directory='/run/borgmatic',
  786. )
  787. assert create_flags == ('borg', 'create', '--extra', '--options')
  788. assert create_positional_arguments == REPO_ARCHIVE
  789. assert not pattern_file
  790. def test_make_base_create_command_with_non_existent_directory_and_source_directories_must_exist_raises():
  791. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  792. flexmock(module).should_receive('check_all_root_patterns_exist').and_raise(ValueError)
  793. with pytest.raises(ValueError):
  794. module.make_base_create_command(
  795. dry_run=False,
  796. repository_path='repo',
  797. config={
  798. 'source_directories': ['foo', 'bar'],
  799. 'repositories': ['repo'],
  800. 'source_directories_must_exist': True,
  801. },
  802. patterns=[Pattern('foo'), Pattern('bar')],
  803. local_borg_version='1.2.3',
  804. global_arguments=flexmock(log_json=False),
  805. borgmatic_runtime_directory='/run/borgmatic',
  806. )
  807. def test_create_archive_calls_borg_with_parameters():
  808. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  809. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  810. flexmock(module).should_receive('make_base_create_command').and_return(
  811. (('borg', 'create'), REPO_ARCHIVE, flexmock())
  812. )
  813. flexmock(module.environment).should_receive('make_environment')
  814. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  815. flexmock(module).should_receive('execute_command').with_args(
  816. ('borg', 'create') + REPO_ARCHIVE,
  817. output_log_level=logging.INFO,
  818. output_file=None,
  819. borg_local_path='borg',
  820. borg_exit_codes=None,
  821. working_directory=None,
  822. environment=None,
  823. )
  824. module.create_archive(
  825. dry_run=False,
  826. repository_path='repo',
  827. config={
  828. 'source_directories': ['foo', 'bar'],
  829. 'repositories': ['repo'],
  830. 'exclude_patterns': None,
  831. },
  832. patterns=[Pattern('foo'), Pattern('bar')],
  833. local_borg_version='1.2.3',
  834. global_arguments=flexmock(log_json=False),
  835. borgmatic_runtime_directory='/borgmatic/run',
  836. )
  837. def test_create_archive_calls_borg_with_environment():
  838. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  839. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  840. flexmock(module).should_receive('make_base_create_command').and_return(
  841. (('borg', 'create'), REPO_ARCHIVE, flexmock())
  842. )
  843. environment = {'BORG_THINGY': 'YUP'}
  844. flexmock(module.environment).should_receive('make_environment').and_return(environment)
  845. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  846. flexmock(module).should_receive('execute_command').with_args(
  847. ('borg', 'create') + REPO_ARCHIVE,
  848. output_log_level=logging.INFO,
  849. output_file=None,
  850. borg_local_path='borg',
  851. borg_exit_codes=None,
  852. working_directory=None,
  853. environment=environment,
  854. )
  855. module.create_archive(
  856. dry_run=False,
  857. repository_path='repo',
  858. config={
  859. 'source_directories': ['foo', 'bar'],
  860. 'repositories': ['repo'],
  861. 'exclude_patterns': None,
  862. },
  863. patterns=[Pattern('foo'), Pattern('bar')],
  864. local_borg_version='1.2.3',
  865. global_arguments=flexmock(log_json=False),
  866. borgmatic_runtime_directory='/borgmatic/run',
  867. )
  868. def test_create_archive_with_log_info_calls_borg_with_info_parameter():
  869. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  870. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  871. flexmock(module).should_receive('make_base_create_command').and_return(
  872. (('borg', 'create'), REPO_ARCHIVE, flexmock())
  873. )
  874. flexmock(module.environment).should_receive('make_environment')
  875. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  876. flexmock(module).should_receive('execute_command').with_args(
  877. ('borg', 'create', '--info') + REPO_ARCHIVE,
  878. output_log_level=logging.INFO,
  879. output_file=None,
  880. borg_local_path='borg',
  881. borg_exit_codes=None,
  882. working_directory=None,
  883. environment=None,
  884. )
  885. insert_logging_mock(logging.INFO)
  886. module.create_archive(
  887. dry_run=False,
  888. repository_path='repo',
  889. config={
  890. 'source_directories': ['foo', 'bar'],
  891. 'repositories': ['repo'],
  892. 'exclude_patterns': None,
  893. },
  894. patterns=[Pattern('foo'), Pattern('bar')],
  895. local_borg_version='1.2.3',
  896. global_arguments=flexmock(log_json=False),
  897. borgmatic_runtime_directory='/borgmatic/run',
  898. )
  899. def test_create_archive_with_log_info_and_json_suppresses_most_borg_output():
  900. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  901. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  902. flexmock(module).should_receive('make_base_create_command').and_return(
  903. (('borg', 'create'), REPO_ARCHIVE, flexmock())
  904. )
  905. flexmock(module.environment).should_receive('make_environment')
  906. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  907. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  908. ('borg', 'create', '--json') + REPO_ARCHIVE,
  909. working_directory=None,
  910. environment=None,
  911. borg_local_path='borg',
  912. borg_exit_codes=None,
  913. )
  914. insert_logging_mock(logging.INFO)
  915. module.create_archive(
  916. dry_run=False,
  917. repository_path='repo',
  918. config={
  919. 'source_directories': ['foo', 'bar'],
  920. 'repositories': ['repo'],
  921. 'exclude_patterns': None,
  922. },
  923. patterns=[Pattern('foo'), Pattern('bar')],
  924. local_borg_version='1.2.3',
  925. global_arguments=flexmock(log_json=False),
  926. borgmatic_runtime_directory='/borgmatic/run',
  927. json=True,
  928. )
  929. def test_create_archive_with_log_debug_calls_borg_with_debug_parameter():
  930. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  931. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  932. flexmock(module).should_receive('make_base_create_command').and_return(
  933. (('borg', 'create'), REPO_ARCHIVE, flexmock())
  934. )
  935. flexmock(module.environment).should_receive('make_environment')
  936. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  937. flexmock(module).should_receive('execute_command').with_args(
  938. ('borg', 'create', '--debug', '--show-rc') + REPO_ARCHIVE,
  939. output_log_level=logging.INFO,
  940. output_file=None,
  941. borg_local_path='borg',
  942. borg_exit_codes=None,
  943. working_directory=None,
  944. environment=None,
  945. )
  946. insert_logging_mock(logging.DEBUG)
  947. module.create_archive(
  948. dry_run=False,
  949. repository_path='repo',
  950. config={
  951. 'source_directories': ['foo', 'bar'],
  952. 'repositories': ['repo'],
  953. 'exclude_patterns': None,
  954. },
  955. patterns=[Pattern('foo'), Pattern('bar')],
  956. local_borg_version='1.2.3',
  957. global_arguments=flexmock(log_json=False),
  958. borgmatic_runtime_directory='/borgmatic/run',
  959. )
  960. def test_create_archive_with_log_debug_and_json_suppresses_most_borg_output():
  961. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  962. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  963. flexmock(module).should_receive('make_base_create_command').and_return(
  964. (('borg', 'create'), REPO_ARCHIVE, flexmock())
  965. )
  966. flexmock(module.environment).should_receive('make_environment')
  967. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  968. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  969. ('borg', 'create', '--json') + REPO_ARCHIVE,
  970. working_directory=None,
  971. environment=None,
  972. borg_local_path='borg',
  973. borg_exit_codes=None,
  974. )
  975. insert_logging_mock(logging.DEBUG)
  976. module.create_archive(
  977. dry_run=False,
  978. repository_path='repo',
  979. config={
  980. 'source_directories': ['foo', 'bar'],
  981. 'repositories': ['repo'],
  982. 'exclude_patterns': None,
  983. },
  984. patterns=[Pattern('foo'), Pattern('bar')],
  985. local_borg_version='1.2.3',
  986. global_arguments=flexmock(log_json=False),
  987. borgmatic_runtime_directory='/borgmatic/run',
  988. json=True,
  989. )
  990. def test_create_archive_with_stats_and_dry_run_calls_borg_without_stats():
  991. # --dry-run and --stats are mutually exclusive, see:
  992. # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
  993. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  994. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  995. flexmock(module).should_receive('make_base_create_command').and_return(
  996. (('borg', 'create', '--dry-run'), REPO_ARCHIVE, flexmock())
  997. )
  998. flexmock(module.environment).should_receive('make_environment')
  999. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1000. flexmock(module).should_receive('execute_command').with_args(
  1001. ('borg', 'create', '--dry-run', '--info') + REPO_ARCHIVE,
  1002. output_log_level=logging.INFO,
  1003. output_file=None,
  1004. borg_local_path='borg',
  1005. borg_exit_codes=None,
  1006. working_directory=None,
  1007. environment=None,
  1008. )
  1009. insert_logging_mock(logging.INFO)
  1010. module.create_archive(
  1011. dry_run=True,
  1012. repository_path='repo',
  1013. config={
  1014. 'source_directories': ['foo', 'bar'],
  1015. 'repositories': ['repo'],
  1016. 'exclude_patterns': None,
  1017. },
  1018. patterns=[Pattern('foo'), Pattern('bar')],
  1019. local_borg_version='1.2.3',
  1020. global_arguments=flexmock(log_json=False),
  1021. borgmatic_runtime_directory='/borgmatic/run',
  1022. stats=True,
  1023. )
  1024. def test_create_archive_with_working_directory_calls_borg_with_working_directory():
  1025. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1026. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1027. flexmock(module).should_receive('make_base_create_command').and_return(
  1028. (('borg', 'create'), REPO_ARCHIVE, flexmock())
  1029. )
  1030. flexmock(module.environment).should_receive('make_environment')
  1031. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1032. '/working/dir'
  1033. )
  1034. flexmock(module).should_receive('execute_command').with_args(
  1035. ('borg', 'create') + REPO_ARCHIVE,
  1036. output_log_level=logging.INFO,
  1037. output_file=None,
  1038. borg_local_path='borg',
  1039. borg_exit_codes=None,
  1040. working_directory='/working/dir',
  1041. environment=None,
  1042. )
  1043. module.create_archive(
  1044. dry_run=False,
  1045. repository_path='repo',
  1046. config={
  1047. 'source_directories': ['foo', 'bar'],
  1048. 'repositories': ['repo'],
  1049. 'working_directory': '/working/dir',
  1050. 'exclude_patterns': None,
  1051. },
  1052. patterns=[Pattern('foo'), Pattern('bar')],
  1053. local_borg_version='1.2.3',
  1054. global_arguments=flexmock(log_json=False),
  1055. borgmatic_runtime_directory='/borgmatic/run',
  1056. )
  1057. def test_create_archive_with_exit_codes_calls_borg_using_them():
  1058. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1059. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1060. flexmock(module).should_receive('make_base_create_command').and_return(
  1061. (('borg', 'create'), REPO_ARCHIVE, flexmock())
  1062. )
  1063. flexmock(module.environment).should_receive('make_environment')
  1064. borg_exit_codes = flexmock()
  1065. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1066. flexmock(module).should_receive('execute_command').with_args(
  1067. ('borg', 'create') + REPO_ARCHIVE,
  1068. output_log_level=logging.INFO,
  1069. output_file=None,
  1070. borg_local_path='borg',
  1071. borg_exit_codes=borg_exit_codes,
  1072. working_directory=None,
  1073. environment=None,
  1074. )
  1075. module.create_archive(
  1076. dry_run=False,
  1077. repository_path='repo',
  1078. config={
  1079. 'source_directories': ['foo', 'bar'],
  1080. 'repositories': ['repo'],
  1081. 'exclude_patterns': None,
  1082. 'borg_exit_codes': borg_exit_codes,
  1083. },
  1084. patterns=[Pattern('foo'), Pattern('bar')],
  1085. local_borg_version='1.2.3',
  1086. global_arguments=flexmock(log_json=False),
  1087. borgmatic_runtime_directory='/borgmatic/run',
  1088. )
  1089. def test_create_archive_with_stats_calls_borg_with_stats_parameter_and_answer_output_log_level():
  1090. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1091. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1092. flexmock(module).should_receive('make_base_create_command').and_return(
  1093. (('borg', 'create'), REPO_ARCHIVE, flexmock())
  1094. )
  1095. flexmock(module.environment).should_receive('make_environment')
  1096. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1097. flexmock(module).should_receive('execute_command').with_args(
  1098. ('borg', 'create', '--stats') + REPO_ARCHIVE,
  1099. output_log_level=module.borgmatic.logger.ANSWER,
  1100. output_file=None,
  1101. borg_local_path='borg',
  1102. borg_exit_codes=None,
  1103. working_directory=None,
  1104. environment=None,
  1105. )
  1106. module.create_archive(
  1107. dry_run=False,
  1108. repository_path='repo',
  1109. config={
  1110. 'source_directories': ['foo', 'bar'],
  1111. 'repositories': ['repo'],
  1112. 'exclude_patterns': None,
  1113. },
  1114. patterns=[Pattern('foo'), Pattern('bar')],
  1115. local_borg_version='1.2.3',
  1116. global_arguments=flexmock(log_json=False),
  1117. borgmatic_runtime_directory='/borgmatic/run',
  1118. stats=True,
  1119. )
  1120. def test_create_archive_with_files_calls_borg_with_answer_output_log_level():
  1121. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1122. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1123. flexmock(module).should_receive('make_base_create_command').and_return(
  1124. (
  1125. ('borg', 'create', '--list', '--filter', 'FOO'),
  1126. REPO_ARCHIVE,
  1127. flexmock(),
  1128. )
  1129. )
  1130. flexmock(module.environment).should_receive('make_environment')
  1131. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1132. flexmock(module).should_receive('execute_command').with_args(
  1133. ('borg', 'create', '--list', '--filter', 'FOO') + REPO_ARCHIVE,
  1134. output_log_level=module.borgmatic.logger.ANSWER,
  1135. output_file=None,
  1136. borg_local_path='borg',
  1137. borg_exit_codes=None,
  1138. working_directory=None,
  1139. environment=None,
  1140. )
  1141. module.create_archive(
  1142. dry_run=False,
  1143. repository_path='repo',
  1144. config={
  1145. 'source_directories': ['foo', 'bar'],
  1146. 'repositories': ['repo'],
  1147. 'exclude_patterns': None,
  1148. },
  1149. patterns=[Pattern('foo'), Pattern('bar')],
  1150. local_borg_version='1.2.3',
  1151. global_arguments=flexmock(log_json=False),
  1152. borgmatic_runtime_directory='/borgmatic/run',
  1153. list_files=True,
  1154. )
  1155. def test_create_archive_with_progress_and_log_info_calls_borg_with_progress_parameter_and_no_list():
  1156. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1157. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1158. flexmock(module).should_receive('make_base_create_command').and_return(
  1159. (('borg', 'create'), REPO_ARCHIVE, flexmock())
  1160. )
  1161. flexmock(module.environment).should_receive('make_environment')
  1162. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1163. flexmock(module).should_receive('execute_command').with_args(
  1164. ('borg', 'create', '--info', '--progress') + REPO_ARCHIVE,
  1165. output_log_level=logging.INFO,
  1166. output_file=module.DO_NOT_CAPTURE,
  1167. borg_local_path='borg',
  1168. borg_exit_codes=None,
  1169. working_directory=None,
  1170. environment=None,
  1171. )
  1172. insert_logging_mock(logging.INFO)
  1173. module.create_archive(
  1174. dry_run=False,
  1175. repository_path='repo',
  1176. config={
  1177. 'source_directories': ['foo', 'bar'],
  1178. 'repositories': ['repo'],
  1179. 'exclude_patterns': None,
  1180. },
  1181. patterns=[Pattern('foo'), Pattern('bar')],
  1182. local_borg_version='1.2.3',
  1183. global_arguments=flexmock(log_json=False),
  1184. borgmatic_runtime_directory='/borgmatic/run',
  1185. progress=True,
  1186. )
  1187. def test_create_archive_with_progress_calls_borg_with_progress_parameter():
  1188. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1189. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1190. flexmock(module).should_receive('make_base_create_command').and_return(
  1191. (('borg', 'create'), REPO_ARCHIVE, flexmock())
  1192. )
  1193. flexmock(module.environment).should_receive('make_environment')
  1194. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1195. flexmock(module).should_receive('execute_command').with_args(
  1196. ('borg', 'create', '--progress') + REPO_ARCHIVE,
  1197. output_log_level=logging.INFO,
  1198. output_file=module.DO_NOT_CAPTURE,
  1199. borg_local_path='borg',
  1200. borg_exit_codes=None,
  1201. working_directory=None,
  1202. environment=None,
  1203. )
  1204. module.create_archive(
  1205. dry_run=False,
  1206. repository_path='repo',
  1207. config={
  1208. 'source_directories': ['foo', 'bar'],
  1209. 'repositories': ['repo'],
  1210. 'exclude_patterns': None,
  1211. },
  1212. patterns=[Pattern('foo'), Pattern('bar')],
  1213. local_borg_version='1.2.3',
  1214. global_arguments=flexmock(log_json=False),
  1215. borgmatic_runtime_directory='/borgmatic/run',
  1216. progress=True,
  1217. )
  1218. def test_create_archive_with_progress_and_stream_processes_calls_borg_with_progress_parameter():
  1219. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1220. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1221. processes = flexmock()
  1222. flexmock(module).should_receive('make_base_create_command').and_return(
  1223. (
  1224. ('borg', 'create', '--read-special'),
  1225. REPO_ARCHIVE,
  1226. flexmock(),
  1227. )
  1228. )
  1229. flexmock(module.environment).should_receive('make_environment')
  1230. create_command = (
  1231. 'borg',
  1232. 'create',
  1233. '--read-special',
  1234. '--progress',
  1235. ) + REPO_ARCHIVE
  1236. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1237. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1238. create_command + ('--dry-run', '--list'),
  1239. processes=processes,
  1240. output_log_level=logging.INFO,
  1241. output_file=module.DO_NOT_CAPTURE,
  1242. borg_local_path='borg',
  1243. borg_exit_codes=None,
  1244. working_directory=None,
  1245. environment=None,
  1246. )
  1247. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1248. create_command,
  1249. processes=processes,
  1250. output_log_level=logging.INFO,
  1251. output_file=module.DO_NOT_CAPTURE,
  1252. borg_local_path='borg',
  1253. borg_exit_codes=None,
  1254. working_directory=None,
  1255. environment=None,
  1256. )
  1257. module.create_archive(
  1258. dry_run=False,
  1259. repository_path='repo',
  1260. config={
  1261. 'source_directories': ['foo', 'bar'],
  1262. 'repositories': ['repo'],
  1263. 'exclude_patterns': None,
  1264. },
  1265. patterns=[Pattern('foo'), Pattern('bar')],
  1266. local_borg_version='1.2.3',
  1267. global_arguments=flexmock(log_json=False),
  1268. borgmatic_runtime_directory='/borgmatic/run',
  1269. progress=True,
  1270. stream_processes=processes,
  1271. )
  1272. def test_create_archive_with_json_calls_borg_with_json_flag():
  1273. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1274. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1275. flexmock(module).should_receive('make_base_create_command').and_return(
  1276. (('borg', 'create'), REPO_ARCHIVE, flexmock())
  1277. )
  1278. flexmock(module.environment).should_receive('make_environment')
  1279. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1280. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  1281. ('borg', 'create', '--json') + REPO_ARCHIVE,
  1282. working_directory=None,
  1283. environment=None,
  1284. borg_local_path='borg',
  1285. borg_exit_codes=None,
  1286. ).and_return('[]')
  1287. json_output = module.create_archive(
  1288. dry_run=False,
  1289. repository_path='repo',
  1290. config={
  1291. 'source_directories': ['foo', 'bar'],
  1292. 'repositories': ['repo'],
  1293. 'exclude_patterns': None,
  1294. },
  1295. patterns=[Pattern('foo'), Pattern('bar')],
  1296. local_borg_version='1.2.3',
  1297. global_arguments=flexmock(log_json=False),
  1298. borgmatic_runtime_directory='/borgmatic/run',
  1299. json=True,
  1300. )
  1301. assert json_output == '[]'
  1302. def test_create_archive_with_stats_and_json_calls_borg_without_stats_flag():
  1303. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1304. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1305. flexmock(module).should_receive('make_base_create_command').and_return(
  1306. (('borg', 'create'), REPO_ARCHIVE, flexmock())
  1307. )
  1308. flexmock(module.environment).should_receive('make_environment')
  1309. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1310. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  1311. ('borg', 'create', '--json') + REPO_ARCHIVE,
  1312. working_directory=None,
  1313. environment=None,
  1314. borg_local_path='borg',
  1315. borg_exit_codes=None,
  1316. ).and_return('[]')
  1317. json_output = module.create_archive(
  1318. dry_run=False,
  1319. repository_path='repo',
  1320. config={
  1321. 'source_directories': ['foo*'],
  1322. 'repositories': ['repo'],
  1323. 'exclude_patterns': None,
  1324. },
  1325. patterns=[Pattern('foo'), Pattern('bar')],
  1326. local_borg_version='1.2.3',
  1327. global_arguments=flexmock(log_json=False),
  1328. borgmatic_runtime_directory='/borgmatic/run',
  1329. json=True,
  1330. stats=True,
  1331. )
  1332. assert json_output == '[]'
  1333. def test_create_archive_calls_borg_with_working_directory():
  1334. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1335. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1336. flexmock(module).should_receive('make_base_create_command').and_return(
  1337. (('borg', 'create'), REPO_ARCHIVE, flexmock())
  1338. )
  1339. flexmock(module.environment).should_receive('make_environment')
  1340. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1341. '/working/dir'
  1342. )
  1343. flexmock(module).should_receive('execute_command').with_args(
  1344. ('borg', 'create') + REPO_ARCHIVE,
  1345. output_log_level=logging.INFO,
  1346. output_file=None,
  1347. borg_local_path='borg',
  1348. borg_exit_codes=None,
  1349. working_directory='/working/dir',
  1350. environment=None,
  1351. )
  1352. module.create_archive(
  1353. dry_run=False,
  1354. repository_path='repo',
  1355. config={
  1356. 'source_directories': ['foo', 'bar'],
  1357. 'repositories': ['repo'],
  1358. 'exclude_patterns': None,
  1359. 'working_directory': '/working/dir',
  1360. },
  1361. patterns=[Pattern('foo'), Pattern('bar')],
  1362. local_borg_version='1.2.3',
  1363. global_arguments=flexmock(log_json=False),
  1364. borgmatic_runtime_directory='/borgmatic/run',
  1365. )
  1366. def test_check_all_root_patterns_exist_with_existent_pattern_path_does_not_raise():
  1367. flexmock(module.os.path).should_receive('exists').and_return(True)
  1368. module.check_all_root_patterns_exist([Pattern('foo')])
  1369. def test_check_all_root_patterns_exist_with_non_root_pattern_skips_existence_check():
  1370. flexmock(module.os.path).should_receive('exists').never()
  1371. module.check_all_root_patterns_exist([Pattern('foo', Pattern_type.INCLUDE)])
  1372. def test_check_all_root_patterns_exist_with_non_existent_pattern_path_raises():
  1373. flexmock(module.os.path).should_receive('exists').and_return(False)
  1374. with pytest.raises(ValueError):
  1375. module.check_all_root_patterns_exist([Pattern('foo')])