test_repo_create.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. 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(),
  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(),
  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(),
  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(),
  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(),
  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(),
  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(),
  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(),
  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={'append_only': True},
  195. local_borg_version='2.3.4',
  196. global_arguments=flexmock(),
  197. encryption_mode='repokey',
  198. append_only=True,
  199. )
  200. def test_create_repository_with_append_only_config_calls_borg_with_append_only_flag():
  201. insert_repo_info_command_not_found_mock()
  202. insert_repo_create_command_mock(REPO_CREATE_COMMAND + ('--append-only', '--repo', 'repo'))
  203. flexmock(module.feature).should_receive('available').and_return(True)
  204. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  205. (
  206. '--repo',
  207. 'repo',
  208. )
  209. )
  210. module.create_repository(
  211. dry_run=False,
  212. repository_path='repo',
  213. config={'append_only': True},
  214. local_borg_version='2.3.4',
  215. global_arguments=flexmock(),
  216. encryption_mode='repokey',
  217. append_only=True,
  218. )
  219. def test_create_repository_with_storage_quota_calls_borg_with_storage_quota_flag():
  220. insert_repo_info_command_not_found_mock()
  221. insert_repo_create_command_mock(
  222. REPO_CREATE_COMMAND + ('--storage-quota', '5G', '--repo', 'repo')
  223. )
  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={'storage_quota': '5G'},
  235. local_borg_version='2.3.4',
  236. global_arguments=flexmock(),
  237. encryption_mode='repokey',
  238. storage_quota='5G',
  239. )
  240. def test_create_repository_with_make_parent_dirs_calls_borg_with_make_parent_dirs_flag():
  241. insert_repo_info_command_not_found_mock()
  242. insert_repo_create_command_mock(REPO_CREATE_COMMAND + ('--make-parent-dirs', '--repo', 'repo'))
  243. flexmock(module.feature).should_receive('available').and_return(True)
  244. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  245. (
  246. '--repo',
  247. 'repo',
  248. )
  249. )
  250. module.create_repository(
  251. dry_run=False,
  252. repository_path='repo',
  253. config={'make_parent_directories': True},
  254. local_borg_version='2.3.4',
  255. global_arguments=flexmock(),
  256. encryption_mode='repokey',
  257. make_parent_directories=True,
  258. )
  259. def test_create_repository_with_log_info_calls_borg_with_info_flag():
  260. insert_repo_info_command_not_found_mock()
  261. insert_repo_create_command_mock(REPO_CREATE_COMMAND + ('--info', '--repo', 'repo'))
  262. insert_logging_mock(logging.INFO)
  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(),
  276. encryption_mode='repokey',
  277. )
  278. def test_create_repository_with_log_debug_calls_borg_with_debug_flag():
  279. insert_repo_info_command_not_found_mock()
  280. insert_repo_create_command_mock(REPO_CREATE_COMMAND + ('--debug', '--repo', 'repo'))
  281. insert_logging_mock(logging.DEBUG)
  282. flexmock(module.feature).should_receive('available').and_return(True)
  283. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  284. (
  285. '--repo',
  286. 'repo',
  287. )
  288. )
  289. module.create_repository(
  290. dry_run=False,
  291. repository_path='repo',
  292. config={},
  293. local_borg_version='2.3.4',
  294. global_arguments=flexmock(),
  295. encryption_mode='repokey',
  296. )
  297. def test_create_repository_with_log_json_calls_borg_with_log_json_flag():
  298. insert_repo_info_command_not_found_mock()
  299. insert_repo_create_command_mock(REPO_CREATE_COMMAND + ('--log-json', '--repo', 'repo'))
  300. flexmock(module.feature).should_receive('available').and_return(True)
  301. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  302. (
  303. '--repo',
  304. 'repo',
  305. )
  306. )
  307. module.create_repository(
  308. dry_run=False,
  309. repository_path='repo',
  310. config={'log_json': True},
  311. local_borg_version='2.3.4',
  312. global_arguments=flexmock(),
  313. encryption_mode='repokey',
  314. )
  315. def test_create_repository_with_lock_wait_calls_borg_with_lock_wait_flag():
  316. insert_repo_info_command_not_found_mock()
  317. insert_repo_create_command_mock(REPO_CREATE_COMMAND + ('--lock-wait', '5', '--repo', 'repo'))
  318. flexmock(module.feature).should_receive('available').and_return(True)
  319. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  320. (
  321. '--repo',
  322. 'repo',
  323. )
  324. )
  325. module.create_repository(
  326. dry_run=False,
  327. repository_path='repo',
  328. config={'lock_wait': 5},
  329. local_borg_version='2.3.4',
  330. global_arguments=flexmock(),
  331. encryption_mode='repokey',
  332. )
  333. def test_create_repository_with_local_path_calls_borg_via_local_path():
  334. insert_repo_info_command_not_found_mock()
  335. insert_repo_create_command_mock(('borg1',) + REPO_CREATE_COMMAND[1:] + ('--repo', 'repo'))
  336. flexmock(module.feature).should_receive('available').and_return(True)
  337. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  338. (
  339. '--repo',
  340. 'repo',
  341. )
  342. )
  343. module.create_repository(
  344. dry_run=False,
  345. repository_path='repo',
  346. config={},
  347. local_borg_version='2.3.4',
  348. global_arguments=flexmock(),
  349. encryption_mode='repokey',
  350. local_path='borg1',
  351. )
  352. def test_create_repository_with_exit_codes_calls_borg_using_them():
  353. borg_exit_codes = flexmock()
  354. insert_repo_info_command_not_found_mock()
  355. insert_repo_create_command_mock(
  356. ('borg',) + REPO_CREATE_COMMAND[1:] + ('--repo', 'repo'), borg_exit_codes=borg_exit_codes
  357. )
  358. flexmock(module.feature).should_receive('available').and_return(True)
  359. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  360. (
  361. '--repo',
  362. 'repo',
  363. )
  364. )
  365. module.create_repository(
  366. dry_run=False,
  367. repository_path='repo',
  368. config={'borg_exit_codes': borg_exit_codes},
  369. local_borg_version='2.3.4',
  370. global_arguments=flexmock(),
  371. encryption_mode='repokey',
  372. )
  373. def test_create_repository_with_remote_path_calls_borg_with_remote_path_flag():
  374. insert_repo_info_command_not_found_mock()
  375. insert_repo_create_command_mock(
  376. REPO_CREATE_COMMAND + ('--remote-path', 'borg1', '--repo', 'repo')
  377. )
  378. flexmock(module.feature).should_receive('available').and_return(True)
  379. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  380. (
  381. '--repo',
  382. 'repo',
  383. )
  384. )
  385. module.create_repository(
  386. dry_run=False,
  387. repository_path='repo',
  388. config={},
  389. local_borg_version='2.3.4',
  390. global_arguments=flexmock(),
  391. encryption_mode='repokey',
  392. remote_path='borg1',
  393. )
  394. def test_create_repository_with_umask_calls_borg_with_umask_flag():
  395. insert_repo_info_command_not_found_mock()
  396. insert_repo_create_command_mock(REPO_CREATE_COMMAND + ('--umask', '077', '--repo', 'repo'))
  397. flexmock(module.feature).should_receive('available').and_return(True)
  398. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  399. (
  400. '--repo',
  401. 'repo',
  402. )
  403. )
  404. module.create_repository(
  405. dry_run=False,
  406. repository_path='repo',
  407. config={'umask': '077'},
  408. local_borg_version='2.3.4',
  409. global_arguments=flexmock(),
  410. encryption_mode='repokey',
  411. )
  412. def test_create_repository_with_extra_borg_options_calls_borg_with_extra_options():
  413. insert_repo_info_command_not_found_mock()
  414. insert_repo_create_command_mock(
  415. REPO_CREATE_COMMAND + ('--extra', '--options', '--repo', 'repo')
  416. )
  417. flexmock(module.feature).should_receive('available').and_return(True)
  418. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  419. (
  420. '--repo',
  421. 'repo',
  422. )
  423. )
  424. module.create_repository(
  425. dry_run=False,
  426. repository_path='repo',
  427. config={'extra_borg_options': {'repo-create': '--extra --options'}},
  428. local_borg_version='2.3.4',
  429. global_arguments=flexmock(),
  430. encryption_mode='repokey',
  431. )
  432. def test_create_repository_calls_borg_with_working_directory():
  433. insert_repo_info_command_not_found_mock()
  434. insert_repo_create_command_mock(
  435. REPO_CREATE_COMMAND + ('--repo', 'repo'), working_directory='/working/dir'
  436. )
  437. flexmock(module.feature).should_receive('available').and_return(True)
  438. flexmock(module.flags).should_receive('make_repository_flags').and_return(
  439. (
  440. '--repo',
  441. 'repo',
  442. )
  443. )
  444. module.create_repository(
  445. dry_run=False,
  446. repository_path='repo',
  447. config={'working_directory': '/working/dir'},
  448. local_borg_version='2.3.4',
  449. global_arguments=flexmock(),
  450. encryption_mode='repokey',
  451. )