test_mount.py 11 KB

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