test_btrfs.py 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.borg.pattern import Pattern, Pattern_source, Pattern_style, Pattern_type
  4. from borgmatic.hooks.data_source import btrfs as module
  5. def test_path_is_a_subvolume_with_btrfs_success_call_returns_true():
  6. module.path_is_a_subvolume.cache_clear()
  7. flexmock(module.borgmatic.execute).should_receive(
  8. 'execute_command',
  9. ).with_args(('btrfs', 'subvolume', 'show', '/mnt0'), output_log_level=None, close_fds=True)
  10. assert module.path_is_a_subvolume('btrfs', '/mnt0') is True
  11. def test_path_is_a_subvolume_with_btrfs_error_returns_false():
  12. module.path_is_a_subvolume.cache_clear()
  13. flexmock(module.borgmatic.execute).should_receive(
  14. 'execute_command',
  15. ).with_args(
  16. ('btrfs', 'subvolume', 'show', '/mnt0'), output_log_level=None, close_fds=True
  17. ).and_raise(
  18. module.subprocess.CalledProcessError(1, 'btrfs'),
  19. )
  20. assert module.path_is_a_subvolume('btrfs', '/mnt0') is False
  21. def test_path_is_a_subvolume_caches_result_after_first_call():
  22. module.path_is_a_subvolume.cache_clear()
  23. flexmock(module.borgmatic.execute).should_receive(
  24. 'execute_command',
  25. ).once()
  26. assert module.path_is_a_subvolume('btrfs', '/mnt0') is True
  27. assert module.path_is_a_subvolume('btrfs', '/mnt0') is True
  28. def test_get_subvolume_property_with_invalid_btrfs_output_errors():
  29. flexmock(module.borgmatic.execute).should_receive(
  30. 'execute_command_and_capture_output',
  31. ).and_return('invalid')
  32. with pytest.raises(ValueError):
  33. module.get_subvolume_property('btrfs', '/foo', 'ro')
  34. def test_get_subvolume_property_with_true_output_returns_true_bool():
  35. flexmock(module.borgmatic.execute).should_receive(
  36. 'execute_command_and_capture_output',
  37. ).and_return('ro=true')
  38. assert module.get_subvolume_property('btrfs', '/foo', 'ro') is True
  39. def test_get_subvolume_property_with_false_output_returns_false_bool():
  40. flexmock(module.borgmatic.execute).should_receive(
  41. 'execute_command_and_capture_output',
  42. ).and_return('ro=false')
  43. assert module.get_subvolume_property('btrfs', '/foo', 'ro') is False
  44. def test_get_subvolume_property_passes_through_general_value():
  45. flexmock(module.borgmatic.execute).should_receive(
  46. 'execute_command_and_capture_output',
  47. ).and_return('thing=value')
  48. assert module.get_subvolume_property('btrfs', '/foo', 'thing') == 'value'
  49. def test_get_containing_subvolume_path_with_subvolume_self_returns_it():
  50. flexmock(module).should_receive('path_is_a_subvolume').with_args(
  51. 'btrfs', '/foo/bar/baz'
  52. ).and_return(True)
  53. flexmock(module).should_receive('path_is_a_subvolume').with_args('btrfs', '/foo/bar').never()
  54. flexmock(module).should_receive('path_is_a_subvolume').with_args('btrfs', '/foo').never()
  55. flexmock(module).should_receive('path_is_a_subvolume').with_args('btrfs', '/').never()
  56. flexmock(module).should_receive('get_subvolume_property').and_return(False)
  57. assert module.get_containing_subvolume_path('btrfs', '/foo/bar/baz') == '/foo/bar/baz'
  58. def test_get_containing_subvolume_path_with_subvolume_parent_returns_it():
  59. flexmock(module).should_receive('path_is_a_subvolume').with_args(
  60. 'btrfs', '/foo/bar/baz'
  61. ).and_return(False)
  62. flexmock(module).should_receive('path_is_a_subvolume').with_args(
  63. 'btrfs', '/foo/bar'
  64. ).and_return(True)
  65. flexmock(module).should_receive('path_is_a_subvolume').with_args('btrfs', '/foo').never()
  66. flexmock(module).should_receive('path_is_a_subvolume').with_args('btrfs', '/').never()
  67. flexmock(module).should_receive('get_subvolume_property').and_return(False)
  68. assert module.get_containing_subvolume_path('btrfs', '/foo/bar/baz') == '/foo/bar'
  69. def test_get_containing_subvolume_path_with_subvolume_grandparent_returns_it():
  70. flexmock(module).should_receive('path_is_a_subvolume').with_args(
  71. 'btrfs', '/foo/bar/baz'
  72. ).and_return(False)
  73. flexmock(module).should_receive('path_is_a_subvolume').with_args(
  74. 'btrfs', '/foo/bar'
  75. ).and_return(False)
  76. flexmock(module).should_receive('path_is_a_subvolume').with_args('btrfs', '/foo').and_return(
  77. True
  78. )
  79. flexmock(module).should_receive('path_is_a_subvolume').with_args('btrfs', '/').never()
  80. flexmock(module).should_receive('get_subvolume_property').and_return(False)
  81. assert module.get_containing_subvolume_path('btrfs', '/foo/bar/baz') == '/foo'
  82. def test_get_containing_subvolume_path_without_subvolume_ancestor_returns_none():
  83. flexmock(module).should_receive('path_is_a_subvolume').with_args(
  84. 'btrfs', '/foo/bar/baz'
  85. ).and_return(False)
  86. flexmock(module).should_receive('path_is_a_subvolume').with_args(
  87. 'btrfs', '/foo/bar'
  88. ).and_return(False)
  89. flexmock(module).should_receive('path_is_a_subvolume').with_args('btrfs', '/foo').and_return(
  90. False
  91. )
  92. flexmock(module).should_receive('path_is_a_subvolume').with_args('btrfs', '/').and_return(False)
  93. flexmock(module).should_receive('get_subvolume_property').and_return(False)
  94. assert module.get_containing_subvolume_path('btrfs', '/foo/bar/baz') is None
  95. def test_get_containing_subvolume_path_with_read_only_subvolume_returns_none():
  96. flexmock(module).should_receive('path_is_a_subvolume').with_args(
  97. 'btrfs', '/foo/bar/baz'
  98. ).and_return(True)
  99. flexmock(module).should_receive('get_subvolume_property').and_return(True)
  100. assert module.get_containing_subvolume_path('btrfs', '/foo/bar/baz') is None
  101. def test_get_containing_subvolume_path_with_read_only_error_returns_none():
  102. flexmock(module).should_receive('path_is_a_subvolume').with_args(
  103. 'btrfs', '/foo/bar/baz'
  104. ).and_return(True)
  105. flexmock(module).should_receive('get_subvolume_property').and_raise(
  106. module.subprocess.CalledProcessError(1, 'wtf')
  107. )
  108. assert module.get_containing_subvolume_path('btrfs', '/foo/bar/baz') is None
  109. def test_get_all_subvolume_paths_skips_non_root_and_non_config_patterns():
  110. flexmock(module).should_receive('get_containing_subvolume_path').with_args(
  111. 'btrfs', '/foo'
  112. ).never()
  113. flexmock(module).should_receive('get_containing_subvolume_path').with_args(
  114. 'btrfs', '/bar'
  115. ).and_return('/bar').once()
  116. flexmock(module).should_receive('get_containing_subvolume_path').with_args(
  117. 'btrfs', '/baz'
  118. ).never()
  119. assert module.get_all_subvolume_paths(
  120. 'btrfs',
  121. (
  122. module.borgmatic.borg.pattern.Pattern(
  123. '/foo',
  124. type=module.borgmatic.borg.pattern.Pattern_type.ROOT,
  125. source=module.borgmatic.borg.pattern.Pattern_source.HOOK,
  126. ),
  127. module.borgmatic.borg.pattern.Pattern(
  128. '/bar',
  129. type=module.borgmatic.borg.pattern.Pattern_type.ROOT,
  130. source=module.borgmatic.borg.pattern.Pattern_source.CONFIG,
  131. ),
  132. module.borgmatic.borg.pattern.Pattern(
  133. '/baz',
  134. type=module.borgmatic.borg.pattern.Pattern_type.INCLUDE,
  135. source=module.borgmatic.borg.pattern.Pattern_source.CONFIG,
  136. ),
  137. ),
  138. ) == ('/bar',)
  139. def test_get_all_subvolume_paths_skips_non_btrfs_patterns():
  140. flexmock(module).should_receive('get_containing_subvolume_path').with_args(
  141. 'btrfs', '/foo'
  142. ).and_return(None).once()
  143. flexmock(module).should_receive('get_containing_subvolume_path').with_args(
  144. 'btrfs', '/bar'
  145. ).and_return('/bar').once()
  146. assert module.get_all_subvolume_paths(
  147. 'btrfs',
  148. (
  149. module.borgmatic.borg.pattern.Pattern(
  150. '/foo',
  151. type=module.borgmatic.borg.pattern.Pattern_type.ROOT,
  152. source=module.borgmatic.borg.pattern.Pattern_source.CONFIG,
  153. ),
  154. module.borgmatic.borg.pattern.Pattern(
  155. '/bar',
  156. type=module.borgmatic.borg.pattern.Pattern_type.ROOT,
  157. source=module.borgmatic.borg.pattern.Pattern_source.CONFIG,
  158. ),
  159. ),
  160. ) == ('/bar',)
  161. def test_get_all_subvolume_paths_sorts_subvolume_paths():
  162. flexmock(module).should_receive('get_containing_subvolume_path').with_args(
  163. 'btrfs', '/foo'
  164. ).and_return('/foo').once()
  165. flexmock(module).should_receive('get_containing_subvolume_path').with_args(
  166. 'btrfs', '/bar'
  167. ).and_return('/bar').once()
  168. flexmock(module).should_receive('get_containing_subvolume_path').with_args(
  169. 'btrfs', '/baz'
  170. ).and_return('/baz').once()
  171. assert module.get_all_subvolume_paths(
  172. 'btrfs',
  173. (
  174. module.borgmatic.borg.pattern.Pattern(
  175. '/foo',
  176. type=module.borgmatic.borg.pattern.Pattern_type.ROOT,
  177. source=module.borgmatic.borg.pattern.Pattern_source.CONFIG,
  178. ),
  179. module.borgmatic.borg.pattern.Pattern(
  180. '/bar',
  181. type=module.borgmatic.borg.pattern.Pattern_type.ROOT,
  182. source=module.borgmatic.borg.pattern.Pattern_source.CONFIG,
  183. ),
  184. module.borgmatic.borg.pattern.Pattern(
  185. '/baz',
  186. type=module.borgmatic.borg.pattern.Pattern_type.ROOT,
  187. source=module.borgmatic.borg.pattern.Pattern_source.CONFIG,
  188. ),
  189. ),
  190. ) == ('/bar', '/baz', '/foo')
  191. def test_get_subvolumes_collects_subvolumes_matching_patterns():
  192. flexmock(module).should_receive('get_all_subvolume_paths').and_return(('/mnt1', '/mnt2'))
  193. contained_pattern = Pattern(
  194. '/mnt1',
  195. type=Pattern_type.ROOT,
  196. source=Pattern_source.CONFIG,
  197. )
  198. flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
  199. 'get_contained_patterns',
  200. ).with_args('/mnt1', object).and_return((contained_pattern,))
  201. flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
  202. 'get_contained_patterns',
  203. ).with_args('/mnt2', object).and_return(())
  204. assert module.get_subvolumes(
  205. 'btrfs',
  206. patterns=[
  207. Pattern('/mnt1'),
  208. Pattern('/mnt3'),
  209. ],
  210. ) == (module.Subvolume('/mnt1', contained_patterns=(contained_pattern,)),)
  211. def test_get_subvolumes_skips_non_root_patterns():
  212. flexmock(module).should_receive('get_all_subvolume_paths').and_return(('/mnt1', '/mnt2'))
  213. flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
  214. 'get_contained_patterns',
  215. ).with_args('/mnt1', object).and_return(
  216. (
  217. Pattern(
  218. '/mnt1',
  219. type=Pattern_type.EXCLUDE,
  220. source=Pattern_source.CONFIG,
  221. ),
  222. ),
  223. )
  224. flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
  225. 'get_contained_patterns',
  226. ).with_args('/mnt2', object).and_return(())
  227. assert (
  228. module.get_subvolumes(
  229. 'btrfs',
  230. patterns=[
  231. Pattern('/mnt1'),
  232. Pattern('/mnt3'),
  233. ],
  234. )
  235. == ()
  236. )
  237. def test_get_subvolumes_skips_non_config_patterns():
  238. flexmock(module).should_receive('get_all_subvolume_paths').and_return(('/mnt1', '/mnt2'))
  239. flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
  240. 'get_contained_patterns',
  241. ).with_args('/mnt1', object).and_return(
  242. (
  243. Pattern(
  244. '/mnt1',
  245. type=Pattern_type.ROOT,
  246. source=Pattern_source.HOOK,
  247. ),
  248. ),
  249. )
  250. flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
  251. 'get_contained_patterns',
  252. ).with_args('/mnt2', object).and_return(())
  253. assert (
  254. module.get_subvolumes(
  255. 'btrfs',
  256. patterns=[
  257. Pattern('/mnt1'),
  258. Pattern('/mnt3'),
  259. ],
  260. )
  261. == ()
  262. )
  263. @pytest.mark.parametrize(
  264. 'subvolume_path,expected_snapshot_path',
  265. (
  266. ('/foo/bar', '/foo/bar/.borgmatic-snapshot-1234/foo/bar'),
  267. ('/', '/.borgmatic-snapshot-1234'),
  268. ),
  269. )
  270. def test_make_snapshot_path_includes_stripped_subvolume_path(
  271. subvolume_path,
  272. expected_snapshot_path,
  273. ):
  274. flexmock(module.os).should_receive('getpid').and_return(1234)
  275. assert module.make_snapshot_path(subvolume_path) == expected_snapshot_path
  276. @pytest.mark.parametrize(
  277. 'subvolume_path,pattern,expected_pattern',
  278. (
  279. (
  280. '/foo/bar',
  281. Pattern('/foo/bar/baz'),
  282. Pattern('/foo/bar/.borgmatic-snapshot-1234/./foo/bar/baz'),
  283. ),
  284. ('/foo/bar', Pattern('/foo/bar'), Pattern('/foo/bar/.borgmatic-snapshot-1234/./foo/bar')),
  285. (
  286. '/foo/bar',
  287. Pattern('^/foo/bar', Pattern_type.INCLUDE, Pattern_style.REGULAR_EXPRESSION),
  288. Pattern(
  289. '^/foo/bar/.borgmatic-snapshot-1234/./foo/bar',
  290. Pattern_type.INCLUDE,
  291. Pattern_style.REGULAR_EXPRESSION,
  292. ),
  293. ),
  294. (
  295. '/foo/bar',
  296. Pattern('/foo/bar', Pattern_type.INCLUDE, Pattern_style.REGULAR_EXPRESSION),
  297. Pattern(
  298. '/foo/bar/.borgmatic-snapshot-1234/./foo/bar',
  299. Pattern_type.INCLUDE,
  300. Pattern_style.REGULAR_EXPRESSION,
  301. ),
  302. ),
  303. ('/', Pattern('/foo'), Pattern('/.borgmatic-snapshot-1234/./foo')),
  304. ('/', Pattern('/'), Pattern('/.borgmatic-snapshot-1234/./')),
  305. (
  306. '/foo/bar',
  307. Pattern('/foo/bar/./baz'),
  308. Pattern('/foo/bar/.borgmatic-snapshot-1234/foo/bar/./baz'),
  309. ),
  310. ),
  311. )
  312. def test_make_borg_snapshot_pattern_includes_slashdot_hack_and_stripped_pattern_path(
  313. subvolume_path,
  314. pattern,
  315. expected_pattern,
  316. ):
  317. flexmock(module.os).should_receive('getpid').and_return(1234)
  318. assert module.make_borg_snapshot_pattern(subvolume_path, pattern) == expected_pattern
  319. def test_dump_data_sources_snapshots_each_subvolume_and_updates_patterns():
  320. patterns = [Pattern('/foo'), Pattern('/mnt/subvol1')]
  321. config = {'btrfs': {}}
  322. flexmock(module).should_receive('get_subvolumes').and_return(
  323. (
  324. module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),
  325. module.Subvolume('/mnt/subvol2', contained_patterns=(Pattern('/mnt/subvol2'),)),
  326. ),
  327. )
  328. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  329. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  330. )
  331. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol2').and_return(
  332. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  333. )
  334. flexmock(module).should_receive('snapshot_subvolume').with_args(
  335. 'btrfs',
  336. '/mnt/subvol1',
  337. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  338. ).once()
  339. flexmock(module).should_receive('snapshot_subvolume').with_args(
  340. 'btrfs',
  341. '/mnt/subvol2',
  342. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  343. ).once()
  344. flexmock(module).should_receive('make_snapshot_exclude_pattern').with_args(
  345. '/mnt/subvol1',
  346. ).and_return(
  347. Pattern(
  348. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  349. Pattern_type.NO_RECURSE,
  350. Pattern_style.FNMATCH,
  351. ),
  352. )
  353. flexmock(module).should_receive('make_snapshot_exclude_pattern').with_args(
  354. '/mnt/subvol2',
  355. ).and_return(
  356. Pattern(
  357. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2/.borgmatic-1234',
  358. Pattern_type.NO_RECURSE,
  359. Pattern_style.FNMATCH,
  360. ),
  361. )
  362. flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
  363. '/mnt/subvol1',
  364. object,
  365. ).and_return(Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'))
  366. flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
  367. '/mnt/subvol2',
  368. object,
  369. ).and_return(Pattern('/mnt/subvol2/.borgmatic-1234/mnt/subvol2'))
  370. assert (
  371. module.dump_data_sources(
  372. hook_config=config['btrfs'],
  373. config=config,
  374. config_paths=('test.yaml',),
  375. borgmatic_runtime_directory='/run/borgmatic',
  376. patterns=patterns,
  377. dry_run=False,
  378. )
  379. == []
  380. )
  381. assert patterns == [
  382. Pattern('/foo'),
  383. Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'),
  384. Pattern(
  385. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  386. Pattern_type.NO_RECURSE,
  387. Pattern_style.FNMATCH,
  388. ),
  389. Pattern('/mnt/subvol2/.borgmatic-1234/mnt/subvol2'),
  390. Pattern(
  391. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2/.borgmatic-1234',
  392. Pattern_type.NO_RECURSE,
  393. Pattern_style.FNMATCH,
  394. ),
  395. ]
  396. assert config == {
  397. 'btrfs': {},
  398. }
  399. def test_dump_data_sources_uses_custom_btrfs_command_in_commands():
  400. patterns = [Pattern('/foo'), Pattern('/mnt/subvol1')]
  401. config = {'btrfs': {'btrfs_command': '/usr/local/bin/btrfs'}}
  402. flexmock(module).should_receive('get_subvolumes').and_return(
  403. (module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),),
  404. )
  405. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  406. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  407. )
  408. flexmock(module).should_receive('snapshot_subvolume').with_args(
  409. '/usr/local/bin/btrfs',
  410. '/mnt/subvol1',
  411. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  412. ).once()
  413. flexmock(module).should_receive('make_snapshot_exclude_pattern').with_args(
  414. '/mnt/subvol1',
  415. ).and_return(
  416. Pattern(
  417. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  418. Pattern_type.NO_RECURSE,
  419. Pattern_style.FNMATCH,
  420. ),
  421. )
  422. flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
  423. '/mnt/subvol1',
  424. object,
  425. ).and_return(Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'))
  426. assert (
  427. module.dump_data_sources(
  428. hook_config=config['btrfs'],
  429. config=config,
  430. config_paths=('test.yaml',),
  431. borgmatic_runtime_directory='/run/borgmatic',
  432. patterns=patterns,
  433. dry_run=False,
  434. )
  435. == []
  436. )
  437. assert patterns == [
  438. Pattern('/foo'),
  439. Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'),
  440. Pattern(
  441. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  442. Pattern_type.NO_RECURSE,
  443. Pattern_style.FNMATCH,
  444. ),
  445. ]
  446. assert config == {
  447. 'btrfs': {
  448. 'btrfs_command': '/usr/local/bin/btrfs',
  449. },
  450. }
  451. def test_dump_data_sources_with_findmnt_command_warns():
  452. patterns = [Pattern('/foo'), Pattern('/mnt/subvol1')]
  453. config = {'btrfs': {'findmnt_command': '/usr/local/bin/findmnt'}}
  454. flexmock(module.logger).should_receive('warning').once()
  455. flexmock(module).should_receive('get_subvolumes').with_args(
  456. 'btrfs',
  457. patterns,
  458. ).and_return(
  459. (module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),),
  460. ).once()
  461. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  462. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  463. )
  464. flexmock(module).should_receive('snapshot_subvolume').with_args(
  465. 'btrfs',
  466. '/mnt/subvol1',
  467. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  468. ).once()
  469. flexmock(module).should_receive('make_snapshot_exclude_pattern').with_args(
  470. '/mnt/subvol1',
  471. ).and_return(
  472. Pattern(
  473. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  474. Pattern_type.NO_RECURSE,
  475. Pattern_style.FNMATCH,
  476. ),
  477. )
  478. flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
  479. '/mnt/subvol1',
  480. object,
  481. ).and_return(Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'))
  482. assert (
  483. module.dump_data_sources(
  484. hook_config=config['btrfs'],
  485. config=config,
  486. config_paths=('test.yaml',),
  487. borgmatic_runtime_directory='/run/borgmatic',
  488. patterns=patterns,
  489. dry_run=False,
  490. )
  491. == []
  492. )
  493. assert patterns == [
  494. Pattern('/foo'),
  495. Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'),
  496. Pattern(
  497. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  498. Pattern_type.NO_RECURSE,
  499. Pattern_style.FNMATCH,
  500. ),
  501. ]
  502. assert config == {
  503. 'btrfs': {
  504. 'findmnt_command': '/usr/local/bin/findmnt',
  505. },
  506. }
  507. def test_dump_data_sources_with_dry_run_skips_snapshot_and_patterns_update():
  508. patterns = [Pattern('/foo'), Pattern('/mnt/subvol1')]
  509. config = {'btrfs': {}}
  510. flexmock(module).should_receive('get_subvolumes').and_return(
  511. (module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),),
  512. )
  513. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  514. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  515. )
  516. flexmock(module).should_receive('snapshot_subvolume').never()
  517. flexmock(module).should_receive('make_snapshot_exclude_pattern').never()
  518. assert (
  519. module.dump_data_sources(
  520. hook_config=config['btrfs'],
  521. config=config,
  522. config_paths=('test.yaml',),
  523. borgmatic_runtime_directory='/run/borgmatic',
  524. patterns=patterns,
  525. dry_run=True,
  526. )
  527. == []
  528. )
  529. assert patterns == [Pattern('/foo'), Pattern('/mnt/subvol1')]
  530. assert config == {'btrfs': {}}
  531. def test_dump_data_sources_without_matching_subvolumes_skips_snapshot_and_patterns_update():
  532. patterns = [Pattern('/foo'), Pattern('/mnt/subvol1')]
  533. config = {'btrfs': {}}
  534. flexmock(module).should_receive('get_subvolumes').and_return(())
  535. flexmock(module).should_receive('make_snapshot_path').never()
  536. flexmock(module).should_receive('snapshot_subvolume').never()
  537. flexmock(module).should_receive('make_snapshot_exclude_pattern').never()
  538. assert (
  539. module.dump_data_sources(
  540. hook_config=config['btrfs'],
  541. config=config,
  542. config_paths=('test.yaml',),
  543. borgmatic_runtime_directory='/run/borgmatic',
  544. patterns=patterns,
  545. dry_run=False,
  546. )
  547. == []
  548. )
  549. assert patterns == [Pattern('/foo'), Pattern('/mnt/subvol1')]
  550. assert config == {'btrfs': {}}
  551. def test_dump_data_sources_snapshots_adds_to_existing_exclude_patterns():
  552. patterns = [Pattern('/foo'), Pattern('/mnt/subvol1')]
  553. config = {'btrfs': {}, 'exclude_patterns': ['/bar']}
  554. flexmock(module).should_receive('get_subvolumes').and_return(
  555. (
  556. module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),
  557. module.Subvolume('/mnt/subvol2', contained_patterns=(Pattern('/mnt/subvol2'),)),
  558. ),
  559. )
  560. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  561. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  562. )
  563. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol2').and_return(
  564. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  565. )
  566. flexmock(module).should_receive('snapshot_subvolume').with_args(
  567. 'btrfs',
  568. '/mnt/subvol1',
  569. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  570. ).once()
  571. flexmock(module).should_receive('snapshot_subvolume').with_args(
  572. 'btrfs',
  573. '/mnt/subvol2',
  574. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  575. ).once()
  576. flexmock(module).should_receive('make_snapshot_exclude_pattern').with_args(
  577. '/mnt/subvol1',
  578. ).and_return(
  579. Pattern(
  580. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  581. Pattern_type.NO_RECURSE,
  582. Pattern_style.FNMATCH,
  583. ),
  584. )
  585. flexmock(module).should_receive('make_snapshot_exclude_pattern').with_args(
  586. '/mnt/subvol2',
  587. ).and_return(
  588. Pattern(
  589. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2/.borgmatic-1234',
  590. Pattern_type.NO_RECURSE,
  591. Pattern_style.FNMATCH,
  592. ),
  593. )
  594. flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
  595. '/mnt/subvol1',
  596. object,
  597. ).and_return(Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'))
  598. flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
  599. '/mnt/subvol2',
  600. object,
  601. ).and_return(Pattern('/mnt/subvol2/.borgmatic-1234/mnt/subvol2'))
  602. assert (
  603. module.dump_data_sources(
  604. hook_config=config['btrfs'],
  605. config=config,
  606. config_paths=('test.yaml',),
  607. borgmatic_runtime_directory='/run/borgmatic',
  608. patterns=patterns,
  609. dry_run=False,
  610. )
  611. == []
  612. )
  613. assert patterns == [
  614. Pattern('/foo'),
  615. Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'),
  616. Pattern(
  617. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  618. Pattern_type.NO_RECURSE,
  619. Pattern_style.FNMATCH,
  620. ),
  621. Pattern('/mnt/subvol2/.borgmatic-1234/mnt/subvol2'),
  622. Pattern(
  623. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2/.borgmatic-1234',
  624. Pattern_type.NO_RECURSE,
  625. Pattern_style.FNMATCH,
  626. ),
  627. ]
  628. assert config == {
  629. 'btrfs': {},
  630. 'exclude_patterns': ['/bar'],
  631. }
  632. def test_remove_data_source_dumps_deletes_snapshots():
  633. config = {'btrfs': {}}
  634. flexmock(module).should_receive('get_subvolumes').and_return(
  635. (
  636. module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),
  637. module.Subvolume('/mnt/subvol2', contained_patterns=(Pattern('/mnt/subvol2'),)),
  638. ),
  639. )
  640. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  641. '/mnt/subvol1/.borgmatic-1234/./mnt/subvol1',
  642. )
  643. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol2').and_return(
  644. '/mnt/subvol2/.borgmatic-1234/./mnt/subvol2',
  645. )
  646. flexmock(module.borgmatic.config.paths).should_receive(
  647. 'replace_temporary_subdirectory_with_glob',
  648. ).with_args(
  649. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  650. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  651. ).and_return('/mnt/subvol1/.borgmatic-*/mnt/subvol1')
  652. flexmock(module.borgmatic.config.paths).should_receive(
  653. 'replace_temporary_subdirectory_with_glob',
  654. ).with_args(
  655. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  656. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  657. ).and_return('/mnt/subvol2/.borgmatic-*/mnt/subvol2')
  658. flexmock(module.glob).should_receive('glob').with_args(
  659. '/mnt/subvol1/.borgmatic-*/mnt/subvol1',
  660. ).and_return(
  661. ('/mnt/subvol1/.borgmatic-1234/mnt/subvol1', '/mnt/subvol1/.borgmatic-5678/mnt/subvol1'),
  662. )
  663. flexmock(module.glob).should_receive('glob').with_args(
  664. '/mnt/subvol2/.borgmatic-*/mnt/subvol2',
  665. ).and_return(
  666. ('/mnt/subvol2/.borgmatic-1234/mnt/subvol2', '/mnt/subvol2/.borgmatic-5678/mnt/subvol2'),
  667. )
  668. flexmock(module.os.path).should_receive('isdir').with_args(
  669. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  670. ).and_return(True)
  671. flexmock(module.os.path).should_receive('isdir').with_args(
  672. '/mnt/subvol1/.borgmatic-5678/mnt/subvol1',
  673. ).and_return(True)
  674. flexmock(module.os.path).should_receive('isdir').with_args(
  675. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  676. ).and_return(True)
  677. flexmock(module.os.path).should_receive('isdir').with_args(
  678. '/mnt/subvol2/.borgmatic-5678/mnt/subvol2',
  679. ).and_return(False)
  680. flexmock(module).should_receive('delete_snapshot').with_args(
  681. 'btrfs',
  682. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  683. ).once()
  684. flexmock(module).should_receive('delete_snapshot').with_args(
  685. 'btrfs',
  686. '/mnt/subvol1/.borgmatic-5678/mnt/subvol1',
  687. ).once()
  688. flexmock(module).should_receive('delete_snapshot').with_args(
  689. 'btrfs',
  690. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  691. ).once()
  692. flexmock(module).should_receive('delete_snapshot').with_args(
  693. 'btrfs',
  694. '/mnt/subvol2/.borgmatic-5678/mnt/subvol2',
  695. ).never()
  696. flexmock(module.os.path).should_receive('isdir').with_args(
  697. '/mnt/subvol1/.borgmatic-1234',
  698. ).and_return(True)
  699. flexmock(module.os.path).should_receive('isdir').with_args(
  700. '/mnt/subvol1/.borgmatic-5678',
  701. ).and_return(True)
  702. flexmock(module.os.path).should_receive('isdir').with_args(
  703. '/mnt/subvol2/.borgmatic-1234',
  704. ).and_return(True)
  705. flexmock(module.os.path).should_receive('isdir').with_args(
  706. '/mnt/subvol2/.borgmatic-5678',
  707. ).and_return(True)
  708. flexmock(module.shutil).should_receive('rmtree').with_args(
  709. '/mnt/subvol1/.borgmatic-1234',
  710. ).once()
  711. flexmock(module.shutil).should_receive('rmtree').with_args(
  712. '/mnt/subvol1/.borgmatic-5678',
  713. ).once()
  714. flexmock(module.shutil).should_receive('rmtree').with_args(
  715. '/mnt/subvol2/.borgmatic-1234',
  716. ).once()
  717. flexmock(module.shutil).should_receive('rmtree').with_args(
  718. '/mnt/subvol2/.borgmatic-5678',
  719. ).never()
  720. module.remove_data_source_dumps(
  721. hook_config=config['btrfs'],
  722. config=config,
  723. borgmatic_runtime_directory='/run/borgmatic',
  724. patterns=flexmock(),
  725. dry_run=False,
  726. )
  727. def test_remove_data_source_dumps_without_hook_configuration_bails():
  728. flexmock(module).should_receive('get_subvolumes').never()
  729. flexmock(module).should_receive('make_snapshot_path').never()
  730. flexmock(module.borgmatic.config.paths).should_receive(
  731. 'replace_temporary_subdirectory_with_glob',
  732. ).never()
  733. flexmock(module).should_receive('delete_snapshot').never()
  734. flexmock(module.shutil).should_receive('rmtree').never()
  735. module.remove_data_source_dumps(
  736. hook_config=None,
  737. config={'source_directories': '/mnt/subvolume'},
  738. borgmatic_runtime_directory='/run/borgmatic',
  739. patterns=flexmock(),
  740. dry_run=False,
  741. )
  742. def test_remove_data_source_dumps_with_get_subvolumes_file_not_found_error_bails():
  743. config = {'btrfs': {}}
  744. flexmock(module).should_receive('get_subvolumes').and_raise(FileNotFoundError)
  745. flexmock(module).should_receive('make_snapshot_path').never()
  746. flexmock(module.borgmatic.config.paths).should_receive(
  747. 'replace_temporary_subdirectory_with_glob',
  748. ).never()
  749. flexmock(module).should_receive('delete_snapshot').never()
  750. flexmock(module.shutil).should_receive('rmtree').never()
  751. module.remove_data_source_dumps(
  752. hook_config=config['btrfs'],
  753. config=config,
  754. borgmatic_runtime_directory='/run/borgmatic',
  755. patterns=flexmock(),
  756. dry_run=False,
  757. )
  758. def test_remove_data_source_dumps_with_get_subvolumes_called_process_error_bails():
  759. config = {'btrfs': {}}
  760. flexmock(module).should_receive('get_subvolumes').and_raise(
  761. module.subprocess.CalledProcessError(1, 'command', 'error'),
  762. )
  763. flexmock(module).should_receive('make_snapshot_path').never()
  764. flexmock(module.borgmatic.config.paths).should_receive(
  765. 'replace_temporary_subdirectory_with_glob',
  766. ).never()
  767. flexmock(module).should_receive('delete_snapshot').never()
  768. flexmock(module.shutil).should_receive('rmtree').never()
  769. module.remove_data_source_dumps(
  770. hook_config=config['btrfs'],
  771. config=config,
  772. borgmatic_runtime_directory='/run/borgmatic',
  773. patterns=flexmock(),
  774. dry_run=False,
  775. )
  776. def test_remove_data_source_dumps_with_dry_run_skips_deletes():
  777. config = {'btrfs': {}}
  778. flexmock(module).should_receive('get_subvolumes').and_return(
  779. (
  780. module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),
  781. module.Subvolume('/mnt/subvol2', contained_patterns=(Pattern('/mnt/subvol2'),)),
  782. ),
  783. )
  784. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  785. '/mnt/subvol1/.borgmatic-1234/./mnt/subvol1',
  786. )
  787. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol2').and_return(
  788. '/mnt/subvol2/.borgmatic-1234/./mnt/subvol2',
  789. )
  790. flexmock(module.borgmatic.config.paths).should_receive(
  791. 'replace_temporary_subdirectory_with_glob',
  792. ).with_args(
  793. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  794. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  795. ).and_return('/mnt/subvol1/.borgmatic-*/mnt/subvol1')
  796. flexmock(module.borgmatic.config.paths).should_receive(
  797. 'replace_temporary_subdirectory_with_glob',
  798. ).with_args(
  799. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  800. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  801. ).and_return('/mnt/subvol2/.borgmatic-*/mnt/subvol2')
  802. flexmock(module.glob).should_receive('glob').with_args(
  803. '/mnt/subvol1/.borgmatic-*/mnt/subvol1',
  804. ).and_return(
  805. ('/mnt/subvol1/.borgmatic-1234/mnt/subvol1', '/mnt/subvol1/.borgmatic-5678/mnt/subvol1'),
  806. )
  807. flexmock(module.glob).should_receive('glob').with_args(
  808. '/mnt/subvol2/.borgmatic-*/mnt/subvol2',
  809. ).and_return(
  810. ('/mnt/subvol2/.borgmatic-1234/mnt/subvol2', '/mnt/subvol2/.borgmatic-5678/mnt/subvol2'),
  811. )
  812. flexmock(module.os.path).should_receive('isdir').with_args(
  813. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  814. ).and_return(True)
  815. flexmock(module.os.path).should_receive('isdir').with_args(
  816. '/mnt/subvol1/.borgmatic-5678/mnt/subvol1',
  817. ).and_return(True)
  818. flexmock(module.os.path).should_receive('isdir').with_args(
  819. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  820. ).and_return(True)
  821. flexmock(module.os.path).should_receive('isdir').with_args(
  822. '/mnt/subvol2/.borgmatic-5678/mnt/subvol2',
  823. ).and_return(False)
  824. flexmock(module).should_receive('delete_snapshot').never()
  825. flexmock(module.shutil).should_receive('rmtree').never()
  826. module.remove_data_source_dumps(
  827. hook_config=config['btrfs'],
  828. config=config,
  829. borgmatic_runtime_directory='/run/borgmatic',
  830. patterns=flexmock(),
  831. dry_run=True,
  832. )
  833. def test_remove_data_source_dumps_without_subvolumes_skips_deletes():
  834. config = {'btrfs': {}}
  835. flexmock(module).should_receive('get_subvolumes').and_return(())
  836. flexmock(module).should_receive('make_snapshot_path').never()
  837. flexmock(module.borgmatic.config.paths).should_receive(
  838. 'replace_temporary_subdirectory_with_glob',
  839. ).never()
  840. flexmock(module).should_receive('delete_snapshot').never()
  841. flexmock(module.shutil).should_receive('rmtree').never()
  842. module.remove_data_source_dumps(
  843. hook_config=config['btrfs'],
  844. config=config,
  845. borgmatic_runtime_directory='/run/borgmatic',
  846. patterns=flexmock(),
  847. dry_run=False,
  848. )
  849. def test_remove_data_source_without_snapshots_skips_deletes():
  850. config = {'btrfs': {}}
  851. flexmock(module).should_receive('get_subvolumes').and_return(
  852. (
  853. module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),
  854. module.Subvolume('/mnt/subvol2', contained_patterns=(Pattern('/mnt/subvol2'),)),
  855. ),
  856. )
  857. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  858. '/mnt/subvol1/.borgmatic-1234/./mnt/subvol1',
  859. )
  860. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol2').and_return(
  861. '/mnt/subvol2/.borgmatic-1234/./mnt/subvol2',
  862. )
  863. flexmock(module.borgmatic.config.paths).should_receive(
  864. 'replace_temporary_subdirectory_with_glob',
  865. ).with_args(
  866. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  867. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  868. ).and_return('/mnt/subvol1/.borgmatic-*/mnt/subvol1')
  869. flexmock(module.borgmatic.config.paths).should_receive(
  870. 'replace_temporary_subdirectory_with_glob',
  871. ).with_args(
  872. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  873. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  874. ).and_return('/mnt/subvol2/.borgmatic-*/mnt/subvol2')
  875. flexmock(module.glob).should_receive('glob').and_return(())
  876. flexmock(module.os.path).should_receive('isdir').never()
  877. flexmock(module).should_receive('delete_snapshot').never()
  878. flexmock(module.shutil).should_receive('rmtree').never()
  879. module.remove_data_source_dumps(
  880. hook_config=config['btrfs'],
  881. config=config,
  882. borgmatic_runtime_directory='/run/borgmatic',
  883. patterns=flexmock(),
  884. dry_run=False,
  885. )
  886. def test_remove_data_source_dumps_with_delete_snapshot_file_not_found_error_bails():
  887. config = {'btrfs': {}}
  888. flexmock(module).should_receive('get_subvolumes').and_return(
  889. (
  890. module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),
  891. module.Subvolume('/mnt/subvol2', contained_patterns=(Pattern('/mnt/subvol2'),)),
  892. ),
  893. )
  894. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  895. '/mnt/subvol1/.borgmatic-1234/./mnt/subvol1',
  896. )
  897. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol2').and_return(
  898. '/mnt/subvol2/.borgmatic-1234/./mnt/subvol2',
  899. )
  900. flexmock(module.borgmatic.config.paths).should_receive(
  901. 'replace_temporary_subdirectory_with_glob',
  902. ).with_args(
  903. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  904. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  905. ).and_return('/mnt/subvol1/.borgmatic-*/mnt/subvol1')
  906. flexmock(module.borgmatic.config.paths).should_receive(
  907. 'replace_temporary_subdirectory_with_glob',
  908. ).with_args(
  909. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  910. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  911. ).and_return('/mnt/subvol2/.borgmatic-*/mnt/subvol2')
  912. flexmock(module.glob).should_receive('glob').with_args(
  913. '/mnt/subvol1/.borgmatic-*/mnt/subvol1',
  914. ).and_return(
  915. ('/mnt/subvol1/.borgmatic-1234/mnt/subvol1', '/mnt/subvol1/.borgmatic-5678/mnt/subvol1'),
  916. )
  917. flexmock(module.glob).should_receive('glob').with_args(
  918. '/mnt/subvol2/.borgmatic-*/mnt/subvol2',
  919. ).and_return(
  920. ('/mnt/subvol2/.borgmatic-1234/mnt/subvol2', '/mnt/subvol2/.borgmatic-5678/mnt/subvol2'),
  921. )
  922. flexmock(module.os.path).should_receive('isdir').with_args(
  923. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  924. ).and_return(True)
  925. flexmock(module.os.path).should_receive('isdir').with_args(
  926. '/mnt/subvol1/.borgmatic-5678/mnt/subvol1',
  927. ).and_return(True)
  928. flexmock(module.os.path).should_receive('isdir').with_args(
  929. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  930. ).and_return(True)
  931. flexmock(module.os.path).should_receive('isdir').with_args(
  932. '/mnt/subvol2/.borgmatic-5678/mnt/subvol2',
  933. ).and_return(False)
  934. flexmock(module).should_receive('delete_snapshot').and_raise(FileNotFoundError)
  935. flexmock(module.shutil).should_receive('rmtree').never()
  936. module.remove_data_source_dumps(
  937. hook_config=config['btrfs'],
  938. config=config,
  939. borgmatic_runtime_directory='/run/borgmatic',
  940. patterns=flexmock(),
  941. dry_run=False,
  942. )
  943. def test_remove_data_source_dumps_with_delete_snapshot_called_process_error_bails():
  944. config = {'btrfs': {}}
  945. flexmock(module).should_receive('get_subvolumes').and_return(
  946. (
  947. module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),
  948. module.Subvolume('/mnt/subvol2', contained_patterns=(Pattern('/mnt/subvol2'),)),
  949. ),
  950. )
  951. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  952. '/mnt/subvol1/.borgmatic-1234/./mnt/subvol1',
  953. )
  954. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol2').and_return(
  955. '/mnt/subvol2/.borgmatic-1234/./mnt/subvol2',
  956. )
  957. flexmock(module.borgmatic.config.paths).should_receive(
  958. 'replace_temporary_subdirectory_with_glob',
  959. ).with_args(
  960. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  961. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  962. ).and_return('/mnt/subvol1/.borgmatic-*/mnt/subvol1')
  963. flexmock(module.borgmatic.config.paths).should_receive(
  964. 'replace_temporary_subdirectory_with_glob',
  965. ).with_args(
  966. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  967. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  968. ).and_return('/mnt/subvol2/.borgmatic-*/mnt/subvol2')
  969. flexmock(module.glob).should_receive('glob').with_args(
  970. '/mnt/subvol1/.borgmatic-*/mnt/subvol1',
  971. ).and_return(
  972. ('/mnt/subvol1/.borgmatic-1234/mnt/subvol1', '/mnt/subvol1/.borgmatic-5678/mnt/subvol1'),
  973. )
  974. flexmock(module.glob).should_receive('glob').with_args(
  975. '/mnt/subvol2/.borgmatic-*/mnt/subvol2',
  976. ).and_return(
  977. ('/mnt/subvol2/.borgmatic-1234/mnt/subvol2', '/mnt/subvol2/.borgmatic-5678/mnt/subvol2'),
  978. )
  979. flexmock(module.os.path).should_receive('isdir').with_args(
  980. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  981. ).and_return(True)
  982. flexmock(module.os.path).should_receive('isdir').with_args(
  983. '/mnt/subvol1/.borgmatic-5678/mnt/subvol1',
  984. ).and_return(True)
  985. flexmock(module.os.path).should_receive('isdir').with_args(
  986. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  987. ).and_return(True)
  988. flexmock(module.os.path).should_receive('isdir').with_args(
  989. '/mnt/subvol2/.borgmatic-5678/mnt/subvol2',
  990. ).and_return(False)
  991. flexmock(module).should_receive('delete_snapshot').and_raise(
  992. module.subprocess.CalledProcessError(1, 'command', 'error'),
  993. )
  994. flexmock(module.shutil).should_receive('rmtree').never()
  995. module.remove_data_source_dumps(
  996. hook_config=config['btrfs'],
  997. config=config,
  998. borgmatic_runtime_directory='/run/borgmatic',
  999. patterns=flexmock(),
  1000. dry_run=False,
  1001. )
  1002. def test_remove_data_source_dumps_with_root_subvolume_skips_duplicate_removal():
  1003. config = {'btrfs': {}}
  1004. flexmock(module).should_receive('get_subvolumes').and_return(
  1005. (module.Subvolume('/', contained_patterns=(Pattern('/etc'),)),),
  1006. )
  1007. flexmock(module).should_receive('make_snapshot_path').with_args('/').and_return(
  1008. '/.borgmatic-1234',
  1009. )
  1010. flexmock(module.borgmatic.config.paths).should_receive(
  1011. 'replace_temporary_subdirectory_with_glob',
  1012. ).with_args(
  1013. '/.borgmatic-1234',
  1014. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  1015. ).and_return('/.borgmatic-*')
  1016. flexmock(module.glob).should_receive('glob').with_args('/.borgmatic-*').and_return(
  1017. ('/.borgmatic-1234', '/.borgmatic-5678'),
  1018. )
  1019. flexmock(module.os.path).should_receive('isdir').with_args('/.borgmatic-1234').and_return(
  1020. True,
  1021. ).and_return(False)
  1022. flexmock(module.os.path).should_receive('isdir').with_args('/.borgmatic-5678').and_return(
  1023. True,
  1024. ).and_return(False)
  1025. flexmock(module).should_receive('delete_snapshot').with_args('btrfs', '/.borgmatic-1234').once()
  1026. flexmock(module).should_receive('delete_snapshot').with_args('btrfs', '/.borgmatic-5678').once()
  1027. flexmock(module.os.path).should_receive('isdir').with_args('').and_return(False)
  1028. flexmock(module.shutil).should_receive('rmtree').never()
  1029. module.remove_data_source_dumps(
  1030. hook_config=config['btrfs'],
  1031. config=config,
  1032. borgmatic_runtime_directory='/run/borgmatic',
  1033. patterns=flexmock(),
  1034. dry_run=False,
  1035. )