test_zfs.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. import os
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.borg.pattern import Pattern, Pattern_style, Pattern_type
  5. from borgmatic.hooks.data_source import zfs as module
  6. def test_get_datasets_to_backup_filters_datasets_by_patterns():
  7. flexmock(module.borgmatic.execute).should_receive(
  8. 'execute_command_and_capture_output'
  9. ).and_return(
  10. 'dataset\t/dataset\t-\nother\t/other\t-',
  11. )
  12. flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
  13. 'get_contained_patterns'
  14. ).with_args('/dataset', object).and_return((Pattern('/dataset'),))
  15. flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
  16. 'get_contained_patterns'
  17. ).with_args('/other', object).and_return(())
  18. assert module.get_datasets_to_backup(
  19. 'zfs',
  20. patterns=(
  21. Pattern('/foo'),
  22. Pattern('/dataset'),
  23. Pattern('/bar'),
  24. ),
  25. ) == (
  26. module.Dataset(
  27. name='dataset',
  28. mount_point='/dataset',
  29. contained_patterns=(Pattern('/dataset'),),
  30. ),
  31. )
  32. def test_get_datasets_to_backup_filters_datasets_by_user_property():
  33. flexmock(module.borgmatic.execute).should_receive(
  34. 'execute_command_and_capture_output'
  35. ).and_return(
  36. 'dataset\t/dataset\tauto\nother\t/other\t-',
  37. )
  38. flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
  39. 'get_contained_patterns'
  40. ).with_args('/dataset', object).and_return(())
  41. flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
  42. 'get_contained_patterns'
  43. ).with_args('/other', object).and_return(())
  44. assert module.get_datasets_to_backup('zfs', patterns=(Pattern('/foo'), Pattern('/bar'))) == (
  45. module.Dataset(
  46. name='dataset',
  47. mount_point='/dataset',
  48. auto_backup=True,
  49. contained_patterns=(Pattern('/dataset'),),
  50. ),
  51. )
  52. def test_get_datasets_to_backup_with_invalid_list_output_raises():
  53. flexmock(module.borgmatic.execute).should_receive(
  54. 'execute_command_and_capture_output'
  55. ).and_return(
  56. 'dataset',
  57. )
  58. flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
  59. 'get_contained_patterns'
  60. ).never()
  61. with pytest.raises(ValueError, match='zfs'):
  62. module.get_datasets_to_backup('zfs', patterns=(Pattern('/foo'), Pattern('/bar')))
  63. def test_get_all_dataset_mount_points_does_not_filter_datasets():
  64. flexmock(module.borgmatic.execute).should_receive(
  65. 'execute_command_and_capture_output'
  66. ).and_return(
  67. '/dataset\n/other',
  68. )
  69. flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
  70. 'get_contained_patterns'
  71. ).and_return((Pattern('/dataset'),))
  72. assert module.get_all_dataset_mount_points('zfs') == (
  73. ('/dataset'),
  74. ('/other'),
  75. )
  76. @pytest.mark.parametrize(
  77. 'pattern,expected_pattern',
  78. (
  79. (
  80. Pattern('/foo/bar/baz'),
  81. Pattern('/run/borgmatic/zfs_snapshots/./foo/bar/baz'),
  82. ),
  83. (Pattern('/foo/bar'), Pattern('/run/borgmatic/zfs_snapshots/./foo/bar')),
  84. (
  85. Pattern('^/foo/bar', Pattern_type.INCLUDE, Pattern_style.REGULAR_EXPRESSION),
  86. Pattern(
  87. '^/run/borgmatic/zfs_snapshots/./foo/bar',
  88. Pattern_type.INCLUDE,
  89. Pattern_style.REGULAR_EXPRESSION,
  90. ),
  91. ),
  92. (
  93. Pattern('/foo/bar', Pattern_type.INCLUDE, Pattern_style.REGULAR_EXPRESSION),
  94. Pattern(
  95. '/run/borgmatic/zfs_snapshots/./foo/bar',
  96. Pattern_type.INCLUDE,
  97. Pattern_style.REGULAR_EXPRESSION,
  98. ),
  99. ),
  100. (Pattern('/foo'), Pattern('/run/borgmatic/zfs_snapshots/./foo')),
  101. (Pattern('/'), Pattern('/run/borgmatic/zfs_snapshots/./')),
  102. ),
  103. )
  104. def test_make_borg_snapshot_pattern_includes_slashdot_hack_and_stripped_pattern_path(
  105. pattern, expected_pattern
  106. ):
  107. assert module.make_borg_snapshot_pattern(pattern, '/run/borgmatic') == expected_pattern
  108. def test_dump_data_sources_snapshots_and_mounts_and_updates_patterns():
  109. flexmock(module).should_receive('get_datasets_to_backup').and_return(
  110. (
  111. flexmock(
  112. name='dataset',
  113. mount_point='/mnt/dataset',
  114. contained_patterns=(Pattern('/mnt/dataset/subdir'),),
  115. )
  116. )
  117. )
  118. flexmock(module.os).should_receive('getpid').and_return(1234)
  119. full_snapshot_name = 'dataset@borgmatic-1234'
  120. flexmock(module).should_receive('snapshot_dataset').with_args(
  121. 'zfs',
  122. full_snapshot_name,
  123. ).once()
  124. snapshot_mount_path = '/run/borgmatic/zfs_snapshots/./mnt/dataset'
  125. flexmock(module).should_receive('mount_snapshot').with_args(
  126. 'mount',
  127. full_snapshot_name,
  128. module.os.path.normpath(snapshot_mount_path),
  129. ).once()
  130. flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
  131. Pattern('/mnt/dataset/subdir'), '/run/borgmatic'
  132. ).and_return(Pattern('/run/borgmatic/zfs_snapshots/./mnt/dataset/subdir'))
  133. patterns = [Pattern('/mnt/dataset/subdir')]
  134. assert (
  135. module.dump_data_sources(
  136. hook_config={},
  137. config={'source_directories': '/mnt/dataset', 'zfs': {}},
  138. config_paths=('test.yaml',),
  139. borgmatic_runtime_directory='/run/borgmatic',
  140. patterns=patterns,
  141. dry_run=False,
  142. )
  143. == []
  144. )
  145. assert patterns == [Pattern(os.path.join(snapshot_mount_path, 'subdir'))]
  146. def test_dump_data_sources_with_no_datasets_skips_snapshots():
  147. flexmock(module).should_receive('get_datasets_to_backup').and_return(())
  148. flexmock(module.os).should_receive('getpid').and_return(1234)
  149. flexmock(module).should_receive('snapshot_dataset').never()
  150. flexmock(module).should_receive('mount_snapshot').never()
  151. patterns = [Pattern('/mnt/dataset')]
  152. assert (
  153. module.dump_data_sources(
  154. hook_config={},
  155. config={'patterns': flexmock(), 'zfs': {}},
  156. config_paths=('test.yaml',),
  157. borgmatic_runtime_directory='/run/borgmatic',
  158. patterns=patterns,
  159. dry_run=False,
  160. )
  161. == []
  162. )
  163. assert patterns == [Pattern('/mnt/dataset')]
  164. def test_dump_data_sources_uses_custom_commands():
  165. flexmock(module).should_receive('get_datasets_to_backup').and_return(
  166. (
  167. flexmock(
  168. name='dataset',
  169. mount_point='/mnt/dataset',
  170. contained_patterns=(Pattern('/mnt/dataset/subdir'),),
  171. )
  172. )
  173. )
  174. flexmock(module.os).should_receive('getpid').and_return(1234)
  175. full_snapshot_name = 'dataset@borgmatic-1234'
  176. flexmock(module).should_receive('snapshot_dataset').with_args(
  177. '/usr/local/bin/zfs',
  178. full_snapshot_name,
  179. ).once()
  180. snapshot_mount_path = '/run/borgmatic/zfs_snapshots/./mnt/dataset'
  181. flexmock(module).should_receive('mount_snapshot').with_args(
  182. '/usr/local/bin/mount',
  183. full_snapshot_name,
  184. module.os.path.normpath(snapshot_mount_path),
  185. ).once()
  186. flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
  187. Pattern('/mnt/dataset/subdir'), '/run/borgmatic'
  188. ).and_return(Pattern('/run/borgmatic/zfs_snapshots/./mnt/dataset/subdir'))
  189. patterns = [Pattern('/mnt/dataset/subdir')]
  190. hook_config = {
  191. 'zfs_command': '/usr/local/bin/zfs',
  192. 'mount_command': '/usr/local/bin/mount',
  193. }
  194. assert (
  195. module.dump_data_sources(
  196. hook_config=hook_config,
  197. config={
  198. 'patterns': flexmock(),
  199. 'zfs': hook_config,
  200. },
  201. config_paths=('test.yaml',),
  202. borgmatic_runtime_directory='/run/borgmatic',
  203. patterns=patterns,
  204. dry_run=False,
  205. )
  206. == []
  207. )
  208. assert patterns == [Pattern(os.path.join(snapshot_mount_path, 'subdir'))]
  209. def test_dump_data_sources_with_dry_run_skips_commands_and_does_not_touch_patterns():
  210. flexmock(module).should_receive('get_datasets_to_backup').and_return(
  211. (flexmock(name='dataset', mount_point='/mnt/dataset'),)
  212. )
  213. flexmock(module.os).should_receive('getpid').and_return(1234)
  214. flexmock(module).should_receive('snapshot_dataset').never()
  215. flexmock(module).should_receive('mount_snapshot').never()
  216. patterns = [Pattern('/mnt/dataset')]
  217. assert (
  218. module.dump_data_sources(
  219. hook_config={},
  220. config={'patterns': ('R /mnt/dataset',), 'zfs': {}},
  221. config_paths=('test.yaml',),
  222. borgmatic_runtime_directory='/run/borgmatic',
  223. patterns=patterns,
  224. dry_run=True,
  225. )
  226. == []
  227. )
  228. assert patterns == [Pattern('/mnt/dataset')]
  229. def test_dump_data_sources_ignores_mismatch_between_given_patterns_and_contained_patterns():
  230. flexmock(module).should_receive('get_datasets_to_backup').and_return(
  231. (
  232. flexmock(
  233. name='dataset',
  234. mount_point='/mnt/dataset',
  235. contained_patterns=(Pattern('/mnt/dataset/subdir'),),
  236. )
  237. )
  238. )
  239. flexmock(module.os).should_receive('getpid').and_return(1234)
  240. full_snapshot_name = 'dataset@borgmatic-1234'
  241. flexmock(module).should_receive('snapshot_dataset').with_args(
  242. 'zfs',
  243. full_snapshot_name,
  244. ).once()
  245. snapshot_mount_path = '/run/borgmatic/zfs_snapshots/./mnt/dataset'
  246. flexmock(module).should_receive('mount_snapshot').with_args(
  247. 'mount',
  248. full_snapshot_name,
  249. module.os.path.normpath(snapshot_mount_path),
  250. ).once()
  251. flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
  252. Pattern('/mnt/dataset/subdir'), '/run/borgmatic'
  253. ).and_return(Pattern('/run/borgmatic/zfs_snapshots/./mnt/dataset/subdir'))
  254. patterns = [Pattern('/hmm')]
  255. assert (
  256. module.dump_data_sources(
  257. hook_config={},
  258. config={'patterns': ('R /mnt/dataset',), 'zfs': {}},
  259. config_paths=('test.yaml',),
  260. borgmatic_runtime_directory='/run/borgmatic',
  261. patterns=patterns,
  262. dry_run=False,
  263. )
  264. == []
  265. )
  266. assert patterns == [Pattern('/hmm'), Pattern(os.path.join(snapshot_mount_path, 'subdir'))]
  267. def test_get_all_snapshots_parses_list_output():
  268. flexmock(module.borgmatic.execute).should_receive(
  269. 'execute_command_and_capture_output'
  270. ).and_return(
  271. 'dataset1@borgmatic-1234\ndataset2@borgmatic-4567',
  272. )
  273. assert module.get_all_snapshots('zfs') == ('dataset1@borgmatic-1234', 'dataset2@borgmatic-4567')
  274. def test_remove_data_source_dumps_unmounts_and_destroys_snapshots():
  275. flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))
  276. flexmock(module.borgmatic.config.paths).should_receive(
  277. 'replace_temporary_subdirectory_with_glob'
  278. ).and_return('/run/borgmatic')
  279. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  280. flexmock(module.os.path).should_receive('isdir').and_return(True)
  281. flexmock(module.shutil).should_receive('rmtree')
  282. flexmock(module).should_receive('unmount_snapshot').with_args(
  283. 'umount', '/run/borgmatic/zfs_snapshots/mnt/dataset'
  284. ).once()
  285. flexmock(module).should_receive('get_all_snapshots').and_return(
  286. ('dataset@borgmatic-1234', 'dataset@other', 'other@other', 'invalid')
  287. )
  288. flexmock(module).should_receive('destroy_snapshot').with_args(
  289. 'zfs', 'dataset@borgmatic-1234'
  290. ).once()
  291. module.remove_data_source_dumps(
  292. hook_config={},
  293. config={'source_directories': '/mnt/dataset', 'zfs': {}},
  294. borgmatic_runtime_directory='/run/borgmatic',
  295. dry_run=False,
  296. )
  297. def test_remove_data_source_dumps_use_custom_commands():
  298. flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))
  299. flexmock(module.borgmatic.config.paths).should_receive(
  300. 'replace_temporary_subdirectory_with_glob'
  301. ).and_return('/run/borgmatic')
  302. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  303. flexmock(module.os.path).should_receive('isdir').and_return(True)
  304. flexmock(module.shutil).should_receive('rmtree')
  305. flexmock(module).should_receive('unmount_snapshot').with_args(
  306. '/usr/local/bin/umount', '/run/borgmatic/zfs_snapshots/mnt/dataset'
  307. ).once()
  308. flexmock(module).should_receive('get_all_snapshots').and_return(
  309. ('dataset@borgmatic-1234', 'dataset@other', 'other@other', 'invalid')
  310. )
  311. flexmock(module).should_receive('destroy_snapshot').with_args(
  312. '/usr/local/bin/zfs', 'dataset@borgmatic-1234'
  313. ).once()
  314. hook_config = {'zfs_command': '/usr/local/bin/zfs', 'umount_command': '/usr/local/bin/umount'}
  315. module.remove_data_source_dumps(
  316. hook_config=hook_config,
  317. config={'source_directories': '/mnt/dataset', 'zfs': hook_config},
  318. borgmatic_runtime_directory='/run/borgmatic',
  319. dry_run=False,
  320. )
  321. def test_remove_data_source_dumps_bails_for_missing_hook_configuration():
  322. flexmock(module).should_receive('get_all_dataset_mount_points').never()
  323. flexmock(module.borgmatic.config.paths).should_receive(
  324. 'replace_temporary_subdirectory_with_glob'
  325. ).never()
  326. module.remove_data_source_dumps(
  327. hook_config=None,
  328. config={'source_directories': '/mnt/dataset'},
  329. borgmatic_runtime_directory='/run/borgmatic',
  330. dry_run=False,
  331. )
  332. def test_remove_data_source_dumps_bails_for_missing_zfs_command():
  333. flexmock(module).should_receive('get_all_dataset_mount_points').and_raise(FileNotFoundError)
  334. flexmock(module.borgmatic.config.paths).should_receive(
  335. 'replace_temporary_subdirectory_with_glob'
  336. ).never()
  337. hook_config = {'zfs_command': 'wtf'}
  338. module.remove_data_source_dumps(
  339. hook_config=hook_config,
  340. config={'source_directories': '/mnt/dataset', 'zfs': hook_config},
  341. borgmatic_runtime_directory='/run/borgmatic',
  342. dry_run=False,
  343. )
  344. def test_remove_data_source_dumps_bails_for_zfs_command_error():
  345. flexmock(module).should_receive('get_all_dataset_mount_points').and_raise(
  346. module.subprocess.CalledProcessError(1, 'wtf')
  347. )
  348. flexmock(module.borgmatic.config.paths).should_receive(
  349. 'replace_temporary_subdirectory_with_glob'
  350. ).never()
  351. hook_config = {'zfs_command': 'wtf'}
  352. module.remove_data_source_dumps(
  353. hook_config=hook_config,
  354. config={'source_directories': '/mnt/dataset', 'zfs': hook_config},
  355. borgmatic_runtime_directory='/run/borgmatic',
  356. dry_run=False,
  357. )
  358. def test_remove_data_source_dumps_bails_for_missing_umount_command():
  359. flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))
  360. flexmock(module.borgmatic.config.paths).should_receive(
  361. 'replace_temporary_subdirectory_with_glob'
  362. ).and_return('/run/borgmatic')
  363. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  364. flexmock(module.os.path).should_receive('isdir').and_return(True)
  365. flexmock(module.shutil).should_receive('rmtree')
  366. flexmock(module).should_receive('unmount_snapshot').with_args(
  367. '/usr/local/bin/umount', '/run/borgmatic/zfs_snapshots/mnt/dataset'
  368. ).and_raise(FileNotFoundError)
  369. flexmock(module).should_receive('get_all_snapshots').never()
  370. flexmock(module).should_receive('destroy_snapshot').never()
  371. hook_config = {'zfs_command': '/usr/local/bin/zfs', 'umount_command': '/usr/local/bin/umount'}
  372. module.remove_data_source_dumps(
  373. hook_config=hook_config,
  374. config={'source_directories': '/mnt/dataset', 'zfs': hook_config},
  375. borgmatic_runtime_directory='/run/borgmatic',
  376. dry_run=False,
  377. )
  378. def test_remove_data_source_dumps_bails_for_umount_command_error():
  379. flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))
  380. flexmock(module.borgmatic.config.paths).should_receive(
  381. 'replace_temporary_subdirectory_with_glob'
  382. ).and_return('/run/borgmatic')
  383. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  384. flexmock(module.os.path).should_receive('isdir').and_return(True)
  385. flexmock(module.shutil).should_receive('rmtree')
  386. flexmock(module).should_receive('unmount_snapshot').with_args(
  387. '/usr/local/bin/umount', '/run/borgmatic/zfs_snapshots/mnt/dataset'
  388. ).and_raise(module.subprocess.CalledProcessError(1, 'wtf'))
  389. flexmock(module).should_receive('get_all_snapshots').never()
  390. flexmock(module).should_receive('destroy_snapshot').never()
  391. hook_config = {'zfs_command': '/usr/local/bin/zfs', 'umount_command': '/usr/local/bin/umount'}
  392. module.remove_data_source_dumps(
  393. hook_config=hook_config,
  394. config={'source_directories': '/mnt/dataset', 'zfs': hook_config},
  395. borgmatic_runtime_directory='/run/borgmatic',
  396. dry_run=False,
  397. )
  398. def test_remove_data_source_dumps_skips_unmount_snapshot_directories_that_are_not_actually_directories():
  399. flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))
  400. flexmock(module.borgmatic.config.paths).should_receive(
  401. 'replace_temporary_subdirectory_with_glob'
  402. ).and_return('/run/borgmatic')
  403. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  404. flexmock(module.os.path).should_receive('isdir').and_return(False)
  405. flexmock(module.shutil).should_receive('rmtree').never()
  406. flexmock(module).should_receive('unmount_snapshot').never()
  407. flexmock(module).should_receive('get_all_snapshots').and_return(
  408. ('dataset@borgmatic-1234', 'dataset@other', 'other@other', 'invalid')
  409. )
  410. flexmock(module).should_receive('destroy_snapshot').with_args(
  411. 'zfs', 'dataset@borgmatic-1234'
  412. ).once()
  413. module.remove_data_source_dumps(
  414. hook_config={},
  415. config={'source_directories': '/mnt/dataset', 'zfs': {}},
  416. borgmatic_runtime_directory='/run/borgmatic',
  417. dry_run=False,
  418. )
  419. def test_remove_data_source_dumps_skips_unmount_snapshot_mount_paths_that_are_not_actually_directories():
  420. flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))
  421. flexmock(module.borgmatic.config.paths).should_receive(
  422. 'replace_temporary_subdirectory_with_glob'
  423. ).and_return('/run/borgmatic')
  424. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  425. flexmock(module.os.path).should_receive('isdir').with_args(
  426. '/run/borgmatic/zfs_snapshots'
  427. ).and_return(True)
  428. flexmock(module.os.path).should_receive('isdir').with_args(
  429. '/run/borgmatic/zfs_snapshots/mnt/dataset'
  430. ).and_return(False)
  431. flexmock(module.shutil).should_receive('rmtree')
  432. flexmock(module).should_receive('unmount_snapshot').never()
  433. flexmock(module).should_receive('get_all_snapshots').and_return(
  434. ('dataset@borgmatic-1234', 'dataset@other', 'other@other', 'invalid')
  435. )
  436. flexmock(module).should_receive('destroy_snapshot').with_args(
  437. 'zfs', 'dataset@borgmatic-1234'
  438. ).once()
  439. module.remove_data_source_dumps(
  440. hook_config={},
  441. config={'source_directories': '/mnt/dataset', 'zfs': {}},
  442. borgmatic_runtime_directory='/run/borgmatic',
  443. dry_run=False,
  444. )
  445. def test_remove_data_source_dumps_skips_unmount_snapshot_mount_paths_after_rmtree_succeeds():
  446. flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))
  447. flexmock(module.borgmatic.config.paths).should_receive(
  448. 'replace_temporary_subdirectory_with_glob'
  449. ).and_return('/run/borgmatic')
  450. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  451. flexmock(module.os.path).should_receive('isdir').with_args(
  452. '/run/borgmatic/zfs_snapshots'
  453. ).and_return(True)
  454. flexmock(module.os.path).should_receive('isdir').with_args(
  455. '/run/borgmatic/zfs_snapshots/mnt/dataset'
  456. ).and_return(True).and_return(False)
  457. flexmock(module.shutil).should_receive('rmtree')
  458. flexmock(module).should_receive('unmount_snapshot').never()
  459. flexmock(module).should_receive('get_all_snapshots').and_return(
  460. ('dataset@borgmatic-1234', 'dataset@other', 'other@other', 'invalid')
  461. )
  462. flexmock(module).should_receive('destroy_snapshot').with_args(
  463. 'zfs', 'dataset@borgmatic-1234'
  464. ).once()
  465. module.remove_data_source_dumps(
  466. hook_config={},
  467. config={'source_directories': '/mnt/dataset', 'zfs': {}},
  468. borgmatic_runtime_directory='/run/borgmatic',
  469. dry_run=False,
  470. )
  471. def test_remove_data_source_dumps_with_dry_run_skips_unmount_and_destroy():
  472. flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))
  473. flexmock(module.borgmatic.config.paths).should_receive(
  474. 'replace_temporary_subdirectory_with_glob'
  475. ).and_return('/run/borgmatic')
  476. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  477. flexmock(module.os.path).should_receive('isdir').and_return(True)
  478. flexmock(module.shutil).should_receive('rmtree').never()
  479. flexmock(module).should_receive('unmount_snapshot').never()
  480. flexmock(module).should_receive('get_all_snapshots').and_return(
  481. ('dataset@borgmatic-1234', 'dataset@other', 'other@other', 'invalid')
  482. )
  483. flexmock(module).should_receive('destroy_snapshot').never()
  484. module.remove_data_source_dumps(
  485. hook_config={},
  486. config={'source_directories': '/mnt/dataset', 'zfs': {}},
  487. borgmatic_runtime_directory='/run/borgmatic',
  488. dry_run=True,
  489. )