2
0

test_create.py 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010
  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.flags).should_receive('make_repository_archive_flags').and_return(
  191. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  192. )
  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_calls_borg_with_environment():
  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.flags).should_receive('make_repository_archive_flags').and_return(
  226. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  227. )
  228. environment = {'BORG_THINGY': 'YUP'}
  229. flexmock(module.environment).should_receive('make_environment').and_return(environment)
  230. flexmock(module).should_receive('execute_command').with_args(
  231. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS,
  232. output_log_level=logging.INFO,
  233. output_file=None,
  234. borg_local_path='borg',
  235. working_directory=None,
  236. extra_environment=environment,
  237. )
  238. module.create_archive(
  239. dry_run=False,
  240. repository='repo',
  241. location_config={
  242. 'source_directories': ['foo', 'bar'],
  243. 'repositories': ['repo'],
  244. 'exclude_patterns': None,
  245. },
  246. storage_config={},
  247. local_borg_version='1.2.3',
  248. )
  249. def test_create_archive_with_patterns_calls_borg_with_patterns():
  250. pattern_flags = ('--patterns-from', 'patterns')
  251. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  252. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  253. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  254. flexmock(module).should_receive('expand_directories').and_return(())
  255. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  256. flexmock(module).should_receive('expand_home_directories').and_return(())
  257. flexmock(module).should_receive('write_pattern_file').and_return(
  258. flexmock(name='/tmp/patterns')
  259. ).and_return(None)
  260. flexmock(module.feature).should_receive('available').and_return(True)
  261. flexmock(module).should_receive('ensure_files_readable')
  262. flexmock(module).should_receive('make_pattern_flags').and_return(pattern_flags)
  263. flexmock(module).should_receive('make_exclude_flags').and_return(())
  264. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  265. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  266. )
  267. flexmock(module.environment).should_receive('make_environment')
  268. flexmock(module).should_receive('execute_command').with_args(
  269. ('borg', 'create') + pattern_flags + REPO_ARCHIVE_WITH_PATHS,
  270. output_log_level=logging.INFO,
  271. output_file=None,
  272. borg_local_path='borg',
  273. working_directory=None,
  274. extra_environment=None,
  275. )
  276. module.create_archive(
  277. dry_run=False,
  278. repository='repo',
  279. location_config={
  280. 'source_directories': ['foo', 'bar'],
  281. 'repositories': ['repo'],
  282. 'patterns': ['pattern'],
  283. },
  284. storage_config={},
  285. local_borg_version='1.2.3',
  286. )
  287. def test_create_archive_with_exclude_patterns_calls_borg_with_excludes():
  288. exclude_flags = ('--exclude-from', 'excludes')
  289. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  290. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  291. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  292. flexmock(module).should_receive('expand_directories').and_return(())
  293. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  294. flexmock(module).should_receive('expand_home_directories').and_return(('exclude',))
  295. flexmock(module).should_receive('write_pattern_file').and_return(None).and_return(
  296. flexmock(name='/tmp/excludes')
  297. )
  298. flexmock(module.feature).should_receive('available').and_return(True)
  299. flexmock(module).should_receive('ensure_files_readable')
  300. flexmock(module).should_receive('make_pattern_flags').and_return(())
  301. flexmock(module).should_receive('make_exclude_flags').and_return(exclude_flags)
  302. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  303. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  304. )
  305. flexmock(module.environment).should_receive('make_environment')
  306. flexmock(module).should_receive('execute_command').with_args(
  307. ('borg', 'create') + exclude_flags + REPO_ARCHIVE_WITH_PATHS,
  308. output_log_level=logging.INFO,
  309. output_file=None,
  310. borg_local_path='borg',
  311. working_directory=None,
  312. extra_environment=None,
  313. )
  314. module.create_archive(
  315. dry_run=False,
  316. repository='repo',
  317. location_config={
  318. 'source_directories': ['foo', 'bar'],
  319. 'repositories': ['repo'],
  320. 'exclude_patterns': ['exclude'],
  321. },
  322. storage_config={},
  323. local_borg_version='1.2.3',
  324. )
  325. def test_create_archive_with_log_info_calls_borg_with_info_parameter():
  326. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  327. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  328. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  329. flexmock(module).should_receive('expand_directories').and_return(())
  330. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  331. flexmock(module).should_receive('expand_home_directories').and_return(())
  332. flexmock(module).should_receive('write_pattern_file').and_return(None)
  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(())
  337. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  338. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  339. )
  340. flexmock(module.environment).should_receive('make_environment')
  341. flexmock(module).should_receive('execute_command').with_args(
  342. ('borg', 'create', '--info') + 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. insert_logging_mock(logging.INFO)
  350. module.create_archive(
  351. dry_run=False,
  352. repository='repo',
  353. location_config={
  354. 'source_directories': ['foo', 'bar'],
  355. 'repositories': ['repo'],
  356. 'exclude_patterns': None,
  357. },
  358. storage_config={},
  359. local_borg_version='1.2.3',
  360. )
  361. def test_create_archive_with_log_info_and_json_suppresses_most_borg_output():
  362. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  363. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  364. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  365. flexmock(module).should_receive('expand_directories').and_return(())
  366. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  367. flexmock(module).should_receive('expand_home_directories').and_return(())
  368. flexmock(module).should_receive('write_pattern_file').and_return(None)
  369. flexmock(module.feature).should_receive('available').and_return(True)
  370. flexmock(module).should_receive('ensure_files_readable')
  371. flexmock(module).should_receive('make_pattern_flags').and_return(())
  372. flexmock(module).should_receive('make_exclude_flags').and_return(())
  373. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  374. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  375. )
  376. flexmock(module.environment).should_receive('make_environment')
  377. flexmock(module).should_receive('execute_command').with_args(
  378. ('borg', 'create', '--json') + REPO_ARCHIVE_WITH_PATHS,
  379. output_log_level=None,
  380. output_file=None,
  381. borg_local_path='borg',
  382. working_directory=None,
  383. extra_environment=None,
  384. )
  385. insert_logging_mock(logging.INFO)
  386. module.create_archive(
  387. dry_run=False,
  388. repository='repo',
  389. location_config={
  390. 'source_directories': ['foo', 'bar'],
  391. 'repositories': ['repo'],
  392. 'exclude_patterns': None,
  393. },
  394. storage_config={},
  395. local_borg_version='1.2.3',
  396. json=True,
  397. )
  398. def test_create_archive_with_log_debug_calls_borg_with_debug_parameter():
  399. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  400. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  401. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  402. flexmock(module).should_receive('expand_directories').and_return(())
  403. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  404. flexmock(module).should_receive('expand_home_directories').and_return(())
  405. flexmock(module).should_receive('write_pattern_file').and_return(None)
  406. flexmock(module.feature).should_receive('available').and_return(True)
  407. flexmock(module).should_receive('ensure_files_readable')
  408. flexmock(module).should_receive('make_pattern_flags').and_return(())
  409. flexmock(module).should_receive('make_exclude_flags').and_return(())
  410. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  411. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  412. )
  413. flexmock(module.environment).should_receive('make_environment')
  414. flexmock(module).should_receive('execute_command').with_args(
  415. ('borg', 'create', '--debug', '--show-rc') + REPO_ARCHIVE_WITH_PATHS,
  416. output_log_level=logging.INFO,
  417. output_file=None,
  418. borg_local_path='borg',
  419. working_directory=None,
  420. extra_environment=None,
  421. )
  422. insert_logging_mock(logging.DEBUG)
  423. module.create_archive(
  424. dry_run=False,
  425. repository='repo',
  426. location_config={
  427. 'source_directories': ['foo', 'bar'],
  428. 'repositories': ['repo'],
  429. 'exclude_patterns': None,
  430. },
  431. storage_config={},
  432. local_borg_version='1.2.3',
  433. )
  434. def test_create_archive_with_log_debug_and_json_suppresses_most_borg_output():
  435. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  436. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  437. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  438. flexmock(module).should_receive('expand_directories').and_return(())
  439. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  440. flexmock(module).should_receive('expand_home_directories').and_return(())
  441. flexmock(module).should_receive('write_pattern_file').and_return(None)
  442. flexmock(module.feature).should_receive('available').and_return(True)
  443. flexmock(module).should_receive('ensure_files_readable')
  444. flexmock(module).should_receive('make_pattern_flags').and_return(())
  445. flexmock(module).should_receive('make_exclude_flags').and_return(())
  446. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  447. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  448. )
  449. flexmock(module.environment).should_receive('make_environment')
  450. flexmock(module).should_receive('execute_command').with_args(
  451. ('borg', 'create', '--json') + REPO_ARCHIVE_WITH_PATHS,
  452. output_log_level=None,
  453. output_file=None,
  454. borg_local_path='borg',
  455. working_directory=None,
  456. extra_environment=None,
  457. )
  458. insert_logging_mock(logging.DEBUG)
  459. module.create_archive(
  460. dry_run=False,
  461. repository='repo',
  462. location_config={
  463. 'source_directories': ['foo', 'bar'],
  464. 'repositories': ['repo'],
  465. 'exclude_patterns': None,
  466. },
  467. storage_config={},
  468. local_borg_version='1.2.3',
  469. json=True,
  470. )
  471. def test_create_archive_with_dry_run_calls_borg_with_dry_run_parameter():
  472. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  473. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  474. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  475. flexmock(module).should_receive('expand_directories').and_return(())
  476. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  477. flexmock(module).should_receive('expand_home_directories').and_return(())
  478. flexmock(module).should_receive('write_pattern_file').and_return(None)
  479. flexmock(module.feature).should_receive('available').and_return(True)
  480. flexmock(module).should_receive('ensure_files_readable')
  481. flexmock(module).should_receive('make_pattern_flags').and_return(())
  482. flexmock(module).should_receive('make_exclude_flags').and_return(())
  483. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  484. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  485. )
  486. flexmock(module.environment).should_receive('make_environment')
  487. flexmock(module).should_receive('execute_command').with_args(
  488. ('borg', 'create', '--dry-run') + REPO_ARCHIVE_WITH_PATHS,
  489. output_log_level=logging.INFO,
  490. output_file=None,
  491. borg_local_path='borg',
  492. working_directory=None,
  493. extra_environment=None,
  494. )
  495. module.create_archive(
  496. dry_run=True,
  497. repository='repo',
  498. location_config={
  499. 'source_directories': ['foo', 'bar'],
  500. 'repositories': ['repo'],
  501. 'exclude_patterns': None,
  502. },
  503. storage_config={},
  504. local_borg_version='1.2.3',
  505. )
  506. def test_create_archive_with_stats_and_dry_run_calls_borg_without_stats_parameter():
  507. # --dry-run and --stats are mutually exclusive, see:
  508. # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
  509. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  510. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  511. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  512. flexmock(module).should_receive('expand_directories').and_return(())
  513. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  514. flexmock(module).should_receive('expand_home_directories').and_return(())
  515. flexmock(module).should_receive('write_pattern_file').and_return(None)
  516. flexmock(module.feature).should_receive('available').and_return(True)
  517. flexmock(module).should_receive('ensure_files_readable')
  518. flexmock(module).should_receive('make_pattern_flags').and_return(())
  519. flexmock(module).should_receive('make_exclude_flags').and_return(())
  520. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  521. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  522. )
  523. flexmock(module.environment).should_receive('make_environment')
  524. flexmock(module).should_receive('execute_command').with_args(
  525. ('borg', 'create', '--info', '--dry-run') + REPO_ARCHIVE_WITH_PATHS,
  526. output_log_level=logging.INFO,
  527. output_file=None,
  528. borg_local_path='borg',
  529. working_directory=None,
  530. extra_environment=None,
  531. )
  532. insert_logging_mock(logging.INFO)
  533. module.create_archive(
  534. dry_run=True,
  535. repository='repo',
  536. location_config={
  537. 'source_directories': ['foo', 'bar'],
  538. 'repositories': ['repo'],
  539. 'exclude_patterns': None,
  540. },
  541. storage_config={},
  542. local_borg_version='1.2.3',
  543. stats=True,
  544. )
  545. def test_create_archive_with_checkpoint_interval_calls_borg_with_checkpoint_interval_parameters():
  546. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  547. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  548. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  549. flexmock(module).should_receive('expand_directories').and_return(())
  550. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  551. flexmock(module).should_receive('expand_home_directories').and_return(())
  552. flexmock(module).should_receive('write_pattern_file').and_return(None)
  553. flexmock(module.feature).should_receive('available').and_return(True)
  554. flexmock(module).should_receive('ensure_files_readable')
  555. flexmock(module).should_receive('make_pattern_flags').and_return(())
  556. flexmock(module).should_receive('make_exclude_flags').and_return(())
  557. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  558. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  559. )
  560. flexmock(module.environment).should_receive('make_environment')
  561. flexmock(module).should_receive('execute_command').with_args(
  562. ('borg', 'create', '--checkpoint-interval', '600') + REPO_ARCHIVE_WITH_PATHS,
  563. output_log_level=logging.INFO,
  564. output_file=None,
  565. borg_local_path='borg',
  566. working_directory=None,
  567. extra_environment=None,
  568. )
  569. module.create_archive(
  570. dry_run=False,
  571. repository='repo',
  572. location_config={
  573. 'source_directories': ['foo', 'bar'],
  574. 'repositories': ['repo'],
  575. 'exclude_patterns': None,
  576. },
  577. storage_config={'checkpoint_interval': 600},
  578. local_borg_version='1.2.3',
  579. )
  580. def test_create_archive_with_chunker_params_calls_borg_with_chunker_params_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.flags).should_receive('make_repository_archive_flags').and_return(
  593. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  594. )
  595. flexmock(module.environment).should_receive('make_environment')
  596. flexmock(module).should_receive('execute_command').with_args(
  597. ('borg', 'create', '--chunker-params', '1,2,3,4') + 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={'chunker_params': '1,2,3,4'},
  613. local_borg_version='1.2.3',
  614. )
  615. def test_create_archive_with_compression_calls_borg_with_compression_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.flags).should_receive('make_repository_archive_flags').and_return(
  628. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  629. )
  630. flexmock(module.environment).should_receive('make_environment')
  631. flexmock(module).should_receive('execute_command').with_args(
  632. ('borg', 'create', '--compression', 'rle') + 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={'compression': 'rle'},
  648. local_borg_version='1.2.3',
  649. )
  650. @pytest.mark.parametrize(
  651. 'feature_available,option_flag', ((True, '--upload-ratelimit'), (False, '--remote-ratelimit')),
  652. )
  653. def test_create_archive_with_upload_rate_limit_calls_borg_with_upload_ratelimit_parameters(
  654. feature_available, option_flag
  655. ):
  656. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  657. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  658. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  659. flexmock(module).should_receive('expand_directories').and_return(())
  660. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  661. flexmock(module).should_receive('expand_home_directories').and_return(())
  662. flexmock(module).should_receive('write_pattern_file').and_return(None)
  663. flexmock(module.feature).should_receive('available').and_return(feature_available)
  664. flexmock(module).should_receive('ensure_files_readable')
  665. flexmock(module).should_receive('make_pattern_flags').and_return(())
  666. flexmock(module).should_receive('make_exclude_flags').and_return(())
  667. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  668. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  669. )
  670. flexmock(module.environment).should_receive('make_environment')
  671. flexmock(module).should_receive('execute_command').with_args(
  672. ('borg', 'create', option_flag, '100') + REPO_ARCHIVE_WITH_PATHS,
  673. output_log_level=logging.INFO,
  674. output_file=None,
  675. borg_local_path='borg',
  676. working_directory=None,
  677. extra_environment=None,
  678. )
  679. module.create_archive(
  680. dry_run=False,
  681. repository='repo',
  682. location_config={
  683. 'source_directories': ['foo', 'bar'],
  684. 'repositories': ['repo'],
  685. 'exclude_patterns': None,
  686. },
  687. storage_config={'upload_rate_limit': 100},
  688. local_borg_version='1.2.3',
  689. )
  690. def test_create_archive_with_working_directory_calls_borg_with_working_directory():
  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').with_args('/working/dir').and_return(
  696. '/working/dir'
  697. )
  698. flexmock(module).should_receive('expand_home_directories').and_return(())
  699. flexmock(module).should_receive('write_pattern_file').and_return(None)
  700. flexmock(module.feature).should_receive('available').and_return(True)
  701. flexmock(module).should_receive('ensure_files_readable')
  702. flexmock(module).should_receive('make_pattern_flags').and_return(())
  703. flexmock(module).should_receive('make_exclude_flags').and_return(())
  704. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  705. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  706. )
  707. flexmock(module.environment).should_receive('make_environment')
  708. flexmock(module).should_receive('execute_command').with_args(
  709. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS,
  710. output_log_level=logging.INFO,
  711. output_file=None,
  712. borg_local_path='borg',
  713. working_directory='/working/dir',
  714. extra_environment=None,
  715. )
  716. module.create_archive(
  717. dry_run=False,
  718. repository='repo',
  719. location_config={
  720. 'source_directories': ['foo', 'bar'],
  721. 'repositories': ['repo'],
  722. 'working_directory': '/working/dir',
  723. 'exclude_patterns': None,
  724. },
  725. storage_config={},
  726. local_borg_version='1.2.3',
  727. )
  728. def test_create_archive_with_one_file_system_calls_borg_with_one_file_system_parameter():
  729. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  730. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  731. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  732. flexmock(module).should_receive('expand_directories').and_return(())
  733. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  734. flexmock(module).should_receive('expand_home_directories').and_return(())
  735. flexmock(module).should_receive('write_pattern_file').and_return(None)
  736. flexmock(module.feature).should_receive('available').and_return(True)
  737. flexmock(module).should_receive('ensure_files_readable')
  738. flexmock(module).should_receive('make_pattern_flags').and_return(())
  739. flexmock(module).should_receive('make_exclude_flags').and_return(())
  740. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  741. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  742. )
  743. flexmock(module.environment).should_receive('make_environment')
  744. flexmock(module).should_receive('execute_command').with_args(
  745. ('borg', 'create', '--one-file-system') + REPO_ARCHIVE_WITH_PATHS,
  746. output_log_level=logging.INFO,
  747. output_file=None,
  748. borg_local_path='borg',
  749. working_directory=None,
  750. extra_environment=None,
  751. )
  752. module.create_archive(
  753. dry_run=False,
  754. repository='repo',
  755. location_config={
  756. 'source_directories': ['foo', 'bar'],
  757. 'repositories': ['repo'],
  758. 'one_file_system': True,
  759. 'exclude_patterns': None,
  760. },
  761. storage_config={},
  762. local_borg_version='1.2.3',
  763. )
  764. @pytest.mark.parametrize(
  765. 'feature_available,option_flag', ((True, '--numeric-ids'), (False, '--numeric-owner')),
  766. )
  767. def test_create_archive_with_numeric_ids_calls_borg_with_numeric_ids_parameter(
  768. feature_available, option_flag
  769. ):
  770. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  771. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  772. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  773. flexmock(module).should_receive('expand_directories').and_return(())
  774. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  775. flexmock(module).should_receive('expand_home_directories').and_return(())
  776. flexmock(module).should_receive('write_pattern_file').and_return(None)
  777. flexmock(module.feature).should_receive('available').and_return(feature_available)
  778. flexmock(module).should_receive('ensure_files_readable')
  779. flexmock(module).should_receive('make_pattern_flags').and_return(())
  780. flexmock(module).should_receive('make_exclude_flags').and_return(())
  781. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  782. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  783. )
  784. flexmock(module.environment).should_receive('make_environment')
  785. flexmock(module).should_receive('execute_command').with_args(
  786. ('borg', 'create', option_flag) + REPO_ARCHIVE_WITH_PATHS,
  787. output_log_level=logging.INFO,
  788. output_file=None,
  789. borg_local_path='borg',
  790. working_directory=None,
  791. extra_environment=None,
  792. )
  793. module.create_archive(
  794. dry_run=False,
  795. repository='repo',
  796. location_config={
  797. 'source_directories': ['foo', 'bar'],
  798. 'repositories': ['repo'],
  799. 'numeric_ids': True,
  800. 'exclude_patterns': None,
  801. },
  802. storage_config={},
  803. local_borg_version='1.2.3',
  804. )
  805. def test_create_archive_with_read_special_calls_borg_with_read_special_parameter():
  806. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  807. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  808. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  809. flexmock(module).should_receive('expand_directories').and_return(())
  810. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  811. flexmock(module).should_receive('expand_home_directories').and_return(())
  812. flexmock(module).should_receive('write_pattern_file').and_return(None)
  813. flexmock(module.feature).should_receive('available').and_return(True)
  814. flexmock(module).should_receive('ensure_files_readable')
  815. flexmock(module).should_receive('make_pattern_flags').and_return(())
  816. flexmock(module).should_receive('make_exclude_flags').and_return(())
  817. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  818. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  819. )
  820. flexmock(module.environment).should_receive('make_environment')
  821. flexmock(module).should_receive('execute_command').with_args(
  822. ('borg', 'create', '--read-special') + REPO_ARCHIVE_WITH_PATHS,
  823. output_log_level=logging.INFO,
  824. output_file=None,
  825. borg_local_path='borg',
  826. working_directory=None,
  827. extra_environment=None,
  828. )
  829. module.create_archive(
  830. dry_run=False,
  831. repository='repo',
  832. location_config={
  833. 'source_directories': ['foo', 'bar'],
  834. 'repositories': ['repo'],
  835. 'read_special': True,
  836. 'exclude_patterns': None,
  837. },
  838. storage_config={},
  839. local_borg_version='1.2.3',
  840. )
  841. @pytest.mark.parametrize(
  842. 'option_name,option_value',
  843. (('ctime', True), ('ctime', False), ('birthtime', True), ('birthtime', False),),
  844. )
  845. def test_create_archive_with_basic_option_calls_borg_with_corresponding_parameter(
  846. option_name, option_value
  847. ):
  848. option_flag = '--no' + option_name.replace('', '') if option_value is False else None
  849. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  850. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  851. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  852. flexmock(module).should_receive('expand_directories').and_return(())
  853. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  854. flexmock(module).should_receive('expand_home_directories').and_return(())
  855. flexmock(module).should_receive('write_pattern_file').and_return(None)
  856. flexmock(module.feature).should_receive('available').and_return(True)
  857. flexmock(module).should_receive('ensure_files_readable')
  858. flexmock(module).should_receive('make_pattern_flags').and_return(())
  859. flexmock(module).should_receive('make_exclude_flags').and_return(())
  860. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  861. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  862. )
  863. flexmock(module.environment).should_receive('make_environment')
  864. flexmock(module).should_receive('execute_command').with_args(
  865. ('borg', 'create') + ((option_flag,) if option_flag else ()) + REPO_ARCHIVE_WITH_PATHS,
  866. output_log_level=logging.INFO,
  867. output_file=None,
  868. borg_local_path='borg',
  869. working_directory=None,
  870. extra_environment=None,
  871. )
  872. module.create_archive(
  873. dry_run=False,
  874. repository='repo',
  875. location_config={
  876. 'source_directories': ['foo', 'bar'],
  877. 'repositories': ['repo'],
  878. option_name: option_value,
  879. 'exclude_patterns': None,
  880. },
  881. storage_config={},
  882. local_borg_version='1.2.3',
  883. )
  884. @pytest.mark.parametrize(
  885. 'option_value,feature_available,option_flag',
  886. (
  887. (True, True, '--atime'),
  888. (True, False, None),
  889. (False, True, None),
  890. (False, False, '--noatime'),
  891. ),
  892. )
  893. def test_create_archive_with_atime_option_calls_borg_with_corresponding_parameter(
  894. option_value, feature_available, option_flag
  895. ):
  896. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  897. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  898. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  899. flexmock(module).should_receive('expand_directories').and_return(())
  900. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  901. flexmock(module).should_receive('expand_home_directories').and_return(())
  902. flexmock(module).should_receive('write_pattern_file').and_return(None)
  903. flexmock(module.feature).should_receive('available').and_return(feature_available)
  904. flexmock(module).should_receive('ensure_files_readable')
  905. flexmock(module).should_receive('make_pattern_flags').and_return(())
  906. flexmock(module).should_receive('make_exclude_flags').and_return(())
  907. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  908. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  909. )
  910. flexmock(module.environment).should_receive('make_environment')
  911. flexmock(module).should_receive('execute_command').with_args(
  912. ('borg', 'create') + ((option_flag,) if option_flag else ()) + REPO_ARCHIVE_WITH_PATHS,
  913. output_log_level=logging.INFO,
  914. output_file=None,
  915. borg_local_path='borg',
  916. working_directory=None,
  917. extra_environment=None,
  918. )
  919. module.create_archive(
  920. dry_run=False,
  921. repository='repo',
  922. location_config={
  923. 'source_directories': ['foo', 'bar'],
  924. 'repositories': ['repo'],
  925. 'atime': option_value,
  926. 'exclude_patterns': None,
  927. },
  928. storage_config={},
  929. local_borg_version='1.2.3',
  930. )
  931. @pytest.mark.parametrize(
  932. 'option_value,feature_available,option_flag',
  933. (
  934. (True, True, None),
  935. (True, False, None),
  936. (False, True, '--noflags'),
  937. (False, False, '--nobsdflags'),
  938. ),
  939. )
  940. def test_create_archive_with_flags_option_calls_borg_with_corresponding_parameter(
  941. option_value, feature_available, option_flag
  942. ):
  943. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  944. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  945. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  946. flexmock(module).should_receive('expand_directories').and_return(())
  947. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  948. flexmock(module).should_receive('expand_home_directories').and_return(())
  949. flexmock(module).should_receive('write_pattern_file').and_return(None)
  950. flexmock(module.feature).should_receive('available').and_return(feature_available)
  951. flexmock(module).should_receive('ensure_files_readable')
  952. flexmock(module).should_receive('make_pattern_flags').and_return(())
  953. flexmock(module).should_receive('make_exclude_flags').and_return(())
  954. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  955. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  956. )
  957. flexmock(module.environment).should_receive('make_environment')
  958. flexmock(module).should_receive('execute_command').with_args(
  959. ('borg', 'create') + ((option_flag,) if option_flag else ()) + REPO_ARCHIVE_WITH_PATHS,
  960. output_log_level=logging.INFO,
  961. output_file=None,
  962. borg_local_path='borg',
  963. working_directory=None,
  964. extra_environment=None,
  965. )
  966. module.create_archive(
  967. dry_run=False,
  968. repository='repo',
  969. location_config={
  970. 'source_directories': ['foo', 'bar'],
  971. 'repositories': ['repo'],
  972. 'flags': option_value,
  973. 'exclude_patterns': None,
  974. },
  975. storage_config={},
  976. local_borg_version='1.2.3',
  977. )
  978. def test_create_archive_with_files_cache_calls_borg_with_files_cache_parameters():
  979. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  980. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  981. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  982. flexmock(module).should_receive('expand_directories').and_return(())
  983. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  984. flexmock(module).should_receive('expand_home_directories').and_return(())
  985. flexmock(module).should_receive('write_pattern_file').and_return(None)
  986. flexmock(module.feature).should_receive('available').and_return(True)
  987. flexmock(module).should_receive('ensure_files_readable')
  988. flexmock(module).should_receive('make_pattern_flags').and_return(())
  989. flexmock(module).should_receive('make_exclude_flags').and_return(())
  990. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  991. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  992. )
  993. flexmock(module.environment).should_receive('make_environment')
  994. flexmock(module).should_receive('execute_command').with_args(
  995. ('borg', 'create', '--files-cache', 'ctime,size') + REPO_ARCHIVE_WITH_PATHS,
  996. output_log_level=logging.INFO,
  997. output_file=None,
  998. borg_local_path='borg',
  999. working_directory=None,
  1000. extra_environment=None,
  1001. )
  1002. module.create_archive(
  1003. dry_run=False,
  1004. repository='repo',
  1005. location_config={
  1006. 'source_directories': ['foo', 'bar'],
  1007. 'repositories': ['repo'],
  1008. 'files_cache': 'ctime,size',
  1009. 'exclude_patterns': None,
  1010. },
  1011. storage_config={},
  1012. local_borg_version='1.2.3',
  1013. )
  1014. def test_create_archive_with_local_path_calls_borg_via_local_path():
  1015. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1016. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1017. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1018. flexmock(module).should_receive('expand_directories').and_return(())
  1019. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1020. flexmock(module).should_receive('expand_home_directories').and_return(())
  1021. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1022. flexmock(module.feature).should_receive('available').and_return(True)
  1023. flexmock(module).should_receive('ensure_files_readable')
  1024. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1025. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1026. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1027. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1028. )
  1029. flexmock(module.environment).should_receive('make_environment')
  1030. flexmock(module).should_receive('execute_command').with_args(
  1031. ('borg1', 'create') + REPO_ARCHIVE_WITH_PATHS,
  1032. output_log_level=logging.INFO,
  1033. output_file=None,
  1034. borg_local_path='borg1',
  1035. working_directory=None,
  1036. extra_environment=None,
  1037. )
  1038. module.create_archive(
  1039. dry_run=False,
  1040. repository='repo',
  1041. location_config={
  1042. 'source_directories': ['foo', 'bar'],
  1043. 'repositories': ['repo'],
  1044. 'exclude_patterns': None,
  1045. },
  1046. storage_config={},
  1047. local_borg_version='1.2.3',
  1048. local_path='borg1',
  1049. )
  1050. def test_create_archive_with_remote_path_calls_borg_with_remote_path_parameters():
  1051. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1052. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1053. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1054. flexmock(module).should_receive('expand_directories').and_return(())
  1055. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1056. flexmock(module).should_receive('expand_home_directories').and_return(())
  1057. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1058. flexmock(module.feature).should_receive('available').and_return(True)
  1059. flexmock(module).should_receive('ensure_files_readable')
  1060. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1061. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1062. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1063. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1064. )
  1065. flexmock(module.environment).should_receive('make_environment')
  1066. flexmock(module).should_receive('execute_command').with_args(
  1067. ('borg', 'create', '--remote-path', 'borg1') + REPO_ARCHIVE_WITH_PATHS,
  1068. output_log_level=logging.INFO,
  1069. output_file=None,
  1070. borg_local_path='borg',
  1071. working_directory=None,
  1072. extra_environment=None,
  1073. )
  1074. module.create_archive(
  1075. dry_run=False,
  1076. repository='repo',
  1077. location_config={
  1078. 'source_directories': ['foo', 'bar'],
  1079. 'repositories': ['repo'],
  1080. 'exclude_patterns': None,
  1081. },
  1082. storage_config={},
  1083. local_borg_version='1.2.3',
  1084. remote_path='borg1',
  1085. )
  1086. def test_create_archive_with_umask_calls_borg_with_umask_parameters():
  1087. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1088. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1089. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1090. flexmock(module).should_receive('expand_directories').and_return(())
  1091. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1092. flexmock(module).should_receive('expand_home_directories').and_return(())
  1093. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1094. flexmock(module.feature).should_receive('available').and_return(True)
  1095. flexmock(module).should_receive('ensure_files_readable')
  1096. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1097. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1098. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1099. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1100. )
  1101. flexmock(module.environment).should_receive('make_environment')
  1102. flexmock(module).should_receive('execute_command').with_args(
  1103. ('borg', 'create', '--umask', '740') + REPO_ARCHIVE_WITH_PATHS,
  1104. output_log_level=logging.INFO,
  1105. output_file=None,
  1106. borg_local_path='borg',
  1107. working_directory=None,
  1108. extra_environment=None,
  1109. )
  1110. module.create_archive(
  1111. dry_run=False,
  1112. repository='repo',
  1113. location_config={
  1114. 'source_directories': ['foo', 'bar'],
  1115. 'repositories': ['repo'],
  1116. 'exclude_patterns': None,
  1117. },
  1118. storage_config={'umask': 740},
  1119. local_borg_version='1.2.3',
  1120. )
  1121. def test_create_archive_with_lock_wait_calls_borg_with_lock_wait_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.flags).should_receive('make_repository_archive_flags').and_return(
  1134. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1135. )
  1136. flexmock(module.environment).should_receive('make_environment')
  1137. flexmock(module).should_receive('execute_command').with_args(
  1138. ('borg', 'create', '--lock-wait', '5') + 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={'lock_wait': 5},
  1154. local_borg_version='1.2.3',
  1155. )
  1156. def test_create_archive_with_stats_calls_borg_with_stats_parameter_and_warning_output_log_level():
  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.flags).should_receive('make_repository_archive_flags').and_return(
  1169. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1170. )
  1171. flexmock(module.environment).should_receive('make_environment')
  1172. flexmock(module).should_receive('execute_command').with_args(
  1173. ('borg', 'create', '--stats') + REPO_ARCHIVE_WITH_PATHS,
  1174. output_log_level=logging.WARNING,
  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={},
  1189. local_borg_version='1.2.3',
  1190. stats=True,
  1191. )
  1192. def test_create_archive_with_stats_and_log_info_calls_borg_with_stats_parameter_and_info_output_log_level():
  1193. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1194. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1195. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1196. flexmock(module).should_receive('expand_directories').and_return(())
  1197. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1198. flexmock(module).should_receive('expand_home_directories').and_return(())
  1199. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1200. flexmock(module.feature).should_receive('available').and_return(True)
  1201. flexmock(module).should_receive('ensure_files_readable')
  1202. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1203. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1204. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1205. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1206. )
  1207. flexmock(module.environment).should_receive('make_environment')
  1208. flexmock(module).should_receive('execute_command').with_args(
  1209. ('borg', 'create', '--info', '--stats') + REPO_ARCHIVE_WITH_PATHS,
  1210. output_log_level=logging.INFO,
  1211. output_file=None,
  1212. borg_local_path='borg',
  1213. working_directory=None,
  1214. extra_environment=None,
  1215. )
  1216. insert_logging_mock(logging.INFO)
  1217. module.create_archive(
  1218. dry_run=False,
  1219. repository='repo',
  1220. location_config={
  1221. 'source_directories': ['foo', 'bar'],
  1222. 'repositories': ['repo'],
  1223. 'exclude_patterns': None,
  1224. },
  1225. storage_config={},
  1226. local_borg_version='1.2.3',
  1227. stats=True,
  1228. )
  1229. def test_create_archive_with_files_calls_borg_with_list_parameter_and_warning_output_log_level():
  1230. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1231. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1232. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1233. flexmock(module).should_receive('expand_directories').and_return(())
  1234. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1235. flexmock(module).should_receive('expand_home_directories').and_return(())
  1236. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1237. flexmock(module.feature).should_receive('available').and_return(True)
  1238. flexmock(module).should_receive('ensure_files_readable')
  1239. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1240. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1241. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1242. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1243. )
  1244. flexmock(module.environment).should_receive('make_environment')
  1245. flexmock(module).should_receive('execute_command').with_args(
  1246. ('borg', 'create', '--list', '--filter', 'AME-') + REPO_ARCHIVE_WITH_PATHS,
  1247. output_log_level=logging.WARNING,
  1248. output_file=None,
  1249. borg_local_path='borg',
  1250. working_directory=None,
  1251. extra_environment=None,
  1252. )
  1253. module.create_archive(
  1254. dry_run=False,
  1255. repository='repo',
  1256. location_config={
  1257. 'source_directories': ['foo', 'bar'],
  1258. 'repositories': ['repo'],
  1259. 'exclude_patterns': None,
  1260. },
  1261. storage_config={},
  1262. local_borg_version='1.2.3',
  1263. list_files=True,
  1264. )
  1265. def test_create_archive_with_files_and_log_info_calls_borg_with_list_parameter_and_info_output_log_level():
  1266. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1267. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1268. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1269. flexmock(module).should_receive('expand_directories').and_return(())
  1270. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1271. flexmock(module).should_receive('expand_home_directories').and_return(())
  1272. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1273. flexmock(module.feature).should_receive('available').and_return(True)
  1274. flexmock(module).should_receive('ensure_files_readable')
  1275. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1276. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1277. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1278. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1279. )
  1280. flexmock(module.environment).should_receive('make_environment')
  1281. flexmock(module).should_receive('execute_command').with_args(
  1282. ('borg', 'create', '--list', '--filter', 'AME-', '--info') + REPO_ARCHIVE_WITH_PATHS,
  1283. output_log_level=logging.INFO,
  1284. output_file=None,
  1285. borg_local_path='borg',
  1286. working_directory=None,
  1287. extra_environment=None,
  1288. )
  1289. insert_logging_mock(logging.INFO)
  1290. module.create_archive(
  1291. dry_run=False,
  1292. repository='repo',
  1293. location_config={
  1294. 'source_directories': ['foo', 'bar'],
  1295. 'repositories': ['repo'],
  1296. 'exclude_patterns': None,
  1297. },
  1298. storage_config={},
  1299. local_borg_version='1.2.3',
  1300. list_files=True,
  1301. )
  1302. def test_create_archive_with_progress_and_log_info_calls_borg_with_progress_parameter_and_no_list():
  1303. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1304. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1305. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1306. flexmock(module).should_receive('expand_directories').and_return(())
  1307. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1308. flexmock(module).should_receive('expand_home_directories').and_return(())
  1309. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1310. flexmock(module.feature).should_receive('available').and_return(True)
  1311. flexmock(module).should_receive('ensure_files_readable')
  1312. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1313. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1314. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1315. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1316. )
  1317. flexmock(module.environment).should_receive('make_environment')
  1318. flexmock(module).should_receive('execute_command').with_args(
  1319. ('borg', 'create', '--info', '--progress') + REPO_ARCHIVE_WITH_PATHS,
  1320. output_log_level=logging.INFO,
  1321. output_file=module.DO_NOT_CAPTURE,
  1322. borg_local_path='borg',
  1323. working_directory=None,
  1324. extra_environment=None,
  1325. )
  1326. insert_logging_mock(logging.INFO)
  1327. module.create_archive(
  1328. dry_run=False,
  1329. repository='repo',
  1330. location_config={
  1331. 'source_directories': ['foo', 'bar'],
  1332. 'repositories': ['repo'],
  1333. 'exclude_patterns': None,
  1334. },
  1335. storage_config={},
  1336. local_borg_version='1.2.3',
  1337. progress=True,
  1338. )
  1339. def test_create_archive_with_progress_calls_borg_with_progress_parameter():
  1340. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1341. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1342. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1343. flexmock(module).should_receive('expand_directories').and_return(())
  1344. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1345. flexmock(module).should_receive('expand_home_directories').and_return(())
  1346. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1347. flexmock(module.feature).should_receive('available').and_return(True)
  1348. flexmock(module).should_receive('ensure_files_readable')
  1349. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1350. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1351. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1352. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1353. )
  1354. flexmock(module.environment).should_receive('make_environment')
  1355. flexmock(module).should_receive('execute_command').with_args(
  1356. ('borg', 'create', '--progress') + REPO_ARCHIVE_WITH_PATHS,
  1357. output_log_level=logging.INFO,
  1358. output_file=module.DO_NOT_CAPTURE,
  1359. borg_local_path='borg',
  1360. working_directory=None,
  1361. extra_environment=None,
  1362. )
  1363. module.create_archive(
  1364. dry_run=False,
  1365. repository='repo',
  1366. location_config={
  1367. 'source_directories': ['foo', 'bar'],
  1368. 'repositories': ['repo'],
  1369. 'exclude_patterns': None,
  1370. },
  1371. storage_config={},
  1372. local_borg_version='1.2.3',
  1373. progress=True,
  1374. )
  1375. def test_create_archive_with_progress_and_stream_processes_calls_borg_with_progress_parameter():
  1376. processes = flexmock()
  1377. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1378. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1379. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1380. flexmock(module).should_receive('expand_directories').and_return(())
  1381. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1382. flexmock(module).should_receive('expand_home_directories').and_return(())
  1383. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1384. flexmock(module.feature).should_receive('available').and_return(True)
  1385. flexmock(module).should_receive('ensure_files_readable')
  1386. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1387. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1388. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1389. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1390. )
  1391. flexmock(module.environment).should_receive('make_environment')
  1392. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1393. ('borg', 'create', '--one-file-system', '--read-special', '--progress')
  1394. + REPO_ARCHIVE_WITH_PATHS,
  1395. processes=processes,
  1396. output_log_level=logging.INFO,
  1397. output_file=module.DO_NOT_CAPTURE,
  1398. borg_local_path='borg',
  1399. working_directory=None,
  1400. extra_environment=None,
  1401. )
  1402. module.create_archive(
  1403. dry_run=False,
  1404. repository='repo',
  1405. location_config={
  1406. 'source_directories': ['foo', 'bar'],
  1407. 'repositories': ['repo'],
  1408. 'exclude_patterns': None,
  1409. },
  1410. storage_config={},
  1411. local_borg_version='1.2.3',
  1412. progress=True,
  1413. stream_processes=processes,
  1414. )
  1415. def test_create_archive_with_json_calls_borg_with_json_parameter():
  1416. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1417. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1418. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1419. flexmock(module).should_receive('expand_directories').and_return(())
  1420. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1421. flexmock(module).should_receive('expand_home_directories').and_return(())
  1422. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1423. flexmock(module.feature).should_receive('available').and_return(True)
  1424. flexmock(module).should_receive('ensure_files_readable')
  1425. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1426. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1427. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1428. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1429. )
  1430. flexmock(module.environment).should_receive('make_environment')
  1431. flexmock(module).should_receive('execute_command').with_args(
  1432. ('borg', 'create', '--json') + REPO_ARCHIVE_WITH_PATHS,
  1433. output_log_level=None,
  1434. output_file=None,
  1435. borg_local_path='borg',
  1436. working_directory=None,
  1437. extra_environment=None,
  1438. ).and_return('[]')
  1439. json_output = module.create_archive(
  1440. dry_run=False,
  1441. repository='repo',
  1442. location_config={
  1443. 'source_directories': ['foo', 'bar'],
  1444. 'repositories': ['repo'],
  1445. 'exclude_patterns': None,
  1446. },
  1447. storage_config={},
  1448. local_borg_version='1.2.3',
  1449. json=True,
  1450. )
  1451. assert json_output == '[]'
  1452. def test_create_archive_with_stats_and_json_calls_borg_without_stats_parameter():
  1453. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1454. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1455. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1456. flexmock(module).should_receive('expand_directories').and_return(())
  1457. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1458. flexmock(module).should_receive('expand_home_directories').and_return(())
  1459. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1460. flexmock(module.feature).should_receive('available').and_return(True)
  1461. flexmock(module).should_receive('ensure_files_readable')
  1462. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1463. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1464. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1465. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1466. )
  1467. flexmock(module.environment).should_receive('make_environment')
  1468. flexmock(module).should_receive('execute_command').with_args(
  1469. ('borg', 'create', '--json') + REPO_ARCHIVE_WITH_PATHS,
  1470. output_log_level=None,
  1471. output_file=None,
  1472. borg_local_path='borg',
  1473. working_directory=None,
  1474. extra_environment=None,
  1475. ).and_return('[]')
  1476. json_output = module.create_archive(
  1477. dry_run=False,
  1478. repository='repo',
  1479. location_config={
  1480. 'source_directories': ['foo', 'bar'],
  1481. 'repositories': ['repo'],
  1482. 'exclude_patterns': None,
  1483. },
  1484. storage_config={},
  1485. local_borg_version='1.2.3',
  1486. json=True,
  1487. stats=True,
  1488. )
  1489. assert json_output == '[]'
  1490. def test_create_archive_with_source_directories_glob_expands():
  1491. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1492. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  1493. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1494. flexmock(module).should_receive('expand_directories').and_return(())
  1495. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1496. flexmock(module).should_receive('expand_home_directories').and_return(())
  1497. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1498. flexmock(module.feature).should_receive('available').and_return(True)
  1499. flexmock(module).should_receive('ensure_files_readable')
  1500. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1501. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1502. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1503. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1504. )
  1505. flexmock(module.environment).should_receive('make_environment')
  1506. flexmock(module).should_receive('execute_command').with_args(
  1507. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  1508. output_log_level=logging.INFO,
  1509. output_file=None,
  1510. borg_local_path='borg',
  1511. working_directory=None,
  1512. extra_environment=None,
  1513. )
  1514. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return(['foo', 'food'])
  1515. module.create_archive(
  1516. dry_run=False,
  1517. repository='repo',
  1518. location_config={
  1519. 'source_directories': ['foo*'],
  1520. 'repositories': ['repo'],
  1521. 'exclude_patterns': None,
  1522. },
  1523. storage_config={},
  1524. local_borg_version='1.2.3',
  1525. )
  1526. def test_create_archive_with_non_matching_source_directories_glob_passes_through():
  1527. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1528. flexmock(module).should_receive('deduplicate_directories').and_return(('foo*',))
  1529. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1530. flexmock(module).should_receive('expand_directories').and_return(())
  1531. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1532. flexmock(module).should_receive('expand_home_directories').and_return(())
  1533. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1534. flexmock(module.feature).should_receive('available').and_return(True)
  1535. flexmock(module).should_receive('ensure_files_readable')
  1536. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1537. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1538. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1539. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1540. )
  1541. flexmock(module.environment).should_receive('make_environment')
  1542. flexmock(module).should_receive('execute_command').with_args(
  1543. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo*'),
  1544. output_log_level=logging.INFO,
  1545. output_file=None,
  1546. borg_local_path='borg',
  1547. working_directory=None,
  1548. extra_environment=None,
  1549. )
  1550. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return([])
  1551. module.create_archive(
  1552. dry_run=False,
  1553. repository='repo',
  1554. location_config={
  1555. 'source_directories': ['foo*'],
  1556. 'repositories': ['repo'],
  1557. 'exclude_patterns': None,
  1558. },
  1559. storage_config={},
  1560. local_borg_version='1.2.3',
  1561. )
  1562. def test_create_archive_with_glob_calls_borg_with_expanded_directories():
  1563. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1564. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  1565. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1566. flexmock(module).should_receive('expand_directories').and_return(())
  1567. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1568. flexmock(module).should_receive('expand_home_directories').and_return(())
  1569. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1570. flexmock(module.feature).should_receive('available').and_return(True)
  1571. flexmock(module).should_receive('ensure_files_readable')
  1572. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1573. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1574. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1575. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1576. )
  1577. flexmock(module.environment).should_receive('make_environment')
  1578. flexmock(module).should_receive('execute_command').with_args(
  1579. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  1580. output_log_level=logging.INFO,
  1581. output_file=None,
  1582. borg_local_path='borg',
  1583. working_directory=None,
  1584. extra_environment=None,
  1585. )
  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_archive_name_format_calls_borg_with_archive_name():
  1598. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1599. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  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.flags).should_receive('make_repository_archive_flags').and_return(
  1610. ('repo::ARCHIVE_NAME',)
  1611. )
  1612. flexmock(module.environment).should_receive('make_environment')
  1613. flexmock(module).should_receive('execute_command').with_args(
  1614. ('borg', 'create', 'repo::ARCHIVE_NAME', 'foo', 'bar'),
  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', 'bar'],
  1626. 'repositories': ['repo'],
  1627. 'exclude_patterns': None,
  1628. },
  1629. storage_config={'archive_name_format': 'ARCHIVE_NAME'},
  1630. local_borg_version='1.2.3',
  1631. )
  1632. def test_create_archive_with_archive_name_format_accepts_borg_placeholders():
  1633. repository_archive_pattern = 'repo::Documents_{hostname}-{now}'
  1634. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1635. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1636. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1637. flexmock(module).should_receive('expand_directories').and_return(())
  1638. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1639. flexmock(module).should_receive('expand_home_directories').and_return(())
  1640. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1641. flexmock(module.feature).should_receive('available').and_return(True)
  1642. flexmock(module).should_receive('ensure_files_readable')
  1643. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1644. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1645. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1646. (repository_archive_pattern,)
  1647. )
  1648. flexmock(module.environment).should_receive('make_environment')
  1649. flexmock(module).should_receive('execute_command').with_args(
  1650. ('borg', 'create', repository_archive_pattern, 'foo', 'bar'),
  1651. output_log_level=logging.INFO,
  1652. output_file=None,
  1653. borg_local_path='borg',
  1654. working_directory=None,
  1655. extra_environment=None,
  1656. )
  1657. module.create_archive(
  1658. dry_run=False,
  1659. repository='repo',
  1660. location_config={
  1661. 'source_directories': ['foo', 'bar'],
  1662. 'repositories': ['repo'],
  1663. 'exclude_patterns': None,
  1664. },
  1665. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
  1666. local_borg_version='1.2.3',
  1667. )
  1668. def test_create_archive_with_repository_accepts_borg_placeholders():
  1669. repository_archive_pattern = '{fqdn}::Documents_{hostname}-{now}'
  1670. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1671. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1672. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1673. flexmock(module).should_receive('expand_directories').and_return(())
  1674. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1675. flexmock(module).should_receive('expand_home_directories').and_return(())
  1676. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1677. flexmock(module.feature).should_receive('available').and_return(True)
  1678. flexmock(module).should_receive('ensure_files_readable')
  1679. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1680. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1681. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1682. (repository_archive_pattern,)
  1683. )
  1684. flexmock(module.environment).should_receive('make_environment')
  1685. flexmock(module).should_receive('execute_command').with_args(
  1686. ('borg', 'create', repository_archive_pattern, 'foo', 'bar'),
  1687. output_log_level=logging.INFO,
  1688. output_file=None,
  1689. borg_local_path='borg',
  1690. working_directory=None,
  1691. extra_environment=None,
  1692. )
  1693. module.create_archive(
  1694. dry_run=False,
  1695. repository='{fqdn}',
  1696. location_config={
  1697. 'source_directories': ['foo', 'bar'],
  1698. 'repositories': ['{fqdn}'],
  1699. 'exclude_patterns': None,
  1700. },
  1701. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
  1702. local_borg_version='1.2.3',
  1703. )
  1704. def test_create_archive_with_extra_borg_options_calls_borg_with_extra_options():
  1705. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1706. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1707. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1708. flexmock(module).should_receive('expand_directories').and_return(())
  1709. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1710. flexmock(module).should_receive('expand_home_directories').and_return(())
  1711. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1712. flexmock(module.feature).should_receive('available').and_return(True)
  1713. flexmock(module).should_receive('ensure_files_readable')
  1714. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1715. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1716. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1717. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1718. )
  1719. flexmock(module.environment).should_receive('make_environment')
  1720. flexmock(module).should_receive('execute_command').with_args(
  1721. ('borg', 'create', '--extra', '--options') + REPO_ARCHIVE_WITH_PATHS,
  1722. output_log_level=logging.INFO,
  1723. output_file=None,
  1724. borg_local_path='borg',
  1725. working_directory=None,
  1726. extra_environment=None,
  1727. )
  1728. module.create_archive(
  1729. dry_run=False,
  1730. repository='repo',
  1731. location_config={
  1732. 'source_directories': ['foo', 'bar'],
  1733. 'repositories': ['repo'],
  1734. 'exclude_patterns': None,
  1735. },
  1736. storage_config={'extra_borg_options': {'create': '--extra --options'}},
  1737. local_borg_version='1.2.3',
  1738. )
  1739. def test_create_archive_with_stream_processes_calls_borg_with_processes():
  1740. processes = flexmock()
  1741. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1742. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1743. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1744. flexmock(module).should_receive('expand_directories').and_return(())
  1745. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1746. flexmock(module).should_receive('expand_home_directories').and_return(())
  1747. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1748. flexmock(module.feature).should_receive('available').and_return(True)
  1749. flexmock(module).should_receive('ensure_files_readable')
  1750. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1751. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1752. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1753. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1754. )
  1755. flexmock(module.environment).should_receive('make_environment')
  1756. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1757. ('borg', 'create', '--one-file-system', '--read-special') + REPO_ARCHIVE_WITH_PATHS,
  1758. processes=processes,
  1759. output_log_level=logging.INFO,
  1760. output_file=None,
  1761. borg_local_path='borg',
  1762. working_directory=None,
  1763. extra_environment=None,
  1764. )
  1765. module.create_archive(
  1766. dry_run=False,
  1767. repository='repo',
  1768. location_config={
  1769. 'source_directories': ['foo', 'bar'],
  1770. 'repositories': ['repo'],
  1771. 'exclude_patterns': None,
  1772. },
  1773. storage_config={},
  1774. local_borg_version='1.2.3',
  1775. stream_processes=processes,
  1776. )