test_bootstrap.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.actions.config import bootstrap as module
  4. def test_make_bootstrap_config_uses_ssh_command_argument():
  5. ssh_command = flexmock()
  6. config = module.make_bootstrap_config(flexmock(ssh_command=ssh_command))
  7. assert config['ssh_command'] == ssh_command
  8. assert config['relocated_repo_access_is_ok']
  9. def test_get_config_paths_returns_list_of_config_paths():
  10. flexmock(module.borgmatic.config.paths).should_receive(
  11. 'get_borgmatic_source_directory'
  12. ).and_return('/source')
  13. flexmock(module).should_receive('make_bootstrap_config').and_return({})
  14. bootstrap_arguments = flexmock(
  15. repository='repo',
  16. archive='archive',
  17. ssh_command=None,
  18. local_path='borg7',
  19. remote_path='borg8',
  20. borgmatic_source_directory=None,
  21. user_runtime_directory=None,
  22. )
  23. global_arguments = flexmock(
  24. dry_run=False,
  25. )
  26. local_borg_version = flexmock()
  27. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  28. flexmock()
  29. )
  30. flexmock(module.borgmatic.config.paths).should_receive(
  31. 'make_runtime_directory_glob'
  32. ).replace_with(lambda path: path)
  33. extract_process = flexmock(
  34. stdout=flexmock(
  35. read=lambda: '{"config_paths": ["/borgmatic/config.yaml"]}',
  36. ),
  37. )
  38. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
  39. extract_process
  40. )
  41. assert module.get_config_paths(
  42. 'archive', bootstrap_arguments, global_arguments, local_borg_version
  43. ) == ['/borgmatic/config.yaml']
  44. def test_get_config_paths_probes_for_manifest():
  45. flexmock(module.borgmatic.config.paths).should_receive(
  46. 'get_borgmatic_source_directory'
  47. ).and_return('/source')
  48. flexmock(module).should_receive('make_bootstrap_config').and_return({})
  49. bootstrap_arguments = flexmock(
  50. repository='repo',
  51. archive='archive',
  52. ssh_command=None,
  53. local_path='borg7',
  54. remote_path='borg8',
  55. borgmatic_source_directory=None,
  56. user_runtime_directory=None,
  57. )
  58. global_arguments = flexmock(
  59. dry_run=False,
  60. )
  61. local_borg_version = flexmock()
  62. borgmatic_runtime_directory = flexmock()
  63. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  64. borgmatic_runtime_directory,
  65. )
  66. flexmock(module.borgmatic.config.paths).should_receive(
  67. 'make_runtime_directory_glob'
  68. ).replace_with(lambda path: path)
  69. flexmock(module.os.path).should_receive('join').with_args(
  70. 'borgmatic', 'bootstrap', 'manifest.json'
  71. ).and_return('borgmatic/bootstrap/manifest.json').once()
  72. flexmock(module.os.path).should_receive('join').with_args(
  73. borgmatic_runtime_directory, 'bootstrap', 'manifest.json'
  74. ).and_return('run/borgmatic/bootstrap/manifest.json').once()
  75. flexmock(module.os.path).should_receive('join').with_args(
  76. '/source', 'bootstrap', 'manifest.json'
  77. ).and_return('/source/bootstrap/manifest.json').once()
  78. manifest_missing_extract_process = flexmock(
  79. stdout=flexmock(read=lambda: None),
  80. )
  81. manifest_found_extract_process = flexmock(
  82. stdout=flexmock(
  83. read=lambda: '{"config_paths": ["/borgmatic/config.yaml"]}',
  84. ),
  85. )
  86. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
  87. manifest_missing_extract_process
  88. ).and_return(manifest_missing_extract_process).and_return(manifest_found_extract_process)
  89. assert module.get_config_paths(
  90. 'archive', bootstrap_arguments, global_arguments, local_borg_version
  91. ) == ['/borgmatic/config.yaml']
  92. def test_get_config_paths_translates_ssh_command_argument_to_config():
  93. flexmock(module.borgmatic.config.paths).should_receive(
  94. 'get_borgmatic_source_directory'
  95. ).and_return('/source')
  96. config = flexmock()
  97. flexmock(module).should_receive('make_bootstrap_config').and_return(config)
  98. bootstrap_arguments = flexmock(
  99. repository='repo',
  100. archive='archive',
  101. ssh_command='ssh -i key',
  102. local_path='borg7',
  103. remote_path='borg8',
  104. borgmatic_source_directory=None,
  105. user_runtime_directory=None,
  106. )
  107. global_arguments = flexmock(
  108. dry_run=False,
  109. )
  110. local_borg_version = flexmock()
  111. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  112. flexmock()
  113. )
  114. flexmock(module.borgmatic.config.paths).should_receive(
  115. 'make_runtime_directory_glob'
  116. ).replace_with(lambda path: path)
  117. extract_process = flexmock(
  118. stdout=flexmock(
  119. read=lambda: '{"config_paths": ["/borgmatic/config.yaml"]}',
  120. ),
  121. )
  122. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').with_args(
  123. False,
  124. 'repo',
  125. 'archive',
  126. object,
  127. config,
  128. object,
  129. object,
  130. extract_to_stdout=True,
  131. local_path='borg7',
  132. remote_path='borg8',
  133. ).and_return(extract_process)
  134. assert module.get_config_paths(
  135. 'archive', bootstrap_arguments, global_arguments, local_borg_version
  136. ) == ['/borgmatic/config.yaml']
  137. def test_get_config_paths_with_missing_manifest_raises_value_error():
  138. flexmock(module.borgmatic.config.paths).should_receive(
  139. 'get_borgmatic_source_directory'
  140. ).and_return('/source')
  141. flexmock(module).should_receive('make_bootstrap_config').and_return({})
  142. bootstrap_arguments = flexmock(
  143. repository='repo',
  144. archive='archive',
  145. ssh_command=None,
  146. local_path='borg7',
  147. remote_path='borg7',
  148. borgmatic_source_directory=None,
  149. user_runtime_directory=None,
  150. )
  151. global_arguments = flexmock(
  152. dry_run=False,
  153. )
  154. local_borg_version = flexmock()
  155. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  156. flexmock()
  157. )
  158. flexmock(module.borgmatic.config.paths).should_receive(
  159. 'make_runtime_directory_glob'
  160. ).replace_with(lambda path: path)
  161. flexmock(module.os.path).should_receive('join').and_return('run/borgmatic')
  162. extract_process = flexmock(stdout=flexmock(read=lambda: ''))
  163. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
  164. extract_process
  165. )
  166. with pytest.raises(ValueError):
  167. module.get_config_paths(
  168. 'archive', bootstrap_arguments, global_arguments, local_borg_version
  169. )
  170. def test_get_config_paths_with_broken_json_raises_value_error():
  171. flexmock(module.borgmatic.config.paths).should_receive(
  172. 'get_borgmatic_source_directory'
  173. ).and_return('/source')
  174. flexmock(module).should_receive('make_bootstrap_config').and_return({})
  175. bootstrap_arguments = flexmock(
  176. repository='repo',
  177. archive='archive',
  178. ssh_command=None,
  179. local_path='borg7',
  180. remote_path='borg7',
  181. borgmatic_source_directory=None,
  182. user_runtime_directory=None,
  183. )
  184. global_arguments = flexmock(
  185. dry_run=False,
  186. )
  187. local_borg_version = flexmock()
  188. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  189. flexmock()
  190. )
  191. flexmock(module.borgmatic.config.paths).should_receive(
  192. 'make_runtime_directory_glob'
  193. ).replace_with(lambda path: path)
  194. extract_process = flexmock(
  195. stdout=flexmock(read=lambda: '{"config_paths": ["/oops'),
  196. )
  197. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
  198. extract_process
  199. )
  200. with pytest.raises(ValueError):
  201. module.get_config_paths(
  202. 'archive', bootstrap_arguments, global_arguments, local_borg_version
  203. )
  204. def test_get_config_paths_with_json_missing_key_raises_value_error():
  205. flexmock(module.borgmatic.config.paths).should_receive(
  206. 'get_borgmatic_source_directory'
  207. ).and_return('/source')
  208. flexmock(module).should_receive('make_bootstrap_config').and_return({})
  209. bootstrap_arguments = flexmock(
  210. repository='repo',
  211. archive='archive',
  212. ssh_command=None,
  213. local_path='borg7',
  214. remote_path='borg7',
  215. borgmatic_source_directory=None,
  216. user_runtime_directory=None,
  217. )
  218. global_arguments = flexmock(
  219. dry_run=False,
  220. )
  221. local_borg_version = flexmock()
  222. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  223. flexmock()
  224. )
  225. flexmock(module.borgmatic.config.paths).should_receive(
  226. 'make_runtime_directory_glob'
  227. ).replace_with(lambda path: path)
  228. extract_process = flexmock(
  229. stdout=flexmock(read=lambda: '{}'),
  230. )
  231. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
  232. extract_process
  233. )
  234. with pytest.raises(ValueError):
  235. module.get_config_paths(
  236. 'archive', bootstrap_arguments, global_arguments, local_borg_version
  237. )
  238. def test_run_bootstrap_does_not_raise():
  239. flexmock(module).should_receive('make_bootstrap_config').and_return({})
  240. flexmock(module).should_receive('get_config_paths').and_return(['/borgmatic/config.yaml'])
  241. bootstrap_arguments = flexmock(
  242. repository='repo',
  243. archive='archive',
  244. destination='dest',
  245. strip_components=1,
  246. progress=False,
  247. user_runtime_directory='/borgmatic',
  248. ssh_command=None,
  249. local_path='borg7',
  250. remote_path='borg8',
  251. )
  252. global_arguments = flexmock(
  253. dry_run=False,
  254. )
  255. local_borg_version = flexmock()
  256. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  257. flexmock()
  258. )
  259. flexmock(module.borgmatic.config.paths).should_receive(
  260. 'make_runtime_directory_glob'
  261. ).replace_with(lambda path: path)
  262. extract_process = flexmock(
  263. stdout=flexmock(
  264. read=lambda: '{"config_paths": ["borgmatic/config.yaml"]}',
  265. ),
  266. )
  267. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
  268. extract_process
  269. ).once()
  270. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  271. 'archive'
  272. )
  273. module.run_bootstrap(bootstrap_arguments, global_arguments, local_borg_version)
  274. def test_run_bootstrap_translates_ssh_command_argument_to_config():
  275. config = flexmock()
  276. flexmock(module).should_receive('make_bootstrap_config').and_return(config)
  277. flexmock(module).should_receive('get_config_paths').and_return(['/borgmatic/config.yaml'])
  278. bootstrap_arguments = flexmock(
  279. repository='repo',
  280. archive='archive',
  281. destination='dest',
  282. strip_components=1,
  283. progress=False,
  284. user_runtime_directory='/borgmatic',
  285. ssh_command='ssh -i key',
  286. local_path='borg7',
  287. remote_path='borg8',
  288. )
  289. global_arguments = flexmock(
  290. dry_run=False,
  291. )
  292. local_borg_version = flexmock()
  293. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  294. flexmock()
  295. )
  296. flexmock(module.borgmatic.config.paths).should_receive(
  297. 'make_runtime_directory_glob'
  298. ).replace_with(lambda path: path)
  299. extract_process = flexmock(
  300. stdout=flexmock(
  301. read=lambda: '{"config_paths": ["borgmatic/config.yaml"]}',
  302. ),
  303. )
  304. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').with_args(
  305. False,
  306. 'repo',
  307. 'archive',
  308. object,
  309. config,
  310. object,
  311. object,
  312. extract_to_stdout=False,
  313. destination_path='dest',
  314. strip_components=1,
  315. progress=False,
  316. local_path='borg7',
  317. remote_path='borg8',
  318. ).and_return(extract_process).once()
  319. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').with_args(
  320. 'repo',
  321. 'archive',
  322. config,
  323. object,
  324. object,
  325. local_path='borg7',
  326. remote_path='borg8',
  327. ).and_return('archive')
  328. module.run_bootstrap(bootstrap_arguments, global_arguments, local_borg_version)