test_create.py 80 KB

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