test_zfs.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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_uses_custom_commands():
  79. flexmock(module).should_receive('get_datasets_to_backup').and_return(
  80. (('dataset', '/mnt/dataset'),)
  81. )
  82. flexmock(module.os).should_receive('getpid').and_return(1234)
  83. full_snapshot_name = 'dataset@borgmatic-1234'
  84. flexmock(module).should_receive('snapshot_dataset').with_args(
  85. '/usr/local/bin/zfs',
  86. full_snapshot_name,
  87. ).once()
  88. snapshot_mount_path = '/run/borgmatic/zfs_snapshots/./mnt/dataset'
  89. flexmock(module).should_receive('mount_snapshot').with_args(
  90. '/usr/local/bin/mount',
  91. full_snapshot_name,
  92. module.os.path.normpath(snapshot_mount_path),
  93. ).once()
  94. source_directories = ['/mnt/dataset']
  95. hook_config = {
  96. 'zfs_command': '/usr/local/bin/zfs',
  97. 'mount_command': '/usr/local/bin/mount',
  98. }
  99. assert (
  100. module.dump_data_sources(
  101. hook_config=hook_config,
  102. config={
  103. 'source_directories': source_directories,
  104. 'zfs': hook_config,
  105. },
  106. log_prefix='test',
  107. config_paths=('test.yaml',),
  108. borgmatic_runtime_directory='/run/borgmatic',
  109. source_directories=source_directories,
  110. dry_run=False,
  111. )
  112. == []
  113. )
  114. assert source_directories == [snapshot_mount_path]
  115. def test_dump_data_sources_with_dry_run_skips_commands_and_does_not_touch_source_directories():
  116. flexmock(module).should_receive('get_datasets_to_backup').and_return(
  117. (('dataset', '/mnt/dataset'),)
  118. )
  119. flexmock(module.os).should_receive('getpid').and_return(1234)
  120. flexmock(module).should_receive('snapshot_dataset').never()
  121. flexmock(module).should_receive('mount_snapshot').never()
  122. source_directories = ['/mnt/dataset']
  123. assert (
  124. module.dump_data_sources(
  125. hook_config={},
  126. config={'source_directories': '/mnt/dataset', 'zfs': {}},
  127. log_prefix='test',
  128. config_paths=('test.yaml',),
  129. borgmatic_runtime_directory='/run/borgmatic',
  130. source_directories=source_directories,
  131. dry_run=True,
  132. )
  133. == []
  134. )
  135. assert source_directories == ['/mnt/dataset']
  136. def test_get_all_snapshots_parses_list_output():
  137. flexmock(module.borgmatic.execute).should_receive(
  138. 'execute_command_and_capture_output'
  139. ).and_return(
  140. 'dataset1@borgmatic-1234\ndataset2@borgmatic-4567',
  141. )
  142. assert module.get_all_snapshots('zfs') == ('dataset1@borgmatic-1234', 'dataset2@borgmatic-4567')
  143. def test_remove_data_source_dumps_unmounts_and_destroys_snapshots():
  144. flexmock(module).should_receive('get_all_datasets').and_return((('dataset', '/mnt/dataset'),))
  145. flexmock(module.borgmatic.config.paths).should_receive(
  146. 'replace_temporary_subdirectory_with_glob'
  147. ).and_return('/run/borgmatic')
  148. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  149. flexmock(module.os.path).should_receive('isdir').and_return(True)
  150. flexmock(module.shutil).should_receive('rmtree')
  151. flexmock(module).should_receive('unmount_snapshot').with_args(
  152. 'umount', '/run/borgmatic/zfs_snapshots/mnt/dataset'
  153. ).once()
  154. flexmock(module).should_receive('get_all_snapshots').and_return(
  155. ('dataset@borgmatic-1234', 'dataset@other', 'other@other', 'invalid')
  156. )
  157. flexmock(module).should_receive('destroy_snapshot').with_args(
  158. 'zfs', 'dataset@borgmatic-1234'
  159. ).once()
  160. module.remove_data_source_dumps(
  161. hook_config={},
  162. config={'source_directories': '/mnt/dataset', 'zfs': {}},
  163. log_prefix='test',
  164. borgmatic_runtime_directory='/run/borgmatic',
  165. dry_run=False,
  166. )
  167. def test_remove_data_source_dumps_use_custom_commands():
  168. flexmock(module).should_receive('get_all_datasets').and_return((('dataset', '/mnt/dataset'),))
  169. flexmock(module.borgmatic.config.paths).should_receive(
  170. 'replace_temporary_subdirectory_with_glob'
  171. ).and_return('/run/borgmatic')
  172. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  173. flexmock(module.os.path).should_receive('isdir').and_return(True)
  174. flexmock(module.shutil).should_receive('rmtree')
  175. flexmock(module).should_receive('unmount_snapshot').with_args(
  176. '/usr/local/bin/umount', '/run/borgmatic/zfs_snapshots/mnt/dataset'
  177. ).once()
  178. flexmock(module).should_receive('get_all_snapshots').and_return(
  179. ('dataset@borgmatic-1234', 'dataset@other', 'other@other', 'invalid')
  180. )
  181. flexmock(module).should_receive('destroy_snapshot').with_args(
  182. '/usr/local/bin/zfs', 'dataset@borgmatic-1234'
  183. ).once()
  184. hook_config = {'zfs_command': '/usr/local/bin/zfs', 'umount_command': '/usr/local/bin/umount'}
  185. module.remove_data_source_dumps(
  186. hook_config=hook_config,
  187. config={'source_directories': '/mnt/dataset', 'zfs': hook_config},
  188. log_prefix='test',
  189. borgmatic_runtime_directory='/run/borgmatic',
  190. dry_run=False,
  191. )
  192. def test_remove_data_source_dumps_bails_for_missing_zfs_command():
  193. flexmock(module).should_receive('get_all_datasets').and_raise(FileNotFoundError)
  194. flexmock(module.borgmatic.config.paths).should_receive(
  195. 'replace_temporary_subdirectory_with_glob'
  196. ).never()
  197. hook_config = {'zfs_command': 'wtf'}
  198. module.remove_data_source_dumps(
  199. hook_config=hook_config,
  200. config={'source_directories': '/mnt/dataset', 'zfs': hook_config},
  201. log_prefix='test',
  202. borgmatic_runtime_directory='/run/borgmatic',
  203. dry_run=False,
  204. )
  205. def test_remove_data_source_dumps_bails_for_zfs_command_error():
  206. flexmock(module).should_receive('get_all_datasets').and_raise(
  207. module.subprocess.CalledProcessError(1, 'wtf')
  208. )
  209. flexmock(module.borgmatic.config.paths).should_receive(
  210. 'replace_temporary_subdirectory_with_glob'
  211. ).never()
  212. hook_config = {'zfs_command': 'wtf'}
  213. module.remove_data_source_dumps(
  214. hook_config=hook_config,
  215. config={'source_directories': '/mnt/dataset', 'zfs': hook_config},
  216. log_prefix='test',
  217. borgmatic_runtime_directory='/run/borgmatic',
  218. dry_run=False,
  219. )
  220. def test_remove_data_source_dumps_bails_for_missing_umount_command():
  221. flexmock(module).should_receive('get_all_datasets').and_return((('dataset', '/mnt/dataset'),))
  222. flexmock(module.borgmatic.config.paths).should_receive(
  223. 'replace_temporary_subdirectory_with_glob'
  224. ).and_return('/run/borgmatic')
  225. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  226. flexmock(module.os.path).should_receive('isdir').and_return(True)
  227. flexmock(module.shutil).should_receive('rmtree')
  228. flexmock(module).should_receive('unmount_snapshot').with_args(
  229. '/usr/local/bin/umount', '/run/borgmatic/zfs_snapshots/mnt/dataset'
  230. ).and_raise(FileNotFoundError)
  231. flexmock(module).should_receive('get_all_snapshots').never()
  232. flexmock(module).should_receive('destroy_snapshot').never()
  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_umount_command_error():
  242. flexmock(module).should_receive('get_all_datasets').and_return((('dataset', '/mnt/dataset'),))
  243. flexmock(module.borgmatic.config.paths).should_receive(
  244. 'replace_temporary_subdirectory_with_glob'
  245. ).and_return('/run/borgmatic')
  246. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  247. flexmock(module.os.path).should_receive('isdir').and_return(True)
  248. flexmock(module.shutil).should_receive('rmtree')
  249. flexmock(module).should_receive('unmount_snapshot').with_args(
  250. '/usr/local/bin/umount', '/run/borgmatic/zfs_snapshots/mnt/dataset'
  251. ).and_raise(module.subprocess.CalledProcessError(1, 'wtf'))
  252. flexmock(module).should_receive('get_all_snapshots').never()
  253. flexmock(module).should_receive('destroy_snapshot').never()
  254. hook_config = {'zfs_command': '/usr/local/bin/zfs', 'umount_command': '/usr/local/bin/umount'}
  255. module.remove_data_source_dumps(
  256. hook_config=hook_config,
  257. config={'source_directories': '/mnt/dataset', 'zfs': hook_config},
  258. log_prefix='test',
  259. borgmatic_runtime_directory='/run/borgmatic',
  260. dry_run=False,
  261. )
  262. def test_remove_data_source_dumps_skips_unmount_snapshot_directories_that_are_not_actually_directories():
  263. flexmock(module).should_receive('get_all_datasets').and_return((('dataset', '/mnt/dataset'),))
  264. flexmock(module.borgmatic.config.paths).should_receive(
  265. 'replace_temporary_subdirectory_with_glob'
  266. ).and_return('/run/borgmatic')
  267. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  268. flexmock(module.os.path).should_receive('isdir').and_return(False)
  269. flexmock(module.shutil).should_receive('rmtree').never()
  270. flexmock(module).should_receive('unmount_snapshot').never()
  271. flexmock(module).should_receive('get_all_snapshots').and_return(
  272. ('dataset@borgmatic-1234', 'dataset@other', 'other@other', 'invalid')
  273. )
  274. flexmock(module).should_receive('destroy_snapshot').with_args(
  275. 'zfs', 'dataset@borgmatic-1234'
  276. ).once()
  277. module.remove_data_source_dumps(
  278. hook_config={},
  279. config={'source_directories': '/mnt/dataset', 'zfs': {}},
  280. log_prefix='test',
  281. borgmatic_runtime_directory='/run/borgmatic',
  282. dry_run=False,
  283. )
  284. def test_remove_data_source_dumps_skips_unmount_snapshot_mount_paths_that_are_not_actually_directories():
  285. flexmock(module).should_receive('get_all_datasets').and_return((('dataset', '/mnt/dataset'),))
  286. flexmock(module.borgmatic.config.paths).should_receive(
  287. 'replace_temporary_subdirectory_with_glob'
  288. ).and_return('/run/borgmatic')
  289. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  290. flexmock(module.os.path).should_receive('isdir').with_args(
  291. '/run/borgmatic/zfs_snapshots'
  292. ).and_return(True)
  293. flexmock(module.os.path).should_receive('isdir').with_args(
  294. '/run/borgmatic/zfs_snapshots/mnt/dataset'
  295. ).and_return(False)
  296. flexmock(module.shutil).should_receive('rmtree')
  297. flexmock(module).should_receive('unmount_snapshot').never()
  298. flexmock(module).should_receive('get_all_snapshots').and_return(
  299. ('dataset@borgmatic-1234', 'dataset@other', 'other@other', 'invalid')
  300. )
  301. flexmock(module).should_receive('destroy_snapshot').with_args(
  302. 'zfs', 'dataset@borgmatic-1234'
  303. ).once()
  304. module.remove_data_source_dumps(
  305. hook_config={},
  306. config={'source_directories': '/mnt/dataset', 'zfs': {}},
  307. log_prefix='test',
  308. borgmatic_runtime_directory='/run/borgmatic',
  309. dry_run=False,
  310. )
  311. def test_remove_data_source_dumps_with_dry_run_skips_unmount_and_destroy():
  312. flexmock(module).should_receive('get_all_datasets').and_return((('dataset', '/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(True)
  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').never()
  324. module.remove_data_source_dumps(
  325. hook_config={},
  326. config={'source_directories': '/mnt/dataset', 'zfs': {}},
  327. log_prefix='test',
  328. borgmatic_runtime_directory='/run/borgmatic',
  329. dry_run=True,
  330. )