test_mount.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. module.mount_archive(
  17. repository_path='repo',
  18. archive=None,
  19. mount_point='/mnt',
  20. paths=None,
  21. foreground=False,
  22. options=None,
  23. storage_config={},
  24. local_borg_version='1.2.3',
  25. global_arguments=flexmock(log_json=False),
  26. )
  27. def test_mount_archive_with_borg_features_calls_borg_with_repository_and_match_archives_flags():
  28. flexmock(module.feature).should_receive('available').and_return(True)
  29. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  30. (
  31. '--repo',
  32. 'repo',
  33. )
  34. )
  35. insert_execute_command_mock(
  36. ('borg', 'mount', '--repo', 'repo', '--match-archives', 'archive', '/mnt')
  37. )
  38. module.mount_archive(
  39. repository_path='repo',
  40. archive='archive',
  41. mount_point='/mnt',
  42. paths=None,
  43. foreground=False,
  44. options=None,
  45. storage_config={},
  46. local_borg_version='1.2.3',
  47. global_arguments=flexmock(log_json=False),
  48. )
  49. def test_mount_archive_without_archive_calls_borg_with_repository_flags_only():
  50. flexmock(module.feature).should_receive('available').and_return(False)
  51. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  52. ('repo::archive',)
  53. )
  54. insert_execute_command_mock(('borg', 'mount', 'repo::archive', '/mnt'))
  55. module.mount_archive(
  56. repository_path='repo',
  57. archive='archive',
  58. mount_point='/mnt',
  59. paths=None,
  60. foreground=False,
  61. options=None,
  62. storage_config={},
  63. local_borg_version='1.2.3',
  64. global_arguments=flexmock(log_json=False),
  65. )
  66. def test_mount_archive_calls_borg_with_path_flags():
  67. flexmock(module.feature).should_receive('available').and_return(False)
  68. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  69. ('repo::archive',)
  70. )
  71. insert_execute_command_mock(('borg', 'mount', 'repo::archive', '/mnt', 'path1', 'path2'))
  72. module.mount_archive(
  73. repository_path='repo',
  74. archive='archive',
  75. mount_point='/mnt',
  76. paths=['path1', 'path2'],
  77. foreground=False,
  78. options=None,
  79. storage_config={},
  80. local_borg_version='1.2.3',
  81. global_arguments=flexmock(log_json=False),
  82. )
  83. def test_mount_archive_calls_borg_with_remote_path_flags():
  84. flexmock(module.feature).should_receive('available').and_return(False)
  85. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  86. ('repo::archive',)
  87. )
  88. insert_execute_command_mock(
  89. ('borg', 'mount', '--remote-path', 'borg1', 'repo::archive', '/mnt')
  90. )
  91. module.mount_archive(
  92. repository_path='repo',
  93. archive='archive',
  94. mount_point='/mnt',
  95. paths=None,
  96. foreground=False,
  97. options=None,
  98. storage_config={},
  99. local_borg_version='1.2.3',
  100. global_arguments=flexmock(log_json=False),
  101. remote_path='borg1',
  102. )
  103. def test_mount_archive_calls_borg_with_umask_flags():
  104. flexmock(module.feature).should_receive('available').and_return(False)
  105. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  106. ('repo::archive',)
  107. )
  108. insert_execute_command_mock(('borg', 'mount', '--umask', '0770', 'repo::archive', '/mnt'))
  109. module.mount_archive(
  110. repository_path='repo',
  111. archive='archive',
  112. mount_point='/mnt',
  113. paths=None,
  114. foreground=False,
  115. options=None,
  116. storage_config={'umask': '0770'},
  117. local_borg_version='1.2.3',
  118. global_arguments=flexmock(log_json=False),
  119. )
  120. def test_mount_archive_calls_borg_with_log_json_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(('borg', 'mount', '--log-json', 'repo::archive', '/mnt'))
  126. module.mount_archive(
  127. repository_path='repo',
  128. archive='archive',
  129. mount_point='/mnt',
  130. paths=None,
  131. foreground=False,
  132. options=None,
  133. storage_config={},
  134. local_borg_version='1.2.3',
  135. global_arguments=flexmock(log_json=True),
  136. )
  137. def test_mount_archive_calls_borg_with_lock_wait_flags():
  138. flexmock(module.feature).should_receive('available').and_return(False)
  139. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  140. ('repo::archive',)
  141. )
  142. insert_execute_command_mock(('borg', 'mount', '--lock-wait', '5', 'repo::archive', '/mnt'))
  143. module.mount_archive(
  144. repository_path='repo',
  145. archive='archive',
  146. mount_point='/mnt',
  147. paths=None,
  148. foreground=False,
  149. options=None,
  150. storage_config={'lock_wait': '5'},
  151. local_borg_version='1.2.3',
  152. global_arguments=flexmock(log_json=False),
  153. )
  154. def test_mount_archive_with_log_info_calls_borg_with_info_parameter():
  155. flexmock(module.feature).should_receive('available').and_return(False)
  156. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  157. ('repo::archive',)
  158. )
  159. insert_execute_command_mock(('borg', 'mount', '--info', 'repo::archive', '/mnt'))
  160. insert_logging_mock(logging.INFO)
  161. module.mount_archive(
  162. repository_path='repo',
  163. archive='archive',
  164. mount_point='/mnt',
  165. paths=None,
  166. foreground=False,
  167. options=None,
  168. storage_config={},
  169. local_borg_version='1.2.3',
  170. global_arguments=flexmock(log_json=False),
  171. )
  172. def test_mount_archive_with_log_debug_calls_borg_with_debug_flags():
  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. insert_execute_command_mock(('borg', 'mount', '--debug', '--show-rc', 'repo::archive', '/mnt'))
  178. insert_logging_mock(logging.DEBUG)
  179. module.mount_archive(
  180. repository_path='repo',
  181. archive='archive',
  182. mount_point='/mnt',
  183. paths=None,
  184. foreground=False,
  185. options=None,
  186. storage_config={},
  187. local_borg_version='1.2.3',
  188. global_arguments=flexmock(log_json=False),
  189. )
  190. def test_mount_archive_calls_borg_with_foreground_parameter():
  191. flexmock(module.feature).should_receive('available').and_return(False)
  192. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  193. ('repo::archive',)
  194. )
  195. flexmock(module.environment).should_receive('make_environment')
  196. flexmock(module).should_receive('execute_command').with_args(
  197. ('borg', 'mount', '--foreground', 'repo::archive', '/mnt'),
  198. output_file=module.DO_NOT_CAPTURE,
  199. borg_local_path='borg',
  200. extra_environment=None,
  201. ).once()
  202. module.mount_archive(
  203. repository_path='repo',
  204. archive='archive',
  205. mount_point='/mnt',
  206. paths=None,
  207. foreground=True,
  208. options=None,
  209. storage_config={},
  210. local_borg_version='1.2.3',
  211. global_arguments=flexmock(log_json=False),
  212. )
  213. def test_mount_archive_calls_borg_with_options_flags():
  214. flexmock(module.feature).should_receive('available').and_return(False)
  215. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  216. ('repo::archive',)
  217. )
  218. insert_execute_command_mock(('borg', 'mount', '-o', 'super_mount', 'repo::archive', '/mnt'))
  219. module.mount_archive(
  220. repository_path='repo',
  221. archive='archive',
  222. mount_point='/mnt',
  223. paths=None,
  224. foreground=False,
  225. options='super_mount',
  226. storage_config={},
  227. local_borg_version='1.2.3',
  228. global_arguments=flexmock(log_json=False),
  229. )