test_zfs.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. import os
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.hooks.data_source import zfs as module
  5. def test_get_datasets_to_backup_filters_datasets_by_source_directories():
  6. flexmock(module.borgmatic.execute).should_receive(
  7. 'execute_command_and_capture_output'
  8. ).and_return(
  9. 'dataset\t/dataset\t-\nother\t/other\t-',
  10. )
  11. flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
  12. 'get_contained_directories'
  13. ).with_args('/dataset', object).and_return(('/dataset',))
  14. flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
  15. 'get_contained_directories'
  16. ).with_args('/other', object).and_return(())
  17. assert module.get_datasets_to_backup(
  18. 'zfs', source_directories=('/foo', '/dataset', '/bar')
  19. ) == (
  20. module.Dataset(name='dataset', mount_point='/dataset', contained_source_directories=('/dataset',)),
  21. )
  22. def test_get_datasets_to_backup_filters_datasets_by_user_property():
  23. flexmock(module.borgmatic.execute).should_receive(
  24. 'execute_command_and_capture_output'
  25. ).and_return(
  26. 'dataset\t/dataset\tauto\nother\t/other\t-',
  27. )
  28. flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
  29. 'get_contained_directories'
  30. ).with_args('/dataset', object).never()
  31. flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
  32. 'get_contained_directories'
  33. ).with_args('/other', object).and_return(())
  34. assert module.get_datasets_to_backup('zfs', source_directories=('/foo', '/bar')) == (
  35. module.Dataset(
  36. name='dataset',
  37. mount_point='/dataset',
  38. auto_backup=True,
  39. contained_source_directories=('/dataset',),
  40. ),
  41. )
  42. def test_get_datasets_to_backup_with_invalid_list_output_raises():
  43. flexmock(module.borgmatic.execute).should_receive(
  44. 'execute_command_and_capture_output'
  45. ).and_return(
  46. 'dataset',
  47. )
  48. flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
  49. 'get_contained_directories'
  50. ).never()
  51. with pytest.raises(ValueError, match='zfs'):
  52. module.get_datasets_to_backup('zfs', source_directories=('/foo', '/bar'))
  53. def test_get_all_dataset_mount_points_does_not_filter_datasets():
  54. flexmock(module.borgmatic.execute).should_receive(
  55. 'execute_command_and_capture_output'
  56. ).and_return(
  57. '/dataset\n/other',
  58. )
  59. flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
  60. 'get_contained_directories'
  61. ).and_return(('/dataset',))
  62. assert module.get_all_dataset_mount_points('zfs') == (
  63. ('/dataset'),
  64. ('/other'),
  65. )
  66. def test_dump_data_sources_snapshots_and_mounts_and_updates_source_directories():
  67. flexmock(module).should_receive('get_datasets_to_backup').and_return(
  68. (
  69. flexmock(
  70. name='dataset',
  71. mount_point='/mnt/dataset',
  72. contained_source_directories=('/mnt/dataset/subdir',),
  73. )
  74. )
  75. )
  76. flexmock(module.os).should_receive('getpid').and_return(1234)
  77. full_snapshot_name = 'dataset@borgmatic-1234'
  78. flexmock(module).should_receive('snapshot_dataset').with_args(
  79. 'zfs',
  80. full_snapshot_name,
  81. ).once()
  82. snapshot_mount_path = '/run/borgmatic/zfs_snapshots/./mnt/dataset'
  83. flexmock(module).should_receive('mount_snapshot').with_args(
  84. 'mount',
  85. full_snapshot_name,
  86. module.os.path.normpath(snapshot_mount_path),
  87. ).once()
  88. source_directories = ['/mnt/dataset/subdir']
  89. assert (
  90. module.dump_data_sources(
  91. hook_config={},
  92. config={'source_directories': '/mnt/dataset', 'zfs': {}},
  93. log_prefix='test',
  94. config_paths=('test.yaml',),
  95. borgmatic_runtime_directory='/run/borgmatic',
  96. source_directories=source_directories,
  97. dry_run=False,
  98. )
  99. == []
  100. )
  101. assert source_directories == [os.path.join(snapshot_mount_path, 'subdir')]
  102. def test_dump_data_sources_snapshots_with_no_datasets_skips_snapshots():
  103. flexmock(module).should_receive('get_datasets_to_backup').and_return(())
  104. flexmock(module.os).should_receive('getpid').and_return(1234)
  105. flexmock(module).should_receive('snapshot_dataset').never()
  106. flexmock(module).should_receive('mount_snapshot').never()
  107. source_directories = ['/mnt/dataset']
  108. assert (
  109. module.dump_data_sources(
  110. hook_config={},
  111. config={'source_directories': '/mnt/dataset', 'zfs': {}},
  112. log_prefix='test',
  113. config_paths=('test.yaml',),
  114. borgmatic_runtime_directory='/run/borgmatic',
  115. source_directories=source_directories,
  116. dry_run=False,
  117. )
  118. == []
  119. )
  120. assert source_directories == ['/mnt/dataset']
  121. def test_dump_data_sources_uses_custom_commands():
  122. flexmock(module).should_receive('get_datasets_to_backup').and_return(
  123. (
  124. flexmock(
  125. name='dataset',
  126. mount_point='/mnt/dataset',
  127. contained_source_directories=('/mnt/dataset/subdir',),
  128. )
  129. )
  130. )
  131. flexmock(module.os).should_receive('getpid').and_return(1234)
  132. full_snapshot_name = 'dataset@borgmatic-1234'
  133. flexmock(module).should_receive('snapshot_dataset').with_args(
  134. '/usr/local/bin/zfs',
  135. full_snapshot_name,
  136. ).once()
  137. snapshot_mount_path = '/run/borgmatic/zfs_snapshots/./mnt/dataset'
  138. flexmock(module).should_receive('mount_snapshot').with_args(
  139. '/usr/local/bin/mount',
  140. full_snapshot_name,
  141. module.os.path.normpath(snapshot_mount_path),
  142. ).once()
  143. source_directories = ['/mnt/dataset/subdir']
  144. hook_config = {
  145. 'zfs_command': '/usr/local/bin/zfs',
  146. 'mount_command': '/usr/local/bin/mount',
  147. }
  148. assert (
  149. module.dump_data_sources(
  150. hook_config=hook_config,
  151. config={
  152. 'source_directories': source_directories,
  153. 'zfs': hook_config,
  154. },
  155. log_prefix='test',
  156. config_paths=('test.yaml',),
  157. borgmatic_runtime_directory='/run/borgmatic',
  158. source_directories=source_directories,
  159. dry_run=False,
  160. )
  161. == []
  162. )
  163. assert source_directories == [os.path.join(snapshot_mount_path, 'subdir')]
  164. def test_dump_data_sources_with_dry_run_skips_commands_and_does_not_touch_source_directories():
  165. flexmock(module).should_receive('get_datasets_to_backup').and_return(
  166. (flexmock(name='dataset', mount_point='/mnt/dataset'),)
  167. )
  168. flexmock(module.os).should_receive('getpid').and_return(1234)
  169. flexmock(module).should_receive('snapshot_dataset').never()
  170. flexmock(module).should_receive('mount_snapshot').never()
  171. source_directories = ['/mnt/dataset']
  172. assert (
  173. module.dump_data_sources(
  174. hook_config={},
  175. config={'source_directories': '/mnt/dataset', 'zfs': {}},
  176. log_prefix='test',
  177. config_paths=('test.yaml',),
  178. borgmatic_runtime_directory='/run/borgmatic',
  179. source_directories=source_directories,
  180. dry_run=True,
  181. )
  182. == []
  183. )
  184. assert source_directories == ['/mnt/dataset']
  185. def test_get_all_snapshots_parses_list_output():
  186. flexmock(module.borgmatic.execute).should_receive(
  187. 'execute_command_and_capture_output'
  188. ).and_return(
  189. 'dataset1@borgmatic-1234\ndataset2@borgmatic-4567',
  190. )
  191. assert module.get_all_snapshots('zfs') == ('dataset1@borgmatic-1234', 'dataset2@borgmatic-4567')
  192. def test_remove_data_source_dumps_unmounts_and_destroys_snapshots():
  193. flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))
  194. flexmock(module.borgmatic.config.paths).should_receive(
  195. 'replace_temporary_subdirectory_with_glob'
  196. ).and_return('/run/borgmatic')
  197. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  198. flexmock(module.os.path).should_receive('isdir').and_return(True)
  199. flexmock(module.shutil).should_receive('rmtree')
  200. flexmock(module).should_receive('unmount_snapshot').with_args(
  201. 'umount', '/run/borgmatic/zfs_snapshots/mnt/dataset'
  202. ).once()
  203. flexmock(module).should_receive('get_all_snapshots').and_return(
  204. ('dataset@borgmatic-1234', 'dataset@other', 'other@other', 'invalid')
  205. )
  206. flexmock(module).should_receive('destroy_snapshot').with_args(
  207. 'zfs', 'dataset@borgmatic-1234'
  208. ).once()
  209. module.remove_data_source_dumps(
  210. hook_config={},
  211. config={'source_directories': '/mnt/dataset', 'zfs': {}},
  212. log_prefix='test',
  213. borgmatic_runtime_directory='/run/borgmatic',
  214. dry_run=False,
  215. )
  216. def test_remove_data_source_dumps_use_custom_commands():
  217. flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))
  218. flexmock(module.borgmatic.config.paths).should_receive(
  219. 'replace_temporary_subdirectory_with_glob'
  220. ).and_return('/run/borgmatic')
  221. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  222. flexmock(module.os.path).should_receive('isdir').and_return(True)
  223. flexmock(module.shutil).should_receive('rmtree')
  224. flexmock(module).should_receive('unmount_snapshot').with_args(
  225. '/usr/local/bin/umount', '/run/borgmatic/zfs_snapshots/mnt/dataset'
  226. ).once()
  227. flexmock(module).should_receive('get_all_snapshots').and_return(
  228. ('dataset@borgmatic-1234', 'dataset@other', 'other@other', 'invalid')
  229. )
  230. flexmock(module).should_receive('destroy_snapshot').with_args(
  231. '/usr/local/bin/zfs', 'dataset@borgmatic-1234'
  232. ).once()
  233. hook_config = {'zfs_command': '/usr/local/bin/zfs', 'umount_command': '/usr/local/bin/umount'}
  234. module.remove_data_source_dumps(
  235. hook_config=hook_config,
  236. config={'source_directories': '/mnt/dataset', 'zfs': hook_config},
  237. log_prefix='test',
  238. borgmatic_runtime_directory='/run/borgmatic',
  239. dry_run=False,
  240. )
  241. def test_remove_data_source_dumps_bails_for_missing_zfs_command():
  242. flexmock(module).should_receive('get_all_dataset_mount_points').and_raise(FileNotFoundError)
  243. flexmock(module.borgmatic.config.paths).should_receive(
  244. 'replace_temporary_subdirectory_with_glob'
  245. ).never()
  246. hook_config = {'zfs_command': 'wtf'}
  247. module.remove_data_source_dumps(
  248. hook_config=hook_config,
  249. config={'source_directories': '/mnt/dataset', 'zfs': hook_config},
  250. log_prefix='test',
  251. borgmatic_runtime_directory='/run/borgmatic',
  252. dry_run=False,
  253. )
  254. def test_remove_data_source_dumps_bails_for_zfs_command_error():
  255. flexmock(module).should_receive('get_all_dataset_mount_points').and_raise(
  256. module.subprocess.CalledProcessError(1, 'wtf')
  257. )
  258. flexmock(module.borgmatic.config.paths).should_receive(
  259. 'replace_temporary_subdirectory_with_glob'
  260. ).never()
  261. hook_config = {'zfs_command': 'wtf'}
  262. module.remove_data_source_dumps(
  263. hook_config=hook_config,
  264. config={'source_directories': '/mnt/dataset', 'zfs': hook_config},
  265. log_prefix='test',
  266. borgmatic_runtime_directory='/run/borgmatic',
  267. dry_run=False,
  268. )
  269. def test_remove_data_source_dumps_bails_for_missing_umount_command():
  270. flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))
  271. flexmock(module.borgmatic.config.paths).should_receive(
  272. 'replace_temporary_subdirectory_with_glob'
  273. ).and_return('/run/borgmatic')
  274. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  275. flexmock(module.os.path).should_receive('isdir').and_return(True)
  276. flexmock(module.shutil).should_receive('rmtree')
  277. flexmock(module).should_receive('unmount_snapshot').with_args(
  278. '/usr/local/bin/umount', '/run/borgmatic/zfs_snapshots/mnt/dataset'
  279. ).and_raise(FileNotFoundError)
  280. flexmock(module).should_receive('get_all_snapshots').never()
  281. flexmock(module).should_receive('destroy_snapshot').never()
  282. hook_config = {'zfs_command': '/usr/local/bin/zfs', 'umount_command': '/usr/local/bin/umount'}
  283. module.remove_data_source_dumps(
  284. hook_config=hook_config,
  285. config={'source_directories': '/mnt/dataset', 'zfs': hook_config},
  286. log_prefix='test',
  287. borgmatic_runtime_directory='/run/borgmatic',
  288. dry_run=False,
  289. )
  290. def test_remove_data_source_dumps_bails_for_umount_command_error():
  291. flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))
  292. flexmock(module.borgmatic.config.paths).should_receive(
  293. 'replace_temporary_subdirectory_with_glob'
  294. ).and_return('/run/borgmatic')
  295. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  296. flexmock(module.os.path).should_receive('isdir').and_return(True)
  297. flexmock(module.shutil).should_receive('rmtree')
  298. flexmock(module).should_receive('unmount_snapshot').with_args(
  299. '/usr/local/bin/umount', '/run/borgmatic/zfs_snapshots/mnt/dataset'
  300. ).and_raise(module.subprocess.CalledProcessError(1, 'wtf'))
  301. flexmock(module).should_receive('get_all_snapshots').never()
  302. flexmock(module).should_receive('destroy_snapshot').never()
  303. hook_config = {'zfs_command': '/usr/local/bin/zfs', 'umount_command': '/usr/local/bin/umount'}
  304. module.remove_data_source_dumps(
  305. hook_config=hook_config,
  306. config={'source_directories': '/mnt/dataset', 'zfs': hook_config},
  307. log_prefix='test',
  308. borgmatic_runtime_directory='/run/borgmatic',
  309. dry_run=False,
  310. )
  311. def test_remove_data_source_dumps_skips_unmount_snapshot_directories_that_are_not_actually_directories():
  312. flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))
  313. flexmock(module.borgmatic.config.paths).should_receive(
  314. 'replace_temporary_subdirectory_with_glob'
  315. ).and_return('/run/borgmatic')
  316. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  317. flexmock(module.os.path).should_receive('isdir').and_return(False)
  318. flexmock(module.shutil).should_receive('rmtree').never()
  319. flexmock(module).should_receive('unmount_snapshot').never()
  320. flexmock(module).should_receive('get_all_snapshots').and_return(
  321. ('dataset@borgmatic-1234', 'dataset@other', 'other@other', 'invalid')
  322. )
  323. flexmock(module).should_receive('destroy_snapshot').with_args(
  324. 'zfs', 'dataset@borgmatic-1234'
  325. ).once()
  326. module.remove_data_source_dumps(
  327. hook_config={},
  328. config={'source_directories': '/mnt/dataset', 'zfs': {}},
  329. log_prefix='test',
  330. borgmatic_runtime_directory='/run/borgmatic',
  331. dry_run=False,
  332. )
  333. def test_remove_data_source_dumps_skips_unmount_snapshot_mount_paths_that_are_not_actually_directories():
  334. flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))
  335. flexmock(module.borgmatic.config.paths).should_receive(
  336. 'replace_temporary_subdirectory_with_glob'
  337. ).and_return('/run/borgmatic')
  338. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  339. flexmock(module.os.path).should_receive('isdir').with_args(
  340. '/run/borgmatic/zfs_snapshots'
  341. ).and_return(True)
  342. flexmock(module.os.path).should_receive('isdir').with_args(
  343. '/run/borgmatic/zfs_snapshots/mnt/dataset'
  344. ).and_return(False)
  345. flexmock(module.shutil).should_receive('rmtree')
  346. flexmock(module).should_receive('unmount_snapshot').never()
  347. flexmock(module).should_receive('get_all_snapshots').and_return(
  348. ('dataset@borgmatic-1234', 'dataset@other', 'other@other', 'invalid')
  349. )
  350. flexmock(module).should_receive('destroy_snapshot').with_args(
  351. 'zfs', 'dataset@borgmatic-1234'
  352. ).once()
  353. module.remove_data_source_dumps(
  354. hook_config={},
  355. config={'source_directories': '/mnt/dataset', 'zfs': {}},
  356. log_prefix='test',
  357. borgmatic_runtime_directory='/run/borgmatic',
  358. dry_run=False,
  359. )
  360. def test_remove_data_source_dumps_with_dry_run_skips_unmount_and_destroy():
  361. flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))
  362. flexmock(module.borgmatic.config.paths).should_receive(
  363. 'replace_temporary_subdirectory_with_glob'
  364. ).and_return('/run/borgmatic')
  365. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  366. flexmock(module.os.path).should_receive('isdir').and_return(True)
  367. flexmock(module.shutil).should_receive('rmtree').never()
  368. flexmock(module).should_receive('unmount_snapshot').never()
  369. flexmock(module).should_receive('get_all_snapshots').and_return(
  370. ('dataset@borgmatic-1234', 'dataset@other', 'other@other', 'invalid')
  371. )
  372. flexmock(module).should_receive('destroy_snapshot').never()
  373. module.remove_data_source_dumps(
  374. hook_config={},
  375. config={'source_directories': '/mnt/dataset', 'zfs': {}},
  376. log_prefix='test',
  377. borgmatic_runtime_directory='/run/borgmatic',
  378. dry_run=True,
  379. )