test_create.py 65 KB

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