test_create.py 89 KB

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