test_create.py 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879
  1. import logging
  2. import sys
  3. import pytest
  4. from flexmock import flexmock
  5. from borgmatic.borg import create as module
  6. from ..test_verbosity import insert_logging_mock
  7. def test_expand_directory_with_basic_path_passes_it_through():
  8. flexmock(module.os.path).should_receive('expanduser').and_return('foo')
  9. flexmock(module.glob).should_receive('glob').and_return([])
  10. paths = module.expand_directory('foo')
  11. assert paths == ['foo']
  12. def test_expand_directory_with_glob_expands():
  13. flexmock(module.os.path).should_receive('expanduser').and_return('foo*')
  14. flexmock(module.glob).should_receive('glob').and_return(['foo', 'food'])
  15. paths = module.expand_directory('foo*')
  16. assert paths == ['foo', 'food']
  17. def test_expand_directories_flattens_expanded_directories():
  18. flexmock(module).should_receive('expand_directory').with_args('~/foo').and_return(['/root/foo'])
  19. flexmock(module).should_receive('expand_directory').with_args('bar*').and_return(
  20. ['bar', 'barf']
  21. )
  22. paths = module.expand_directories(('~/foo', 'bar*'))
  23. assert paths == ('/root/foo', 'bar', 'barf')
  24. def test_expand_directories_considers_none_as_no_directories():
  25. paths = module.expand_directories(None)
  26. assert paths == ()
  27. def test_expand_home_directories_expands_tildes():
  28. flexmock(module.os.path).should_receive('expanduser').with_args('~/bar').and_return('/foo/bar')
  29. flexmock(module.os.path).should_receive('expanduser').with_args('baz').and_return('baz')
  30. paths = module.expand_home_directories(('~/bar', 'baz'))
  31. assert paths == ('/foo/bar', 'baz')
  32. def test_expand_home_directories_considers_none_as_no_directories():
  33. paths = module.expand_home_directories(None)
  34. assert paths == ()
  35. def test_map_directories_to_devices_gives_device_id_per_path():
  36. flexmock(module.os).should_receive('stat').with_args('/foo').and_return(flexmock(st_dev=55))
  37. flexmock(module.os).should_receive('stat').with_args('/bar').and_return(flexmock(st_dev=66))
  38. device_map = module.map_directories_to_devices(('/foo', '/bar'))
  39. assert device_map == {
  40. '/foo': 55,
  41. '/bar': 66,
  42. }
  43. def test_map_directories_to_devices_with_missing_path_does_not_error():
  44. flexmock(module.os).should_receive('stat').with_args('/foo').and_return(flexmock(st_dev=55))
  45. flexmock(module.os).should_receive('stat').with_args('/bar').and_raise(FileNotFoundError)
  46. device_map = module.map_directories_to_devices(('/foo', '/bar'))
  47. assert device_map == {
  48. '/foo': 55,
  49. '/bar': None,
  50. }
  51. @pytest.mark.parametrize(
  52. 'directories,expected_directories',
  53. (
  54. ({'/': 1, '/root': 1}, ('/',)),
  55. ({'/': 1, '/root/': 1}, ('/',)),
  56. ({'/': 1, '/root': 2}, ('/', '/root')),
  57. ({'/root': 1, '/': 1}, ('/',)),
  58. ({'/root': 1, '/root/foo': 1}, ('/root',)),
  59. ({'/root/': 1, '/root/foo': 1}, ('/root/',)),
  60. ({'/root': 1, '/root/foo/': 1}, ('/root',)),
  61. ({'/root': 1, '/root/foo': 2}, ('/root', '/root/foo')),
  62. ({'/root/foo': 1, '/root': 1}, ('/root',)),
  63. ({'/root': None, '/root/foo': None}, ('/root', '/root/foo')),
  64. ({'/root': 1, '/etc': 1, '/root/foo/bar': 1}, ('/etc', '/root')),
  65. ({'/root': 1, '/root/foo': 1, '/root/foo/bar': 1}, ('/root',)),
  66. ({'/dup': 1, '/dup': 1}, ('/dup',)),
  67. ({'/foo': 1, '/bar': 1}, ('/bar', '/foo')),
  68. ({'/foo': 1, '/bar': 2}, ('/bar', '/foo')),
  69. ),
  70. )
  71. def test_deduplicate_directories_removes_child_paths_on_the_same_filesystem(
  72. directories, expected_directories
  73. ):
  74. assert module.deduplicate_directories(directories) == expected_directories
  75. def test_write_pattern_file_does_not_raise():
  76. temporary_file = flexmock(name='filename', write=lambda mode: None, flush=lambda: None)
  77. flexmock(module.tempfile).should_receive('NamedTemporaryFile').and_return(temporary_file)
  78. module.write_pattern_file(['exclude'])
  79. def test_write_pattern_file_with_empty_exclude_patterns_does_not_raise():
  80. module.write_pattern_file([])
  81. @pytest.mark.parametrize(
  82. 'filename_lists,opened_filenames',
  83. (
  84. ([('foo', 'bar'), ('baz', 'quux')], ('foo', 'bar', 'baz', 'quux')),
  85. ([None, ('foo', 'bar')], ('foo', 'bar')),
  86. ([None, None], ()),
  87. ),
  88. )
  89. def test_ensure_files_readable_opens_filenames(filename_lists, opened_filenames):
  90. for expected_filename in opened_filenames:
  91. flexmock(sys.modules['builtins']).should_receive('open').with_args(
  92. expected_filename
  93. ).and_return(flexmock(close=lambda: None))
  94. module.ensure_files_readable(*filename_lists)
  95. def test_make_pattern_flags_includes_pattern_filename_when_given():
  96. pattern_flags = module.make_pattern_flags(
  97. location_config={'patterns': ['R /', '- /var']}, pattern_filename='/tmp/patterns'
  98. )
  99. assert pattern_flags == ('--patterns-from', '/tmp/patterns')
  100. def test_make_pattern_flags_includes_patterns_from_filenames_when_in_config():
  101. pattern_flags = module.make_pattern_flags(
  102. location_config={'patterns_from': ['patterns', 'other']}
  103. )
  104. assert pattern_flags == ('--patterns-from', 'patterns', '--patterns-from', 'other')
  105. def test_make_pattern_flags_includes_both_filenames_when_patterns_given_and_patterns_from_in_config():
  106. pattern_flags = module.make_pattern_flags(
  107. location_config={'patterns_from': ['patterns']}, pattern_filename='/tmp/patterns'
  108. )
  109. assert pattern_flags == ('--patterns-from', 'patterns', '--patterns-from', '/tmp/patterns')
  110. def test_make_pattern_flags_considers_none_patterns_from_filenames_as_empty():
  111. pattern_flags = module.make_pattern_flags(location_config={'patterns_from': None})
  112. assert pattern_flags == ()
  113. def test_make_exclude_flags_includes_exclude_patterns_filename_when_given():
  114. exclude_flags = module.make_exclude_flags(
  115. location_config={'exclude_patterns': ['*.pyc', '/var']}, exclude_filename='/tmp/excludes'
  116. )
  117. assert exclude_flags == ('--exclude-from', '/tmp/excludes')
  118. def test_make_exclude_flags_includes_exclude_from_filenames_when_in_config():
  119. exclude_flags = module.make_exclude_flags(
  120. location_config={'exclude_from': ['excludes', 'other']}
  121. )
  122. assert exclude_flags == ('--exclude-from', 'excludes', '--exclude-from', 'other')
  123. def test_make_exclude_flags_includes_both_filenames_when_patterns_given_and_exclude_from_in_config():
  124. exclude_flags = module.make_exclude_flags(
  125. location_config={'exclude_from': ['excludes']}, exclude_filename='/tmp/excludes'
  126. )
  127. assert exclude_flags == ('--exclude-from', 'excludes', '--exclude-from', '/tmp/excludes')
  128. def test_make_exclude_flags_considers_none_exclude_from_filenames_as_empty():
  129. exclude_flags = module.make_exclude_flags(location_config={'exclude_from': None})
  130. assert exclude_flags == ()
  131. def test_make_exclude_flags_includes_exclude_caches_when_true_in_config():
  132. exclude_flags = module.make_exclude_flags(location_config={'exclude_caches': True})
  133. assert exclude_flags == ('--exclude-caches',)
  134. def test_make_exclude_flags_does_not_include_exclude_caches_when_false_in_config():
  135. exclude_flags = module.make_exclude_flags(location_config={'exclude_caches': False})
  136. assert exclude_flags == ()
  137. def test_make_exclude_flags_includes_exclude_if_present_when_in_config():
  138. exclude_flags = module.make_exclude_flags(
  139. location_config={'exclude_if_present': ['exclude_me', 'also_me']}
  140. )
  141. assert exclude_flags == (
  142. '--exclude-if-present',
  143. 'exclude_me',
  144. '--exclude-if-present',
  145. 'also_me',
  146. )
  147. def test_make_exclude_flags_includes_keep_exclude_tags_when_true_in_config():
  148. exclude_flags = module.make_exclude_flags(location_config={'keep_exclude_tags': True})
  149. assert exclude_flags == ('--keep-exclude-tags',)
  150. def test_make_exclude_flags_does_not_include_keep_exclude_tags_when_false_in_config():
  151. exclude_flags = module.make_exclude_flags(location_config={'keep_exclude_tags': False})
  152. assert exclude_flags == ()
  153. def test_make_exclude_flags_includes_exclude_nodump_when_true_in_config():
  154. exclude_flags = module.make_exclude_flags(location_config={'exclude_nodump': True})
  155. assert exclude_flags == ('--exclude-nodump',)
  156. def test_make_exclude_flags_does_not_include_exclude_nodump_when_false_in_config():
  157. exclude_flags = module.make_exclude_flags(location_config={'exclude_nodump': False})
  158. assert exclude_flags == ()
  159. def test_make_exclude_flags_is_empty_when_config_has_no_excludes():
  160. exclude_flags = module.make_exclude_flags(location_config={})
  161. assert exclude_flags == ()
  162. def test_borgmatic_source_directories_set_when_directory_exists():
  163. flexmock(module.os.path).should_receive('exists').and_return(True)
  164. flexmock(module.os.path).should_receive('expanduser')
  165. assert module.borgmatic_source_directories('/tmp') == ['/tmp']
  166. def test_borgmatic_source_directories_empty_when_directory_does_not_exist():
  167. flexmock(module.os.path).should_receive('exists').and_return(False)
  168. flexmock(module.os.path).should_receive('expanduser')
  169. assert module.borgmatic_source_directories('/tmp') == []
  170. def test_borgmatic_source_directories_defaults_when_directory_not_given():
  171. flexmock(module.os.path).should_receive('exists').and_return(True)
  172. flexmock(module.os.path).should_receive('expanduser')
  173. assert module.borgmatic_source_directories(None) == [
  174. module.state.DEFAULT_BORGMATIC_SOURCE_DIRECTORY
  175. ]
  176. DEFAULT_ARCHIVE_NAME = '{hostname}-{now:%Y-%m-%dT%H:%M:%S.%f}'
  177. ARCHIVE_WITH_PATHS = ('repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'bar')
  178. def test_create_archive_calls_borg_with_parameters():
  179. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  180. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  181. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  182. flexmock(module).should_receive('expand_directories').and_return(())
  183. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  184. flexmock(module).should_receive('expand_home_directories').and_return(())
  185. flexmock(module).should_receive('write_pattern_file').and_return(None)
  186. flexmock(module.feature).should_receive('available').and_return(True)
  187. flexmock(module).should_receive('ensure_files_readable')
  188. flexmock(module).should_receive('make_pattern_flags').and_return(())
  189. flexmock(module).should_receive('make_exclude_flags').and_return(())
  190. flexmock(module.environment).should_receive('make_environment')
  191. flexmock(module).should_receive('execute_command').with_args(
  192. ('borg', 'create') + ARCHIVE_WITH_PATHS,
  193. output_log_level=logging.INFO,
  194. output_file=None,
  195. borg_local_path='borg',
  196. working_directory=None,
  197. extra_environment=None,
  198. )
  199. module.create_archive(
  200. dry_run=False,
  201. repository='repo',
  202. location_config={
  203. 'source_directories': ['foo', 'bar'],
  204. 'repositories': ['repo'],
  205. 'exclude_patterns': None,
  206. },
  207. storage_config={},
  208. local_borg_version='1.2.3',
  209. )
  210. def test_create_archive_calls_borg_with_environment():
  211. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  212. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  213. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  214. flexmock(module).should_receive('expand_directories').and_return(())
  215. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  216. flexmock(module).should_receive('expand_home_directories').and_return(())
  217. flexmock(module).should_receive('write_pattern_file').and_return(None)
  218. flexmock(module.feature).should_receive('available').and_return(True)
  219. flexmock(module).should_receive('ensure_files_readable')
  220. flexmock(module).should_receive('make_pattern_flags').and_return(())
  221. flexmock(module).should_receive('make_exclude_flags').and_return(())
  222. environment = {'BORG_THINGY': 'YUP'}
  223. flexmock(module.environment).should_receive('make_environment').and_return(environment)
  224. flexmock(module).should_receive('execute_command').with_args(
  225. ('borg', 'create') + ARCHIVE_WITH_PATHS,
  226. output_log_level=logging.INFO,
  227. output_file=None,
  228. borg_local_path='borg',
  229. working_directory=None,
  230. extra_environment=environment,
  231. )
  232. module.create_archive(
  233. dry_run=False,
  234. repository='repo',
  235. location_config={
  236. 'source_directories': ['foo', 'bar'],
  237. 'repositories': ['repo'],
  238. 'exclude_patterns': None,
  239. },
  240. storage_config={},
  241. local_borg_version='1.2.3',
  242. )
  243. def test_create_archive_with_patterns_calls_borg_with_patterns():
  244. pattern_flags = ('--patterns-from', 'patterns')
  245. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  246. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  247. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  248. flexmock(module).should_receive('expand_directories').and_return(())
  249. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  250. flexmock(module).should_receive('expand_home_directories').and_return(())
  251. flexmock(module).should_receive('write_pattern_file').and_return(
  252. flexmock(name='/tmp/patterns')
  253. ).and_return(None)
  254. flexmock(module.feature).should_receive('available').and_return(True)
  255. flexmock(module).should_receive('ensure_files_readable')
  256. flexmock(module).should_receive('make_pattern_flags').and_return(pattern_flags)
  257. flexmock(module).should_receive('make_exclude_flags').and_return(())
  258. flexmock(module.environment).should_receive('make_environment')
  259. flexmock(module).should_receive('execute_command').with_args(
  260. ('borg', 'create') + pattern_flags + ARCHIVE_WITH_PATHS,
  261. output_log_level=logging.INFO,
  262. output_file=None,
  263. borg_local_path='borg',
  264. working_directory=None,
  265. extra_environment=None,
  266. )
  267. module.create_archive(
  268. dry_run=False,
  269. repository='repo',
  270. location_config={
  271. 'source_directories': ['foo', 'bar'],
  272. 'repositories': ['repo'],
  273. 'patterns': ['pattern'],
  274. },
  275. storage_config={},
  276. local_borg_version='1.2.3',
  277. )
  278. def test_create_archive_with_exclude_patterns_calls_borg_with_excludes():
  279. exclude_flags = ('--exclude-from', 'excludes')
  280. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  281. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  282. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  283. flexmock(module).should_receive('expand_directories').and_return(())
  284. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  285. flexmock(module).should_receive('expand_home_directories').and_return(('exclude',))
  286. flexmock(module).should_receive('write_pattern_file').and_return(None).and_return(
  287. flexmock(name='/tmp/excludes')
  288. )
  289. flexmock(module.feature).should_receive('available').and_return(True)
  290. flexmock(module).should_receive('ensure_files_readable')
  291. flexmock(module).should_receive('make_pattern_flags').and_return(())
  292. flexmock(module).should_receive('make_exclude_flags').and_return(exclude_flags)
  293. flexmock(module.environment).should_receive('make_environment')
  294. flexmock(module).should_receive('execute_command').with_args(
  295. ('borg', 'create') + exclude_flags + ARCHIVE_WITH_PATHS,
  296. output_log_level=logging.INFO,
  297. output_file=None,
  298. borg_local_path='borg',
  299. working_directory=None,
  300. extra_environment=None,
  301. )
  302. module.create_archive(
  303. dry_run=False,
  304. repository='repo',
  305. location_config={
  306. 'source_directories': ['foo', 'bar'],
  307. 'repositories': ['repo'],
  308. 'exclude_patterns': ['exclude'],
  309. },
  310. storage_config={},
  311. local_borg_version='1.2.3',
  312. )
  313. def test_create_archive_with_log_info_calls_borg_with_info_parameter():
  314. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  315. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  316. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  317. flexmock(module).should_receive('expand_directories').and_return(())
  318. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  319. flexmock(module).should_receive('expand_home_directories').and_return(())
  320. flexmock(module).should_receive('write_pattern_file').and_return(None)
  321. flexmock(module.feature).should_receive('available').and_return(True)
  322. flexmock(module).should_receive('ensure_files_readable')
  323. flexmock(module).should_receive('make_pattern_flags').and_return(())
  324. flexmock(module).should_receive('make_exclude_flags').and_return(())
  325. flexmock(module.environment).should_receive('make_environment')
  326. flexmock(module).should_receive('execute_command').with_args(
  327. ('borg', 'create', '--info') + ARCHIVE_WITH_PATHS,
  328. output_log_level=logging.INFO,
  329. output_file=None,
  330. borg_local_path='borg',
  331. working_directory=None,
  332. extra_environment=None,
  333. )
  334. insert_logging_mock(logging.INFO)
  335. module.create_archive(
  336. dry_run=False,
  337. repository='repo',
  338. location_config={
  339. 'source_directories': ['foo', 'bar'],
  340. 'repositories': ['repo'],
  341. 'exclude_patterns': None,
  342. },
  343. storage_config={},
  344. local_borg_version='1.2.3',
  345. )
  346. def test_create_archive_with_log_info_and_json_suppresses_most_borg_output():
  347. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  348. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  349. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  350. flexmock(module).should_receive('expand_directories').and_return(())
  351. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  352. flexmock(module).should_receive('expand_home_directories').and_return(())
  353. flexmock(module).should_receive('write_pattern_file').and_return(None)
  354. flexmock(module.feature).should_receive('available').and_return(True)
  355. flexmock(module).should_receive('ensure_files_readable')
  356. flexmock(module).should_receive('make_pattern_flags').and_return(())
  357. flexmock(module).should_receive('make_exclude_flags').and_return(())
  358. flexmock(module.environment).should_receive('make_environment')
  359. flexmock(module).should_receive('execute_command').with_args(
  360. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  361. output_log_level=None,
  362. output_file=None,
  363. borg_local_path='borg',
  364. working_directory=None,
  365. extra_environment=None,
  366. )
  367. insert_logging_mock(logging.INFO)
  368. module.create_archive(
  369. dry_run=False,
  370. repository='repo',
  371. location_config={
  372. 'source_directories': ['foo', 'bar'],
  373. 'repositories': ['repo'],
  374. 'exclude_patterns': None,
  375. },
  376. storage_config={},
  377. local_borg_version='1.2.3',
  378. json=True,
  379. )
  380. def test_create_archive_with_log_debug_calls_borg_with_debug_parameter():
  381. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  382. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  383. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  384. flexmock(module).should_receive('expand_directories').and_return(())
  385. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  386. flexmock(module).should_receive('expand_home_directories').and_return(())
  387. flexmock(module).should_receive('write_pattern_file').and_return(None)
  388. flexmock(module.feature).should_receive('available').and_return(True)
  389. flexmock(module).should_receive('ensure_files_readable')
  390. flexmock(module).should_receive('make_pattern_flags').and_return(())
  391. flexmock(module).should_receive('make_exclude_flags').and_return(())
  392. flexmock(module.environment).should_receive('make_environment')
  393. flexmock(module).should_receive('execute_command').with_args(
  394. ('borg', 'create', '--debug', '--show-rc') + ARCHIVE_WITH_PATHS,
  395. output_log_level=logging.INFO,
  396. output_file=None,
  397. borg_local_path='borg',
  398. working_directory=None,
  399. extra_environment=None,
  400. )
  401. insert_logging_mock(logging.DEBUG)
  402. module.create_archive(
  403. dry_run=False,
  404. repository='repo',
  405. location_config={
  406. 'source_directories': ['foo', 'bar'],
  407. 'repositories': ['repo'],
  408. 'exclude_patterns': None,
  409. },
  410. storage_config={},
  411. local_borg_version='1.2.3',
  412. )
  413. def test_create_archive_with_log_debug_and_json_suppresses_most_borg_output():
  414. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  415. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  416. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  417. flexmock(module).should_receive('expand_directories').and_return(())
  418. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  419. flexmock(module).should_receive('expand_home_directories').and_return(())
  420. flexmock(module).should_receive('write_pattern_file').and_return(None)
  421. flexmock(module.feature).should_receive('available').and_return(True)
  422. flexmock(module).should_receive('ensure_files_readable')
  423. flexmock(module).should_receive('make_pattern_flags').and_return(())
  424. flexmock(module).should_receive('make_exclude_flags').and_return(())
  425. flexmock(module.environment).should_receive('make_environment')
  426. flexmock(module).should_receive('execute_command').with_args(
  427. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  428. output_log_level=None,
  429. output_file=None,
  430. borg_local_path='borg',
  431. working_directory=None,
  432. extra_environment=None,
  433. )
  434. insert_logging_mock(logging.DEBUG)
  435. module.create_archive(
  436. dry_run=False,
  437. repository='repo',
  438. location_config={
  439. 'source_directories': ['foo', 'bar'],
  440. 'repositories': ['repo'],
  441. 'exclude_patterns': None,
  442. },
  443. storage_config={},
  444. local_borg_version='1.2.3',
  445. json=True,
  446. )
  447. def test_create_archive_with_dry_run_calls_borg_with_dry_run_parameter():
  448. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  449. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  450. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  451. flexmock(module).should_receive('expand_directories').and_return(())
  452. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  453. flexmock(module).should_receive('expand_home_directories').and_return(())
  454. flexmock(module).should_receive('write_pattern_file').and_return(None)
  455. flexmock(module.feature).should_receive('available').and_return(True)
  456. flexmock(module).should_receive('ensure_files_readable')
  457. flexmock(module).should_receive('make_pattern_flags').and_return(())
  458. flexmock(module).should_receive('make_exclude_flags').and_return(())
  459. flexmock(module.environment).should_receive('make_environment')
  460. flexmock(module).should_receive('execute_command').with_args(
  461. ('borg', 'create', '--dry-run') + ARCHIVE_WITH_PATHS,
  462. output_log_level=logging.INFO,
  463. output_file=None,
  464. borg_local_path='borg',
  465. working_directory=None,
  466. extra_environment=None,
  467. )
  468. module.create_archive(
  469. dry_run=True,
  470. repository='repo',
  471. location_config={
  472. 'source_directories': ['foo', 'bar'],
  473. 'repositories': ['repo'],
  474. 'exclude_patterns': None,
  475. },
  476. storage_config={},
  477. local_borg_version='1.2.3',
  478. )
  479. def test_create_archive_with_stats_and_dry_run_calls_borg_without_stats_parameter():
  480. # --dry-run and --stats are mutually exclusive, see:
  481. # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
  482. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  483. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  484. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  485. flexmock(module).should_receive('expand_directories').and_return(())
  486. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  487. flexmock(module).should_receive('expand_home_directories').and_return(())
  488. flexmock(module).should_receive('write_pattern_file').and_return(None)
  489. flexmock(module.feature).should_receive('available').and_return(True)
  490. flexmock(module).should_receive('ensure_files_readable')
  491. flexmock(module).should_receive('make_pattern_flags').and_return(())
  492. flexmock(module).should_receive('make_exclude_flags').and_return(())
  493. flexmock(module.environment).should_receive('make_environment')
  494. flexmock(module).should_receive('execute_command').with_args(
  495. ('borg', 'create', '--info', '--dry-run') + ARCHIVE_WITH_PATHS,
  496. output_log_level=logging.INFO,
  497. output_file=None,
  498. borg_local_path='borg',
  499. working_directory=None,
  500. extra_environment=None,
  501. )
  502. insert_logging_mock(logging.INFO)
  503. module.create_archive(
  504. dry_run=True,
  505. repository='repo',
  506. location_config={
  507. 'source_directories': ['foo', 'bar'],
  508. 'repositories': ['repo'],
  509. 'exclude_patterns': None,
  510. },
  511. storage_config={},
  512. local_borg_version='1.2.3',
  513. stats=True,
  514. )
  515. def test_create_archive_with_checkpoint_interval_calls_borg_with_checkpoint_interval_parameters():
  516. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  517. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  518. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  519. flexmock(module).should_receive('expand_directories').and_return(())
  520. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  521. flexmock(module).should_receive('expand_home_directories').and_return(())
  522. flexmock(module).should_receive('write_pattern_file').and_return(None)
  523. flexmock(module.feature).should_receive('available').and_return(True)
  524. flexmock(module).should_receive('ensure_files_readable')
  525. flexmock(module).should_receive('make_pattern_flags').and_return(())
  526. flexmock(module).should_receive('make_exclude_flags').and_return(())
  527. flexmock(module.environment).should_receive('make_environment')
  528. flexmock(module).should_receive('execute_command').with_args(
  529. ('borg', 'create', '--checkpoint-interval', '600') + ARCHIVE_WITH_PATHS,
  530. output_log_level=logging.INFO,
  531. output_file=None,
  532. borg_local_path='borg',
  533. working_directory=None,
  534. extra_environment=None,
  535. )
  536. module.create_archive(
  537. dry_run=False,
  538. repository='repo',
  539. location_config={
  540. 'source_directories': ['foo', 'bar'],
  541. 'repositories': ['repo'],
  542. 'exclude_patterns': None,
  543. },
  544. storage_config={'checkpoint_interval': 600},
  545. local_borg_version='1.2.3',
  546. )
  547. def test_create_archive_with_chunker_params_calls_borg_with_chunker_params_parameters():
  548. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  549. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  550. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  551. flexmock(module).should_receive('expand_directories').and_return(())
  552. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  553. flexmock(module).should_receive('expand_home_directories').and_return(())
  554. flexmock(module).should_receive('write_pattern_file').and_return(None)
  555. flexmock(module.feature).should_receive('available').and_return(True)
  556. flexmock(module).should_receive('ensure_files_readable')
  557. flexmock(module).should_receive('make_pattern_flags').and_return(())
  558. flexmock(module).should_receive('make_exclude_flags').and_return(())
  559. flexmock(module.environment).should_receive('make_environment')
  560. flexmock(module).should_receive('execute_command').with_args(
  561. ('borg', 'create', '--chunker-params', '1,2,3,4') + ARCHIVE_WITH_PATHS,
  562. output_log_level=logging.INFO,
  563. output_file=None,
  564. borg_local_path='borg',
  565. working_directory=None,
  566. extra_environment=None,
  567. )
  568. module.create_archive(
  569. dry_run=False,
  570. repository='repo',
  571. location_config={
  572. 'source_directories': ['foo', 'bar'],
  573. 'repositories': ['repo'],
  574. 'exclude_patterns': None,
  575. },
  576. storage_config={'chunker_params': '1,2,3,4'},
  577. local_borg_version='1.2.3',
  578. )
  579. def test_create_archive_with_compression_calls_borg_with_compression_parameters():
  580. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  581. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  582. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  583. flexmock(module).should_receive('expand_directories').and_return(())
  584. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  585. flexmock(module).should_receive('expand_home_directories').and_return(())
  586. flexmock(module).should_receive('write_pattern_file').and_return(None)
  587. flexmock(module.feature).should_receive('available').and_return(True)
  588. flexmock(module).should_receive('ensure_files_readable')
  589. flexmock(module).should_receive('make_pattern_flags').and_return(())
  590. flexmock(module).should_receive('make_exclude_flags').and_return(())
  591. flexmock(module.environment).should_receive('make_environment')
  592. flexmock(module).should_receive('execute_command').with_args(
  593. ('borg', 'create', '--compression', 'rle') + ARCHIVE_WITH_PATHS,
  594. output_log_level=logging.INFO,
  595. output_file=None,
  596. borg_local_path='borg',
  597. working_directory=None,
  598. extra_environment=None,
  599. )
  600. module.create_archive(
  601. dry_run=False,
  602. repository='repo',
  603. location_config={
  604. 'source_directories': ['foo', 'bar'],
  605. 'repositories': ['repo'],
  606. 'exclude_patterns': None,
  607. },
  608. storage_config={'compression': 'rle'},
  609. local_borg_version='1.2.3',
  610. )
  611. @pytest.mark.parametrize(
  612. 'feature_available,option_flag', ((True, '--upload-ratelimit'), (False, '--remote-ratelimit')),
  613. )
  614. def test_create_archive_with_remote_rate_limit_calls_borg_with_upload_ratelimit_parameters(
  615. feature_available, option_flag
  616. ):
  617. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  618. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  619. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  620. flexmock(module).should_receive('expand_directories').and_return(())
  621. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  622. flexmock(module).should_receive('expand_home_directories').and_return(())
  623. flexmock(module).should_receive('write_pattern_file').and_return(None)
  624. flexmock(module.feature).should_receive('available').and_return(feature_available)
  625. flexmock(module).should_receive('ensure_files_readable')
  626. flexmock(module).should_receive('make_pattern_flags').and_return(())
  627. flexmock(module).should_receive('make_exclude_flags').and_return(())
  628. flexmock(module.environment).should_receive('make_environment')
  629. flexmock(module).should_receive('execute_command').with_args(
  630. ('borg', 'create', option_flag, '100') + ARCHIVE_WITH_PATHS,
  631. output_log_level=logging.INFO,
  632. output_file=None,
  633. borg_local_path='borg',
  634. working_directory=None,
  635. extra_environment=None,
  636. )
  637. module.create_archive(
  638. dry_run=False,
  639. repository='repo',
  640. location_config={
  641. 'source_directories': ['foo', 'bar'],
  642. 'repositories': ['repo'],
  643. 'exclude_patterns': None,
  644. },
  645. storage_config={'remote_rate_limit': 100},
  646. local_borg_version='1.2.3',
  647. )
  648. def test_create_archive_with_working_directory_calls_borg_with_working_directory():
  649. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  650. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  651. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  652. flexmock(module).should_receive('expand_directories').and_return(())
  653. flexmock(module.os.path).should_receive('expanduser').with_args('/working/dir').and_return(
  654. '/working/dir'
  655. )
  656. flexmock(module).should_receive('expand_home_directories').and_return(())
  657. flexmock(module).should_receive('write_pattern_file').and_return(None)
  658. flexmock(module.feature).should_receive('available').and_return(True)
  659. flexmock(module).should_receive('ensure_files_readable')
  660. flexmock(module).should_receive('make_pattern_flags').and_return(())
  661. flexmock(module).should_receive('make_exclude_flags').and_return(())
  662. flexmock(module.environment).should_receive('make_environment')
  663. flexmock(module).should_receive('execute_command').with_args(
  664. ('borg', 'create') + ARCHIVE_WITH_PATHS,
  665. output_log_level=logging.INFO,
  666. output_file=None,
  667. borg_local_path='borg',
  668. working_directory='/working/dir',
  669. extra_environment=None,
  670. )
  671. module.create_archive(
  672. dry_run=False,
  673. repository='repo',
  674. location_config={
  675. 'source_directories': ['foo', 'bar'],
  676. 'repositories': ['repo'],
  677. 'working_directory': '/working/dir',
  678. 'exclude_patterns': None,
  679. },
  680. storage_config={},
  681. local_borg_version='1.2.3',
  682. )
  683. def test_create_archive_with_one_file_system_calls_borg_with_one_file_system_parameter():
  684. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  685. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  686. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  687. flexmock(module).should_receive('expand_directories').and_return(())
  688. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  689. flexmock(module).should_receive('expand_home_directories').and_return(())
  690. flexmock(module).should_receive('write_pattern_file').and_return(None)
  691. flexmock(module.feature).should_receive('available').and_return(True)
  692. flexmock(module).should_receive('ensure_files_readable')
  693. flexmock(module).should_receive('make_pattern_flags').and_return(())
  694. flexmock(module).should_receive('make_exclude_flags').and_return(())
  695. flexmock(module.environment).should_receive('make_environment')
  696. flexmock(module).should_receive('execute_command').with_args(
  697. ('borg', 'create', '--one-file-system') + ARCHIVE_WITH_PATHS,
  698. output_log_level=logging.INFO,
  699. output_file=None,
  700. borg_local_path='borg',
  701. working_directory=None,
  702. extra_environment=None,
  703. )
  704. module.create_archive(
  705. dry_run=False,
  706. repository='repo',
  707. location_config={
  708. 'source_directories': ['foo', 'bar'],
  709. 'repositories': ['repo'],
  710. 'one_file_system': True,
  711. 'exclude_patterns': None,
  712. },
  713. storage_config={},
  714. local_borg_version='1.2.3',
  715. )
  716. @pytest.mark.parametrize(
  717. 'feature_available,option_flag', ((True, '--numeric-ids'), (False, '--numeric-owner')),
  718. )
  719. def test_create_archive_with_numeric_owner_calls_borg_with_numeric_ids_parameter(
  720. feature_available, option_flag
  721. ):
  722. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  723. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  724. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  725. flexmock(module).should_receive('expand_directories').and_return(())
  726. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  727. flexmock(module).should_receive('expand_home_directories').and_return(())
  728. flexmock(module).should_receive('write_pattern_file').and_return(None)
  729. flexmock(module.feature).should_receive('available').and_return(feature_available)
  730. flexmock(module).should_receive('ensure_files_readable')
  731. flexmock(module).should_receive('make_pattern_flags').and_return(())
  732. flexmock(module).should_receive('make_exclude_flags').and_return(())
  733. flexmock(module.environment).should_receive('make_environment')
  734. flexmock(module).should_receive('execute_command').with_args(
  735. ('borg', 'create', option_flag) + ARCHIVE_WITH_PATHS,
  736. output_log_level=logging.INFO,
  737. output_file=None,
  738. borg_local_path='borg',
  739. working_directory=None,
  740. extra_environment=None,
  741. )
  742. module.create_archive(
  743. dry_run=False,
  744. repository='repo',
  745. location_config={
  746. 'source_directories': ['foo', 'bar'],
  747. 'repositories': ['repo'],
  748. 'numeric_owner': True,
  749. 'exclude_patterns': None,
  750. },
  751. storage_config={},
  752. local_borg_version='1.2.3',
  753. )
  754. def test_create_archive_with_read_special_calls_borg_with_read_special_parameter():
  755. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  756. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  757. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  758. flexmock(module).should_receive('expand_directories').and_return(())
  759. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  760. flexmock(module).should_receive('expand_home_directories').and_return(())
  761. flexmock(module).should_receive('write_pattern_file').and_return(None)
  762. flexmock(module.feature).should_receive('available').and_return(True)
  763. flexmock(module).should_receive('ensure_files_readable')
  764. flexmock(module).should_receive('make_pattern_flags').and_return(())
  765. flexmock(module).should_receive('make_exclude_flags').and_return(())
  766. flexmock(module.environment).should_receive('make_environment')
  767. flexmock(module).should_receive('execute_command').with_args(
  768. ('borg', 'create', '--read-special') + ARCHIVE_WITH_PATHS,
  769. output_log_level=logging.INFO,
  770. output_file=None,
  771. borg_local_path='borg',
  772. working_directory=None,
  773. extra_environment=None,
  774. )
  775. module.create_archive(
  776. dry_run=False,
  777. repository='repo',
  778. location_config={
  779. 'source_directories': ['foo', 'bar'],
  780. 'repositories': ['repo'],
  781. 'read_special': True,
  782. 'exclude_patterns': None,
  783. },
  784. storage_config={},
  785. local_borg_version='1.2.3',
  786. )
  787. @pytest.mark.parametrize(
  788. 'option_name,option_value',
  789. (('ctime', True), ('ctime', False), ('birthtime', True), ('birthtime', False),),
  790. )
  791. def test_create_archive_with_basic_option_calls_borg_with_corresponding_parameter(
  792. option_name, option_value
  793. ):
  794. option_flag = '--no' + option_name.replace('', '') if option_value is False else None
  795. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  796. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  797. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  798. flexmock(module).should_receive('expand_directories').and_return(())
  799. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  800. flexmock(module).should_receive('expand_home_directories').and_return(())
  801. flexmock(module).should_receive('write_pattern_file').and_return(None)
  802. flexmock(module.feature).should_receive('available').and_return(True)
  803. flexmock(module).should_receive('ensure_files_readable')
  804. flexmock(module).should_receive('make_pattern_flags').and_return(())
  805. flexmock(module).should_receive('make_exclude_flags').and_return(())
  806. flexmock(module.environment).should_receive('make_environment')
  807. flexmock(module).should_receive('execute_command').with_args(
  808. ('borg', 'create') + ((option_flag,) if option_flag else ()) + ARCHIVE_WITH_PATHS,
  809. output_log_level=logging.INFO,
  810. output_file=None,
  811. borg_local_path='borg',
  812. working_directory=None,
  813. extra_environment=None,
  814. )
  815. module.create_archive(
  816. dry_run=False,
  817. repository='repo',
  818. location_config={
  819. 'source_directories': ['foo', 'bar'],
  820. 'repositories': ['repo'],
  821. option_name: option_value,
  822. 'exclude_patterns': None,
  823. },
  824. storage_config={},
  825. local_borg_version='1.2.3',
  826. )
  827. @pytest.mark.parametrize(
  828. 'option_value,feature_available,option_flag',
  829. (
  830. (True, True, '--atime'),
  831. (True, False, None),
  832. (False, True, None),
  833. (False, False, '--noatime'),
  834. ),
  835. )
  836. def test_create_archive_with_atime_option_calls_borg_with_corresponding_parameter(
  837. option_value, feature_available, option_flag
  838. ):
  839. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  840. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  841. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  842. flexmock(module).should_receive('expand_directories').and_return(())
  843. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  844. flexmock(module).should_receive('expand_home_directories').and_return(())
  845. flexmock(module).should_receive('write_pattern_file').and_return(None)
  846. flexmock(module.feature).should_receive('available').and_return(feature_available)
  847. flexmock(module).should_receive('ensure_files_readable')
  848. flexmock(module).should_receive('make_pattern_flags').and_return(())
  849. flexmock(module).should_receive('make_exclude_flags').and_return(())
  850. flexmock(module.environment).should_receive('make_environment')
  851. flexmock(module).should_receive('execute_command').with_args(
  852. ('borg', 'create') + ((option_flag,) if option_flag else ()) + ARCHIVE_WITH_PATHS,
  853. output_log_level=logging.INFO,
  854. output_file=None,
  855. borg_local_path='borg',
  856. working_directory=None,
  857. extra_environment=None,
  858. )
  859. module.create_archive(
  860. dry_run=False,
  861. repository='repo',
  862. location_config={
  863. 'source_directories': ['foo', 'bar'],
  864. 'repositories': ['repo'],
  865. 'atime': option_value,
  866. 'exclude_patterns': None,
  867. },
  868. storage_config={},
  869. local_borg_version='1.2.3',
  870. )
  871. @pytest.mark.parametrize(
  872. 'option_value,feature_available,option_flag',
  873. (
  874. (True, True, None),
  875. (True, False, None),
  876. (False, True, '--noflags'),
  877. (False, False, '--nobsdflags'),
  878. ),
  879. )
  880. def test_create_archive_with_bsd_flags_option_calls_borg_with_corresponding_parameter(
  881. option_value, feature_available, option_flag
  882. ):
  883. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  884. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  885. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  886. flexmock(module).should_receive('expand_directories').and_return(())
  887. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  888. flexmock(module).should_receive('expand_home_directories').and_return(())
  889. flexmock(module).should_receive('write_pattern_file').and_return(None)
  890. flexmock(module.feature).should_receive('available').and_return(feature_available)
  891. flexmock(module).should_receive('ensure_files_readable')
  892. flexmock(module).should_receive('make_pattern_flags').and_return(())
  893. flexmock(module).should_receive('make_exclude_flags').and_return(())
  894. flexmock(module.environment).should_receive('make_environment')
  895. flexmock(module).should_receive('execute_command').with_args(
  896. ('borg', 'create') + ((option_flag,) if option_flag else ()) + ARCHIVE_WITH_PATHS,
  897. output_log_level=logging.INFO,
  898. output_file=None,
  899. borg_local_path='borg',
  900. working_directory=None,
  901. extra_environment=None,
  902. )
  903. module.create_archive(
  904. dry_run=False,
  905. repository='repo',
  906. location_config={
  907. 'source_directories': ['foo', 'bar'],
  908. 'repositories': ['repo'],
  909. 'bsd_flags': option_value,
  910. 'exclude_patterns': None,
  911. },
  912. storage_config={},
  913. local_borg_version='1.2.3',
  914. )
  915. def test_create_archive_with_files_cache_calls_borg_with_files_cache_parameters():
  916. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  917. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  918. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  919. flexmock(module).should_receive('expand_directories').and_return(())
  920. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  921. flexmock(module).should_receive('expand_home_directories').and_return(())
  922. flexmock(module).should_receive('write_pattern_file').and_return(None)
  923. flexmock(module.feature).should_receive('available').and_return(True)
  924. flexmock(module).should_receive('ensure_files_readable')
  925. flexmock(module).should_receive('make_pattern_flags').and_return(())
  926. flexmock(module).should_receive('make_exclude_flags').and_return(())
  927. flexmock(module.environment).should_receive('make_environment')
  928. flexmock(module).should_receive('execute_command').with_args(
  929. ('borg', 'create', '--files-cache', 'ctime,size') + ARCHIVE_WITH_PATHS,
  930. output_log_level=logging.INFO,
  931. output_file=None,
  932. borg_local_path='borg',
  933. working_directory=None,
  934. extra_environment=None,
  935. )
  936. module.create_archive(
  937. dry_run=False,
  938. repository='repo',
  939. location_config={
  940. 'source_directories': ['foo', 'bar'],
  941. 'repositories': ['repo'],
  942. 'files_cache': 'ctime,size',
  943. 'exclude_patterns': None,
  944. },
  945. storage_config={},
  946. local_borg_version='1.2.3',
  947. )
  948. def test_create_archive_with_local_path_calls_borg_via_local_path():
  949. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  950. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  951. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  952. flexmock(module).should_receive('expand_directories').and_return(())
  953. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  954. flexmock(module).should_receive('expand_home_directories').and_return(())
  955. flexmock(module).should_receive('write_pattern_file').and_return(None)
  956. flexmock(module.feature).should_receive('available').and_return(True)
  957. flexmock(module).should_receive('ensure_files_readable')
  958. flexmock(module).should_receive('make_pattern_flags').and_return(())
  959. flexmock(module).should_receive('make_exclude_flags').and_return(())
  960. flexmock(module.environment).should_receive('make_environment')
  961. flexmock(module).should_receive('execute_command').with_args(
  962. ('borg1', 'create') + ARCHIVE_WITH_PATHS,
  963. output_log_level=logging.INFO,
  964. output_file=None,
  965. borg_local_path='borg1',
  966. working_directory=None,
  967. extra_environment=None,
  968. )
  969. module.create_archive(
  970. dry_run=False,
  971. repository='repo',
  972. location_config={
  973. 'source_directories': ['foo', 'bar'],
  974. 'repositories': ['repo'],
  975. 'exclude_patterns': None,
  976. },
  977. storage_config={},
  978. local_borg_version='1.2.3',
  979. local_path='borg1',
  980. )
  981. def test_create_archive_with_remote_path_calls_borg_with_remote_path_parameters():
  982. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  983. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  984. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  985. flexmock(module).should_receive('expand_directories').and_return(())
  986. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  987. flexmock(module).should_receive('expand_home_directories').and_return(())
  988. flexmock(module).should_receive('write_pattern_file').and_return(None)
  989. flexmock(module.feature).should_receive('available').and_return(True)
  990. flexmock(module).should_receive('ensure_files_readable')
  991. flexmock(module).should_receive('make_pattern_flags').and_return(())
  992. flexmock(module).should_receive('make_exclude_flags').and_return(())
  993. flexmock(module.environment).should_receive('make_environment')
  994. flexmock(module).should_receive('execute_command').with_args(
  995. ('borg', 'create', '--remote-path', 'borg1') + ARCHIVE_WITH_PATHS,
  996. output_log_level=logging.INFO,
  997. output_file=None,
  998. borg_local_path='borg',
  999. working_directory=None,
  1000. extra_environment=None,
  1001. )
  1002. module.create_archive(
  1003. dry_run=False,
  1004. repository='repo',
  1005. location_config={
  1006. 'source_directories': ['foo', 'bar'],
  1007. 'repositories': ['repo'],
  1008. 'exclude_patterns': None,
  1009. },
  1010. storage_config={},
  1011. local_borg_version='1.2.3',
  1012. remote_path='borg1',
  1013. )
  1014. def test_create_archive_with_umask_calls_borg_with_umask_parameters():
  1015. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1016. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1017. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1018. flexmock(module).should_receive('expand_directories').and_return(())
  1019. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1020. flexmock(module).should_receive('expand_home_directories').and_return(())
  1021. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1022. flexmock(module.feature).should_receive('available').and_return(True)
  1023. flexmock(module).should_receive('ensure_files_readable')
  1024. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1025. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1026. flexmock(module.environment).should_receive('make_environment')
  1027. flexmock(module).should_receive('execute_command').with_args(
  1028. ('borg', 'create', '--umask', '740') + ARCHIVE_WITH_PATHS,
  1029. output_log_level=logging.INFO,
  1030. output_file=None,
  1031. borg_local_path='borg',
  1032. working_directory=None,
  1033. extra_environment=None,
  1034. )
  1035. module.create_archive(
  1036. dry_run=False,
  1037. repository='repo',
  1038. location_config={
  1039. 'source_directories': ['foo', 'bar'],
  1040. 'repositories': ['repo'],
  1041. 'exclude_patterns': None,
  1042. },
  1043. storage_config={'umask': 740},
  1044. local_borg_version='1.2.3',
  1045. )
  1046. def test_create_archive_with_lock_wait_calls_borg_with_lock_wait_parameters():
  1047. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1048. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1049. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1050. flexmock(module).should_receive('expand_directories').and_return(())
  1051. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1052. flexmock(module).should_receive('expand_home_directories').and_return(())
  1053. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1054. flexmock(module.feature).should_receive('available').and_return(True)
  1055. flexmock(module).should_receive('ensure_files_readable')
  1056. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1057. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1058. flexmock(module.environment).should_receive('make_environment')
  1059. flexmock(module).should_receive('execute_command').with_args(
  1060. ('borg', 'create', '--lock-wait', '5') + ARCHIVE_WITH_PATHS,
  1061. output_log_level=logging.INFO,
  1062. output_file=None,
  1063. borg_local_path='borg',
  1064. working_directory=None,
  1065. extra_environment=None,
  1066. )
  1067. module.create_archive(
  1068. dry_run=False,
  1069. repository='repo',
  1070. location_config={
  1071. 'source_directories': ['foo', 'bar'],
  1072. 'repositories': ['repo'],
  1073. 'exclude_patterns': None,
  1074. },
  1075. storage_config={'lock_wait': 5},
  1076. local_borg_version='1.2.3',
  1077. )
  1078. def test_create_archive_with_stats_calls_borg_with_stats_parameter_and_warning_output_log_level():
  1079. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1080. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1081. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1082. flexmock(module).should_receive('expand_directories').and_return(())
  1083. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1084. flexmock(module).should_receive('expand_home_directories').and_return(())
  1085. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1086. flexmock(module.feature).should_receive('available').and_return(True)
  1087. flexmock(module).should_receive('ensure_files_readable')
  1088. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1089. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1090. flexmock(module.environment).should_receive('make_environment')
  1091. flexmock(module).should_receive('execute_command').with_args(
  1092. ('borg', 'create', '--stats') + ARCHIVE_WITH_PATHS,
  1093. output_log_level=logging.WARNING,
  1094. output_file=None,
  1095. borg_local_path='borg',
  1096. working_directory=None,
  1097. extra_environment=None,
  1098. )
  1099. module.create_archive(
  1100. dry_run=False,
  1101. repository='repo',
  1102. location_config={
  1103. 'source_directories': ['foo', 'bar'],
  1104. 'repositories': ['repo'],
  1105. 'exclude_patterns': None,
  1106. },
  1107. storage_config={},
  1108. local_borg_version='1.2.3',
  1109. stats=True,
  1110. )
  1111. def test_create_archive_with_stats_and_log_info_calls_borg_with_stats_parameter_and_info_output_log_level():
  1112. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1113. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1114. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1115. flexmock(module).should_receive('expand_directories').and_return(())
  1116. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1117. flexmock(module).should_receive('expand_home_directories').and_return(())
  1118. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1119. flexmock(module.feature).should_receive('available').and_return(True)
  1120. flexmock(module).should_receive('ensure_files_readable')
  1121. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1122. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1123. flexmock(module.environment).should_receive('make_environment')
  1124. flexmock(module).should_receive('execute_command').with_args(
  1125. ('borg', 'create', '--info', '--stats') + ARCHIVE_WITH_PATHS,
  1126. output_log_level=logging.INFO,
  1127. output_file=None,
  1128. borg_local_path='borg',
  1129. working_directory=None,
  1130. extra_environment=None,
  1131. )
  1132. insert_logging_mock(logging.INFO)
  1133. module.create_archive(
  1134. dry_run=False,
  1135. repository='repo',
  1136. location_config={
  1137. 'source_directories': ['foo', 'bar'],
  1138. 'repositories': ['repo'],
  1139. 'exclude_patterns': None,
  1140. },
  1141. storage_config={},
  1142. local_borg_version='1.2.3',
  1143. stats=True,
  1144. )
  1145. def test_create_archive_with_files_calls_borg_with_list_parameter_and_warning_output_log_level():
  1146. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1147. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1148. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1149. flexmock(module).should_receive('expand_directories').and_return(())
  1150. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1151. flexmock(module).should_receive('expand_home_directories').and_return(())
  1152. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1153. flexmock(module.feature).should_receive('available').and_return(True)
  1154. flexmock(module).should_receive('ensure_files_readable')
  1155. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1156. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1157. flexmock(module.environment).should_receive('make_environment')
  1158. flexmock(module).should_receive('execute_command').with_args(
  1159. ('borg', 'create', '--list', '--filter', 'AME-') + ARCHIVE_WITH_PATHS,
  1160. output_log_level=logging.WARNING,
  1161. output_file=None,
  1162. borg_local_path='borg',
  1163. working_directory=None,
  1164. extra_environment=None,
  1165. )
  1166. module.create_archive(
  1167. dry_run=False,
  1168. repository='repo',
  1169. location_config={
  1170. 'source_directories': ['foo', 'bar'],
  1171. 'repositories': ['repo'],
  1172. 'exclude_patterns': None,
  1173. },
  1174. storage_config={},
  1175. local_borg_version='1.2.3',
  1176. files=True,
  1177. )
  1178. def test_create_archive_with_files_and_log_info_calls_borg_with_list_parameter_and_info_output_log_level():
  1179. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1180. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1181. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1182. flexmock(module).should_receive('expand_directories').and_return(())
  1183. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1184. flexmock(module).should_receive('expand_home_directories').and_return(())
  1185. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1186. flexmock(module.feature).should_receive('available').and_return(True)
  1187. flexmock(module).should_receive('ensure_files_readable')
  1188. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1189. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1190. flexmock(module.environment).should_receive('make_environment')
  1191. flexmock(module).should_receive('execute_command').with_args(
  1192. ('borg', 'create', '--list', '--filter', 'AME-', '--info') + ARCHIVE_WITH_PATHS,
  1193. output_log_level=logging.INFO,
  1194. output_file=None,
  1195. borg_local_path='borg',
  1196. working_directory=None,
  1197. extra_environment=None,
  1198. )
  1199. insert_logging_mock(logging.INFO)
  1200. module.create_archive(
  1201. dry_run=False,
  1202. repository='repo',
  1203. location_config={
  1204. 'source_directories': ['foo', 'bar'],
  1205. 'repositories': ['repo'],
  1206. 'exclude_patterns': None,
  1207. },
  1208. storage_config={},
  1209. local_borg_version='1.2.3',
  1210. files=True,
  1211. )
  1212. def test_create_archive_with_progress_and_log_info_calls_borg_with_progress_parameter_and_no_list():
  1213. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1214. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1215. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1216. flexmock(module).should_receive('expand_directories').and_return(())
  1217. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1218. flexmock(module).should_receive('expand_home_directories').and_return(())
  1219. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1220. flexmock(module.feature).should_receive('available').and_return(True)
  1221. flexmock(module).should_receive('ensure_files_readable')
  1222. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1223. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1224. flexmock(module.environment).should_receive('make_environment')
  1225. flexmock(module).should_receive('execute_command').with_args(
  1226. ('borg', 'create', '--info', '--progress') + ARCHIVE_WITH_PATHS,
  1227. output_log_level=logging.INFO,
  1228. output_file=module.DO_NOT_CAPTURE,
  1229. borg_local_path='borg',
  1230. working_directory=None,
  1231. extra_environment=None,
  1232. )
  1233. insert_logging_mock(logging.INFO)
  1234. module.create_archive(
  1235. dry_run=False,
  1236. repository='repo',
  1237. location_config={
  1238. 'source_directories': ['foo', 'bar'],
  1239. 'repositories': ['repo'],
  1240. 'exclude_patterns': None,
  1241. },
  1242. storage_config={},
  1243. local_borg_version='1.2.3',
  1244. progress=True,
  1245. )
  1246. def test_create_archive_with_progress_calls_borg_with_progress_parameter():
  1247. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1248. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1249. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1250. flexmock(module).should_receive('expand_directories').and_return(())
  1251. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1252. flexmock(module).should_receive('expand_home_directories').and_return(())
  1253. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1254. flexmock(module.feature).should_receive('available').and_return(True)
  1255. flexmock(module).should_receive('ensure_files_readable')
  1256. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1257. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1258. flexmock(module.environment).should_receive('make_environment')
  1259. flexmock(module).should_receive('execute_command').with_args(
  1260. ('borg', 'create', '--progress') + ARCHIVE_WITH_PATHS,
  1261. output_log_level=logging.INFO,
  1262. output_file=module.DO_NOT_CAPTURE,
  1263. borg_local_path='borg',
  1264. working_directory=None,
  1265. extra_environment=None,
  1266. )
  1267. module.create_archive(
  1268. dry_run=False,
  1269. repository='repo',
  1270. location_config={
  1271. 'source_directories': ['foo', 'bar'],
  1272. 'repositories': ['repo'],
  1273. 'exclude_patterns': None,
  1274. },
  1275. storage_config={},
  1276. local_borg_version='1.2.3',
  1277. progress=True,
  1278. )
  1279. def test_create_archive_with_progress_and_stream_processes_calls_borg_with_progress_parameter():
  1280. processes = flexmock()
  1281. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1282. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1283. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1284. flexmock(module).should_receive('expand_directories').and_return(())
  1285. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1286. flexmock(module).should_receive('expand_home_directories').and_return(())
  1287. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1288. flexmock(module.feature).should_receive('available').and_return(True)
  1289. flexmock(module).should_receive('ensure_files_readable')
  1290. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1291. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1292. flexmock(module.environment).should_receive('make_environment')
  1293. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1294. ('borg', 'create', '--one-file-system', '--read-special', '--progress')
  1295. + ARCHIVE_WITH_PATHS,
  1296. processes=processes,
  1297. output_log_level=logging.INFO,
  1298. output_file=module.DO_NOT_CAPTURE,
  1299. borg_local_path='borg',
  1300. working_directory=None,
  1301. extra_environment=None,
  1302. )
  1303. module.create_archive(
  1304. dry_run=False,
  1305. repository='repo',
  1306. location_config={
  1307. 'source_directories': ['foo', 'bar'],
  1308. 'repositories': ['repo'],
  1309. 'exclude_patterns': None,
  1310. },
  1311. storage_config={},
  1312. local_borg_version='1.2.3',
  1313. progress=True,
  1314. stream_processes=processes,
  1315. )
  1316. def test_create_archive_with_json_calls_borg_with_json_parameter():
  1317. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1318. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1319. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1320. flexmock(module).should_receive('expand_directories').and_return(())
  1321. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1322. flexmock(module).should_receive('expand_home_directories').and_return(())
  1323. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1324. flexmock(module.feature).should_receive('available').and_return(True)
  1325. flexmock(module).should_receive('ensure_files_readable')
  1326. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1327. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1328. flexmock(module.environment).should_receive('make_environment')
  1329. flexmock(module).should_receive('execute_command').with_args(
  1330. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  1331. output_log_level=None,
  1332. output_file=None,
  1333. borg_local_path='borg',
  1334. working_directory=None,
  1335. extra_environment=None,
  1336. ).and_return('[]')
  1337. json_output = module.create_archive(
  1338. dry_run=False,
  1339. repository='repo',
  1340. location_config={
  1341. 'source_directories': ['foo', 'bar'],
  1342. 'repositories': ['repo'],
  1343. 'exclude_patterns': None,
  1344. },
  1345. storage_config={},
  1346. local_borg_version='1.2.3',
  1347. json=True,
  1348. )
  1349. assert json_output == '[]'
  1350. def test_create_archive_with_stats_and_json_calls_borg_without_stats_parameter():
  1351. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1352. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1353. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1354. flexmock(module).should_receive('expand_directories').and_return(())
  1355. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1356. flexmock(module).should_receive('expand_home_directories').and_return(())
  1357. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1358. flexmock(module.feature).should_receive('available').and_return(True)
  1359. flexmock(module).should_receive('ensure_files_readable')
  1360. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1361. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1362. flexmock(module.environment).should_receive('make_environment')
  1363. flexmock(module).should_receive('execute_command').with_args(
  1364. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  1365. output_log_level=None,
  1366. output_file=None,
  1367. borg_local_path='borg',
  1368. working_directory=None,
  1369. extra_environment=None,
  1370. ).and_return('[]')
  1371. json_output = module.create_archive(
  1372. dry_run=False,
  1373. repository='repo',
  1374. location_config={
  1375. 'source_directories': ['foo', 'bar'],
  1376. 'repositories': ['repo'],
  1377. 'exclude_patterns': None,
  1378. },
  1379. storage_config={},
  1380. local_borg_version='1.2.3',
  1381. json=True,
  1382. stats=True,
  1383. )
  1384. assert json_output == '[]'
  1385. def test_create_archive_with_source_directories_glob_expands():
  1386. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1387. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  1388. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1389. flexmock(module).should_receive('expand_directories').and_return(())
  1390. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1391. flexmock(module).should_receive('expand_home_directories').and_return(())
  1392. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1393. flexmock(module.feature).should_receive('available').and_return(True)
  1394. flexmock(module).should_receive('ensure_files_readable')
  1395. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1396. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1397. flexmock(module.environment).should_receive('make_environment')
  1398. flexmock(module).should_receive('execute_command').with_args(
  1399. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  1400. output_log_level=logging.INFO,
  1401. output_file=None,
  1402. borg_local_path='borg',
  1403. working_directory=None,
  1404. extra_environment=None,
  1405. )
  1406. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return(['foo', 'food'])
  1407. module.create_archive(
  1408. dry_run=False,
  1409. repository='repo',
  1410. location_config={
  1411. 'source_directories': ['foo*'],
  1412. 'repositories': ['repo'],
  1413. 'exclude_patterns': None,
  1414. },
  1415. storage_config={},
  1416. local_borg_version='1.2.3',
  1417. )
  1418. def test_create_archive_with_non_matching_source_directories_glob_passes_through():
  1419. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1420. flexmock(module).should_receive('deduplicate_directories').and_return(('foo*',))
  1421. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1422. flexmock(module).should_receive('expand_directories').and_return(())
  1423. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1424. flexmock(module).should_receive('expand_home_directories').and_return(())
  1425. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1426. flexmock(module.feature).should_receive('available').and_return(True)
  1427. flexmock(module).should_receive('ensure_files_readable')
  1428. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1429. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1430. flexmock(module.environment).should_receive('make_environment')
  1431. flexmock(module).should_receive('execute_command').with_args(
  1432. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo*'),
  1433. output_log_level=logging.INFO,
  1434. output_file=None,
  1435. borg_local_path='borg',
  1436. working_directory=None,
  1437. extra_environment=None,
  1438. )
  1439. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return([])
  1440. module.create_archive(
  1441. dry_run=False,
  1442. repository='repo',
  1443. location_config={
  1444. 'source_directories': ['foo*'],
  1445. 'repositories': ['repo'],
  1446. 'exclude_patterns': None,
  1447. },
  1448. storage_config={},
  1449. local_borg_version='1.2.3',
  1450. )
  1451. def test_create_archive_with_glob_calls_borg_with_expanded_directories():
  1452. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1453. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  1454. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1455. flexmock(module).should_receive('expand_directories').and_return(())
  1456. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1457. flexmock(module).should_receive('expand_home_directories').and_return(())
  1458. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1459. flexmock(module.feature).should_receive('available').and_return(True)
  1460. flexmock(module).should_receive('ensure_files_readable')
  1461. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1462. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1463. flexmock(module.environment).should_receive('make_environment')
  1464. flexmock(module).should_receive('execute_command').with_args(
  1465. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  1466. output_log_level=logging.INFO,
  1467. output_file=None,
  1468. borg_local_path='borg',
  1469. working_directory=None,
  1470. extra_environment=None,
  1471. )
  1472. module.create_archive(
  1473. dry_run=False,
  1474. repository='repo',
  1475. location_config={
  1476. 'source_directories': ['foo*'],
  1477. 'repositories': ['repo'],
  1478. 'exclude_patterns': None,
  1479. },
  1480. storage_config={},
  1481. local_borg_version='1.2.3',
  1482. )
  1483. def test_create_archive_with_archive_name_format_calls_borg_with_archive_name():
  1484. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1485. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1486. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1487. flexmock(module).should_receive('expand_directories').and_return(())
  1488. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1489. flexmock(module).should_receive('expand_home_directories').and_return(())
  1490. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1491. flexmock(module.feature).should_receive('available').and_return(True)
  1492. flexmock(module).should_receive('ensure_files_readable')
  1493. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1494. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1495. flexmock(module.environment).should_receive('make_environment')
  1496. flexmock(module).should_receive('execute_command').with_args(
  1497. ('borg', 'create', 'repo::ARCHIVE_NAME', 'foo', 'bar'),
  1498. output_log_level=logging.INFO,
  1499. output_file=None,
  1500. borg_local_path='borg',
  1501. working_directory=None,
  1502. extra_environment=None,
  1503. )
  1504. module.create_archive(
  1505. dry_run=False,
  1506. repository='repo',
  1507. location_config={
  1508. 'source_directories': ['foo', 'bar'],
  1509. 'repositories': ['repo'],
  1510. 'exclude_patterns': None,
  1511. },
  1512. storage_config={'archive_name_format': 'ARCHIVE_NAME'},
  1513. local_borg_version='1.2.3',
  1514. )
  1515. def test_create_archive_with_archive_name_format_accepts_borg_placeholders():
  1516. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1517. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1518. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1519. flexmock(module).should_receive('expand_directories').and_return(())
  1520. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1521. flexmock(module).should_receive('expand_home_directories').and_return(())
  1522. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1523. flexmock(module.feature).should_receive('available').and_return(True)
  1524. flexmock(module).should_receive('ensure_files_readable')
  1525. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1526. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1527. flexmock(module.environment).should_receive('make_environment')
  1528. flexmock(module).should_receive('execute_command').with_args(
  1529. ('borg', 'create', 'repo::Documents_{hostname}-{now}', 'foo', 'bar'),
  1530. output_log_level=logging.INFO,
  1531. output_file=None,
  1532. borg_local_path='borg',
  1533. working_directory=None,
  1534. extra_environment=None,
  1535. )
  1536. module.create_archive(
  1537. dry_run=False,
  1538. repository='repo',
  1539. location_config={
  1540. 'source_directories': ['foo', 'bar'],
  1541. 'repositories': ['repo'],
  1542. 'exclude_patterns': None,
  1543. },
  1544. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
  1545. local_borg_version='1.2.3',
  1546. )
  1547. def test_create_archive_with_repository_accepts_borg_placeholders():
  1548. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1549. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1550. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1551. flexmock(module).should_receive('expand_directories').and_return(())
  1552. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1553. flexmock(module).should_receive('expand_home_directories').and_return(())
  1554. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1555. flexmock(module.feature).should_receive('available').and_return(True)
  1556. flexmock(module).should_receive('ensure_files_readable')
  1557. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1558. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1559. flexmock(module.environment).should_receive('make_environment')
  1560. flexmock(module).should_receive('execute_command').with_args(
  1561. ('borg', 'create', '{fqdn}::Documents_{hostname}-{now}', 'foo', 'bar'),
  1562. output_log_level=logging.INFO,
  1563. output_file=None,
  1564. borg_local_path='borg',
  1565. working_directory=None,
  1566. extra_environment=None,
  1567. )
  1568. module.create_archive(
  1569. dry_run=False,
  1570. repository='{fqdn}',
  1571. location_config={
  1572. 'source_directories': ['foo', 'bar'],
  1573. 'repositories': ['{fqdn}'],
  1574. 'exclude_patterns': None,
  1575. },
  1576. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
  1577. local_borg_version='1.2.3',
  1578. )
  1579. def test_create_archive_with_extra_borg_options_calls_borg_with_extra_options():
  1580. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1581. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1582. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1583. flexmock(module).should_receive('expand_directories').and_return(())
  1584. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1585. flexmock(module).should_receive('expand_home_directories').and_return(())
  1586. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1587. flexmock(module.feature).should_receive('available').and_return(True)
  1588. flexmock(module).should_receive('ensure_files_readable')
  1589. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1590. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1591. flexmock(module.environment).should_receive('make_environment')
  1592. flexmock(module).should_receive('execute_command').with_args(
  1593. ('borg', 'create', '--extra', '--options') + ARCHIVE_WITH_PATHS,
  1594. output_log_level=logging.INFO,
  1595. output_file=None,
  1596. borg_local_path='borg',
  1597. working_directory=None,
  1598. extra_environment=None,
  1599. )
  1600. module.create_archive(
  1601. dry_run=False,
  1602. repository='repo',
  1603. location_config={
  1604. 'source_directories': ['foo', 'bar'],
  1605. 'repositories': ['repo'],
  1606. 'exclude_patterns': None,
  1607. },
  1608. storage_config={'extra_borg_options': {'create': '--extra --options'}},
  1609. local_borg_version='1.2.3',
  1610. )
  1611. def test_create_archive_with_stream_processes_calls_borg_with_processes():
  1612. processes = flexmock()
  1613. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1614. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1615. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1616. flexmock(module).should_receive('expand_directories').and_return(())
  1617. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1618. flexmock(module).should_receive('expand_home_directories').and_return(())
  1619. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1620. flexmock(module.feature).should_receive('available').and_return(True)
  1621. flexmock(module).should_receive('ensure_files_readable')
  1622. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1623. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1624. flexmock(module.environment).should_receive('make_environment')
  1625. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1626. ('borg', 'create', '--one-file-system', '--read-special') + ARCHIVE_WITH_PATHS,
  1627. processes=processes,
  1628. output_log_level=logging.INFO,
  1629. output_file=None,
  1630. borg_local_path='borg',
  1631. working_directory=None,
  1632. extra_environment=None,
  1633. )
  1634. module.create_archive(
  1635. dry_run=False,
  1636. repository='repo',
  1637. location_config={
  1638. 'source_directories': ['foo', 'bar'],
  1639. 'repositories': ['repo'],
  1640. 'exclude_patterns': None,
  1641. },
  1642. storage_config={},
  1643. local_borg_version='1.2.3',
  1644. stream_processes=processes,
  1645. )