2
0

test_btrfs.py 41 KB

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