2
0

test_repo_create.py 14 KB

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