2
0

test_create.py 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757
  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) == [module.DEFAULT_BORGMATIC_SOURCE_DIRECTORY]
  174. DEFAULT_ARCHIVE_NAME = '{hostname}-{now:%Y-%m-%dT%H:%M:%S.%f}'
  175. ARCHIVE_WITH_PATHS = ('repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'bar')
  176. def test_create_archive_calls_borg_with_parameters():
  177. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  178. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  179. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  180. flexmock(module).should_receive('expand_directories').and_return(())
  181. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  182. flexmock(module).should_receive('expand_home_directories').and_return(())
  183. flexmock(module).should_receive('write_pattern_file').and_return(None)
  184. flexmock(module.feature).should_receive('available').and_return(True)
  185. flexmock(module).should_receive('ensure_files_readable')
  186. flexmock(module).should_receive('make_pattern_flags').and_return(())
  187. flexmock(module).should_receive('make_exclude_flags').and_return(())
  188. flexmock(module).should_receive('execute_command').with_args(
  189. ('borg', 'create') + ARCHIVE_WITH_PATHS,
  190. output_log_level=logging.INFO,
  191. output_file=None,
  192. borg_local_path='borg',
  193. working_directory=None,
  194. )
  195. module.create_archive(
  196. dry_run=False,
  197. repository='repo',
  198. location_config={
  199. 'source_directories': ['foo', 'bar'],
  200. 'repositories': ['repo'],
  201. 'exclude_patterns': None,
  202. },
  203. storage_config={},
  204. local_borg_version='1.2.3',
  205. )
  206. def test_create_archive_with_patterns_calls_borg_with_patterns():
  207. pattern_flags = ('--patterns-from', 'patterns')
  208. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  209. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  210. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  211. flexmock(module).should_receive('expand_directories').and_return(())
  212. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  213. flexmock(module).should_receive('expand_home_directories').and_return(())
  214. flexmock(module).should_receive('write_pattern_file').and_return(
  215. flexmock(name='/tmp/patterns')
  216. ).and_return(None)
  217. flexmock(module.feature).should_receive('available').and_return(True)
  218. flexmock(module).should_receive('ensure_files_readable')
  219. flexmock(module).should_receive('make_pattern_flags').and_return(pattern_flags)
  220. flexmock(module).should_receive('make_exclude_flags').and_return(())
  221. flexmock(module).should_receive('execute_command').with_args(
  222. ('borg', 'create') + pattern_flags + ARCHIVE_WITH_PATHS,
  223. output_log_level=logging.INFO,
  224. output_file=None,
  225. borg_local_path='borg',
  226. working_directory=None,
  227. )
  228. module.create_archive(
  229. dry_run=False,
  230. repository='repo',
  231. location_config={
  232. 'source_directories': ['foo', 'bar'],
  233. 'repositories': ['repo'],
  234. 'patterns': ['pattern'],
  235. },
  236. storage_config={},
  237. local_borg_version='1.2.3',
  238. )
  239. def test_create_archive_with_exclude_patterns_calls_borg_with_excludes():
  240. exclude_flags = ('--exclude-from', 'excludes')
  241. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  242. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  243. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  244. flexmock(module).should_receive('expand_directories').and_return(())
  245. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  246. flexmock(module).should_receive('expand_home_directories').and_return(('exclude',))
  247. flexmock(module).should_receive('write_pattern_file').and_return(None).and_return(
  248. flexmock(name='/tmp/excludes')
  249. )
  250. flexmock(module.feature).should_receive('available').and_return(True)
  251. flexmock(module).should_receive('ensure_files_readable')
  252. flexmock(module).should_receive('make_pattern_flags').and_return(())
  253. flexmock(module).should_receive('make_exclude_flags').and_return(exclude_flags)
  254. flexmock(module).should_receive('execute_command').with_args(
  255. ('borg', 'create') + exclude_flags + ARCHIVE_WITH_PATHS,
  256. output_log_level=logging.INFO,
  257. output_file=None,
  258. borg_local_path='borg',
  259. working_directory=None,
  260. )
  261. module.create_archive(
  262. dry_run=False,
  263. repository='repo',
  264. location_config={
  265. 'source_directories': ['foo', 'bar'],
  266. 'repositories': ['repo'],
  267. 'exclude_patterns': ['exclude'],
  268. },
  269. storage_config={},
  270. local_borg_version='1.2.3',
  271. )
  272. def test_create_archive_with_log_info_calls_borg_with_info_parameter():
  273. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  274. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  275. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  276. flexmock(module).should_receive('expand_directories').and_return(())
  277. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  278. flexmock(module).should_receive('expand_home_directories').and_return(())
  279. flexmock(module).should_receive('write_pattern_file').and_return(None)
  280. flexmock(module.feature).should_receive('available').and_return(True)
  281. flexmock(module).should_receive('ensure_files_readable')
  282. flexmock(module).should_receive('make_pattern_flags').and_return(())
  283. flexmock(module).should_receive('make_exclude_flags').and_return(())
  284. flexmock(module).should_receive('execute_command').with_args(
  285. ('borg', 'create', '--info') + ARCHIVE_WITH_PATHS,
  286. output_log_level=logging.INFO,
  287. output_file=None,
  288. borg_local_path='borg',
  289. working_directory=None,
  290. )
  291. insert_logging_mock(logging.INFO)
  292. module.create_archive(
  293. dry_run=False,
  294. repository='repo',
  295. location_config={
  296. 'source_directories': ['foo', 'bar'],
  297. 'repositories': ['repo'],
  298. 'exclude_patterns': None,
  299. },
  300. storage_config={},
  301. local_borg_version='1.2.3',
  302. )
  303. def test_create_archive_with_log_info_and_json_suppresses_most_borg_output():
  304. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  305. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  306. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  307. flexmock(module).should_receive('expand_directories').and_return(())
  308. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  309. flexmock(module).should_receive('expand_home_directories').and_return(())
  310. flexmock(module).should_receive('write_pattern_file').and_return(None)
  311. flexmock(module.feature).should_receive('available').and_return(True)
  312. flexmock(module).should_receive('ensure_files_readable')
  313. flexmock(module).should_receive('make_pattern_flags').and_return(())
  314. flexmock(module).should_receive('make_exclude_flags').and_return(())
  315. flexmock(module).should_receive('execute_command').with_args(
  316. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  317. output_log_level=None,
  318. output_file=None,
  319. borg_local_path='borg',
  320. working_directory=None,
  321. )
  322. insert_logging_mock(logging.INFO)
  323. module.create_archive(
  324. dry_run=False,
  325. repository='repo',
  326. location_config={
  327. 'source_directories': ['foo', 'bar'],
  328. 'repositories': ['repo'],
  329. 'exclude_patterns': None,
  330. },
  331. storage_config={},
  332. local_borg_version='1.2.3',
  333. json=True,
  334. )
  335. def test_create_archive_with_log_debug_calls_borg_with_debug_parameter():
  336. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  337. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  338. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  339. flexmock(module).should_receive('expand_directories').and_return(())
  340. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  341. flexmock(module).should_receive('expand_home_directories').and_return(())
  342. flexmock(module).should_receive('write_pattern_file').and_return(None)
  343. flexmock(module.feature).should_receive('available').and_return(True)
  344. flexmock(module).should_receive('ensure_files_readable')
  345. flexmock(module).should_receive('make_pattern_flags').and_return(())
  346. flexmock(module).should_receive('make_exclude_flags').and_return(())
  347. flexmock(module).should_receive('execute_command').with_args(
  348. ('borg', 'create', '--debug', '--show-rc') + ARCHIVE_WITH_PATHS,
  349. output_log_level=logging.INFO,
  350. output_file=None,
  351. borg_local_path='borg',
  352. working_directory=None,
  353. )
  354. insert_logging_mock(logging.DEBUG)
  355. module.create_archive(
  356. dry_run=False,
  357. repository='repo',
  358. location_config={
  359. 'source_directories': ['foo', 'bar'],
  360. 'repositories': ['repo'],
  361. 'exclude_patterns': None,
  362. },
  363. storage_config={},
  364. local_borg_version='1.2.3',
  365. )
  366. def test_create_archive_with_log_debug_and_json_suppresses_most_borg_output():
  367. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  368. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  369. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  370. flexmock(module).should_receive('expand_directories').and_return(())
  371. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  372. flexmock(module).should_receive('expand_home_directories').and_return(())
  373. flexmock(module).should_receive('write_pattern_file').and_return(None)
  374. flexmock(module.feature).should_receive('available').and_return(True)
  375. flexmock(module).should_receive('ensure_files_readable')
  376. flexmock(module).should_receive('make_pattern_flags').and_return(())
  377. flexmock(module).should_receive('make_exclude_flags').and_return(())
  378. flexmock(module).should_receive('execute_command').with_args(
  379. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  380. output_log_level=None,
  381. output_file=None,
  382. borg_local_path='borg',
  383. working_directory=None,
  384. )
  385. insert_logging_mock(logging.DEBUG)
  386. module.create_archive(
  387. dry_run=False,
  388. repository='repo',
  389. location_config={
  390. 'source_directories': ['foo', 'bar'],
  391. 'repositories': ['repo'],
  392. 'exclude_patterns': None,
  393. },
  394. storage_config={},
  395. local_borg_version='1.2.3',
  396. json=True,
  397. )
  398. def test_create_archive_with_dry_run_calls_borg_with_dry_run_parameter():
  399. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  400. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  401. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  402. flexmock(module).should_receive('expand_directories').and_return(())
  403. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  404. flexmock(module).should_receive('expand_home_directories').and_return(())
  405. flexmock(module).should_receive('write_pattern_file').and_return(None)
  406. flexmock(module.feature).should_receive('available').and_return(True)
  407. flexmock(module).should_receive('ensure_files_readable')
  408. flexmock(module).should_receive('make_pattern_flags').and_return(())
  409. flexmock(module).should_receive('make_exclude_flags').and_return(())
  410. flexmock(module).should_receive('execute_command').with_args(
  411. ('borg', 'create', '--dry-run') + ARCHIVE_WITH_PATHS,
  412. output_log_level=logging.INFO,
  413. output_file=None,
  414. borg_local_path='borg',
  415. working_directory=None,
  416. )
  417. module.create_archive(
  418. dry_run=True,
  419. repository='repo',
  420. location_config={
  421. 'source_directories': ['foo', 'bar'],
  422. 'repositories': ['repo'],
  423. 'exclude_patterns': None,
  424. },
  425. storage_config={},
  426. local_borg_version='1.2.3',
  427. )
  428. def test_create_archive_with_stats_and_dry_run_calls_borg_without_stats_parameter():
  429. # --dry-run and --stats are mutually exclusive, see:
  430. # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
  431. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  432. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  433. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  434. flexmock(module).should_receive('expand_directories').and_return(())
  435. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  436. flexmock(module).should_receive('expand_home_directories').and_return(())
  437. flexmock(module).should_receive('write_pattern_file').and_return(None)
  438. flexmock(module.feature).should_receive('available').and_return(True)
  439. flexmock(module).should_receive('ensure_files_readable')
  440. flexmock(module).should_receive('make_pattern_flags').and_return(())
  441. flexmock(module).should_receive('make_exclude_flags').and_return(())
  442. flexmock(module).should_receive('execute_command').with_args(
  443. ('borg', 'create', '--info', '--dry-run') + ARCHIVE_WITH_PATHS,
  444. output_log_level=logging.INFO,
  445. output_file=None,
  446. borg_local_path='borg',
  447. working_directory=None,
  448. )
  449. insert_logging_mock(logging.INFO)
  450. module.create_archive(
  451. dry_run=True,
  452. repository='repo',
  453. location_config={
  454. 'source_directories': ['foo', 'bar'],
  455. 'repositories': ['repo'],
  456. 'exclude_patterns': None,
  457. },
  458. storage_config={},
  459. local_borg_version='1.2.3',
  460. stats=True,
  461. )
  462. def test_create_archive_with_checkpoint_interval_calls_borg_with_checkpoint_interval_parameters():
  463. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  464. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  465. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  466. flexmock(module).should_receive('expand_directories').and_return(())
  467. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  468. flexmock(module).should_receive('expand_home_directories').and_return(())
  469. flexmock(module).should_receive('write_pattern_file').and_return(None)
  470. flexmock(module.feature).should_receive('available').and_return(True)
  471. flexmock(module).should_receive('ensure_files_readable')
  472. flexmock(module).should_receive('make_pattern_flags').and_return(())
  473. flexmock(module).should_receive('make_exclude_flags').and_return(())
  474. flexmock(module).should_receive('execute_command').with_args(
  475. ('borg', 'create', '--checkpoint-interval', '600') + ARCHIVE_WITH_PATHS,
  476. output_log_level=logging.INFO,
  477. output_file=None,
  478. borg_local_path='borg',
  479. working_directory=None,
  480. )
  481. module.create_archive(
  482. dry_run=False,
  483. repository='repo',
  484. location_config={
  485. 'source_directories': ['foo', 'bar'],
  486. 'repositories': ['repo'],
  487. 'exclude_patterns': None,
  488. },
  489. storage_config={'checkpoint_interval': 600},
  490. local_borg_version='1.2.3',
  491. )
  492. def test_create_archive_with_chunker_params_calls_borg_with_chunker_params_parameters():
  493. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  494. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  495. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  496. flexmock(module).should_receive('expand_directories').and_return(())
  497. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  498. flexmock(module).should_receive('expand_home_directories').and_return(())
  499. flexmock(module).should_receive('write_pattern_file').and_return(None)
  500. flexmock(module.feature).should_receive('available').and_return(True)
  501. flexmock(module).should_receive('ensure_files_readable')
  502. flexmock(module).should_receive('make_pattern_flags').and_return(())
  503. flexmock(module).should_receive('make_exclude_flags').and_return(())
  504. flexmock(module).should_receive('execute_command').with_args(
  505. ('borg', 'create', '--chunker-params', '1,2,3,4') + ARCHIVE_WITH_PATHS,
  506. output_log_level=logging.INFO,
  507. output_file=None,
  508. borg_local_path='borg',
  509. working_directory=None,
  510. )
  511. module.create_archive(
  512. dry_run=False,
  513. repository='repo',
  514. location_config={
  515. 'source_directories': ['foo', 'bar'],
  516. 'repositories': ['repo'],
  517. 'exclude_patterns': None,
  518. },
  519. storage_config={'chunker_params': '1,2,3,4'},
  520. local_borg_version='1.2.3',
  521. )
  522. def test_create_archive_with_compression_calls_borg_with_compression_parameters():
  523. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  524. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  525. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  526. flexmock(module).should_receive('expand_directories').and_return(())
  527. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  528. flexmock(module).should_receive('expand_home_directories').and_return(())
  529. flexmock(module).should_receive('write_pattern_file').and_return(None)
  530. flexmock(module.feature).should_receive('available').and_return(True)
  531. flexmock(module).should_receive('ensure_files_readable')
  532. flexmock(module).should_receive('make_pattern_flags').and_return(())
  533. flexmock(module).should_receive('make_exclude_flags').and_return(())
  534. flexmock(module).should_receive('execute_command').with_args(
  535. ('borg', 'create', '--compression', 'rle') + ARCHIVE_WITH_PATHS,
  536. output_log_level=logging.INFO,
  537. output_file=None,
  538. borg_local_path='borg',
  539. working_directory=None,
  540. )
  541. module.create_archive(
  542. dry_run=False,
  543. repository='repo',
  544. location_config={
  545. 'source_directories': ['foo', 'bar'],
  546. 'repositories': ['repo'],
  547. 'exclude_patterns': None,
  548. },
  549. storage_config={'compression': 'rle'},
  550. local_borg_version='1.2.3',
  551. )
  552. @pytest.mark.parametrize(
  553. 'feature_available,option_flag', ((True, '--upload-ratelimit'), (False, '--remote-ratelimit')),
  554. )
  555. def test_create_archive_with_remote_rate_limit_calls_borg_with_upload_ratelimit_parameters(
  556. feature_available, option_flag
  557. ):
  558. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  559. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  560. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  561. flexmock(module).should_receive('expand_directories').and_return(())
  562. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  563. flexmock(module).should_receive('expand_home_directories').and_return(())
  564. flexmock(module).should_receive('write_pattern_file').and_return(None)
  565. flexmock(module.feature).should_receive('available').and_return(feature_available)
  566. flexmock(module).should_receive('ensure_files_readable')
  567. flexmock(module).should_receive('make_pattern_flags').and_return(())
  568. flexmock(module).should_receive('make_exclude_flags').and_return(())
  569. flexmock(module).should_receive('execute_command').with_args(
  570. ('borg', 'create', option_flag, '100') + ARCHIVE_WITH_PATHS,
  571. output_log_level=logging.INFO,
  572. output_file=None,
  573. borg_local_path='borg',
  574. working_directory=None,
  575. )
  576. module.create_archive(
  577. dry_run=False,
  578. repository='repo',
  579. location_config={
  580. 'source_directories': ['foo', 'bar'],
  581. 'repositories': ['repo'],
  582. 'exclude_patterns': None,
  583. },
  584. storage_config={'remote_rate_limit': 100},
  585. local_borg_version='1.2.3',
  586. )
  587. def test_create_archive_with_working_directory_calls_borg_with_working_directory():
  588. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  589. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  590. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  591. flexmock(module).should_receive('expand_directories').and_return(())
  592. flexmock(module.os.path).should_receive('expanduser').with_args('/working/dir').and_return(
  593. '/working/dir'
  594. )
  595. flexmock(module).should_receive('expand_home_directories').and_return(())
  596. flexmock(module).should_receive('write_pattern_file').and_return(None)
  597. flexmock(module.feature).should_receive('available').and_return(True)
  598. flexmock(module).should_receive('ensure_files_readable')
  599. flexmock(module).should_receive('make_pattern_flags').and_return(())
  600. flexmock(module).should_receive('make_exclude_flags').and_return(())
  601. flexmock(module).should_receive('execute_command').with_args(
  602. ('borg', 'create') + ARCHIVE_WITH_PATHS,
  603. output_log_level=logging.INFO,
  604. output_file=None,
  605. borg_local_path='borg',
  606. working_directory='/working/dir',
  607. )
  608. module.create_archive(
  609. dry_run=False,
  610. repository='repo',
  611. location_config={
  612. 'source_directories': ['foo', 'bar'],
  613. 'repositories': ['repo'],
  614. 'working_directory': '/working/dir',
  615. 'exclude_patterns': None,
  616. },
  617. storage_config={},
  618. local_borg_version='1.2.3',
  619. )
  620. def test_create_archive_with_one_file_system_calls_borg_with_one_file_system_parameter():
  621. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  622. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  623. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  624. flexmock(module).should_receive('expand_directories').and_return(())
  625. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  626. flexmock(module).should_receive('expand_home_directories').and_return(())
  627. flexmock(module).should_receive('write_pattern_file').and_return(None)
  628. flexmock(module.feature).should_receive('available').and_return(True)
  629. flexmock(module).should_receive('ensure_files_readable')
  630. flexmock(module).should_receive('make_pattern_flags').and_return(())
  631. flexmock(module).should_receive('make_exclude_flags').and_return(())
  632. flexmock(module).should_receive('execute_command').with_args(
  633. ('borg', 'create', '--one-file-system') + ARCHIVE_WITH_PATHS,
  634. output_log_level=logging.INFO,
  635. output_file=None,
  636. borg_local_path='borg',
  637. working_directory=None,
  638. )
  639. module.create_archive(
  640. dry_run=False,
  641. repository='repo',
  642. location_config={
  643. 'source_directories': ['foo', 'bar'],
  644. 'repositories': ['repo'],
  645. 'one_file_system': True,
  646. 'exclude_patterns': None,
  647. },
  648. storage_config={},
  649. local_borg_version='1.2.3',
  650. )
  651. @pytest.mark.parametrize(
  652. 'feature_available,option_flag', ((True, '--numeric-ids'), (False, '--numeric-owner')),
  653. )
  654. def test_create_archive_with_numeric_owner_calls_borg_with_numeric_ids_parameter(
  655. feature_available, option_flag
  656. ):
  657. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  658. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  659. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  660. flexmock(module).should_receive('expand_directories').and_return(())
  661. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  662. flexmock(module).should_receive('expand_home_directories').and_return(())
  663. flexmock(module).should_receive('write_pattern_file').and_return(None)
  664. flexmock(module.feature).should_receive('available').and_return(feature_available)
  665. flexmock(module).should_receive('ensure_files_readable')
  666. flexmock(module).should_receive('make_pattern_flags').and_return(())
  667. flexmock(module).should_receive('make_exclude_flags').and_return(())
  668. flexmock(module).should_receive('execute_command').with_args(
  669. ('borg', 'create', option_flag) + ARCHIVE_WITH_PATHS,
  670. output_log_level=logging.INFO,
  671. output_file=None,
  672. borg_local_path='borg',
  673. working_directory=None,
  674. )
  675. module.create_archive(
  676. dry_run=False,
  677. repository='repo',
  678. location_config={
  679. 'source_directories': ['foo', 'bar'],
  680. 'repositories': ['repo'],
  681. 'numeric_owner': True,
  682. 'exclude_patterns': None,
  683. },
  684. storage_config={},
  685. local_borg_version='1.2.3',
  686. )
  687. def test_create_archive_with_read_special_calls_borg_with_read_special_parameter():
  688. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  689. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  690. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  691. flexmock(module).should_receive('expand_directories').and_return(())
  692. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  693. flexmock(module).should_receive('expand_home_directories').and_return(())
  694. flexmock(module).should_receive('write_pattern_file').and_return(None)
  695. flexmock(module.feature).should_receive('available').and_return(True)
  696. flexmock(module).should_receive('ensure_files_readable')
  697. flexmock(module).should_receive('make_pattern_flags').and_return(())
  698. flexmock(module).should_receive('make_exclude_flags').and_return(())
  699. flexmock(module).should_receive('execute_command').with_args(
  700. ('borg', 'create', '--read-special') + ARCHIVE_WITH_PATHS,
  701. output_log_level=logging.INFO,
  702. output_file=None,
  703. borg_local_path='borg',
  704. working_directory=None,
  705. )
  706. module.create_archive(
  707. dry_run=False,
  708. repository='repo',
  709. location_config={
  710. 'source_directories': ['foo', 'bar'],
  711. 'repositories': ['repo'],
  712. 'read_special': True,
  713. 'exclude_patterns': None,
  714. },
  715. storage_config={},
  716. local_borg_version='1.2.3',
  717. )
  718. @pytest.mark.parametrize(
  719. 'option_name,option_value',
  720. (('ctime', True), ('ctime', False), ('birthtime', True), ('birthtime', False),),
  721. )
  722. def test_create_archive_with_basic_option_calls_borg_with_corresponding_parameter(
  723. option_name, option_value
  724. ):
  725. option_flag = '--no' + option_name.replace('', '') if option_value is False else None
  726. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  727. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  728. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  729. flexmock(module).should_receive('expand_directories').and_return(())
  730. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  731. flexmock(module).should_receive('expand_home_directories').and_return(())
  732. flexmock(module).should_receive('write_pattern_file').and_return(None)
  733. flexmock(module.feature).should_receive('available').and_return(True)
  734. flexmock(module).should_receive('ensure_files_readable')
  735. flexmock(module).should_receive('make_pattern_flags').and_return(())
  736. flexmock(module).should_receive('make_exclude_flags').and_return(())
  737. flexmock(module).should_receive('execute_command').with_args(
  738. ('borg', 'create') + ((option_flag,) if option_flag else ()) + ARCHIVE_WITH_PATHS,
  739. output_log_level=logging.INFO,
  740. output_file=None,
  741. borg_local_path='borg',
  742. working_directory=None,
  743. )
  744. module.create_archive(
  745. dry_run=False,
  746. repository='repo',
  747. location_config={
  748. 'source_directories': ['foo', 'bar'],
  749. 'repositories': ['repo'],
  750. option_name: option_value,
  751. 'exclude_patterns': None,
  752. },
  753. storage_config={},
  754. local_borg_version='1.2.3',
  755. )
  756. @pytest.mark.parametrize(
  757. 'option_value,feature_available,option_flag',
  758. (
  759. (True, True, '--atime'),
  760. (True, False, None),
  761. (False, True, None),
  762. (False, False, '--noatime'),
  763. ),
  764. )
  765. def test_create_archive_with_atime_option_calls_borg_with_corresponding_parameter(
  766. option_value, feature_available, option_flag
  767. ):
  768. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  769. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  770. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  771. flexmock(module).should_receive('expand_directories').and_return(())
  772. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  773. flexmock(module).should_receive('expand_home_directories').and_return(())
  774. flexmock(module).should_receive('write_pattern_file').and_return(None)
  775. flexmock(module.feature).should_receive('available').and_return(feature_available)
  776. flexmock(module).should_receive('ensure_files_readable')
  777. flexmock(module).should_receive('make_pattern_flags').and_return(())
  778. flexmock(module).should_receive('make_exclude_flags').and_return(())
  779. flexmock(module).should_receive('execute_command').with_args(
  780. ('borg', 'create') + ((option_flag,) if option_flag else ()) + ARCHIVE_WITH_PATHS,
  781. output_log_level=logging.INFO,
  782. output_file=None,
  783. borg_local_path='borg',
  784. working_directory=None,
  785. )
  786. module.create_archive(
  787. dry_run=False,
  788. repository='repo',
  789. location_config={
  790. 'source_directories': ['foo', 'bar'],
  791. 'repositories': ['repo'],
  792. 'atime': option_value,
  793. 'exclude_patterns': None,
  794. },
  795. storage_config={},
  796. local_borg_version='1.2.3',
  797. )
  798. @pytest.mark.parametrize(
  799. 'option_value,feature_available,option_flag',
  800. (
  801. (True, True, None),
  802. (True, False, None),
  803. (False, True, '--noflags'),
  804. (False, False, '--nobsdflags'),
  805. ),
  806. )
  807. def test_create_archive_with_bsd_flags_option_calls_borg_with_corresponding_parameter(
  808. option_value, feature_available, option_flag
  809. ):
  810. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  811. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  812. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  813. flexmock(module).should_receive('expand_directories').and_return(())
  814. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  815. flexmock(module).should_receive('expand_home_directories').and_return(())
  816. flexmock(module).should_receive('write_pattern_file').and_return(None)
  817. flexmock(module.feature).should_receive('available').and_return(feature_available)
  818. flexmock(module).should_receive('ensure_files_readable')
  819. flexmock(module).should_receive('make_pattern_flags').and_return(())
  820. flexmock(module).should_receive('make_exclude_flags').and_return(())
  821. flexmock(module).should_receive('execute_command').with_args(
  822. ('borg', 'create') + ((option_flag,) if option_flag else ()) + ARCHIVE_WITH_PATHS,
  823. output_log_level=logging.INFO,
  824. output_file=None,
  825. borg_local_path='borg',
  826. working_directory=None,
  827. )
  828. module.create_archive(
  829. dry_run=False,
  830. repository='repo',
  831. location_config={
  832. 'source_directories': ['foo', 'bar'],
  833. 'repositories': ['repo'],
  834. 'bsd_flags': option_value,
  835. 'exclude_patterns': None,
  836. },
  837. storage_config={},
  838. local_borg_version='1.2.3',
  839. )
  840. def test_create_archive_with_files_cache_calls_borg_with_files_cache_parameters():
  841. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  842. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  843. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  844. flexmock(module).should_receive('expand_directories').and_return(())
  845. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  846. flexmock(module).should_receive('expand_home_directories').and_return(())
  847. flexmock(module).should_receive('write_pattern_file').and_return(None)
  848. flexmock(module.feature).should_receive('available').and_return(True)
  849. flexmock(module).should_receive('ensure_files_readable')
  850. flexmock(module).should_receive('make_pattern_flags').and_return(())
  851. flexmock(module).should_receive('make_exclude_flags').and_return(())
  852. flexmock(module).should_receive('execute_command').with_args(
  853. ('borg', 'create', '--files-cache', 'ctime,size') + ARCHIVE_WITH_PATHS,
  854. output_log_level=logging.INFO,
  855. output_file=None,
  856. borg_local_path='borg',
  857. working_directory=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. 'files_cache': 'ctime,size',
  866. 'exclude_patterns': None,
  867. },
  868. storage_config={},
  869. local_borg_version='1.2.3',
  870. )
  871. def test_create_archive_with_local_path_calls_borg_via_local_path():
  872. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  873. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  874. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  875. flexmock(module).should_receive('expand_directories').and_return(())
  876. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  877. flexmock(module).should_receive('expand_home_directories').and_return(())
  878. flexmock(module).should_receive('write_pattern_file').and_return(None)
  879. flexmock(module.feature).should_receive('available').and_return(True)
  880. flexmock(module).should_receive('ensure_files_readable')
  881. flexmock(module).should_receive('make_pattern_flags').and_return(())
  882. flexmock(module).should_receive('make_exclude_flags').and_return(())
  883. flexmock(module).should_receive('execute_command').with_args(
  884. ('borg1', 'create') + ARCHIVE_WITH_PATHS,
  885. output_log_level=logging.INFO,
  886. output_file=None,
  887. borg_local_path='borg1',
  888. working_directory=None,
  889. )
  890. module.create_archive(
  891. dry_run=False,
  892. repository='repo',
  893. location_config={
  894. 'source_directories': ['foo', 'bar'],
  895. 'repositories': ['repo'],
  896. 'exclude_patterns': None,
  897. },
  898. storage_config={},
  899. local_borg_version='1.2.3',
  900. local_path='borg1',
  901. )
  902. def test_create_archive_with_remote_path_calls_borg_with_remote_path_parameters():
  903. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  904. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  905. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  906. flexmock(module).should_receive('expand_directories').and_return(())
  907. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  908. flexmock(module).should_receive('expand_home_directories').and_return(())
  909. flexmock(module).should_receive('write_pattern_file').and_return(None)
  910. flexmock(module.feature).should_receive('available').and_return(True)
  911. flexmock(module).should_receive('ensure_files_readable')
  912. flexmock(module).should_receive('make_pattern_flags').and_return(())
  913. flexmock(module).should_receive('make_exclude_flags').and_return(())
  914. flexmock(module).should_receive('execute_command').with_args(
  915. ('borg', 'create', '--remote-path', 'borg1') + ARCHIVE_WITH_PATHS,
  916. output_log_level=logging.INFO,
  917. output_file=None,
  918. borg_local_path='borg',
  919. working_directory=None,
  920. )
  921. module.create_archive(
  922. dry_run=False,
  923. repository='repo',
  924. location_config={
  925. 'source_directories': ['foo', 'bar'],
  926. 'repositories': ['repo'],
  927. 'exclude_patterns': None,
  928. },
  929. storage_config={},
  930. local_borg_version='1.2.3',
  931. remote_path='borg1',
  932. )
  933. def test_create_archive_with_umask_calls_borg_with_umask_parameters():
  934. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  935. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  936. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  937. flexmock(module).should_receive('expand_directories').and_return(())
  938. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  939. flexmock(module).should_receive('expand_home_directories').and_return(())
  940. flexmock(module).should_receive('write_pattern_file').and_return(None)
  941. flexmock(module.feature).should_receive('available').and_return(True)
  942. flexmock(module).should_receive('ensure_files_readable')
  943. flexmock(module).should_receive('make_pattern_flags').and_return(())
  944. flexmock(module).should_receive('make_exclude_flags').and_return(())
  945. flexmock(module).should_receive('execute_command').with_args(
  946. ('borg', 'create', '--umask', '740') + ARCHIVE_WITH_PATHS,
  947. output_log_level=logging.INFO,
  948. output_file=None,
  949. borg_local_path='borg',
  950. working_directory=None,
  951. )
  952. module.create_archive(
  953. dry_run=False,
  954. repository='repo',
  955. location_config={
  956. 'source_directories': ['foo', 'bar'],
  957. 'repositories': ['repo'],
  958. 'exclude_patterns': None,
  959. },
  960. storage_config={'umask': 740},
  961. local_borg_version='1.2.3',
  962. )
  963. def test_create_archive_with_lock_wait_calls_borg_with_lock_wait_parameters():
  964. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  965. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  966. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  967. flexmock(module).should_receive('expand_directories').and_return(())
  968. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  969. flexmock(module).should_receive('expand_home_directories').and_return(())
  970. flexmock(module).should_receive('write_pattern_file').and_return(None)
  971. flexmock(module.feature).should_receive('available').and_return(True)
  972. flexmock(module).should_receive('ensure_files_readable')
  973. flexmock(module).should_receive('make_pattern_flags').and_return(())
  974. flexmock(module).should_receive('make_exclude_flags').and_return(())
  975. flexmock(module).should_receive('execute_command').with_args(
  976. ('borg', 'create', '--lock-wait', '5') + ARCHIVE_WITH_PATHS,
  977. output_log_level=logging.INFO,
  978. output_file=None,
  979. borg_local_path='borg',
  980. working_directory=None,
  981. )
  982. module.create_archive(
  983. dry_run=False,
  984. repository='repo',
  985. location_config={
  986. 'source_directories': ['foo', 'bar'],
  987. 'repositories': ['repo'],
  988. 'exclude_patterns': None,
  989. },
  990. storage_config={'lock_wait': 5},
  991. local_borg_version='1.2.3',
  992. )
  993. def test_create_archive_with_stats_calls_borg_with_stats_parameter_and_warning_output_log_level():
  994. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  995. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  996. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  997. flexmock(module).should_receive('expand_directories').and_return(())
  998. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  999. flexmock(module).should_receive('expand_home_directories').and_return(())
  1000. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1001. flexmock(module.feature).should_receive('available').and_return(True)
  1002. flexmock(module).should_receive('ensure_files_readable')
  1003. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1004. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1005. flexmock(module).should_receive('execute_command').with_args(
  1006. ('borg', 'create', '--stats') + ARCHIVE_WITH_PATHS,
  1007. output_log_level=logging.WARNING,
  1008. output_file=None,
  1009. borg_local_path='borg',
  1010. working_directory=None,
  1011. )
  1012. module.create_archive(
  1013. dry_run=False,
  1014. repository='repo',
  1015. location_config={
  1016. 'source_directories': ['foo', 'bar'],
  1017. 'repositories': ['repo'],
  1018. 'exclude_patterns': None,
  1019. },
  1020. storage_config={},
  1021. local_borg_version='1.2.3',
  1022. stats=True,
  1023. )
  1024. def test_create_archive_with_stats_and_log_info_calls_borg_with_stats_parameter_and_info_output_log_level():
  1025. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1026. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1027. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1028. flexmock(module).should_receive('expand_directories').and_return(())
  1029. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1030. flexmock(module).should_receive('expand_home_directories').and_return(())
  1031. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1032. flexmock(module.feature).should_receive('available').and_return(True)
  1033. flexmock(module).should_receive('ensure_files_readable')
  1034. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1035. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1036. flexmock(module).should_receive('execute_command').with_args(
  1037. ('borg', 'create', '--info', '--stats') + ARCHIVE_WITH_PATHS,
  1038. output_log_level=logging.INFO,
  1039. output_file=None,
  1040. borg_local_path='borg',
  1041. working_directory=None,
  1042. )
  1043. insert_logging_mock(logging.INFO)
  1044. module.create_archive(
  1045. dry_run=False,
  1046. repository='repo',
  1047. location_config={
  1048. 'source_directories': ['foo', 'bar'],
  1049. 'repositories': ['repo'],
  1050. 'exclude_patterns': None,
  1051. },
  1052. storage_config={},
  1053. local_borg_version='1.2.3',
  1054. stats=True,
  1055. )
  1056. def test_create_archive_with_files_calls_borg_with_list_parameter_and_warning_output_log_level():
  1057. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1058. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1059. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1060. flexmock(module).should_receive('expand_directories').and_return(())
  1061. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1062. flexmock(module).should_receive('expand_home_directories').and_return(())
  1063. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1064. flexmock(module.feature).should_receive('available').and_return(True)
  1065. flexmock(module).should_receive('ensure_files_readable')
  1066. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1067. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1068. flexmock(module).should_receive('execute_command').with_args(
  1069. ('borg', 'create', '--list', '--filter', 'AME-') + ARCHIVE_WITH_PATHS,
  1070. output_log_level=logging.WARNING,
  1071. output_file=None,
  1072. borg_local_path='borg',
  1073. working_directory=None,
  1074. )
  1075. module.create_archive(
  1076. dry_run=False,
  1077. repository='repo',
  1078. location_config={
  1079. 'source_directories': ['foo', 'bar'],
  1080. 'repositories': ['repo'],
  1081. 'exclude_patterns': None,
  1082. },
  1083. storage_config={},
  1084. local_borg_version='1.2.3',
  1085. files=True,
  1086. )
  1087. def test_create_archive_with_files_and_log_info_calls_borg_with_list_parameter_and_info_output_log_level():
  1088. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1089. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1090. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1091. flexmock(module).should_receive('expand_directories').and_return(())
  1092. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1093. flexmock(module).should_receive('expand_home_directories').and_return(())
  1094. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1095. flexmock(module.feature).should_receive('available').and_return(True)
  1096. flexmock(module).should_receive('ensure_files_readable')
  1097. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1098. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1099. flexmock(module).should_receive('execute_command').with_args(
  1100. ('borg', 'create', '--list', '--filter', 'AME-', '--info') + ARCHIVE_WITH_PATHS,
  1101. output_log_level=logging.INFO,
  1102. output_file=None,
  1103. borg_local_path='borg',
  1104. working_directory=None,
  1105. )
  1106. insert_logging_mock(logging.INFO)
  1107. module.create_archive(
  1108. dry_run=False,
  1109. repository='repo',
  1110. location_config={
  1111. 'source_directories': ['foo', 'bar'],
  1112. 'repositories': ['repo'],
  1113. 'exclude_patterns': None,
  1114. },
  1115. storage_config={},
  1116. local_borg_version='1.2.3',
  1117. files=True,
  1118. )
  1119. def test_create_archive_with_progress_and_log_info_calls_borg_with_progress_parameter_and_no_list():
  1120. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1121. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1122. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1123. flexmock(module).should_receive('expand_directories').and_return(())
  1124. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1125. flexmock(module).should_receive('expand_home_directories').and_return(())
  1126. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1127. flexmock(module.feature).should_receive('available').and_return(True)
  1128. flexmock(module).should_receive('ensure_files_readable')
  1129. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1130. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1131. flexmock(module).should_receive('execute_command').with_args(
  1132. ('borg', 'create', '--info', '--progress') + ARCHIVE_WITH_PATHS,
  1133. output_log_level=logging.INFO,
  1134. output_file=module.DO_NOT_CAPTURE,
  1135. borg_local_path='borg',
  1136. working_directory=None,
  1137. )
  1138. insert_logging_mock(logging.INFO)
  1139. module.create_archive(
  1140. dry_run=False,
  1141. repository='repo',
  1142. location_config={
  1143. 'source_directories': ['foo', 'bar'],
  1144. 'repositories': ['repo'],
  1145. 'exclude_patterns': None,
  1146. },
  1147. storage_config={},
  1148. local_borg_version='1.2.3',
  1149. progress=True,
  1150. )
  1151. def test_create_archive_with_progress_calls_borg_with_progress_parameter():
  1152. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1153. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1154. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1155. flexmock(module).should_receive('expand_directories').and_return(())
  1156. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1157. flexmock(module).should_receive('expand_home_directories').and_return(())
  1158. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1159. flexmock(module.feature).should_receive('available').and_return(True)
  1160. flexmock(module).should_receive('ensure_files_readable')
  1161. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1162. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1163. flexmock(module).should_receive('execute_command').with_args(
  1164. ('borg', 'create', '--progress') + ARCHIVE_WITH_PATHS,
  1165. output_log_level=logging.INFO,
  1166. output_file=module.DO_NOT_CAPTURE,
  1167. borg_local_path='borg',
  1168. working_directory=None,
  1169. )
  1170. module.create_archive(
  1171. dry_run=False,
  1172. repository='repo',
  1173. location_config={
  1174. 'source_directories': ['foo', 'bar'],
  1175. 'repositories': ['repo'],
  1176. 'exclude_patterns': None,
  1177. },
  1178. storage_config={},
  1179. local_borg_version='1.2.3',
  1180. progress=True,
  1181. )
  1182. def test_create_archive_with_progress_and_stream_processes_calls_borg_with_progress_parameter():
  1183. processes = flexmock()
  1184. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1185. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1186. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1187. flexmock(module).should_receive('expand_directories').and_return(())
  1188. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1189. flexmock(module).should_receive('expand_home_directories').and_return(())
  1190. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1191. flexmock(module.feature).should_receive('available').and_return(True)
  1192. flexmock(module).should_receive('ensure_files_readable')
  1193. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1194. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1195. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1196. ('borg', 'create', '--one-file-system', '--read-special', '--progress')
  1197. + ARCHIVE_WITH_PATHS,
  1198. processes=processes,
  1199. output_log_level=logging.INFO,
  1200. output_file=module.DO_NOT_CAPTURE,
  1201. borg_local_path='borg',
  1202. working_directory=None,
  1203. )
  1204. module.create_archive(
  1205. dry_run=False,
  1206. repository='repo',
  1207. location_config={
  1208. 'source_directories': ['foo', 'bar'],
  1209. 'repositories': ['repo'],
  1210. 'exclude_patterns': None,
  1211. },
  1212. storage_config={},
  1213. local_borg_version='1.2.3',
  1214. progress=True,
  1215. stream_processes=processes,
  1216. )
  1217. def test_create_archive_with_json_calls_borg_with_json_parameter():
  1218. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1219. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1220. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1221. flexmock(module).should_receive('expand_directories').and_return(())
  1222. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1223. flexmock(module).should_receive('expand_home_directories').and_return(())
  1224. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1225. flexmock(module.feature).should_receive('available').and_return(True)
  1226. flexmock(module).should_receive('ensure_files_readable')
  1227. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1228. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1229. flexmock(module).should_receive('execute_command').with_args(
  1230. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  1231. output_log_level=None,
  1232. output_file=None,
  1233. borg_local_path='borg',
  1234. working_directory=None,
  1235. ).and_return('[]')
  1236. json_output = module.create_archive(
  1237. dry_run=False,
  1238. repository='repo',
  1239. location_config={
  1240. 'source_directories': ['foo', 'bar'],
  1241. 'repositories': ['repo'],
  1242. 'exclude_patterns': None,
  1243. },
  1244. storage_config={},
  1245. local_borg_version='1.2.3',
  1246. json=True,
  1247. )
  1248. assert json_output == '[]'
  1249. def test_create_archive_with_stats_and_json_calls_borg_without_stats_parameter():
  1250. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1251. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1252. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1253. flexmock(module).should_receive('expand_directories').and_return(())
  1254. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1255. flexmock(module).should_receive('expand_home_directories').and_return(())
  1256. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1257. flexmock(module.feature).should_receive('available').and_return(True)
  1258. flexmock(module).should_receive('ensure_files_readable')
  1259. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1260. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1261. flexmock(module).should_receive('execute_command').with_args(
  1262. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  1263. output_log_level=None,
  1264. output_file=None,
  1265. borg_local_path='borg',
  1266. working_directory=None,
  1267. ).and_return('[]')
  1268. json_output = module.create_archive(
  1269. dry_run=False,
  1270. repository='repo',
  1271. location_config={
  1272. 'source_directories': ['foo', 'bar'],
  1273. 'repositories': ['repo'],
  1274. 'exclude_patterns': None,
  1275. },
  1276. storage_config={},
  1277. local_borg_version='1.2.3',
  1278. json=True,
  1279. stats=True,
  1280. )
  1281. assert json_output == '[]'
  1282. def test_create_archive_with_source_directories_glob_expands():
  1283. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1284. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  1285. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1286. flexmock(module).should_receive('expand_directories').and_return(())
  1287. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1288. flexmock(module).should_receive('expand_home_directories').and_return(())
  1289. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1290. flexmock(module.feature).should_receive('available').and_return(True)
  1291. flexmock(module).should_receive('ensure_files_readable')
  1292. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1293. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1294. flexmock(module).should_receive('execute_command').with_args(
  1295. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  1296. output_log_level=logging.INFO,
  1297. output_file=None,
  1298. borg_local_path='borg',
  1299. working_directory=None,
  1300. )
  1301. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return(['foo', 'food'])
  1302. module.create_archive(
  1303. dry_run=False,
  1304. repository='repo',
  1305. location_config={
  1306. 'source_directories': ['foo*'],
  1307. 'repositories': ['repo'],
  1308. 'exclude_patterns': None,
  1309. },
  1310. storage_config={},
  1311. local_borg_version='1.2.3',
  1312. )
  1313. def test_create_archive_with_non_matching_source_directories_glob_passes_through():
  1314. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1315. flexmock(module).should_receive('deduplicate_directories').and_return(('foo*',))
  1316. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1317. flexmock(module).should_receive('expand_directories').and_return(())
  1318. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1319. flexmock(module).should_receive('expand_home_directories').and_return(())
  1320. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1321. flexmock(module.feature).should_receive('available').and_return(True)
  1322. flexmock(module).should_receive('ensure_files_readable')
  1323. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1324. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1325. flexmock(module).should_receive('execute_command').with_args(
  1326. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo*'),
  1327. output_log_level=logging.INFO,
  1328. output_file=None,
  1329. borg_local_path='borg',
  1330. working_directory=None,
  1331. )
  1332. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return([])
  1333. module.create_archive(
  1334. dry_run=False,
  1335. repository='repo',
  1336. location_config={
  1337. 'source_directories': ['foo*'],
  1338. 'repositories': ['repo'],
  1339. 'exclude_patterns': None,
  1340. },
  1341. storage_config={},
  1342. local_borg_version='1.2.3',
  1343. )
  1344. def test_create_archive_with_glob_calls_borg_with_expanded_directories():
  1345. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1346. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  1347. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1348. flexmock(module).should_receive('expand_directories').and_return(())
  1349. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1350. flexmock(module).should_receive('expand_home_directories').and_return(())
  1351. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1352. flexmock(module.feature).should_receive('available').and_return(True)
  1353. flexmock(module).should_receive('ensure_files_readable')
  1354. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1355. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1356. flexmock(module).should_receive('execute_command').with_args(
  1357. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  1358. output_log_level=logging.INFO,
  1359. output_file=None,
  1360. borg_local_path='borg',
  1361. working_directory=None,
  1362. )
  1363. module.create_archive(
  1364. dry_run=False,
  1365. repository='repo',
  1366. location_config={
  1367. 'source_directories': ['foo*'],
  1368. 'repositories': ['repo'],
  1369. 'exclude_patterns': None,
  1370. },
  1371. storage_config={},
  1372. local_borg_version='1.2.3',
  1373. )
  1374. def test_create_archive_with_archive_name_format_calls_borg_with_archive_name():
  1375. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1376. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1377. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1378. flexmock(module).should_receive('expand_directories').and_return(())
  1379. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1380. flexmock(module).should_receive('expand_home_directories').and_return(())
  1381. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1382. flexmock(module.feature).should_receive('available').and_return(True)
  1383. flexmock(module).should_receive('ensure_files_readable')
  1384. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1385. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1386. flexmock(module).should_receive('execute_command').with_args(
  1387. ('borg', 'create', 'repo::ARCHIVE_NAME', 'foo', 'bar'),
  1388. output_log_level=logging.INFO,
  1389. output_file=None,
  1390. borg_local_path='borg',
  1391. working_directory=None,
  1392. )
  1393. module.create_archive(
  1394. dry_run=False,
  1395. repository='repo',
  1396. location_config={
  1397. 'source_directories': ['foo', 'bar'],
  1398. 'repositories': ['repo'],
  1399. 'exclude_patterns': None,
  1400. },
  1401. storage_config={'archive_name_format': 'ARCHIVE_NAME'},
  1402. local_borg_version='1.2.3',
  1403. )
  1404. def test_create_archive_with_archive_name_format_accepts_borg_placeholders():
  1405. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1406. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1407. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1408. flexmock(module).should_receive('expand_directories').and_return(())
  1409. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1410. flexmock(module).should_receive('expand_home_directories').and_return(())
  1411. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1412. flexmock(module.feature).should_receive('available').and_return(True)
  1413. flexmock(module).should_receive('ensure_files_readable')
  1414. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1415. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1416. flexmock(module).should_receive('execute_command').with_args(
  1417. ('borg', 'create', 'repo::Documents_{hostname}-{now}', 'foo', 'bar'),
  1418. output_log_level=logging.INFO,
  1419. output_file=None,
  1420. borg_local_path='borg',
  1421. working_directory=None,
  1422. )
  1423. module.create_archive(
  1424. dry_run=False,
  1425. repository='repo',
  1426. location_config={
  1427. 'source_directories': ['foo', 'bar'],
  1428. 'repositories': ['repo'],
  1429. 'exclude_patterns': None,
  1430. },
  1431. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
  1432. local_borg_version='1.2.3',
  1433. )
  1434. def test_create_archive_with_repository_accepts_borg_placeholders():
  1435. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1436. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1437. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1438. flexmock(module).should_receive('expand_directories').and_return(())
  1439. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1440. flexmock(module).should_receive('expand_home_directories').and_return(())
  1441. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1442. flexmock(module.feature).should_receive('available').and_return(True)
  1443. flexmock(module).should_receive('ensure_files_readable')
  1444. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1445. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1446. flexmock(module).should_receive('execute_command').with_args(
  1447. ('borg', 'create', '{fqdn}::Documents_{hostname}-{now}', 'foo', 'bar'),
  1448. output_log_level=logging.INFO,
  1449. output_file=None,
  1450. borg_local_path='borg',
  1451. working_directory=None,
  1452. )
  1453. module.create_archive(
  1454. dry_run=False,
  1455. repository='{fqdn}',
  1456. location_config={
  1457. 'source_directories': ['foo', 'bar'],
  1458. 'repositories': ['{fqdn}'],
  1459. 'exclude_patterns': None,
  1460. },
  1461. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
  1462. local_borg_version='1.2.3',
  1463. )
  1464. def test_create_archive_with_extra_borg_options_calls_borg_with_extra_options():
  1465. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1466. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1467. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1468. flexmock(module).should_receive('expand_directories').and_return(())
  1469. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1470. flexmock(module).should_receive('expand_home_directories').and_return(())
  1471. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1472. flexmock(module.feature).should_receive('available').and_return(True)
  1473. flexmock(module).should_receive('ensure_files_readable')
  1474. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1475. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1476. flexmock(module).should_receive('execute_command').with_args(
  1477. ('borg', 'create', '--extra', '--options') + ARCHIVE_WITH_PATHS,
  1478. output_log_level=logging.INFO,
  1479. output_file=None,
  1480. borg_local_path='borg',
  1481. working_directory=None,
  1482. )
  1483. module.create_archive(
  1484. dry_run=False,
  1485. repository='repo',
  1486. location_config={
  1487. 'source_directories': ['foo', 'bar'],
  1488. 'repositories': ['repo'],
  1489. 'exclude_patterns': None,
  1490. },
  1491. storage_config={'extra_borg_options': {'create': '--extra --options'}},
  1492. local_borg_version='1.2.3',
  1493. )
  1494. def test_create_archive_with_stream_processes_calls_borg_with_processes():
  1495. processes = flexmock()
  1496. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1497. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1498. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1499. flexmock(module).should_receive('expand_directories').and_return(())
  1500. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1501. flexmock(module).should_receive('expand_home_directories').and_return(())
  1502. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1503. flexmock(module.feature).should_receive('available').and_return(True)
  1504. flexmock(module).should_receive('ensure_files_readable')
  1505. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1506. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1507. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1508. ('borg', 'create', '--one-file-system', '--read-special') + ARCHIVE_WITH_PATHS,
  1509. processes=processes,
  1510. output_log_level=logging.INFO,
  1511. output_file=None,
  1512. borg_local_path='borg',
  1513. working_directory=None,
  1514. )
  1515. module.create_archive(
  1516. dry_run=False,
  1517. repository='repo',
  1518. location_config={
  1519. 'source_directories': ['foo', 'bar'],
  1520. 'repositories': ['repo'],
  1521. 'exclude_patterns': None,
  1522. },
  1523. storage_config={},
  1524. local_borg_version='1.2.3',
  1525. stream_processes=processes,
  1526. )