2
0

test_create.py 59 KB

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