test_mount.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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.options).should_receive('get_working_directory').and_return(
  8. working_directory,
  9. )
  10. flexmock(module).should_receive('execute_command').with_args(
  11. command,
  12. extra_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(log_json=False),
  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(log_json=False),
  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(log_json=False),
  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(log_json=False),
  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(log_json=False),
  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(log_json=False),
  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(log_json=False),
  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(log_json=False),
  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={},
  162. local_borg_version='1.2.3',
  163. global_arguments=flexmock(log_json=True),
  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(log_json=False),
  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(log_json=False),
  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(log_json=False),
  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.options).should_receive('get_working_directory').and_return(
  219. None
  220. )
  221. flexmock(module).should_receive('execute_command').with_args(
  222. ('borg', 'mount', '--foreground', 'repo::archive', '/mnt'),
  223. output_file=module.DO_NOT_CAPTURE,
  224. extra_environment=None,
  225. working_directory=None,
  226. borg_local_path='borg',
  227. borg_exit_codes=None,
  228. ).once()
  229. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=True)
  230. module.mount_archive(
  231. repository_path='repo',
  232. archive='archive',
  233. mount_arguments=mount_arguments,
  234. config={},
  235. local_borg_version='1.2.3',
  236. global_arguments=flexmock(log_json=False),
  237. )
  238. def test_mount_archive_calls_borg_with_options_flags():
  239. flexmock(module.feature).should_receive('available').and_return(False)
  240. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  241. ('repo::archive',)
  242. )
  243. insert_execute_command_mock(('borg', 'mount', '-o', 'super_mount', 'repo::archive', '/mnt'))
  244. mount_arguments = flexmock(
  245. mount_point='/mnt', options='super_mount', paths=None, foreground=False
  246. )
  247. module.mount_archive(
  248. repository_path='repo',
  249. archive='archive',
  250. mount_arguments=mount_arguments,
  251. config={},
  252. local_borg_version='1.2.3',
  253. global_arguments=flexmock(log_json=False),
  254. )
  255. def test_mount_archive_with_date_based_matching_calls_borg_with_date_based_flags():
  256. flexmock(module.flags).should_receive('make_flags').and_return(())
  257. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  258. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  259. (
  260. '--newer',
  261. '1d',
  262. '--newest',
  263. '1y',
  264. '--older',
  265. '1m',
  266. '--oldest',
  267. '1w',
  268. '--match-archives',
  269. None,
  270. )
  271. )
  272. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  273. flexmock(module.environment).should_receive('make_environment')
  274. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  275. None
  276. )
  277. flexmock(module).should_receive('execute_command').with_args(
  278. (
  279. 'borg',
  280. 'mount',
  281. '--newer',
  282. '1d',
  283. '--newest',
  284. '1y',
  285. '--older',
  286. '1m',
  287. '--oldest',
  288. '1w',
  289. '--match-archives',
  290. None,
  291. '--repo',
  292. 'repo',
  293. '/mnt',
  294. ),
  295. extra_environment=None,
  296. working_directory=None,
  297. borg_local_path='borg',
  298. borg_exit_codes=None,
  299. )
  300. mount_arguments = flexmock(
  301. mount_point='/mnt',
  302. options=None,
  303. paths=None,
  304. foreground=False,
  305. newer='1d',
  306. newest='1y',
  307. older='1m',
  308. oldest='1w',
  309. )
  310. module.mount_archive(
  311. repository_path='repo',
  312. archive=None,
  313. mount_arguments=mount_arguments,
  314. config={},
  315. local_borg_version='1.2.3',
  316. global_arguments=flexmock(log_json=False),
  317. )
  318. def test_mount_archive_calls_borg_with_working_directory():
  319. flexmock(module.feature).should_receive('available').and_return(False)
  320. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  321. insert_execute_command_mock(('borg', 'mount', 'repo', '/mnt'), working_directory='/working/dir')
  322. mount_arguments = flexmock(mount_point='/mnt', options=None, paths=None, foreground=False)
  323. module.mount_archive(
  324. repository_path='repo',
  325. archive=None,
  326. mount_arguments=mount_arguments,
  327. config={'working_directory': '/working/dir'},
  328. local_borg_version='1.2.3',
  329. global_arguments=flexmock(log_json=False),
  330. )