test_create.py 92 KB

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