test_create.py 80 KB

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