test_mount.py 12 KB

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