test_mount.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. import logging
  2. from flexmock import flexmock
  3. from borgmatic.borg import mount as module
  4. from ..test_verbosity import insert_logging_mock
  5. def insert_execute_command_mock(command, working_directory=None, borg_exit_codes=None):
  6. flexmock(module.environment).should_receive('make_environment')
  7. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  8. working_directory,
  9. )
  10. flexmock(module).should_receive('execute_command').with_args(
  11. command,
  12. environment=None,
  13. working_directory=working_directory,
  14. borg_local_path=command[0],
  15. borg_exit_codes=borg_exit_codes,
  16. ).once()
  17. def test_mount_archive_calls_borg_with_required_flags():
  18. flexmock(module.feature).should_receive('available').and_return(False)
  19. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  20. insert_execute_command_mock(('borg', 'mount', 'repo', '/mnt'))
  21. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  22. module.mount_archive(
  23. repository_path='repo',
  24. archive=None,
  25. mount_arguments=mount_arguments,
  26. config={},
  27. local_borg_version='1.2.3',
  28. global_arguments=flexmock(),
  29. )
  30. def test_mount_archive_with_borg_features_calls_borg_with_repository_and_match_archives_flags():
  31. flexmock(module.feature).should_receive('available').and_return(True)
  32. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  33. (
  34. '--repo',
  35. 'repo',
  36. ),
  37. )
  38. insert_execute_command_mock(
  39. ('borg', 'mount', '--repo', 'repo', '--match-archives', 'archive', '/mnt'),
  40. )
  41. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  42. module.mount_archive(
  43. repository_path='repo',
  44. archive='archive',
  45. mount_arguments=mount_arguments,
  46. config={},
  47. local_borg_version='1.2.3',
  48. global_arguments=flexmock(),
  49. )
  50. def test_mount_archive_without_archive_calls_borg_with_repository_flags_only():
  51. flexmock(module.feature).should_receive('available').and_return(False)
  52. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  53. ('repo::archive',),
  54. )
  55. insert_execute_command_mock(('borg', 'mount', 'repo::archive', '/mnt'))
  56. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  57. module.mount_archive(
  58. repository_path='repo',
  59. archive='archive',
  60. mount_arguments=mount_arguments,
  61. config={},
  62. local_borg_version='1.2.3',
  63. global_arguments=flexmock(),
  64. )
  65. def test_mount_archive_calls_borg_with_path_flags():
  66. flexmock(module.feature).should_receive('available').and_return(False)
  67. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  68. ('repo::archive',),
  69. )
  70. insert_execute_command_mock(('borg', 'mount', 'repo::archive', '/mnt', 'path1', 'path2'))
  71. mount_arguments = flexmock(
  72. mount_point='/mnt',
  73. options=None,
  74. paths=['path1', 'path2'],
  75. foreground=False,
  76. )
  77. module.mount_archive(
  78. repository_path='repo',
  79. archive='archive',
  80. mount_arguments=mount_arguments,
  81. config={},
  82. local_borg_version='1.2.3',
  83. global_arguments=flexmock(),
  84. )
  85. def test_mount_archive_calls_borg_with_local_path():
  86. flexmock(module.feature).should_receive('available').and_return(False)
  87. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  88. ('repo::archive',),
  89. )
  90. insert_execute_command_mock(('borg1', 'mount', 'repo::archive', '/mnt'))
  91. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  92. module.mount_archive(
  93. repository_path='repo',
  94. archive='archive',
  95. mount_arguments=mount_arguments,
  96. config={},
  97. local_borg_version='1.2.3',
  98. global_arguments=flexmock(),
  99. local_path='borg1',
  100. )
  101. def test_mount_archive_calls_borg_using_exit_codes():
  102. flexmock(module.feature).should_receive('available').and_return(False)
  103. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  104. ('repo::archive',),
  105. )
  106. borg_exit_codes = flexmock()
  107. insert_execute_command_mock(
  108. ('borg', 'mount', 'repo::archive', '/mnt'),
  109. borg_exit_codes=borg_exit_codes,
  110. )
  111. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  112. module.mount_archive(
  113. repository_path='repo',
  114. archive='archive',
  115. mount_arguments=mount_arguments,
  116. config={'borg_exit_codes': borg_exit_codes},
  117. local_borg_version='1.2.3',
  118. global_arguments=flexmock(),
  119. )
  120. def test_mount_archive_calls_borg_with_remote_path_flags():
  121. flexmock(module.feature).should_receive('available').and_return(False)
  122. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  123. ('repo::archive',),
  124. )
  125. insert_execute_command_mock(
  126. ('borg', 'mount', '--remote-path', 'borg1', 'repo::archive', '/mnt'),
  127. )
  128. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  129. module.mount_archive(
  130. repository_path='repo',
  131. archive='archive',
  132. mount_arguments=mount_arguments,
  133. config={},
  134. local_borg_version='1.2.3',
  135. global_arguments=flexmock(),
  136. remote_path='borg1',
  137. )
  138. def test_mount_archive_calls_borg_with_umask_flags():
  139. flexmock(module.feature).should_receive('available').and_return(False)
  140. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  141. ('repo::archive',),
  142. )
  143. insert_execute_command_mock(('borg', 'mount', '--umask', '0770', 'repo::archive', '/mnt'))
  144. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  145. module.mount_archive(
  146. repository_path='repo',
  147. archive='archive',
  148. mount_arguments=mount_arguments,
  149. config={'umask': '0770'},
  150. local_borg_version='1.2.3',
  151. global_arguments=flexmock(),
  152. )
  153. def test_mount_archive_calls_borg_with_log_json_flags():
  154. flexmock(module.feature).should_receive('available').and_return(False)
  155. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  156. ('repo::archive',),
  157. )
  158. insert_execute_command_mock(('borg', 'mount', '--log-json', 'repo::archive', '/mnt'))
  159. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  160. module.mount_archive(
  161. repository_path='repo',
  162. archive='archive',
  163. mount_arguments=mount_arguments,
  164. config={'log_json': True},
  165. local_borg_version='1.2.3',
  166. global_arguments=flexmock(),
  167. )
  168. def test_mount_archive_calls_borg_with_lock_wait_flags():
  169. flexmock(module.feature).should_receive('available').and_return(False)
  170. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  171. ('repo::archive',),
  172. )
  173. insert_execute_command_mock(('borg', 'mount', '--lock-wait', '5', 'repo::archive', '/mnt'))
  174. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  175. module.mount_archive(
  176. repository_path='repo',
  177. archive='archive',
  178. mount_arguments=mount_arguments,
  179. config={'lock_wait': '5'},
  180. local_borg_version='1.2.3',
  181. global_arguments=flexmock(),
  182. )
  183. def test_mount_archive_calls_borg_with_extra_borg_options():
  184. flexmock(module.feature).should_receive('available').and_return(False)
  185. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  186. ('repo::archive',),
  187. )
  188. insert_execute_command_mock(
  189. ('borg', 'mount', '--extra', 'value with space', 'repo::archive', '/mnt')
  190. )
  191. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  192. module.mount_archive(
  193. repository_path='repo',
  194. archive='archive',
  195. mount_arguments=mount_arguments,
  196. config={'extra_borg_options': {'mount': '--extra "value with space"'}},
  197. local_borg_version='1.2.3',
  198. global_arguments=flexmock(),
  199. )
  200. def test_mount_archive_with_log_info_calls_borg_with_info_parameter():
  201. flexmock(module.feature).should_receive('available').and_return(False)
  202. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  203. ('repo::archive',),
  204. )
  205. insert_execute_command_mock(('borg', 'mount', '--info', 'repo::archive', '/mnt'))
  206. insert_logging_mock(logging.INFO)
  207. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  208. module.mount_archive(
  209. repository_path='repo',
  210. archive='archive',
  211. mount_arguments=mount_arguments,
  212. config={},
  213. local_borg_version='1.2.3',
  214. global_arguments=flexmock(),
  215. )
  216. def test_mount_archive_with_log_debug_calls_borg_with_debug_flags():
  217. flexmock(module.feature).should_receive('available').and_return(False)
  218. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  219. ('repo::archive',),
  220. )
  221. insert_execute_command_mock(('borg', 'mount', '--debug', '--show-rc', 'repo::archive', '/mnt'))
  222. insert_logging_mock(logging.DEBUG)
  223. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  224. module.mount_archive(
  225. repository_path='repo',
  226. archive='archive',
  227. mount_arguments=mount_arguments,
  228. config={},
  229. local_borg_version='1.2.3',
  230. global_arguments=flexmock(),
  231. )
  232. def test_mount_archive_calls_borg_with_foreground_parameter():
  233. flexmock(module.feature).should_receive('available').and_return(False)
  234. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  235. ('repo::archive',),
  236. )
  237. flexmock(module.environment).should_receive('make_environment')
  238. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  239. flexmock(module).should_receive('execute_command').with_args(
  240. ('borg', 'mount', '--foreground', 'repo::archive', '/mnt'),
  241. output_file=module.DO_NOT_CAPTURE,
  242. environment=None,
  243. working_directory=None,
  244. borg_local_path='borg',
  245. borg_exit_codes=None,
  246. ).once()
  247. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=True)
  248. module.mount_archive(
  249. repository_path='repo',
  250. archive='archive',
  251. mount_arguments=mount_arguments,
  252. config={},
  253. local_borg_version='1.2.3',
  254. global_arguments=flexmock(),
  255. )
  256. def test_mount_archive_calls_borg_with_options_flags():
  257. flexmock(module.feature).should_receive('available').and_return(False)
  258. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  259. ('repo::archive',),
  260. )
  261. insert_execute_command_mock(('borg', 'mount', '-o', 'super_mount', 'repo::archive', '/mnt'))
  262. mount_arguments = flexmock(
  263. mount_point='/mnt',
  264. options='super_mount',
  265. paths=None,
  266. foreground=False,
  267. )
  268. module.mount_archive(
  269. repository_path='repo',
  270. archive='archive',
  271. mount_arguments=mount_arguments,
  272. config={},
  273. local_borg_version='1.2.3',
  274. global_arguments=flexmock(),
  275. )
  276. def test_mount_archive_with_date_based_matching_calls_borg_with_date_based_flags():
  277. flexmock(module.flags).should_receive('make_flags').and_return(())
  278. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  279. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  280. (
  281. '--newer',
  282. '1d',
  283. '--newest',
  284. '1y',
  285. '--older',
  286. '1m',
  287. '--oldest',
  288. '1w',
  289. '--match-archives',
  290. None,
  291. ),
  292. )
  293. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  294. flexmock(module.environment).should_receive('make_environment')
  295. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  296. flexmock(module).should_receive('execute_command').with_args(
  297. (
  298. 'borg',
  299. 'mount',
  300. '--newer',
  301. '1d',
  302. '--newest',
  303. '1y',
  304. '--older',
  305. '1m',
  306. '--oldest',
  307. '1w',
  308. '--match-archives',
  309. None,
  310. '--repo',
  311. 'repo',
  312. '/mnt',
  313. ),
  314. environment=None,
  315. working_directory=None,
  316. borg_local_path='borg',
  317. borg_exit_codes=None,
  318. )
  319. mount_arguments = flexmock(
  320. mount_point='/mnt',
  321. options=None,
  322. paths=None,
  323. foreground=False,
  324. newer='1d',
  325. newest='1y',
  326. older='1m',
  327. oldest='1w',
  328. )
  329. module.mount_archive(
  330. repository_path='repo',
  331. archive=None,
  332. mount_arguments=mount_arguments,
  333. config={},
  334. local_borg_version='1.2.3',
  335. global_arguments=flexmock(),
  336. )
  337. def test_mount_archive_calls_borg_with_working_directory():
  338. flexmock(module.feature).should_receive('available').and_return(False)
  339. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  340. insert_execute_command_mock(('borg', 'mount', 'repo', '/mnt'), working_directory='/working/dir')
  341. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  342. module.mount_archive(
  343. repository_path='repo',
  344. archive=None,
  345. mount_arguments=mount_arguments,
  346. config={'working_directory': '/working/dir'},
  347. local_borg_version='1.2.3',
  348. global_arguments=flexmock(),
  349. )