2
0

test_arguments.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.commands import arguments as module
  4. def test_parse_arguments_with_no_arguments_uses_defaults():
  5. config_paths = ['default']
  6. flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)
  7. arguments = module.parse_arguments()
  8. global_arguments = arguments['global']
  9. assert global_arguments.config_paths == config_paths
  10. assert global_arguments.excludes_filename is None
  11. assert global_arguments.verbosity == 0
  12. assert global_arguments.syslog_verbosity == 0
  13. assert global_arguments.log_file_verbosity == 0
  14. def test_parse_arguments_with_multiple_config_paths_parses_as_list():
  15. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  16. arguments = module.parse_arguments('--config', 'myconfig', 'otherconfig')
  17. global_arguments = arguments['global']
  18. assert global_arguments.config_paths == ['myconfig', 'otherconfig']
  19. assert global_arguments.verbosity == 0
  20. assert global_arguments.syslog_verbosity == 0
  21. assert global_arguments.log_file_verbosity == 0
  22. def test_parse_arguments_with_action_after_config_path_omits_action():
  23. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  24. arguments = module.parse_arguments('--config', 'myconfig', 'list', '--json')
  25. global_arguments = arguments['global']
  26. assert global_arguments.config_paths == ['myconfig']
  27. assert 'list' in arguments
  28. assert arguments['list'].json
  29. def test_parse_arguments_with_verbosity_overrides_default():
  30. config_paths = ['default']
  31. flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)
  32. arguments = module.parse_arguments('--verbosity', '1')
  33. global_arguments = arguments['global']
  34. assert global_arguments.config_paths == config_paths
  35. assert global_arguments.excludes_filename is None
  36. assert global_arguments.verbosity == 1
  37. assert global_arguments.syslog_verbosity == 0
  38. assert global_arguments.log_file_verbosity == 0
  39. def test_parse_arguments_with_syslog_verbosity_overrides_default():
  40. config_paths = ['default']
  41. flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)
  42. arguments = module.parse_arguments('--syslog-verbosity', '2')
  43. global_arguments = arguments['global']
  44. assert global_arguments.config_paths == config_paths
  45. assert global_arguments.excludes_filename is None
  46. assert global_arguments.verbosity == 0
  47. assert global_arguments.syslog_verbosity == 2
  48. def test_parse_arguments_with_log_file_verbosity_overrides_default():
  49. config_paths = ['default']
  50. flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)
  51. arguments = module.parse_arguments('--log-file-verbosity', '-1')
  52. global_arguments = arguments['global']
  53. assert global_arguments.config_paths == config_paths
  54. assert global_arguments.excludes_filename is None
  55. assert global_arguments.verbosity == 0
  56. assert global_arguments.syslog_verbosity == 0
  57. assert global_arguments.log_file_verbosity == -1
  58. def test_parse_arguments_with_single_override_parses():
  59. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  60. arguments = module.parse_arguments('--override', 'foo.bar=baz')
  61. global_arguments = arguments['global']
  62. assert global_arguments.overrides == ['foo.bar=baz']
  63. def test_parse_arguments_with_multiple_overrides_parses():
  64. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  65. arguments = module.parse_arguments('--override', 'foo.bar=baz', 'foo.quux=7')
  66. global_arguments = arguments['global']
  67. assert global_arguments.overrides == ['foo.bar=baz', 'foo.quux=7']
  68. def test_parse_arguments_with_multiple_overrides_and_flags_parses():
  69. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  70. arguments = module.parse_arguments(
  71. '--override', 'foo.bar=baz', '--override', 'foo.quux=7', 'this.that=8'
  72. )
  73. global_arguments = arguments['global']
  74. assert global_arguments.overrides == ['foo.bar=baz', 'foo.quux=7', 'this.that=8']
  75. def test_parse_arguments_with_list_json_overrides_default():
  76. arguments = module.parse_arguments('list', '--json')
  77. assert 'list' in arguments
  78. assert arguments['list'].json is True
  79. def test_parse_arguments_with_no_actions_defaults_to_all_actions_enabled():
  80. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  81. arguments = module.parse_arguments()
  82. assert 'prune' in arguments
  83. assert 'create' in arguments
  84. assert 'check' in arguments
  85. def test_parse_arguments_with_no_actions_passes_argument_to_relevant_actions():
  86. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  87. arguments = module.parse_arguments('--stats', '--list')
  88. assert 'prune' in arguments
  89. assert arguments['prune'].stats
  90. assert arguments['prune'].list_archives
  91. assert 'create' in arguments
  92. assert arguments['create'].stats
  93. assert arguments['create'].list_files
  94. assert 'check' in arguments
  95. def test_parse_arguments_with_help_and_no_actions_shows_global_help(capsys):
  96. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  97. with pytest.raises(SystemExit) as exit:
  98. module.parse_arguments('--help')
  99. assert exit.value.code == 0
  100. captured = capsys.readouterr()
  101. assert 'global arguments:' in captured.out
  102. assert 'actions:' in captured.out
  103. def test_parse_arguments_with_help_and_action_shows_action_help(capsys):
  104. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  105. with pytest.raises(SystemExit) as exit:
  106. module.parse_arguments('create', '--help')
  107. assert exit.value.code == 0
  108. captured = capsys.readouterr()
  109. assert 'global arguments:' not in captured.out
  110. assert 'actions:' not in captured.out
  111. assert 'create arguments:' in captured.out
  112. def test_parse_arguments_with_action_before_global_options_parses_options():
  113. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  114. arguments = module.parse_arguments('prune', '--verbosity', '2')
  115. assert 'prune' in arguments
  116. assert arguments['global'].verbosity == 2
  117. def test_parse_arguments_with_global_options_before_action_parses_options():
  118. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  119. arguments = module.parse_arguments('--verbosity', '2', 'prune')
  120. assert 'prune' in arguments
  121. assert arguments['global'].verbosity == 2
  122. def test_parse_arguments_with_prune_action_leaves_other_actions_disabled():
  123. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  124. arguments = module.parse_arguments('prune')
  125. assert 'prune' in arguments
  126. assert 'create' not in arguments
  127. assert 'check' not in arguments
  128. def test_parse_arguments_with_multiple_actions_leaves_other_action_disabled():
  129. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  130. arguments = module.parse_arguments('create', 'check')
  131. assert 'prune' not in arguments
  132. assert 'create' in arguments
  133. assert 'check' in arguments
  134. def test_parse_arguments_disallows_invalid_argument():
  135. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  136. with pytest.raises(ValueError):
  137. module.parse_arguments('--posix-me-harder')
  138. def test_parse_arguments_disallows_deprecated_excludes_option():
  139. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  140. with pytest.raises(ValueError):
  141. module.parse_arguments('--config', 'myconfig', '--excludes', 'myexcludes')
  142. def test_parse_arguments_disallows_encryption_mode_without_init():
  143. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  144. with pytest.raises(ValueError):
  145. module.parse_arguments('--config', 'myconfig', '--encryption', 'repokey')
  146. def test_parse_arguments_allows_encryption_mode_with_init():
  147. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  148. module.parse_arguments('--config', 'myconfig', 'init', '--encryption', 'repokey')
  149. def test_parse_arguments_requires_encryption_mode_with_init():
  150. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  151. with pytest.raises(SystemExit):
  152. module.parse_arguments('--config', 'myconfig', 'init')
  153. def test_parse_arguments_disallows_append_only_without_init():
  154. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  155. with pytest.raises(ValueError):
  156. module.parse_arguments('--config', 'myconfig', '--append-only')
  157. def test_parse_arguments_disallows_storage_quota_without_init():
  158. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  159. with pytest.raises(ValueError):
  160. module.parse_arguments('--config', 'myconfig', '--storage-quota', '5G')
  161. def test_parse_arguments_allows_init_and_prune():
  162. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  163. module.parse_arguments('--config', 'myconfig', 'init', '--encryption', 'repokey', 'prune')
  164. def test_parse_arguments_allows_init_and_create():
  165. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  166. module.parse_arguments('--config', 'myconfig', 'init', '--encryption', 'repokey', 'create')
  167. def test_parse_arguments_allows_repository_with_extract():
  168. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  169. module.parse_arguments(
  170. '--config', 'myconfig', 'extract', '--repository', 'test.borg', '--archive', 'test'
  171. )
  172. def test_parse_arguments_allows_repository_with_mount():
  173. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  174. module.parse_arguments(
  175. '--config',
  176. 'myconfig',
  177. 'mount',
  178. '--repository',
  179. 'test.borg',
  180. '--archive',
  181. 'test',
  182. '--mount-point',
  183. '/mnt',
  184. )
  185. def test_parse_arguments_allows_repository_with_list():
  186. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  187. module.parse_arguments('--config', 'myconfig', 'list', '--repository', 'test.borg')
  188. def test_parse_arguments_disallows_archive_unless_action_consumes_it():
  189. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  190. with pytest.raises(ValueError):
  191. module.parse_arguments('--config', 'myconfig', '--archive', 'test')
  192. def test_parse_arguments_disallows_paths_unless_action_consumes_it():
  193. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  194. with pytest.raises(ValueError):
  195. module.parse_arguments('--config', 'myconfig', '--path', 'test')
  196. def test_parse_arguments_disallows_other_actions_with_config_bootstrap():
  197. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  198. with pytest.raises(ValueError):
  199. module.parse_arguments('config', 'bootstrap', '--repository', 'test.borg', 'list')
  200. def test_parse_arguments_allows_archive_with_extract():
  201. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  202. module.parse_arguments('--config', 'myconfig', 'extract', '--archive', 'test')
  203. def test_parse_arguments_allows_archive_with_mount():
  204. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  205. module.parse_arguments(
  206. '--config', 'myconfig', 'mount', '--archive', 'test', '--mount-point', '/mnt'
  207. )
  208. def test_parse_arguments_allows_archive_with_restore():
  209. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  210. module.parse_arguments('--config', 'myconfig', 'restore', '--archive', 'test')
  211. def test_parse_arguments_allows_archive_with_list():
  212. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  213. module.parse_arguments('--config', 'myconfig', 'list', '--archive', 'test')
  214. def test_parse_arguments_requires_archive_with_extract():
  215. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  216. with pytest.raises(SystemExit):
  217. module.parse_arguments('--config', 'myconfig', 'extract')
  218. def test_parse_arguments_requires_archive_with_restore():
  219. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  220. with pytest.raises(SystemExit):
  221. module.parse_arguments('--config', 'myconfig', 'restore')
  222. def test_parse_arguments_requires_mount_point_with_mount():
  223. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  224. with pytest.raises(SystemExit):
  225. module.parse_arguments('--config', 'myconfig', 'mount', '--archive', 'test')
  226. def test_parse_arguments_requires_mount_point_with_umount():
  227. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  228. with pytest.raises(SystemExit):
  229. module.parse_arguments('--config', 'myconfig', 'umount')
  230. def test_parse_arguments_allows_progress_before_create():
  231. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  232. module.parse_arguments('--progress', 'create', 'list')
  233. def test_parse_arguments_allows_progress_after_create():
  234. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  235. module.parse_arguments('create', '--progress', 'list')
  236. def test_parse_arguments_allows_progress_and_extract():
  237. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  238. module.parse_arguments('--progress', 'extract', '--archive', 'test', 'list')
  239. def test_parse_arguments_disallows_progress_without_create():
  240. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  241. with pytest.raises(ValueError):
  242. module.parse_arguments('--progress', 'list')
  243. def test_parse_arguments_with_stats_and_create_flags_does_not_raise():
  244. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  245. module.parse_arguments('--stats', 'create', 'list')
  246. def test_parse_arguments_with_stats_and_prune_flags_does_not_raise():
  247. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  248. module.parse_arguments('--stats', 'prune', 'list')
  249. def test_parse_arguments_with_stats_flag_but_no_create_or_prune_flag_raises_value_error():
  250. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  251. with pytest.raises(ValueError):
  252. module.parse_arguments('--stats', 'list')
  253. def test_parse_arguments_with_list_and_create_flags_does_not_raise():
  254. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  255. module.parse_arguments('--list', 'create')
  256. def test_parse_arguments_with_list_and_prune_flags_does_not_raise():
  257. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  258. module.parse_arguments('--list', 'prune')
  259. def test_parse_arguments_with_list_flag_but_no_relevant_action_raises_value_error():
  260. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  261. with pytest.raises(SystemExit):
  262. module.parse_arguments('--list', 'rcreate')
  263. def test_parse_arguments_disallows_list_with_progress_for_create_action():
  264. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  265. with pytest.raises(ValueError):
  266. module.parse_arguments('create', '--list', '--progress')
  267. def test_parse_arguments_disallows_list_with_json_for_create_action():
  268. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  269. with pytest.raises(ValueError):
  270. module.parse_arguments('create', '--list', '--json')
  271. def test_parse_arguments_allows_json_with_list_or_info():
  272. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  273. module.parse_arguments('list', '--json')
  274. module.parse_arguments('info', '--json')
  275. def test_parse_arguments_disallows_json_with_both_list_and_info():
  276. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  277. with pytest.raises(ValueError):
  278. module.parse_arguments('list', 'info', '--json')
  279. def test_parse_arguments_disallows_json_with_both_list_and_rinfo():
  280. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  281. with pytest.raises(ValueError):
  282. module.parse_arguments('list', 'rinfo', '--json')
  283. def test_parse_arguments_disallows_json_with_both_rinfo_and_info():
  284. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  285. with pytest.raises(ValueError):
  286. module.parse_arguments('rinfo', 'info', '--json')
  287. def test_parse_arguments_disallows_transfer_with_both_archive_and_match_archives():
  288. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  289. with pytest.raises(ValueError):
  290. module.parse_arguments(
  291. 'transfer',
  292. '--source-repository',
  293. 'source.borg',
  294. '--archive',
  295. 'foo',
  296. '--match-archives',
  297. 'sh:*bar',
  298. )
  299. def test_parse_arguments_disallows_list_with_both_prefix_and_match_archives():
  300. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  301. with pytest.raises(ValueError):
  302. module.parse_arguments('list', '--prefix', 'foo', '--match-archives', 'sh:*bar')
  303. def test_parse_arguments_disallows_rlist_with_both_prefix_and_match_archives():
  304. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  305. with pytest.raises(ValueError):
  306. module.parse_arguments('rlist', '--prefix', 'foo', '--match-archives', 'sh:*bar')
  307. def test_parse_arguments_disallows_info_with_both_archive_and_match_archives():
  308. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  309. with pytest.raises(ValueError):
  310. module.parse_arguments('info', '--archive', 'foo', '--match-archives', 'sh:*bar')
  311. def test_parse_arguments_disallows_info_with_both_archive_and_prefix():
  312. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  313. with pytest.raises(ValueError):
  314. module.parse_arguments('info', '--archive', 'foo', '--prefix', 'bar')
  315. def test_parse_arguments_disallows_info_with_both_prefix_and_match_archives():
  316. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  317. with pytest.raises(ValueError):
  318. module.parse_arguments('info', '--prefix', 'foo', '--match-archives', 'sh:*bar')
  319. def test_parse_arguments_check_only_extract_does_not_raise_extract_subparser_error():
  320. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  321. module.parse_arguments('check', '--only', 'extract')
  322. def test_parse_arguments_extract_archive_check_does_not_raise_check_subparser_error():
  323. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  324. module.parse_arguments('extract', '--archive', 'check')
  325. def test_parse_arguments_extract_with_check_only_extract_does_not_raise():
  326. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  327. module.parse_arguments('extract', '--archive', 'name', 'check', '--only', 'extract')
  328. def test_parse_arguments_bootstrap_without_config_errors():
  329. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  330. with pytest.raises(ValueError):
  331. module.parse_arguments('bootstrap')
  332. def test_parse_arguments_config_with_no_subaction_errors():
  333. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  334. with pytest.raises(ValueError):
  335. module.parse_arguments('config')
  336. def test_parse_arguments_config_with_help_shows_config_help(capsys):
  337. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  338. with pytest.raises(SystemExit) as exit:
  339. module.parse_arguments('config', '--help')
  340. assert exit.value.code == 0
  341. captured = capsys.readouterr()
  342. assert 'global arguments:' not in captured.out
  343. assert 'config arguments:' in captured.out
  344. assert 'config sub-actions:' in captured.out
  345. def test_parse_arguments_config_with_subaction_but_missing_flags_errors():
  346. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  347. with pytest.raises(SystemExit) as exit:
  348. module.parse_arguments('config', 'bootstrap')
  349. assert exit.value.code == 2
  350. def test_parse_arguments_config_with_subaction_and_help_shows_subaction_help(capsys):
  351. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  352. with pytest.raises(SystemExit) as exit:
  353. module.parse_arguments('config', 'bootstrap', '--help')
  354. assert exit.value.code == 0
  355. captured = capsys.readouterr()
  356. assert 'config bootstrap arguments:' in captured.out
  357. def test_parse_arguments_config_with_subaction_and_required_flags_does_not_raise():
  358. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  359. module.parse_arguments('config', 'bootstrap', '--repository', 'repo.borg')
  360. def test_parse_arguments_config_with_subaction_and_global_flags_at_start_does_not_raise():
  361. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  362. module.parse_arguments('--verbosity', '1', 'config', 'bootstrap', '--repository', 'repo.borg')
  363. def test_parse_arguments_config_with_subaction_and_global_flags_at_end_does_not_raise():
  364. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  365. module.parse_arguments('config', 'bootstrap', '--repository', 'repo.borg', '--verbosity', '1')
  366. def test_parse_arguments_config_with_subaction_and_explicit_config_file_does_not_raise():
  367. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  368. module.parse_arguments(
  369. 'config', 'bootstrap', '--repository', 'repo.borg', '--config', 'test.yaml'
  370. )