test_mount.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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_with_log_info_calls_borg_with_info_parameter():
  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(('borg', 'mount', '--info', 'repo::archive', '/mnt'))
  189. insert_logging_mock(logging.INFO)
  190. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  191. module.mount_archive(
  192. repository_path='repo',
  193. archive='archive',
  194. mount_arguments=mount_arguments,
  195. config={},
  196. local_borg_version='1.2.3',
  197. global_arguments=flexmock(),
  198. )
  199. def test_mount_archive_with_log_debug_calls_borg_with_debug_flags():
  200. flexmock(module.feature).should_receive('available').and_return(False)
  201. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  202. ('repo::archive',),
  203. )
  204. insert_execute_command_mock(('borg', 'mount', '--debug', '--show-rc', 'repo::archive', '/mnt'))
  205. insert_logging_mock(logging.DEBUG)
  206. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  207. module.mount_archive(
  208. repository_path='repo',
  209. archive='archive',
  210. mount_arguments=mount_arguments,
  211. config={},
  212. local_borg_version='1.2.3',
  213. global_arguments=flexmock(),
  214. )
  215. def test_mount_archive_calls_borg_with_foreground_parameter():
  216. flexmock(module.feature).should_receive('available').and_return(False)
  217. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  218. ('repo::archive',),
  219. )
  220. flexmock(module.environment).should_receive('make_environment')
  221. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  222. flexmock(module).should_receive('execute_command').with_args(
  223. ('borg', 'mount', '--foreground', 'repo::archive', '/mnt'),
  224. output_file=module.DO_NOT_CAPTURE,
  225. environment=None,
  226. working_directory=None,
  227. borg_local_path='borg',
  228. borg_exit_codes=None,
  229. ).once()
  230. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=True)
  231. module.mount_archive(
  232. repository_path='repo',
  233. archive='archive',
  234. mount_arguments=mount_arguments,
  235. config={},
  236. local_borg_version='1.2.3',
  237. global_arguments=flexmock(),
  238. )
  239. def test_mount_archive_calls_borg_with_options_flags():
  240. flexmock(module.feature).should_receive('available').and_return(False)
  241. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  242. ('repo::archive',),
  243. )
  244. insert_execute_command_mock(('borg', 'mount', '-o', 'super_mount', 'repo::archive', '/mnt'))
  245. mount_arguments = flexmock(
  246. mount_point='/mnt',
  247. options='super_mount',
  248. paths=None,
  249. foreground=False,
  250. )
  251. module.mount_archive(
  252. repository_path='repo',
  253. archive='archive',
  254. mount_arguments=mount_arguments,
  255. config={},
  256. local_borg_version='1.2.3',
  257. global_arguments=flexmock(),
  258. )
  259. def test_mount_archive_with_date_based_matching_calls_borg_with_date_based_flags():
  260. flexmock(module.flags).should_receive('make_flags').and_return(())
  261. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  262. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  263. (
  264. '--newer',
  265. '1d',
  266. '--newest',
  267. '1y',
  268. '--older',
  269. '1m',
  270. '--oldest',
  271. '1w',
  272. '--match-archives',
  273. None,
  274. ),
  275. )
  276. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  277. flexmock(module.environment).should_receive('make_environment')
  278. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  279. flexmock(module).should_receive('execute_command').with_args(
  280. (
  281. 'borg',
  282. 'mount',
  283. '--newer',
  284. '1d',
  285. '--newest',
  286. '1y',
  287. '--older',
  288. '1m',
  289. '--oldest',
  290. '1w',
  291. '--match-archives',
  292. None,
  293. '--repo',
  294. 'repo',
  295. '/mnt',
  296. ),
  297. environment=None,
  298. working_directory=None,
  299. borg_local_path='borg',
  300. borg_exit_codes=None,
  301. )
  302. mount_arguments = flexmock(
  303. mount_point='/mnt',
  304. options=None,
  305. paths=None,
  306. foreground=False,
  307. newer='1d',
  308. newest='1y',
  309. older='1m',
  310. oldest='1w',
  311. )
  312. module.mount_archive(
  313. repository_path='repo',
  314. archive=None,
  315. mount_arguments=mount_arguments,
  316. config={},
  317. local_borg_version='1.2.3',
  318. global_arguments=flexmock(),
  319. )
  320. def test_mount_archive_calls_borg_with_working_directory():
  321. flexmock(module.feature).should_receive('available').and_return(False)
  322. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  323. insert_execute_command_mock(('borg', 'mount', 'repo', '/mnt'), working_directory='/working/dir')
  324. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  325. module.mount_archive(
  326. repository_path='repo',
  327. archive=None,
  328. mount_arguments=mount_arguments,
  329. config={'working_directory': '/working/dir'},
  330. local_borg_version='1.2.3',
  331. global_arguments=flexmock(),
  332. )