test_zfs.py 12 KB

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