test_btrfs.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.borg.pattern import Pattern, Pattern_style, Pattern_type
  4. from borgmatic.hooks.data_source import btrfs as module
  5. def test_get_filesystem_mount_points_parses_findmnt_output():
  6. flexmock(module.borgmatic.execute).should_receive(
  7. 'execute_command_and_capture_output'
  8. ).and_return(
  9. '''{
  10. "filesystems": [
  11. {
  12. "target": "/mnt0",
  13. "source": "/dev/loop0",
  14. "fstype": "btrfs",
  15. "options": "rw,relatime,ssd,space_cache=v2,subvolid=5,subvol=/"
  16. },
  17. {
  18. "target": "/mnt1",
  19. "source": "/dev/loop0",
  20. "fstype": "btrfs",
  21. "options": "rw,relatime,ssd,space_cache=v2,subvolid=5,subvol=/"
  22. }
  23. ]
  24. }
  25. '''
  26. )
  27. assert module.get_filesystem_mount_points('findmnt') == ('/mnt0', '/mnt1')
  28. def test_get_filesystem_mount_points_with_invalid_findmnt_json_errors():
  29. flexmock(module.borgmatic.execute).should_receive(
  30. 'execute_command_and_capture_output'
  31. ).and_return('{')
  32. with pytest.raises(ValueError):
  33. module.get_filesystem_mount_points('findmnt')
  34. def test_get_filesystem_mount_points_with_findmnt_json_missing_filesystems_errors():
  35. flexmock(module.borgmatic.execute).should_receive(
  36. 'execute_command_and_capture_output'
  37. ).and_return('{"wtf": "something is wrong here"}')
  38. with pytest.raises(ValueError):
  39. module.get_filesystem_mount_points('findmnt')
  40. def test_get_subvolumes_for_filesystem_parses_subvolume_list_output():
  41. flexmock(module.borgmatic.execute).should_receive(
  42. 'execute_command_and_capture_output'
  43. ).and_return(
  44. 'ID 270 gen 107 top level 5 path subvol1\nID 272 gen 74 top level 5 path subvol2\n'
  45. )
  46. assert module.get_subvolumes_for_filesystem('btrfs', '/mnt') == (
  47. '/mnt',
  48. '/mnt/subvol1',
  49. '/mnt/subvol2',
  50. )
  51. def test_get_subvolumes_for_filesystem_skips_empty_subvolume_paths():
  52. flexmock(module.borgmatic.execute).should_receive(
  53. 'execute_command_and_capture_output'
  54. ).and_return('\n \nID 272 gen 74 top level 5 path subvol2\n')
  55. assert module.get_subvolumes_for_filesystem('btrfs', '/mnt') == ('/mnt', '/mnt/subvol2')
  56. def test_get_subvolumes_for_filesystem_skips_empty_filesystem_mount_points():
  57. flexmock(module.borgmatic.execute).should_receive(
  58. 'execute_command_and_capture_output'
  59. ).and_return(
  60. 'ID 270 gen 107 top level 5 path subvol1\nID 272 gen 74 top level 5 path subvol2\n'
  61. )
  62. assert module.get_subvolumes_for_filesystem('btrfs', ' ') == ()
  63. def test_get_subvolumes_collects_subvolumes_matching_patterns_from_all_filesystems():
  64. flexmock(module).should_receive('get_filesystem_mount_points').and_return(('/mnt1', '/mnt2'))
  65. flexmock(module).should_receive('get_subvolumes_for_filesystem').with_args(
  66. 'btrfs', '/mnt1'
  67. ).and_return(('/one', '/two'))
  68. flexmock(module).should_receive('get_subvolumes_for_filesystem').with_args(
  69. 'btrfs', '/mnt2'
  70. ).and_return(('/three', '/four'))
  71. for path in ('/one', '/four'):
  72. flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
  73. 'get_contained_patterns'
  74. ).with_args(path, object).and_return((Pattern(path),))
  75. for path in ('/two', '/three'):
  76. flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
  77. 'get_contained_patterns'
  78. ).with_args(path, object).and_return(())
  79. assert module.get_subvolumes(
  80. 'btrfs',
  81. 'findmnt',
  82. patterns=[
  83. Pattern('/one'),
  84. Pattern('/four'),
  85. Pattern('/five'),
  86. Pattern('/six'),
  87. Pattern('/mnt2'),
  88. Pattern('/mnt3'),
  89. ],
  90. ) == (
  91. module.Subvolume('/four', contained_patterns=(Pattern('/four'),)),
  92. module.Subvolume('/one', contained_patterns=(Pattern('/one'),)),
  93. )
  94. def test_get_subvolumes_without_patterns_collects_all_subvolumes_from_all_filesystems():
  95. flexmock(module).should_receive('get_filesystem_mount_points').and_return(('/mnt1', '/mnt2'))
  96. flexmock(module).should_receive('get_subvolumes_for_filesystem').with_args(
  97. 'btrfs', '/mnt1'
  98. ).and_return(('/one', '/two'))
  99. flexmock(module).should_receive('get_subvolumes_for_filesystem').with_args(
  100. 'btrfs', '/mnt2'
  101. ).and_return(('/three', '/four'))
  102. for path in ('/one', '/two', '/three', '/four'):
  103. flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
  104. 'get_contained_patterns'
  105. ).with_args(path, object).and_return((Pattern(path),))
  106. assert module.get_subvolumes('btrfs', 'findmnt') == (
  107. module.Subvolume('/four', contained_patterns=(Pattern('/four'),)),
  108. module.Subvolume('/one', contained_patterns=(Pattern('/one'),)),
  109. module.Subvolume('/three', contained_patterns=(Pattern('/three'),)),
  110. module.Subvolume('/two', contained_patterns=(Pattern('/two'),)),
  111. )
  112. @pytest.mark.parametrize(
  113. 'subvolume_path,expected_snapshot_path',
  114. (
  115. ('/foo/bar', '/foo/bar/.borgmatic-snapshot-1234/foo/bar'),
  116. ('/', '/.borgmatic-snapshot-1234'),
  117. ),
  118. )
  119. def test_make_snapshot_path_includes_stripped_subvolume_path(
  120. subvolume_path, expected_snapshot_path
  121. ):
  122. flexmock(module.os).should_receive('getpid').and_return(1234)
  123. assert module.make_snapshot_path(subvolume_path) == expected_snapshot_path
  124. @pytest.mark.parametrize(
  125. 'subvolume_path,pattern_path,expected_path',
  126. (
  127. ('/foo/bar', '/foo/bar/baz', '/foo/bar/.borgmatic-snapshot-1234/./foo/bar/baz'),
  128. ('/foo/bar', '/foo/bar', '/foo/bar/.borgmatic-snapshot-1234/./foo/bar'),
  129. ('/', '/foo', '/.borgmatic-snapshot-1234/./foo'),
  130. ('/', '/', '/.borgmatic-snapshot-1234/./'),
  131. ),
  132. )
  133. def test_make_borg_snapshot_pattern_includes_slashdot_hack_and_stripped_pattern_path(
  134. subvolume_path, pattern_path, expected_path
  135. ):
  136. flexmock(module.os).should_receive('getpid').and_return(1234)
  137. assert module.make_borg_snapshot_pattern(subvolume_path, Pattern(pattern_path)) == Pattern(
  138. expected_path
  139. )
  140. def test_dump_data_sources_snapshots_each_subvolume_and_updates_patterns():
  141. patterns = [Pattern('/foo'), Pattern('/mnt/subvol1')]
  142. config = {'btrfs': {}}
  143. flexmock(module).should_receive('get_subvolumes').and_return(
  144. (
  145. module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),
  146. module.Subvolume('/mnt/subvol2', contained_patterns=(Pattern('/mnt/subvol2'),)),
  147. )
  148. )
  149. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  150. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1'
  151. )
  152. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol2').and_return(
  153. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2'
  154. )
  155. flexmock(module).should_receive('snapshot_subvolume').with_args(
  156. 'btrfs', '/mnt/subvol1', '/mnt/subvol1/.borgmatic-1234/mnt/subvol1'
  157. ).once()
  158. flexmock(module).should_receive('snapshot_subvolume').with_args(
  159. 'btrfs', '/mnt/subvol2', '/mnt/subvol2/.borgmatic-1234/mnt/subvol2'
  160. ).once()
  161. flexmock(module).should_receive('make_snapshot_exclude_pattern').with_args(
  162. '/mnt/subvol1'
  163. ).and_return(
  164. Pattern(
  165. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  166. Pattern_type.EXCLUDE,
  167. Pattern_style.FNMATCH,
  168. )
  169. )
  170. flexmock(module).should_receive('make_snapshot_exclude_pattern').with_args(
  171. '/mnt/subvol2'
  172. ).and_return(
  173. Pattern(
  174. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2/.borgmatic-1234',
  175. Pattern_type.EXCLUDE,
  176. Pattern_style.FNMATCH,
  177. )
  178. )
  179. flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
  180. '/mnt/subvol1', object
  181. ).and_return(Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'))
  182. flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
  183. '/mnt/subvol2', object
  184. ).and_return(Pattern('/mnt/subvol2/.borgmatic-1234/mnt/subvol2'))
  185. assert (
  186. module.dump_data_sources(
  187. hook_config=config['btrfs'],
  188. config=config,
  189. log_prefix='test',
  190. config_paths=('test.yaml',),
  191. borgmatic_runtime_directory='/run/borgmatic',
  192. patterns=patterns,
  193. dry_run=False,
  194. )
  195. == []
  196. )
  197. assert patterns == [
  198. Pattern('/foo'),
  199. Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'),
  200. Pattern(
  201. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  202. Pattern_type.EXCLUDE,
  203. Pattern_style.FNMATCH,
  204. ),
  205. Pattern('/mnt/subvol2/.borgmatic-1234/mnt/subvol2'),
  206. Pattern(
  207. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2/.borgmatic-1234',
  208. Pattern_type.EXCLUDE,
  209. Pattern_style.FNMATCH,
  210. ),
  211. ]
  212. assert config == {
  213. 'btrfs': {},
  214. }
  215. def test_dump_data_sources_uses_custom_btrfs_command_in_commands():
  216. patterns = [Pattern('/foo'), Pattern('/mnt/subvol1')]
  217. config = {'btrfs': {'btrfs_command': '/usr/local/bin/btrfs'}}
  218. flexmock(module).should_receive('get_subvolumes').and_return(
  219. (module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),)
  220. )
  221. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  222. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1'
  223. )
  224. flexmock(module).should_receive('snapshot_subvolume').with_args(
  225. '/usr/local/bin/btrfs', '/mnt/subvol1', '/mnt/subvol1/.borgmatic-1234/mnt/subvol1'
  226. ).once()
  227. flexmock(module).should_receive('make_snapshot_exclude_pattern').with_args(
  228. '/mnt/subvol1'
  229. ).and_return(
  230. Pattern(
  231. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  232. Pattern_type.EXCLUDE,
  233. Pattern_style.FNMATCH,
  234. )
  235. )
  236. flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
  237. '/mnt/subvol1', object
  238. ).and_return(Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'))
  239. assert (
  240. module.dump_data_sources(
  241. hook_config=config['btrfs'],
  242. config=config,
  243. log_prefix='test',
  244. config_paths=('test.yaml',),
  245. borgmatic_runtime_directory='/run/borgmatic',
  246. patterns=patterns,
  247. dry_run=False,
  248. )
  249. == []
  250. )
  251. assert patterns == [
  252. Pattern('/foo'),
  253. Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'),
  254. Pattern(
  255. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  256. Pattern_type.EXCLUDE,
  257. Pattern_style.FNMATCH,
  258. ),
  259. ]
  260. assert config == {
  261. 'btrfs': {
  262. 'btrfs_command': '/usr/local/bin/btrfs',
  263. },
  264. }
  265. def test_dump_data_sources_uses_custom_findmnt_command_in_commands():
  266. patterns = [Pattern('/foo'), Pattern('/mnt/subvol1')]
  267. config = {'btrfs': {'findmnt_command': '/usr/local/bin/findmnt'}}
  268. flexmock(module).should_receive('get_subvolumes').with_args(
  269. 'btrfs', '/usr/local/bin/findmnt', patterns
  270. ).and_return(
  271. (module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),)
  272. ).once()
  273. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  274. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1'
  275. )
  276. flexmock(module).should_receive('snapshot_subvolume').with_args(
  277. 'btrfs', '/mnt/subvol1', '/mnt/subvol1/.borgmatic-1234/mnt/subvol1'
  278. ).once()
  279. flexmock(module).should_receive('make_snapshot_exclude_pattern').with_args(
  280. '/mnt/subvol1'
  281. ).and_return(
  282. Pattern(
  283. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  284. Pattern_type.EXCLUDE,
  285. Pattern_style.FNMATCH,
  286. )
  287. )
  288. flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
  289. '/mnt/subvol1', object
  290. ).and_return(Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'))
  291. assert (
  292. module.dump_data_sources(
  293. hook_config=config['btrfs'],
  294. config=config,
  295. log_prefix='test',
  296. config_paths=('test.yaml',),
  297. borgmatic_runtime_directory='/run/borgmatic',
  298. patterns=patterns,
  299. dry_run=False,
  300. )
  301. == []
  302. )
  303. assert patterns == [
  304. Pattern('/foo'),
  305. Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'),
  306. Pattern(
  307. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  308. Pattern_type.EXCLUDE,
  309. Pattern_style.FNMATCH,
  310. ),
  311. ]
  312. assert config == {
  313. 'btrfs': {
  314. 'findmnt_command': '/usr/local/bin/findmnt',
  315. },
  316. }
  317. def test_dump_data_sources_with_dry_run_skips_snapshot_and_patterns_update():
  318. patterns = [Pattern('/foo'), Pattern('/mnt/subvol1')]
  319. config = {'btrfs': {}}
  320. flexmock(module).should_receive('get_subvolumes').and_return(
  321. (module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),)
  322. )
  323. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  324. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1'
  325. )
  326. flexmock(module).should_receive('snapshot_subvolume').never()
  327. flexmock(module).should_receive('make_snapshot_exclude_pattern').never()
  328. assert (
  329. module.dump_data_sources(
  330. hook_config=config['btrfs'],
  331. config=config,
  332. log_prefix='test',
  333. config_paths=('test.yaml',),
  334. borgmatic_runtime_directory='/run/borgmatic',
  335. patterns=patterns,
  336. dry_run=True,
  337. )
  338. == []
  339. )
  340. assert patterns == [Pattern('/foo'), Pattern('/mnt/subvol1')]
  341. assert config == {'btrfs': {}}
  342. def test_dump_data_sources_without_matching_subvolumes_skips_snapshot_and_patterns_update():
  343. patterns = [Pattern('/foo'), Pattern('/mnt/subvol1')]
  344. config = {'btrfs': {}}
  345. flexmock(module).should_receive('get_subvolumes').and_return(())
  346. flexmock(module).should_receive('make_snapshot_path').never()
  347. flexmock(module).should_receive('snapshot_subvolume').never()
  348. flexmock(module).should_receive('make_snapshot_exclude_pattern').never()
  349. assert (
  350. module.dump_data_sources(
  351. hook_config=config['btrfs'],
  352. config=config,
  353. log_prefix='test',
  354. config_paths=('test.yaml',),
  355. borgmatic_runtime_directory='/run/borgmatic',
  356. patterns=patterns,
  357. dry_run=False,
  358. )
  359. == []
  360. )
  361. assert patterns == [Pattern('/foo'), Pattern('/mnt/subvol1')]
  362. assert config == {'btrfs': {}}
  363. def test_dump_data_sources_snapshots_adds_to_existing_exclude_patterns():
  364. patterns = [Pattern('/foo'), Pattern('/mnt/subvol1')]
  365. config = {'btrfs': {}, 'exclude_patterns': ['/bar']}
  366. flexmock(module).should_receive('get_subvolumes').and_return(
  367. (
  368. module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),
  369. module.Subvolume('/mnt/subvol2', contained_patterns=(Pattern('/mnt/subvol2'),)),
  370. )
  371. )
  372. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  373. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1'
  374. )
  375. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol2').and_return(
  376. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2'
  377. )
  378. flexmock(module).should_receive('snapshot_subvolume').with_args(
  379. 'btrfs', '/mnt/subvol1', '/mnt/subvol1/.borgmatic-1234/mnt/subvol1'
  380. ).once()
  381. flexmock(module).should_receive('snapshot_subvolume').with_args(
  382. 'btrfs', '/mnt/subvol2', '/mnt/subvol2/.borgmatic-1234/mnt/subvol2'
  383. ).once()
  384. flexmock(module).should_receive('make_snapshot_exclude_pattern').with_args(
  385. '/mnt/subvol1'
  386. ).and_return(
  387. Pattern(
  388. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  389. Pattern_type.EXCLUDE,
  390. Pattern_style.FNMATCH,
  391. )
  392. )
  393. flexmock(module).should_receive('make_snapshot_exclude_pattern').with_args(
  394. '/mnt/subvol2'
  395. ).and_return(
  396. Pattern(
  397. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2/.borgmatic-1234',
  398. Pattern_type.EXCLUDE,
  399. Pattern_style.FNMATCH,
  400. )
  401. )
  402. flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
  403. '/mnt/subvol1', object
  404. ).and_return(Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'))
  405. flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
  406. '/mnt/subvol2', object
  407. ).and_return(Pattern('/mnt/subvol2/.borgmatic-1234/mnt/subvol2'))
  408. assert (
  409. module.dump_data_sources(
  410. hook_config=config['btrfs'],
  411. config=config,
  412. log_prefix='test',
  413. config_paths=('test.yaml',),
  414. borgmatic_runtime_directory='/run/borgmatic',
  415. patterns=patterns,
  416. dry_run=False,
  417. )
  418. == []
  419. )
  420. assert patterns == [
  421. Pattern('/foo'),
  422. Pattern('/mnt/subvol1/.borgmatic-1234/mnt/subvol1'),
  423. Pattern(
  424. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1/.borgmatic-1234',
  425. Pattern_type.EXCLUDE,
  426. Pattern_style.FNMATCH,
  427. ),
  428. Pattern('/mnt/subvol2/.borgmatic-1234/mnt/subvol2'),
  429. Pattern(
  430. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2/.borgmatic-1234',
  431. Pattern_type.EXCLUDE,
  432. Pattern_style.FNMATCH,
  433. ),
  434. ]
  435. assert config == {
  436. 'btrfs': {},
  437. 'exclude_patterns': ['/bar'],
  438. }
  439. def test_remove_data_source_dumps_deletes_snapshots():
  440. config = {'btrfs': {}}
  441. flexmock(module).should_receive('get_subvolumes').and_return(
  442. (
  443. module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),
  444. module.Subvolume('/mnt/subvol2', contained_patterns=(Pattern('/mnt/subvol2'),)),
  445. )
  446. )
  447. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  448. '/mnt/subvol1/.borgmatic-1234/./mnt/subvol1'
  449. )
  450. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol2').and_return(
  451. '/mnt/subvol2/.borgmatic-1234/./mnt/subvol2'
  452. )
  453. flexmock(module.borgmatic.config.paths).should_receive(
  454. 'replace_temporary_subdirectory_with_glob'
  455. ).with_args(
  456. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  457. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  458. ).and_return(
  459. '/mnt/subvol1/.borgmatic-*/mnt/subvol1'
  460. )
  461. flexmock(module.borgmatic.config.paths).should_receive(
  462. 'replace_temporary_subdirectory_with_glob'
  463. ).with_args(
  464. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  465. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  466. ).and_return(
  467. '/mnt/subvol2/.borgmatic-*/mnt/subvol2'
  468. )
  469. flexmock(module.glob).should_receive('glob').with_args(
  470. '/mnt/subvol1/.borgmatic-*/mnt/subvol1'
  471. ).and_return(
  472. ('/mnt/subvol1/.borgmatic-1234/mnt/subvol1', '/mnt/subvol1/.borgmatic-5678/mnt/subvol1')
  473. )
  474. flexmock(module.glob).should_receive('glob').with_args(
  475. '/mnt/subvol2/.borgmatic-*/mnt/subvol2'
  476. ).and_return(
  477. ('/mnt/subvol2/.borgmatic-1234/mnt/subvol2', '/mnt/subvol2/.borgmatic-5678/mnt/subvol2')
  478. )
  479. flexmock(module.os.path).should_receive('isdir').with_args(
  480. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1'
  481. ).and_return(True)
  482. flexmock(module.os.path).should_receive('isdir').with_args(
  483. '/mnt/subvol1/.borgmatic-5678/mnt/subvol1'
  484. ).and_return(True)
  485. flexmock(module.os.path).should_receive('isdir').with_args(
  486. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2'
  487. ).and_return(True)
  488. flexmock(module.os.path).should_receive('isdir').with_args(
  489. '/mnt/subvol2/.borgmatic-5678/mnt/subvol2'
  490. ).and_return(False)
  491. flexmock(module).should_receive('delete_snapshot').with_args(
  492. 'btrfs', '/mnt/subvol1/.borgmatic-1234/mnt/subvol1'
  493. ).once()
  494. flexmock(module).should_receive('delete_snapshot').with_args(
  495. 'btrfs', '/mnt/subvol1/.borgmatic-5678/mnt/subvol1'
  496. ).once()
  497. flexmock(module).should_receive('delete_snapshot').with_args(
  498. 'btrfs', '/mnt/subvol2/.borgmatic-1234/mnt/subvol2'
  499. ).once()
  500. flexmock(module).should_receive('delete_snapshot').with_args(
  501. 'btrfs', '/mnt/subvol2/.borgmatic-5678/mnt/subvol2'
  502. ).never()
  503. flexmock(module.shutil).should_receive('rmtree').with_args(
  504. '/mnt/subvol1/.borgmatic-1234'
  505. ).once()
  506. flexmock(module.shutil).should_receive('rmtree').with_args(
  507. '/mnt/subvol1/.borgmatic-5678'
  508. ).once()
  509. flexmock(module.shutil).should_receive('rmtree').with_args(
  510. '/mnt/subvol2/.borgmatic-1234'
  511. ).once()
  512. flexmock(module.shutil).should_receive('rmtree').with_args(
  513. '/mnt/subvol2/.borgmatic-5678'
  514. ).never()
  515. module.remove_data_source_dumps(
  516. hook_config=config['btrfs'],
  517. config=config,
  518. log_prefix='test',
  519. borgmatic_runtime_directory='/run/borgmatic',
  520. dry_run=False,
  521. )
  522. def test_remove_data_source_dumps_without_hook_configuration_bails():
  523. flexmock(module).should_receive('get_subvolumes').never()
  524. flexmock(module).should_receive('make_snapshot_path').never()
  525. flexmock(module.borgmatic.config.paths).should_receive(
  526. 'replace_temporary_subdirectory_with_glob'
  527. ).never()
  528. flexmock(module).should_receive('delete_snapshot').never()
  529. flexmock(module.shutil).should_receive('rmtree').never()
  530. module.remove_data_source_dumps(
  531. hook_config=None,
  532. config={'source_directories': '/mnt/subvolume'},
  533. log_prefix='test',
  534. borgmatic_runtime_directory='/run/borgmatic',
  535. dry_run=False,
  536. )
  537. def test_remove_data_source_dumps_with_get_subvolumes_file_not_found_error_bails():
  538. config = {'btrfs': {}}
  539. flexmock(module).should_receive('get_subvolumes').and_raise(FileNotFoundError)
  540. flexmock(module).should_receive('make_snapshot_path').never()
  541. flexmock(module.borgmatic.config.paths).should_receive(
  542. 'replace_temporary_subdirectory_with_glob'
  543. ).never()
  544. flexmock(module).should_receive('delete_snapshot').never()
  545. flexmock(module.shutil).should_receive('rmtree').never()
  546. module.remove_data_source_dumps(
  547. hook_config=config['btrfs'],
  548. config=config,
  549. log_prefix='test',
  550. borgmatic_runtime_directory='/run/borgmatic',
  551. dry_run=False,
  552. )
  553. def test_remove_data_source_dumps_with_get_subvolumes_called_process_error_bails():
  554. config = {'btrfs': {}}
  555. flexmock(module).should_receive('get_subvolumes').and_raise(
  556. module.subprocess.CalledProcessError(1, 'command', 'error')
  557. )
  558. flexmock(module).should_receive('make_snapshot_path').never()
  559. flexmock(module.borgmatic.config.paths).should_receive(
  560. 'replace_temporary_subdirectory_with_glob'
  561. ).never()
  562. flexmock(module).should_receive('delete_snapshot').never()
  563. flexmock(module.shutil).should_receive('rmtree').never()
  564. module.remove_data_source_dumps(
  565. hook_config=config['btrfs'],
  566. config=config,
  567. log_prefix='test',
  568. borgmatic_runtime_directory='/run/borgmatic',
  569. dry_run=False,
  570. )
  571. def test_remove_data_source_dumps_with_dry_run_skips_deletes():
  572. config = {'btrfs': {}}
  573. flexmock(module).should_receive('get_subvolumes').and_return(
  574. (
  575. module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),
  576. module.Subvolume('/mnt/subvol2', contained_patterns=(Pattern('/mnt/subvol2'),)),
  577. )
  578. )
  579. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  580. '/mnt/subvol1/.borgmatic-1234/./mnt/subvol1'
  581. )
  582. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol2').and_return(
  583. '/mnt/subvol2/.borgmatic-1234/./mnt/subvol2'
  584. )
  585. flexmock(module.borgmatic.config.paths).should_receive(
  586. 'replace_temporary_subdirectory_with_glob'
  587. ).with_args(
  588. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  589. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  590. ).and_return(
  591. '/mnt/subvol1/.borgmatic-*/mnt/subvol1'
  592. )
  593. flexmock(module.borgmatic.config.paths).should_receive(
  594. 'replace_temporary_subdirectory_with_glob'
  595. ).with_args(
  596. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  597. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  598. ).and_return(
  599. '/mnt/subvol2/.borgmatic-*/mnt/subvol2'
  600. )
  601. flexmock(module.glob).should_receive('glob').with_args(
  602. '/mnt/subvol1/.borgmatic-*/mnt/subvol1'
  603. ).and_return(
  604. ('/mnt/subvol1/.borgmatic-1234/mnt/subvol1', '/mnt/subvol1/.borgmatic-5678/mnt/subvol1')
  605. )
  606. flexmock(module.glob).should_receive('glob').with_args(
  607. '/mnt/subvol2/.borgmatic-*/mnt/subvol2'
  608. ).and_return(
  609. ('/mnt/subvol2/.borgmatic-1234/mnt/subvol2', '/mnt/subvol2/.borgmatic-5678/mnt/subvol2')
  610. )
  611. flexmock(module.os.path).should_receive('isdir').with_args(
  612. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1'
  613. ).and_return(True)
  614. flexmock(module.os.path).should_receive('isdir').with_args(
  615. '/mnt/subvol1/.borgmatic-5678/mnt/subvol1'
  616. ).and_return(True)
  617. flexmock(module.os.path).should_receive('isdir').with_args(
  618. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2'
  619. ).and_return(True)
  620. flexmock(module.os.path).should_receive('isdir').with_args(
  621. '/mnt/subvol2/.borgmatic-5678/mnt/subvol2'
  622. ).and_return(False)
  623. flexmock(module).should_receive('delete_snapshot').never()
  624. flexmock(module.shutil).should_receive('rmtree').never()
  625. module.remove_data_source_dumps(
  626. hook_config=config['btrfs'],
  627. config=config,
  628. log_prefix='test',
  629. borgmatic_runtime_directory='/run/borgmatic',
  630. dry_run=True,
  631. )
  632. def test_remove_data_source_dumps_without_subvolumes_skips_deletes():
  633. config = {'btrfs': {}}
  634. flexmock(module).should_receive('get_subvolumes').and_return(())
  635. flexmock(module).should_receive('make_snapshot_path').never()
  636. flexmock(module.borgmatic.config.paths).should_receive(
  637. 'replace_temporary_subdirectory_with_glob'
  638. ).never()
  639. flexmock(module).should_receive('delete_snapshot').never()
  640. flexmock(module.shutil).should_receive('rmtree').never()
  641. module.remove_data_source_dumps(
  642. hook_config=config['btrfs'],
  643. config=config,
  644. log_prefix='test',
  645. borgmatic_runtime_directory='/run/borgmatic',
  646. dry_run=False,
  647. )
  648. def test_remove_data_source_without_snapshots_skips_deletes():
  649. config = {'btrfs': {}}
  650. flexmock(module).should_receive('get_subvolumes').and_return(
  651. (
  652. module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),
  653. module.Subvolume('/mnt/subvol2', contained_patterns=(Pattern('/mnt/subvol2'),)),
  654. )
  655. )
  656. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  657. '/mnt/subvol1/.borgmatic-1234/./mnt/subvol1'
  658. )
  659. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol2').and_return(
  660. '/mnt/subvol2/.borgmatic-1234/./mnt/subvol2'
  661. )
  662. flexmock(module.borgmatic.config.paths).should_receive(
  663. 'replace_temporary_subdirectory_with_glob'
  664. ).with_args(
  665. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  666. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  667. ).and_return(
  668. '/mnt/subvol1/.borgmatic-*/mnt/subvol1'
  669. )
  670. flexmock(module.borgmatic.config.paths).should_receive(
  671. 'replace_temporary_subdirectory_with_glob'
  672. ).with_args(
  673. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  674. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  675. ).and_return(
  676. '/mnt/subvol2/.borgmatic-*/mnt/subvol2'
  677. )
  678. flexmock(module.glob).should_receive('glob').and_return(())
  679. flexmock(module.os.path).should_receive('isdir').never()
  680. flexmock(module).should_receive('delete_snapshot').never()
  681. flexmock(module.shutil).should_receive('rmtree').never()
  682. module.remove_data_source_dumps(
  683. hook_config=config['btrfs'],
  684. config=config,
  685. log_prefix='test',
  686. borgmatic_runtime_directory='/run/borgmatic',
  687. dry_run=False,
  688. )
  689. def test_remove_data_source_dumps_with_delete_snapshot_file_not_found_error_bails():
  690. config = {'btrfs': {}}
  691. flexmock(module).should_receive('get_subvolumes').and_return(
  692. (
  693. module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),
  694. module.Subvolume('/mnt/subvol2', contained_patterns=(Pattern('/mnt/subvol2'),)),
  695. )
  696. )
  697. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  698. '/mnt/subvol1/.borgmatic-1234/./mnt/subvol1'
  699. )
  700. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol2').and_return(
  701. '/mnt/subvol2/.borgmatic-1234/./mnt/subvol2'
  702. )
  703. flexmock(module.borgmatic.config.paths).should_receive(
  704. 'replace_temporary_subdirectory_with_glob'
  705. ).with_args(
  706. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  707. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  708. ).and_return(
  709. '/mnt/subvol1/.borgmatic-*/mnt/subvol1'
  710. )
  711. flexmock(module.borgmatic.config.paths).should_receive(
  712. 'replace_temporary_subdirectory_with_glob'
  713. ).with_args(
  714. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  715. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  716. ).and_return(
  717. '/mnt/subvol2/.borgmatic-*/mnt/subvol2'
  718. )
  719. flexmock(module.glob).should_receive('glob').with_args(
  720. '/mnt/subvol1/.borgmatic-*/mnt/subvol1'
  721. ).and_return(
  722. ('/mnt/subvol1/.borgmatic-1234/mnt/subvol1', '/mnt/subvol1/.borgmatic-5678/mnt/subvol1')
  723. )
  724. flexmock(module.glob).should_receive('glob').with_args(
  725. '/mnt/subvol2/.borgmatic-*/mnt/subvol2'
  726. ).and_return(
  727. ('/mnt/subvol2/.borgmatic-1234/mnt/subvol2', '/mnt/subvol2/.borgmatic-5678/mnt/subvol2')
  728. )
  729. flexmock(module.os.path).should_receive('isdir').with_args(
  730. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1'
  731. ).and_return(True)
  732. flexmock(module.os.path).should_receive('isdir').with_args(
  733. '/mnt/subvol1/.borgmatic-5678/mnt/subvol1'
  734. ).and_return(True)
  735. flexmock(module.os.path).should_receive('isdir').with_args(
  736. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2'
  737. ).and_return(True)
  738. flexmock(module.os.path).should_receive('isdir').with_args(
  739. '/mnt/subvol2/.borgmatic-5678/mnt/subvol2'
  740. ).and_return(False)
  741. flexmock(module).should_receive('delete_snapshot').and_raise(FileNotFoundError)
  742. flexmock(module.shutil).should_receive('rmtree').never()
  743. module.remove_data_source_dumps(
  744. hook_config=config['btrfs'],
  745. config=config,
  746. log_prefix='test',
  747. borgmatic_runtime_directory='/run/borgmatic',
  748. dry_run=False,
  749. )
  750. def test_remove_data_source_dumps_with_delete_snapshot_called_process_error_bails():
  751. config = {'btrfs': {}}
  752. flexmock(module).should_receive('get_subvolumes').and_return(
  753. (
  754. module.Subvolume('/mnt/subvol1', contained_patterns=(Pattern('/mnt/subvol1'),)),
  755. module.Subvolume('/mnt/subvol2', contained_patterns=(Pattern('/mnt/subvol2'),)),
  756. )
  757. )
  758. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol1').and_return(
  759. '/mnt/subvol1/.borgmatic-1234/./mnt/subvol1'
  760. )
  761. flexmock(module).should_receive('make_snapshot_path').with_args('/mnt/subvol2').and_return(
  762. '/mnt/subvol2/.borgmatic-1234/./mnt/subvol2'
  763. )
  764. flexmock(module.borgmatic.config.paths).should_receive(
  765. 'replace_temporary_subdirectory_with_glob'
  766. ).with_args(
  767. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1',
  768. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  769. ).and_return(
  770. '/mnt/subvol1/.borgmatic-*/mnt/subvol1'
  771. )
  772. flexmock(module.borgmatic.config.paths).should_receive(
  773. 'replace_temporary_subdirectory_with_glob'
  774. ).with_args(
  775. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2',
  776. temporary_directory_prefix=module.BORGMATIC_SNAPSHOT_PREFIX,
  777. ).and_return(
  778. '/mnt/subvol2/.borgmatic-*/mnt/subvol2'
  779. )
  780. flexmock(module.glob).should_receive('glob').with_args(
  781. '/mnt/subvol1/.borgmatic-*/mnt/subvol1'
  782. ).and_return(
  783. ('/mnt/subvol1/.borgmatic-1234/mnt/subvol1', '/mnt/subvol1/.borgmatic-5678/mnt/subvol1')
  784. )
  785. flexmock(module.glob).should_receive('glob').with_args(
  786. '/mnt/subvol2/.borgmatic-*/mnt/subvol2'
  787. ).and_return(
  788. ('/mnt/subvol2/.borgmatic-1234/mnt/subvol2', '/mnt/subvol2/.borgmatic-5678/mnt/subvol2')
  789. )
  790. flexmock(module.os.path).should_receive('isdir').with_args(
  791. '/mnt/subvol1/.borgmatic-1234/mnt/subvol1'
  792. ).and_return(True)
  793. flexmock(module.os.path).should_receive('isdir').with_args(
  794. '/mnt/subvol1/.borgmatic-5678/mnt/subvol1'
  795. ).and_return(True)
  796. flexmock(module.os.path).should_receive('isdir').with_args(
  797. '/mnt/subvol2/.borgmatic-1234/mnt/subvol2'
  798. ).and_return(True)
  799. flexmock(module.os.path).should_receive('isdir').with_args(
  800. '/mnt/subvol2/.borgmatic-5678/mnt/subvol2'
  801. ).and_return(False)
  802. flexmock(module).should_receive('delete_snapshot').and_raise(
  803. module.subprocess.CalledProcessError(1, 'command', 'error')
  804. )
  805. flexmock(module.shutil).should_receive('rmtree').never()
  806. module.remove_data_source_dumps(
  807. hook_config=config['btrfs'],
  808. config=config,
  809. log_prefix='test',
  810. borgmatic_runtime_directory='/run/borgmatic',
  811. dry_run=False,
  812. )