test_repo_create.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. import logging
  2. import subprocess
  3. import pytest
  4. from flexmock import flexmock
  5. from borgmatic.borg import repo_create as module
  6. from ..test_verbosity import insert_logging_mock
  7. REPO_INFO_SOME_UNKNOWN_EXIT_CODE = -999
  8. REPO_CREATE_COMMAND = ('borg', 'repo-create', '--encryption', 'repokey')
  9. def insert_repo_info_command_found_mock():
  10. flexmock(module.repo_info).should_receive('display_repository_info').and_return(
  11. '{"encryption": {"mode": "repokey"}}'
  12. )
  13. def insert_repo_info_command_not_found_mock():
  14. flexmock(module.repo_info).should_receive('display_repository_info').and_raise(
  15. subprocess.CalledProcessError(
  16. sorted(module.REPO_INFO_REPOSITORY_NOT_FOUND_EXIT_CODES)[0], []
  17. )
  18. )
  19. def insert_repo_create_command_mock(
  20. repo_create_command, working_directory=None, borg_exit_codes=None, **kwargs
  21. ):
  22. flexmock(module.environment).should_receive('make_environment')
  23. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  24. working_directory,
  25. )
  26. flexmock(module).should_receive('execute_command').with_args(
  27. repo_create_command,
  28. output_file=module.DO_NOT_CAPTURE,
  29. extra_environment=None,
  30. working_directory=working_directory,
  31. borg_local_path=repo_create_command[0],
  32. borg_exit_codes=borg_exit_codes,
  33. ).once()
  34. def test_create_repository_calls_borg_with_flags():
  35. insert_repo_info_command_not_found_mock()
  36. insert_repo_create_command_mock(REPO_CREATE_COMMAND + ('--repo', 'repo'))
  37. flexmock(module.feature).should_receive('available').and_return(True)
  38. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  39. (
  40. '--repo',
  41. 'repo',
  42. )
  43. )
  44. module.create_repository(
  45. dry_run=False,
  46. repository_path='repo',
  47. config={},
  48. local_borg_version='2.3.4',
  49. global_arguments=flexmock(log_json=False),
  50. encryption_mode='repokey',
  51. )
  52. def test_create_repository_with_dry_run_skips_borg_call():
  53. insert_repo_info_command_not_found_mock()
  54. flexmock(module).should_receive('execute_command').never()
  55. flexmock(module.feature).should_receive('available').and_return(True)
  56. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  57. (
  58. '--repo',
  59. 'repo',
  60. )
  61. )
  62. module.create_repository(
  63. dry_run=True,
  64. repository_path='repo',
  65. config={},
  66. local_borg_version='2.3.4',
  67. global_arguments=flexmock(log_json=False),
  68. encryption_mode='repokey',
  69. )
  70. def test_create_repository_raises_for_borg_repo_create_error():
  71. insert_repo_info_command_not_found_mock()
  72. flexmock(module.feature).should_receive('available').and_return(True)
  73. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  74. (
  75. '--repo',
  76. 'repo',
  77. )
  78. )
  79. flexmock(module.environment).should_receive('make_environment')
  80. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  81. flexmock(module).should_receive('execute_command').and_raise(
  82. module.subprocess.CalledProcessError(2, 'borg repo_create')
  83. )
  84. with pytest.raises(subprocess.CalledProcessError):
  85. module.create_repository(
  86. dry_run=False,
  87. repository_path='repo',
  88. config={},
  89. local_borg_version='2.3.4',
  90. global_arguments=flexmock(log_json=False),
  91. encryption_mode='repokey',
  92. )
  93. def test_create_repository_skips_creation_when_repository_already_exists():
  94. insert_repo_info_command_found_mock()
  95. flexmock(module.feature).should_receive('available').and_return(True)
  96. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  97. (
  98. '--repo',
  99. 'repo',
  100. )
  101. )
  102. module.create_repository(
  103. dry_run=False,
  104. repository_path='repo',
  105. config={},
  106. local_borg_version='2.3.4',
  107. global_arguments=flexmock(log_json=False),
  108. encryption_mode='repokey',
  109. )
  110. def test_create_repository_errors_when_repository_with_differing_encryption_mode_already_exists():
  111. insert_repo_info_command_found_mock()
  112. flexmock(module.feature).should_receive('available').and_return(True)
  113. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  114. (
  115. '--repo',
  116. 'repo',
  117. )
  118. )
  119. with pytest.raises(ValueError):
  120. module.create_repository(
  121. dry_run=False,
  122. repository_path='repo',
  123. config={},
  124. local_borg_version='2.3.4',
  125. global_arguments=flexmock(log_json=False),
  126. encryption_mode='repokey-blake2',
  127. )
  128. def test_create_repository_raises_for_unknown_repo_info_command_error():
  129. flexmock(module.repo_info).should_receive('display_repository_info').and_raise(
  130. subprocess.CalledProcessError(REPO_INFO_SOME_UNKNOWN_EXIT_CODE, [])
  131. )
  132. with pytest.raises(subprocess.CalledProcessError):
  133. module.create_repository(
  134. dry_run=False,
  135. repository_path='repo',
  136. config={},
  137. local_borg_version='2.3.4',
  138. global_arguments=flexmock(log_json=False),
  139. encryption_mode='repokey',
  140. )
  141. def test_create_repository_with_source_repository_calls_borg_with_other_repo_flag():
  142. insert_repo_info_command_not_found_mock()
  143. insert_repo_create_command_mock(
  144. REPO_CREATE_COMMAND + ('--other-repo', 'other.borg', '--repo', 'repo')
  145. )
  146. flexmock(module.feature).should_receive('available').and_return(True)
  147. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  148. (
  149. '--repo',
  150. 'repo',
  151. )
  152. )
  153. module.create_repository(
  154. dry_run=False,
  155. repository_path='repo',
  156. config={},
  157. local_borg_version='2.3.4',
  158. global_arguments=flexmock(log_json=False),
  159. encryption_mode='repokey',
  160. source_repository='other.borg',
  161. )
  162. def test_create_repository_with_copy_crypt_key_calls_borg_with_copy_crypt_key_flag():
  163. insert_repo_info_command_not_found_mock()
  164. insert_repo_create_command_mock(REPO_CREATE_COMMAND + ('--copy-crypt-key', '--repo', 'repo'))
  165. flexmock(module.feature).should_receive('available').and_return(True)
  166. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  167. (
  168. '--repo',
  169. 'repo',
  170. )
  171. )
  172. module.create_repository(
  173. dry_run=False,
  174. repository_path='repo',
  175. config={},
  176. local_borg_version='2.3.4',
  177. global_arguments=flexmock(log_json=False),
  178. encryption_mode='repokey',
  179. copy_crypt_key=True,
  180. )
  181. def test_create_repository_with_append_only_calls_borg_with_append_only_flag():
  182. insert_repo_info_command_not_found_mock()
  183. insert_repo_create_command_mock(REPO_CREATE_COMMAND + ('--append-only', '--repo', 'repo'))
  184. flexmock(module.feature).should_receive('available').and_return(True)
  185. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  186. (
  187. '--repo',
  188. 'repo',
  189. )
  190. )
  191. module.create_repository(
  192. dry_run=False,
  193. repository_path='repo',
  194. config={},
  195. local_borg_version='2.3.4',
  196. global_arguments=flexmock(log_json=False),
  197. encryption_mode='repokey',
  198. append_only=True,
  199. )
  200. def test_create_repository_with_storage_quota_calls_borg_with_storage_quota_flag():
  201. insert_repo_info_command_not_found_mock()
  202. insert_repo_create_command_mock(
  203. REPO_CREATE_COMMAND + ('--storage-quota', '5G', '--repo', 'repo')
  204. )
  205. flexmock(module.feature).should_receive('available').and_return(True)
  206. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  207. (
  208. '--repo',
  209. 'repo',
  210. )
  211. )
  212. module.create_repository(
  213. dry_run=False,
  214. repository_path='repo',
  215. config={},
  216. local_borg_version='2.3.4',
  217. global_arguments=flexmock(log_json=False),
  218. encryption_mode='repokey',
  219. storage_quota='5G',
  220. )
  221. def test_create_repository_with_make_parent_dirs_calls_borg_with_make_parent_dirs_flag():
  222. insert_repo_info_command_not_found_mock()
  223. insert_repo_create_command_mock(REPO_CREATE_COMMAND + ('--make-parent-dirs', '--repo', 'repo'))
  224. flexmock(module.feature).should_receive('available').and_return(True)
  225. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  226. (
  227. '--repo',
  228. 'repo',
  229. )
  230. )
  231. module.create_repository(
  232. dry_run=False,
  233. repository_path='repo',
  234. config={},
  235. local_borg_version='2.3.4',
  236. global_arguments=flexmock(log_json=False),
  237. encryption_mode='repokey',
  238. make_parent_dirs=True,
  239. )
  240. def test_create_repository_with_log_info_calls_borg_with_info_flag():
  241. insert_repo_info_command_not_found_mock()
  242. insert_repo_create_command_mock(REPO_CREATE_COMMAND + ('--info', '--repo', 'repo'))
  243. insert_logging_mock(logging.INFO)
  244. flexmock(module.feature).should_receive('available').and_return(True)
  245. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  246. (
  247. '--repo',
  248. 'repo',
  249. )
  250. )
  251. module.create_repository(
  252. dry_run=False,
  253. repository_path='repo',
  254. config={},
  255. local_borg_version='2.3.4',
  256. global_arguments=flexmock(log_json=False),
  257. encryption_mode='repokey',
  258. )
  259. def test_create_repository_with_log_debug_calls_borg_with_debug_flag():
  260. insert_repo_info_command_not_found_mock()
  261. insert_repo_create_command_mock(REPO_CREATE_COMMAND + ('--debug', '--repo', 'repo'))
  262. insert_logging_mock(logging.DEBUG)
  263. flexmock(module.feature).should_receive('available').and_return(True)
  264. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  265. (
  266. '--repo',
  267. 'repo',
  268. )
  269. )
  270. module.create_repository(
  271. dry_run=False,
  272. repository_path='repo',
  273. config={},
  274. local_borg_version='2.3.4',
  275. global_arguments=flexmock(log_json=False),
  276. encryption_mode='repokey',
  277. )
  278. def test_create_repository_with_log_json_calls_borg_with_log_json_flag():
  279. insert_repo_info_command_not_found_mock()
  280. insert_repo_create_command_mock(REPO_CREATE_COMMAND + ('--log-json', '--repo', 'repo'))
  281. flexmock(module.feature).should_receive('available').and_return(True)
  282. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  283. (
  284. '--repo',
  285. 'repo',
  286. )
  287. )
  288. module.create_repository(
  289. dry_run=False,
  290. repository_path='repo',
  291. config={},
  292. local_borg_version='2.3.4',
  293. global_arguments=flexmock(log_json=True),
  294. encryption_mode='repokey',
  295. )
  296. def test_create_repository_with_lock_wait_calls_borg_with_lock_wait_flag():
  297. insert_repo_info_command_not_found_mock()
  298. insert_repo_create_command_mock(REPO_CREATE_COMMAND + ('--lock-wait', '5', '--repo', 'repo'))
  299. flexmock(module.feature).should_receive('available').and_return(True)
  300. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  301. (
  302. '--repo',
  303. 'repo',
  304. )
  305. )
  306. module.create_repository(
  307. dry_run=False,
  308. repository_path='repo',
  309. config={'lock_wait': 5},
  310. local_borg_version='2.3.4',
  311. global_arguments=flexmock(log_json=False),
  312. encryption_mode='repokey',
  313. )
  314. def test_create_repository_with_local_path_calls_borg_via_local_path():
  315. insert_repo_info_command_not_found_mock()
  316. insert_repo_create_command_mock(('borg1',) + REPO_CREATE_COMMAND[1:] + ('--repo', 'repo'))
  317. flexmock(module.feature).should_receive('available').and_return(True)
  318. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  319. (
  320. '--repo',
  321. 'repo',
  322. )
  323. )
  324. module.create_repository(
  325. dry_run=False,
  326. repository_path='repo',
  327. config={},
  328. local_borg_version='2.3.4',
  329. global_arguments=flexmock(log_json=False),
  330. encryption_mode='repokey',
  331. local_path='borg1',
  332. )
  333. def test_create_repository_with_exit_codes_calls_borg_using_them():
  334. borg_exit_codes = flexmock()
  335. insert_repo_info_command_not_found_mock()
  336. insert_repo_create_command_mock(
  337. ('borg',) + REPO_CREATE_COMMAND[1:] + ('--repo', 'repo'), borg_exit_codes=borg_exit_codes
  338. )
  339. flexmock(module.feature).should_receive('available').and_return(True)
  340. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  341. (
  342. '--repo',
  343. 'repo',
  344. )
  345. )
  346. module.create_repository(
  347. dry_run=False,
  348. repository_path='repo',
  349. config={'borg_exit_codes': borg_exit_codes},
  350. local_borg_version='2.3.4',
  351. global_arguments=flexmock(log_json=False),
  352. encryption_mode='repokey',
  353. )
  354. def test_create_repository_with_remote_path_calls_borg_with_remote_path_flag():
  355. insert_repo_info_command_not_found_mock()
  356. insert_repo_create_command_mock(
  357. REPO_CREATE_COMMAND + ('--remote-path', 'borg1', '--repo', 'repo')
  358. )
  359. flexmock(module.feature).should_receive('available').and_return(True)
  360. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  361. (
  362. '--repo',
  363. 'repo',
  364. )
  365. )
  366. module.create_repository(
  367. dry_run=False,
  368. repository_path='repo',
  369. config={},
  370. local_borg_version='2.3.4',
  371. global_arguments=flexmock(log_json=False),
  372. encryption_mode='repokey',
  373. remote_path='borg1',
  374. )
  375. def test_create_repository_with_extra_borg_options_calls_borg_with_extra_options():
  376. insert_repo_info_command_not_found_mock()
  377. insert_repo_create_command_mock(
  378. REPO_CREATE_COMMAND + ('--extra', '--options', '--repo', 'repo')
  379. )
  380. flexmock(module.feature).should_receive('available').and_return(True)
  381. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  382. (
  383. '--repo',
  384. 'repo',
  385. )
  386. )
  387. module.create_repository(
  388. dry_run=False,
  389. repository_path='repo',
  390. config={'extra_borg_options': {'repo-create': '--extra --options'}},
  391. local_borg_version='2.3.4',
  392. global_arguments=flexmock(log_json=False),
  393. encryption_mode='repokey',
  394. )
  395. def test_create_repository_calls_borg_with_working_directory():
  396. insert_repo_info_command_not_found_mock()
  397. insert_repo_create_command_mock(
  398. REPO_CREATE_COMMAND + ('--repo', 'repo'), working_directory='/working/dir'
  399. )
  400. flexmock(module.feature).should_receive('available').and_return(True)
  401. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  402. (
  403. '--repo',
  404. 'repo',
  405. )
  406. )
  407. module.create_repository(
  408. dry_run=False,
  409. repository_path='repo',
  410. config={'working_directory': '/working/dir'},
  411. local_borg_version='2.3.4',
  412. global_arguments=flexmock(log_json=False),
  413. encryption_mode='repokey',
  414. )