test_btrfs.py 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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. )
  276. def test_make_borg_snapshot_pattern_includes_slashdot_hack_and_stripped_pattern_path(
  277. subvolume_path,
  278. pattern,
  279. expected_pattern,
  280. ):
  281. flexmock(module.os).should_receive('getpid').and_return(1234)
  282. assert module.make_borg_snapshot_pattern(subvolume_path, pattern) == expected_pattern
  283. def test_dump_data_sources_snapshots_each_subvolume_and_updates_patterns():
  284. patterns = [Pattern('/foo'), Pattern('/mnt/subvol1')]
  285. config = {'btrfs': {}}
  286. flexmock(module).should_receive('get_subvolumes').and_return(
  287. (
  288. module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),
  289. module.Subvolume('/mnt/subvol2', contained_patterns=(Pattern('/mnt/subvol2'),)),
  290. ),
  291. )
  292. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  293. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  294. )
  295. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol2').and_return(
  296. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  297. )
  298. flexmock(module).should_receive('snapshot_subvolume').with_args(
  299. 'btrfs',
  300. '/mnt/subvol1',
  301. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  302. ).once()
  303. flexmock(module).should_receive('snapshot_subvolume').with_args(
  304. 'btrfs',
  305. '/mnt/subvol2',
  306. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  307. ).once()
  308. flexmock(module).should_receive('make_snapshot_exclude_pattern').with_args(
  309. '/mnt/subvol1',
  310. ).and_return(
  311. Pattern(
  312. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  313. Pattern_type.NO_RECURSE,
  314. Pattern_style.FNMATCH,
  315. ),
  316. )
  317. flexmock(module).should_receive('make_snapshot_exclude_pattern').with_args(
  318. '/mnt/subvol2',
  319. ).and_return(
  320. Pattern(
  321. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2/.borgmatic-1234',
  322. Pattern_type.NO_RECURSE,
  323. Pattern_style.FNMATCH,
  324. ),
  325. )
  326. flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
  327. '/mnt/subvol1',
  328. object,
  329. ).and_return(Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'))
  330. flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
  331. '/mnt/subvol2',
  332. object,
  333. ).and_return(Pattern('/mnt/subvol2/.borgmatic-1234/mnt/subvol2'))
  334. assert (
  335. module.dump_data_sources(
  336. hook_config=config['btrfs'],
  337. config=config,
  338. config_paths=('test.yaml',),
  339. borgmatic_runtime_directory='/run/borgmatic',
  340. patterns=patterns,
  341. dry_run=False,
  342. )
  343. == []
  344. )
  345. assert patterns == [
  346. Pattern('/foo'),
  347. Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'),
  348. Pattern(
  349. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  350. Pattern_type.NO_RECURSE,
  351. Pattern_style.FNMATCH,
  352. ),
  353. Pattern('/mnt/subvol2/.borgmatic-1234/mnt/subvol2'),
  354. Pattern(
  355. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2/.borgmatic-1234',
  356. Pattern_type.NO_RECURSE,
  357. Pattern_style.FNMATCH,
  358. ),
  359. ]
  360. assert config == {
  361. 'btrfs': {},
  362. }
  363. def test_dump_data_sources_uses_custom_btrfs_command_in_commands():
  364. patterns = [Pattern('/foo'), Pattern('/mnt/subvol1')]
  365. config = {'btrfs': {'btrfs_command': '/usr/local/bin/btrfs'}}
  366. flexmock(module).should_receive('get_subvolumes').and_return(
  367. (module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),),
  368. )
  369. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  370. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  371. )
  372. flexmock(module).should_receive('snapshot_subvolume').with_args(
  373. '/usr/local/bin/btrfs',
  374. '/mnt/subvol1',
  375. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  376. ).once()
  377. flexmock(module).should_receive('make_snapshot_exclude_pattern').with_args(
  378. '/mnt/subvol1',
  379. ).and_return(
  380. Pattern(
  381. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  382. Pattern_type.NO_RECURSE,
  383. Pattern_style.FNMATCH,
  384. ),
  385. )
  386. flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
  387. '/mnt/subvol1',
  388. object,
  389. ).and_return(Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'))
  390. assert (
  391. module.dump_data_sources(
  392. hook_config=config['btrfs'],
  393. config=config,
  394. config_paths=('test.yaml',),
  395. borgmatic_runtime_directory='/run/borgmatic',
  396. patterns=patterns,
  397. dry_run=False,
  398. )
  399. == []
  400. )
  401. assert patterns == [
  402. Pattern('/foo'),
  403. Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'),
  404. Pattern(
  405. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  406. Pattern_type.NO_RECURSE,
  407. Pattern_style.FNMATCH,
  408. ),
  409. ]
  410. assert config == {
  411. 'btrfs': {
  412. 'btrfs_command': '/usr/local/bin/btrfs',
  413. },
  414. }
  415. def test_dump_data_sources_uses_custom_findmnt_command_in_commands():
  416. patterns = [Pattern('/foo'), Pattern('/mnt/subvol1')]
  417. config = {'btrfs': {'findmnt_command': '/usr/local/bin/findmnt'}}
  418. flexmock(module).should_receive('get_subvolumes').with_args(
  419. 'btrfs',
  420. '/usr/local/bin/findmnt',
  421. patterns,
  422. ).and_return(
  423. (module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),),
  424. ).once()
  425. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  426. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  427. )
  428. flexmock(module).should_receive('snapshot_subvolume').with_args(
  429. 'btrfs',
  430. '/mnt/subvol1',
  431. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  432. ).once()
  433. flexmock(module).should_receive('make_snapshot_exclude_pattern').with_args(
  434. '/mnt/subvol1',
  435. ).and_return(
  436. Pattern(
  437. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  438. Pattern_type.NO_RECURSE,
  439. Pattern_style.FNMATCH,
  440. ),
  441. )
  442. flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
  443. '/mnt/subvol1',
  444. object,
  445. ).and_return(Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'))
  446. assert (
  447. module.dump_data_sources(
  448. hook_config=config['btrfs'],
  449. config=config,
  450. config_paths=('test.yaml',),
  451. borgmatic_runtime_directory='/run/borgmatic',
  452. patterns=patterns,
  453. dry_run=False,
  454. )
  455. == []
  456. )
  457. assert patterns == [
  458. Pattern('/foo'),
  459. Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'),
  460. Pattern(
  461. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  462. Pattern_type.NO_RECURSE,
  463. Pattern_style.FNMATCH,
  464. ),
  465. ]
  466. assert config == {
  467. 'btrfs': {
  468. 'findmnt_command': '/usr/local/bin/findmnt',
  469. },
  470. }
  471. def test_dump_data_sources_with_dry_run_skips_snapshot_and_patterns_update():
  472. patterns = [Pattern('/foo'), Pattern('/mnt/subvol1')]
  473. config = {'btrfs': {}}
  474. flexmock(module).should_receive('get_subvolumes').and_return(
  475. (module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),),
  476. )
  477. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  478. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  479. )
  480. flexmock(module).should_receive('snapshot_subvolume').never()
  481. flexmock(module).should_receive('make_snapshot_exclude_pattern').never()
  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=True,
  490. )
  491. == []
  492. )
  493. assert patterns == [Pattern('/foo'), Pattern('/mnt/subvol1')]
  494. assert config == {'btrfs': {}}
  495. def test_dump_data_sources_without_matching_subvolumes_skips_snapshot_and_patterns_update():
  496. patterns = [Pattern('/foo'), Pattern('/mnt/subvol1')]
  497. config = {'btrfs': {}}
  498. flexmock(module).should_receive('get_subvolumes').and_return(())
  499. flexmock(module).should_receive('make_snapshot_path').never()
  500. flexmock(module).should_receive('snapshot_subvolume').never()
  501. flexmock(module).should_receive('make_snapshot_exclude_pattern').never()
  502. assert (
  503. module.dump_data_sources(
  504. hook_config=config['btrfs'],
  505. config=config,
  506. config_paths=('test.yaml',),
  507. borgmatic_runtime_directory='/run/borgmatic',
  508. patterns=patterns,
  509. dry_run=False,
  510. )
  511. == []
  512. )
  513. assert patterns == [Pattern('/foo'), Pattern('/mnt/subvol1')]
  514. assert config == {'btrfs': {}}
  515. def test_dump_data_sources_snapshots_adds_to_existing_exclude_patterns():
  516. patterns = [Pattern('/foo'), Pattern('/mnt/subvol1')]
  517. config = {'btrfs': {}, 'exclude_patterns': ['/bar']}
  518. flexmock(module).should_receive('get_subvolumes').and_return(
  519. (
  520. module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),
  521. module.Subvolume('/mnt/subvol2', contained_patterns=(Pattern('/mnt/subvol2'),)),
  522. ),
  523. )
  524. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  525. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  526. )
  527. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol2').and_return(
  528. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  529. )
  530. flexmock(module).should_receive('snapshot_subvolume').with_args(
  531. 'btrfs',
  532. '/mnt/subvol1',
  533. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  534. ).once()
  535. flexmock(module).should_receive('snapshot_subvolume').with_args(
  536. 'btrfs',
  537. '/mnt/subvol2',
  538. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  539. ).once()
  540. flexmock(module).should_receive('make_snapshot_exclude_pattern').with_args(
  541. '/mnt/subvol1',
  542. ).and_return(
  543. Pattern(
  544. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  545. Pattern_type.NO_RECURSE,
  546. Pattern_style.FNMATCH,
  547. ),
  548. )
  549. flexmock(module).should_receive('make_snapshot_exclude_pattern').with_args(
  550. '/mnt/subvol2',
  551. ).and_return(
  552. Pattern(
  553. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2/.borgmatic-1234',
  554. Pattern_type.NO_RECURSE,
  555. Pattern_style.FNMATCH,
  556. ),
  557. )
  558. flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
  559. '/mnt/subvol1',
  560. object,
  561. ).and_return(Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'))
  562. flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
  563. '/mnt/subvol2',
  564. object,
  565. ).and_return(Pattern('/mnt/subvol2/.borgmatic-1234/mnt/subvol2'))
  566. assert (
  567. module.dump_data_sources(
  568. hook_config=config['btrfs'],
  569. config=config,
  570. config_paths=('test.yaml',),
  571. borgmatic_runtime_directory='/run/borgmatic',
  572. patterns=patterns,
  573. dry_run=False,
  574. )
  575. == []
  576. )
  577. assert patterns == [
  578. Pattern('/foo'),
  579. Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'),
  580. Pattern(
  581. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  582. Pattern_type.NO_RECURSE,
  583. Pattern_style.FNMATCH,
  584. ),
  585. Pattern('/mnt/subvol2/.borgmatic-1234/mnt/subvol2'),
  586. Pattern(
  587. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2/.borgmatic-1234',
  588. Pattern_type.NO_RECURSE,
  589. Pattern_style.FNMATCH,
  590. ),
  591. ]
  592. assert config == {
  593. 'btrfs': {},
  594. 'exclude_patterns': ['/bar'],
  595. }
  596. def test_remove_data_source_dumps_deletes_snapshots():
  597. config = {'btrfs': {}}
  598. flexmock(module).should_receive('get_subvolumes').and_return(
  599. (
  600. module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),
  601. module.Subvolume('/mnt/subvol2', contained_patterns=(Pattern('/mnt/subvol2'),)),
  602. ),
  603. )
  604. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  605. '/mnt/subvol1/.borgmatic-1234/./mnt/subvol1',
  606. )
  607. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol2').and_return(
  608. '/mnt/subvol2/.borgmatic-1234/./mnt/subvol2',
  609. )
  610. flexmock(module.borgmatic.config.paths).should_receive(
  611. 'replace_temporary_subdirectory_with_glob',
  612. ).with_args(
  613. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  614. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  615. ).and_return('/mnt/subvol1/.borgmatic-*/mnt/subvol1')
  616. flexmock(module.borgmatic.config.paths).should_receive(
  617. 'replace_temporary_subdirectory_with_glob',
  618. ).with_args(
  619. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  620. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  621. ).and_return('/mnt/subvol2/.borgmatic-*/mnt/subvol2')
  622. flexmock(module.glob).should_receive('glob').with_args(
  623. '/mnt/subvol1/.borgmatic-*/mnt/subvol1',
  624. ).and_return(
  625. ('/mnt/subvol1/.borgmatic-1234/mnt/subvol1', '/mnt/subvol1/.borgmatic-5678/mnt/subvol1'),
  626. )
  627. flexmock(module.glob).should_receive('glob').with_args(
  628. '/mnt/subvol2/.borgmatic-*/mnt/subvol2',
  629. ).and_return(
  630. ('/mnt/subvol2/.borgmatic-1234/mnt/subvol2', '/mnt/subvol2/.borgmatic-5678/mnt/subvol2'),
  631. )
  632. flexmock(module.os.path).should_receive('isdir').with_args(
  633. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  634. ).and_return(True)
  635. flexmock(module.os.path).should_receive('isdir').with_args(
  636. '/mnt/subvol1/.borgmatic-5678/mnt/subvol1',
  637. ).and_return(True)
  638. flexmock(module.os.path).should_receive('isdir').with_args(
  639. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  640. ).and_return(True)
  641. flexmock(module.os.path).should_receive('isdir').with_args(
  642. '/mnt/subvol2/.borgmatic-5678/mnt/subvol2',
  643. ).and_return(False)
  644. flexmock(module).should_receive('delete_snapshot').with_args(
  645. 'btrfs',
  646. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  647. ).once()
  648. flexmock(module).should_receive('delete_snapshot').with_args(
  649. 'btrfs',
  650. '/mnt/subvol1/.borgmatic-5678/mnt/subvol1',
  651. ).once()
  652. flexmock(module).should_receive('delete_snapshot').with_args(
  653. 'btrfs',
  654. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  655. ).once()
  656. flexmock(module).should_receive('delete_snapshot').with_args(
  657. 'btrfs',
  658. '/mnt/subvol2/.borgmatic-5678/mnt/subvol2',
  659. ).never()
  660. flexmock(module.os.path).should_receive('isdir').with_args(
  661. '/mnt/subvol1/.borgmatic-1234',
  662. ).and_return(True)
  663. flexmock(module.os.path).should_receive('isdir').with_args(
  664. '/mnt/subvol1/.borgmatic-5678',
  665. ).and_return(True)
  666. flexmock(module.os.path).should_receive('isdir').with_args(
  667. '/mnt/subvol2/.borgmatic-1234',
  668. ).and_return(True)
  669. flexmock(module.os.path).should_receive('isdir').with_args(
  670. '/mnt/subvol2/.borgmatic-5678',
  671. ).and_return(True)
  672. flexmock(module.shutil).should_receive('rmtree').with_args(
  673. '/mnt/subvol1/.borgmatic-1234',
  674. ).once()
  675. flexmock(module.shutil).should_receive('rmtree').with_args(
  676. '/mnt/subvol1/.borgmatic-5678',
  677. ).once()
  678. flexmock(module.shutil).should_receive('rmtree').with_args(
  679. '/mnt/subvol2/.borgmatic-1234',
  680. ).once()
  681. flexmock(module.shutil).should_receive('rmtree').with_args(
  682. '/mnt/subvol2/.borgmatic-5678',
  683. ).never()
  684. module.remove_data_source_dumps(
  685. hook_config=config['btrfs'],
  686. config=config,
  687. borgmatic_runtime_directory='/run/borgmatic',
  688. dry_run=False,
  689. )
  690. def test_remove_data_source_dumps_without_hook_configuration_bails():
  691. flexmock(module).should_receive('get_subvolumes').never()
  692. flexmock(module).should_receive('make_snapshot_path').never()
  693. flexmock(module.borgmatic.config.paths).should_receive(
  694. 'replace_temporary_subdirectory_with_glob',
  695. ).never()
  696. flexmock(module).should_receive('delete_snapshot').never()
  697. flexmock(module.shutil).should_receive('rmtree').never()
  698. module.remove_data_source_dumps(
  699. hook_config=None,
  700. config={'source_directories': '/mnt/subvolume'},
  701. borgmatic_runtime_directory='/run/borgmatic',
  702. dry_run=False,
  703. )
  704. def test_remove_data_source_dumps_with_get_subvolumes_file_not_found_error_bails():
  705. config = {'btrfs': {}}
  706. flexmock(module).should_receive('get_subvolumes').and_raise(FileNotFoundError)
  707. flexmock(module).should_receive('make_snapshot_path').never()
  708. flexmock(module.borgmatic.config.paths).should_receive(
  709. 'replace_temporary_subdirectory_with_glob',
  710. ).never()
  711. flexmock(module).should_receive('delete_snapshot').never()
  712. flexmock(module.shutil).should_receive('rmtree').never()
  713. module.remove_data_source_dumps(
  714. hook_config=config['btrfs'],
  715. config=config,
  716. borgmatic_runtime_directory='/run/borgmatic',
  717. dry_run=False,
  718. )
  719. def test_remove_data_source_dumps_with_get_subvolumes_called_process_error_bails():
  720. config = {'btrfs': {}}
  721. flexmock(module).should_receive('get_subvolumes').and_raise(
  722. module.subprocess.CalledProcessError(1, 'command', 'error'),
  723. )
  724. flexmock(module).should_receive('make_snapshot_path').never()
  725. flexmock(module.borgmatic.config.paths).should_receive(
  726. 'replace_temporary_subdirectory_with_glob',
  727. ).never()
  728. flexmock(module).should_receive('delete_snapshot').never()
  729. flexmock(module.shutil).should_receive('rmtree').never()
  730. module.remove_data_source_dumps(
  731. hook_config=config['btrfs'],
  732. config=config,
  733. borgmatic_runtime_directory='/run/borgmatic',
  734. dry_run=False,
  735. )
  736. def test_remove_data_source_dumps_with_dry_run_skips_deletes():
  737. config = {'btrfs': {}}
  738. flexmock(module).should_receive('get_subvolumes').and_return(
  739. (
  740. module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),
  741. module.Subvolume('/mnt/subvol2', contained_patterns=(Pattern('/mnt/subvol2'),)),
  742. ),
  743. )
  744. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  745. '/mnt/subvol1/.borgmatic-1234/./mnt/subvol1',
  746. )
  747. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol2').and_return(
  748. '/mnt/subvol2/.borgmatic-1234/./mnt/subvol2',
  749. )
  750. flexmock(module.borgmatic.config.paths).should_receive(
  751. 'replace_temporary_subdirectory_with_glob',
  752. ).with_args(
  753. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  754. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  755. ).and_return('/mnt/subvol1/.borgmatic-*/mnt/subvol1')
  756. flexmock(module.borgmatic.config.paths).should_receive(
  757. 'replace_temporary_subdirectory_with_glob',
  758. ).with_args(
  759. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  760. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  761. ).and_return('/mnt/subvol2/.borgmatic-*/mnt/subvol2')
  762. flexmock(module.glob).should_receive('glob').with_args(
  763. '/mnt/subvol1/.borgmatic-*/mnt/subvol1',
  764. ).and_return(
  765. ('/mnt/subvol1/.borgmatic-1234/mnt/subvol1', '/mnt/subvol1/.borgmatic-5678/mnt/subvol1'),
  766. )
  767. flexmock(module.glob).should_receive('glob').with_args(
  768. '/mnt/subvol2/.borgmatic-*/mnt/subvol2',
  769. ).and_return(
  770. ('/mnt/subvol2/.borgmatic-1234/mnt/subvol2', '/mnt/subvol2/.borgmatic-5678/mnt/subvol2'),
  771. )
  772. flexmock(module.os.path).should_receive('isdir').with_args(
  773. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  774. ).and_return(True)
  775. flexmock(module.os.path).should_receive('isdir').with_args(
  776. '/mnt/subvol1/.borgmatic-5678/mnt/subvol1',
  777. ).and_return(True)
  778. flexmock(module.os.path).should_receive('isdir').with_args(
  779. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  780. ).and_return(True)
  781. flexmock(module.os.path).should_receive('isdir').with_args(
  782. '/mnt/subvol2/.borgmatic-5678/mnt/subvol2',
  783. ).and_return(False)
  784. flexmock(module).should_receive('delete_snapshot').never()
  785. flexmock(module.shutil).should_receive('rmtree').never()
  786. module.remove_data_source_dumps(
  787. hook_config=config['btrfs'],
  788. config=config,
  789. borgmatic_runtime_directory='/run/borgmatic',
  790. dry_run=True,
  791. )
  792. def test_remove_data_source_dumps_without_subvolumes_skips_deletes():
  793. config = {'btrfs': {}}
  794. flexmock(module).should_receive('get_subvolumes').and_return(())
  795. flexmock(module).should_receive('make_snapshot_path').never()
  796. flexmock(module.borgmatic.config.paths).should_receive(
  797. 'replace_temporary_subdirectory_with_glob',
  798. ).never()
  799. flexmock(module).should_receive('delete_snapshot').never()
  800. flexmock(module.shutil).should_receive('rmtree').never()
  801. module.remove_data_source_dumps(
  802. hook_config=config['btrfs'],
  803. config=config,
  804. borgmatic_runtime_directory='/run/borgmatic',
  805. dry_run=False,
  806. )
  807. def test_remove_data_source_without_snapshots_skips_deletes():
  808. config = {'btrfs': {}}
  809. flexmock(module).should_receive('get_subvolumes').and_return(
  810. (
  811. module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),
  812. module.Subvolume('/mnt/subvol2', contained_patterns=(Pattern('/mnt/subvol2'),)),
  813. ),
  814. )
  815. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  816. '/mnt/subvol1/.borgmatic-1234/./mnt/subvol1',
  817. )
  818. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol2').and_return(
  819. '/mnt/subvol2/.borgmatic-1234/./mnt/subvol2',
  820. )
  821. flexmock(module.borgmatic.config.paths).should_receive(
  822. 'replace_temporary_subdirectory_with_glob',
  823. ).with_args(
  824. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  825. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  826. ).and_return('/mnt/subvol1/.borgmatic-*/mnt/subvol1')
  827. flexmock(module.borgmatic.config.paths).should_receive(
  828. 'replace_temporary_subdirectory_with_glob',
  829. ).with_args(
  830. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  831. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  832. ).and_return('/mnt/subvol2/.borgmatic-*/mnt/subvol2')
  833. flexmock(module.glob).should_receive('glob').and_return(())
  834. flexmock(module.os.path).should_receive('isdir').never()
  835. flexmock(module).should_receive('delete_snapshot').never()
  836. flexmock(module.shutil).should_receive('rmtree').never()
  837. module.remove_data_source_dumps(
  838. hook_config=config['btrfs'],
  839. config=config,
  840. borgmatic_runtime_directory='/run/borgmatic',
  841. dry_run=False,
  842. )
  843. def test_remove_data_source_dumps_with_delete_snapshot_file_not_found_error_bails():
  844. config = {'btrfs': {}}
  845. flexmock(module).should_receive('get_subvolumes').and_return(
  846. (
  847. module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),
  848. module.Subvolume('/mnt/subvol2', contained_patterns=(Pattern('/mnt/subvol2'),)),
  849. ),
  850. )
  851. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  852. '/mnt/subvol1/.borgmatic-1234/./mnt/subvol1',
  853. )
  854. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol2').and_return(
  855. '/mnt/subvol2/.borgmatic-1234/./mnt/subvol2',
  856. )
  857. flexmock(module.borgmatic.config.paths).should_receive(
  858. 'replace_temporary_subdirectory_with_glob',
  859. ).with_args(
  860. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  861. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  862. ).and_return('/mnt/subvol1/.borgmatic-*/mnt/subvol1')
  863. flexmock(module.borgmatic.config.paths).should_receive(
  864. 'replace_temporary_subdirectory_with_glob',
  865. ).with_args(
  866. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  867. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  868. ).and_return('/mnt/subvol2/.borgmatic-*/mnt/subvol2')
  869. flexmock(module.glob).should_receive('glob').with_args(
  870. '/mnt/subvol1/.borgmatic-*/mnt/subvol1',
  871. ).and_return(
  872. ('/mnt/subvol1/.borgmatic-1234/mnt/subvol1', '/mnt/subvol1/.borgmatic-5678/mnt/subvol1'),
  873. )
  874. flexmock(module.glob).should_receive('glob').with_args(
  875. '/mnt/subvol2/.borgmatic-*/mnt/subvol2',
  876. ).and_return(
  877. ('/mnt/subvol2/.borgmatic-1234/mnt/subvol2', '/mnt/subvol2/.borgmatic-5678/mnt/subvol2'),
  878. )
  879. flexmock(module.os.path).should_receive('isdir').with_args(
  880. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  881. ).and_return(True)
  882. flexmock(module.os.path).should_receive('isdir').with_args(
  883. '/mnt/subvol1/.borgmatic-5678/mnt/subvol1',
  884. ).and_return(True)
  885. flexmock(module.os.path).should_receive('isdir').with_args(
  886. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  887. ).and_return(True)
  888. flexmock(module.os.path).should_receive('isdir').with_args(
  889. '/mnt/subvol2/.borgmatic-5678/mnt/subvol2',
  890. ).and_return(False)
  891. flexmock(module).should_receive('delete_snapshot').and_raise(FileNotFoundError)
  892. flexmock(module.shutil).should_receive('rmtree').never()
  893. module.remove_data_source_dumps(
  894. hook_config=config['btrfs'],
  895. config=config,
  896. borgmatic_runtime_directory='/run/borgmatic',
  897. dry_run=False,
  898. )
  899. def test_remove_data_source_dumps_with_delete_snapshot_called_process_error_bails():
  900. config = {'btrfs': {}}
  901. flexmock(module).should_receive('get_subvolumes').and_return(
  902. (
  903. module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),
  904. module.Subvolume('/mnt/subvol2', contained_patterns=(Pattern('/mnt/subvol2'),)),
  905. ),
  906. )
  907. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  908. '/mnt/subvol1/.borgmatic-1234/./mnt/subvol1',
  909. )
  910. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol2').and_return(
  911. '/mnt/subvol2/.borgmatic-1234/./mnt/subvol2',
  912. )
  913. flexmock(module.borgmatic.config.paths).should_receive(
  914. 'replace_temporary_subdirectory_with_glob',
  915. ).with_args(
  916. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  917. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  918. ).and_return('/mnt/subvol1/.borgmatic-*/mnt/subvol1')
  919. flexmock(module.borgmatic.config.paths).should_receive(
  920. 'replace_temporary_subdirectory_with_glob',
  921. ).with_args(
  922. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  923. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  924. ).and_return('/mnt/subvol2/.borgmatic-*/mnt/subvol2')
  925. flexmock(module.glob).should_receive('glob').with_args(
  926. '/mnt/subvol1/.borgmatic-*/mnt/subvol1',
  927. ).and_return(
  928. ('/mnt/subvol1/.borgmatic-1234/mnt/subvol1', '/mnt/subvol1/.borgmatic-5678/mnt/subvol1'),
  929. )
  930. flexmock(module.glob).should_receive('glob').with_args(
  931. '/mnt/subvol2/.borgmatic-*/mnt/subvol2',
  932. ).and_return(
  933. ('/mnt/subvol2/.borgmatic-1234/mnt/subvol2', '/mnt/subvol2/.borgmatic-5678/mnt/subvol2'),
  934. )
  935. flexmock(module.os.path).should_receive('isdir').with_args(
  936. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  937. ).and_return(True)
  938. flexmock(module.os.path).should_receive('isdir').with_args(
  939. '/mnt/subvol1/.borgmatic-5678/mnt/subvol1',
  940. ).and_return(True)
  941. flexmock(module.os.path).should_receive('isdir').with_args(
  942. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  943. ).and_return(True)
  944. flexmock(module.os.path).should_receive('isdir').with_args(
  945. '/mnt/subvol2/.borgmatic-5678/mnt/subvol2',
  946. ).and_return(False)
  947. flexmock(module).should_receive('delete_snapshot').and_raise(
  948. module.subprocess.CalledProcessError(1, 'command', 'error'),
  949. )
  950. flexmock(module.shutil).should_receive('rmtree').never()
  951. module.remove_data_source_dumps(
  952. hook_config=config['btrfs'],
  953. config=config,
  954. borgmatic_runtime_directory='/run/borgmatic',
  955. dry_run=False,
  956. )
  957. def test_remove_data_source_dumps_with_root_subvolume_skips_duplicate_removal():
  958. config = {'btrfs': {}}
  959. flexmock(module).should_receive('get_subvolumes').and_return(
  960. (module.Subvolume('/', contained_patterns=(Pattern('/etc'),)),),
  961. )
  962. flexmock(module).should_receive('make_snapshot_path').with_args('/').and_return(
  963. '/.borgmatic-1234',
  964. )
  965. flexmock(module.borgmatic.config.paths).should_receive(
  966. 'replace_temporary_subdirectory_with_glob',
  967. ).with_args(
  968. '/.borgmatic-1234',
  969. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  970. ).and_return('/.borgmatic-*')
  971. flexmock(module.glob).should_receive('glob').with_args('/.borgmatic-*').and_return(
  972. ('/.borgmatic-1234', '/.borgmatic-5678'),
  973. )
  974. flexmock(module.os.path).should_receive('isdir').with_args('/.borgmatic-1234').and_return(
  975. True,
  976. ).and_return(False)
  977. flexmock(module.os.path).should_receive('isdir').with_args('/.borgmatic-5678').and_return(
  978. True,
  979. ).and_return(False)
  980. flexmock(module).should_receive('delete_snapshot').with_args('btrfs', '/.borgmatic-1234').once()
  981. flexmock(module).should_receive('delete_snapshot').with_args('btrfs', '/.borgmatic-5678').once()
  982. flexmock(module.os.path).should_receive('isdir').with_args('').and_return(False)
  983. flexmock(module.shutil).should_receive('rmtree').never()
  984. module.remove_data_source_dumps(
  985. hook_config=config['btrfs'],
  986. config=config,
  987. borgmatic_runtime_directory='/run/borgmatic',
  988. dry_run=False,
  989. )