test_mount.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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', options=None, paths=['path1', 'path2'], foreground=False
  73. )
  74. module.mount_archive(
  75. repository_path='repo',
  76. archive='archive',
  77. mount_arguments=mount_arguments,
  78. config={},
  79. local_borg_version='1.2.3',
  80. global_arguments=flexmock(),
  81. )
  82. def test_mount_archive_calls_borg_with_local_path():
  83. flexmock(module.feature).should_receive('available').and_return(False)
  84. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  85. ('repo::archive',)
  86. )
  87. insert_execute_command_mock(('borg1', 'mount', 'repo::archive', '/mnt'))
  88. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  89. module.mount_archive(
  90. repository_path='repo',
  91. archive='archive',
  92. mount_arguments=mount_arguments,
  93. config={},
  94. local_borg_version='1.2.3',
  95. global_arguments=flexmock(),
  96. local_path='borg1',
  97. )
  98. def test_mount_archive_calls_borg_using_exit_codes():
  99. flexmock(module.feature).should_receive('available').and_return(False)
  100. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  101. ('repo::archive',)
  102. )
  103. borg_exit_codes = flexmock()
  104. insert_execute_command_mock(
  105. ('borg', 'mount', 'repo::archive', '/mnt'),
  106. borg_exit_codes=borg_exit_codes,
  107. )
  108. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  109. module.mount_archive(
  110. repository_path='repo',
  111. archive='archive',
  112. mount_arguments=mount_arguments,
  113. config={'borg_exit_codes': borg_exit_codes},
  114. local_borg_version='1.2.3',
  115. global_arguments=flexmock(),
  116. )
  117. def test_mount_archive_calls_borg_with_remote_path_flags():
  118. flexmock(module.feature).should_receive('available').and_return(False)
  119. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  120. ('repo::archive',)
  121. )
  122. insert_execute_command_mock(
  123. ('borg', 'mount', '--remote-path', 'borg1', 'repo::archive', '/mnt')
  124. )
  125. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  126. module.mount_archive(
  127. repository_path='repo',
  128. archive='archive',
  129. mount_arguments=mount_arguments,
  130. config={},
  131. local_borg_version='1.2.3',
  132. global_arguments=flexmock(),
  133. remote_path='borg1',
  134. )
  135. def test_mount_archive_calls_borg_with_umask_flags():
  136. flexmock(module.feature).should_receive('available').and_return(False)
  137. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  138. ('repo::archive',)
  139. )
  140. insert_execute_command_mock(('borg', 'mount', '--umask', '0770', 'repo::archive', '/mnt'))
  141. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  142. module.mount_archive(
  143. repository_path='repo',
  144. archive='archive',
  145. mount_arguments=mount_arguments,
  146. config={'umask': '0770'},
  147. local_borg_version='1.2.3',
  148. global_arguments=flexmock(),
  149. )
  150. def test_mount_archive_calls_borg_with_log_json_flags():
  151. flexmock(module.feature).should_receive('available').and_return(False)
  152. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  153. ('repo::archive',)
  154. )
  155. insert_execute_command_mock(('borg', 'mount', '--log-json', 'repo::archive', '/mnt'))
  156. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  157. module.mount_archive(
  158. repository_path='repo',
  159. archive='archive',
  160. mount_arguments=mount_arguments,
  161. config={'log_json': True},
  162. local_borg_version='1.2.3',
  163. global_arguments=flexmock(),
  164. )
  165. def test_mount_archive_calls_borg_with_lock_wait_flags():
  166. flexmock(module.feature).should_receive('available').and_return(False)
  167. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  168. ('repo::archive',)
  169. )
  170. insert_execute_command_mock(('borg', 'mount', '--lock-wait', '5', 'repo::archive', '/mnt'))
  171. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  172. module.mount_archive(
  173. repository_path='repo',
  174. archive='archive',
  175. mount_arguments=mount_arguments,
  176. config={'lock_wait': '5'},
  177. local_borg_version='1.2.3',
  178. global_arguments=flexmock(),
  179. )
  180. def test_mount_archive_with_log_info_calls_borg_with_info_parameter():
  181. flexmock(module.feature).should_receive('available').and_return(False)
  182. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  183. ('repo::archive',)
  184. )
  185. insert_execute_command_mock(('borg', 'mount', '--info', 'repo::archive', '/mnt'))
  186. insert_logging_mock(logging.INFO)
  187. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  188. module.mount_archive(
  189. repository_path='repo',
  190. archive='archive',
  191. mount_arguments=mount_arguments,
  192. config={},
  193. local_borg_version='1.2.3',
  194. global_arguments=flexmock(),
  195. )
  196. def test_mount_archive_with_log_debug_calls_borg_with_debug_flags():
  197. flexmock(module.feature).should_receive('available').and_return(False)
  198. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  199. ('repo::archive',)
  200. )
  201. insert_execute_command_mock(('borg', 'mount', '--debug', '--show-rc', 'repo::archive', '/mnt'))
  202. insert_logging_mock(logging.DEBUG)
  203. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  204. module.mount_archive(
  205. repository_path='repo',
  206. archive='archive',
  207. mount_arguments=mount_arguments,
  208. config={},
  209. local_borg_version='1.2.3',
  210. global_arguments=flexmock(),
  211. )
  212. def test_mount_archive_calls_borg_with_foreground_parameter():
  213. flexmock(module.feature).should_receive('available').and_return(False)
  214. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  215. ('repo::archive',)
  216. )
  217. flexmock(module.environment).should_receive('make_environment')
  218. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  219. flexmock(module).should_receive('execute_command').with_args(
  220. ('borg', 'mount', '--foreground', 'repo::archive', '/mnt'),
  221. output_file=module.DO_NOT_CAPTURE,
  222. environment=None,
  223. working_directory=None,
  224. borg_local_path='borg',
  225. borg_exit_codes=None,
  226. ).once()
  227. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=True)
  228. module.mount_archive(
  229. repository_path='repo',
  230. archive='archive',
  231. mount_arguments=mount_arguments,
  232. config={},
  233. local_borg_version='1.2.3',
  234. global_arguments=flexmock(),
  235. )
  236. def test_mount_archive_calls_borg_with_options_flags():
  237. flexmock(module.feature).should_receive('available').and_return(False)
  238. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  239. ('repo::archive',)
  240. )
  241. insert_execute_command_mock(('borg', 'mount', '-o', 'super_mount', 'repo::archive', '/mnt'))
  242. mount_arguments = flexmock(
  243. mount_point='/mnt', options='super_mount', paths=None, foreground=False
  244. )
  245. module.mount_archive(
  246. repository_path='repo',
  247. archive='archive',
  248. mount_arguments=mount_arguments,
  249. config={},
  250. local_borg_version='1.2.3',
  251. global_arguments=flexmock(),
  252. )
  253. def test_mount_archive_with_date_based_matching_calls_borg_with_date_based_flags():
  254. flexmock(module.flags).should_receive('make_flags').and_return(())
  255. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  256. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  257. (
  258. '--newer',
  259. '1d',
  260. '--newest',
  261. '1y',
  262. '--older',
  263. '1m',
  264. '--oldest',
  265. '1w',
  266. '--match-archives',
  267. None,
  268. )
  269. )
  270. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  271. flexmock(module.environment).should_receive('make_environment')
  272. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  273. flexmock(module).should_receive('execute_command').with_args(
  274. (
  275. 'borg',
  276. 'mount',
  277. '--newer',
  278. '1d',
  279. '--newest',
  280. '1y',
  281. '--older',
  282. '1m',
  283. '--oldest',
  284. '1w',
  285. '--match-archives',
  286. None,
  287. '--repo',
  288. 'repo',
  289. '/mnt',
  290. ),
  291. environment=None,
  292. working_directory=None,
  293. borg_local_path='borg',
  294. borg_exit_codes=None,
  295. )
  296. mount_arguments = flexmock(
  297. mount_point='/mnt',
  298. options=None,
  299. paths=None,
  300. foreground=False,
  301. newer='1d',
  302. newest='1y',
  303. older='1m',
  304. oldest='1w',
  305. )
  306. module.mount_archive(
  307. repository_path='repo',
  308. archive=None,
  309. mount_arguments=mount_arguments,
  310. config={},
  311. local_borg_version='1.2.3',
  312. global_arguments=flexmock(),
  313. )
  314. def test_mount_archive_calls_borg_with_working_directory():
  315. flexmock(module.feature).should_receive('available').and_return(False)
  316. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  317. insert_execute_command_mock(('borg', 'mount', 'repo', '/mnt'), working_directory='/working/dir')
  318. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  319. module.mount_archive(
  320. repository_path='repo',
  321. archive=None,
  322. mount_arguments=mount_arguments,
  323. config={'working_directory': '/working/dir'},
  324. local_borg_version='1.2.3',
  325. global_arguments=flexmock(),
  326. )