test_zfs.py 16 KB

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