test_arguments.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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_verbosity_overrides_default():
  23. config_paths = ['default']
  24. flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)
  25. arguments = module.parse_arguments('--verbosity', '1')
  26. global_arguments = arguments['global']
  27. assert global_arguments.config_paths == config_paths
  28. assert global_arguments.excludes_filename is None
  29. assert global_arguments.verbosity == 1
  30. assert global_arguments.syslog_verbosity == 0
  31. assert global_arguments.log_file_verbosity == 0
  32. def test_parse_arguments_with_syslog_verbosity_overrides_default():
  33. config_paths = ['default']
  34. flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)
  35. arguments = module.parse_arguments('--syslog-verbosity', '2')
  36. global_arguments = arguments['global']
  37. assert global_arguments.config_paths == config_paths
  38. assert global_arguments.excludes_filename is None
  39. assert global_arguments.verbosity == 0
  40. assert global_arguments.syslog_verbosity == 2
  41. def test_parse_arguments_with_log_file_verbosity_overrides_default():
  42. config_paths = ['default']
  43. flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)
  44. arguments = module.parse_arguments('--log-file-verbosity', '-1')
  45. global_arguments = arguments['global']
  46. assert global_arguments.config_paths == config_paths
  47. assert global_arguments.excludes_filename is None
  48. assert global_arguments.verbosity == 0
  49. assert global_arguments.syslog_verbosity == 0
  50. assert global_arguments.log_file_verbosity == -1
  51. def test_parse_arguments_with_single_override_parses():
  52. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  53. arguments = module.parse_arguments('--override', 'foo.bar=baz')
  54. global_arguments = arguments['global']
  55. assert global_arguments.overrides == ['foo.bar=baz']
  56. def test_parse_arguments_with_multiple_overrides_parses():
  57. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  58. arguments = module.parse_arguments('--override', 'foo.bar=baz', 'foo.quux=7')
  59. global_arguments = arguments['global']
  60. assert global_arguments.overrides == ['foo.bar=baz', 'foo.quux=7']
  61. def test_parse_arguments_with_multiple_overrides_and_flags_parses():
  62. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  63. arguments = module.parse_arguments(
  64. '--override', 'foo.bar=baz', '--override', 'foo.quux=7', 'this.that=8'
  65. )
  66. global_arguments = arguments['global']
  67. assert global_arguments.overrides == ['foo.bar=baz', 'foo.quux=7', 'this.that=8']
  68. def test_parse_arguments_with_list_json_overrides_default():
  69. arguments = module.parse_arguments('list', '--json')
  70. assert 'list' in arguments
  71. assert arguments['list'].json is True
  72. def test_parse_arguments_with_dashed_list_json_overrides_default():
  73. arguments = module.parse_arguments('--list', '--json')
  74. assert 'list' in arguments
  75. assert arguments['list'].json is True
  76. def test_parse_arguments_with_no_actions_defaults_to_all_actions_enabled():
  77. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  78. arguments = module.parse_arguments()
  79. assert 'prune' in arguments
  80. assert 'create' in arguments
  81. assert 'check' in arguments
  82. def test_parse_arguments_with_no_actions_passes_argument_to_relevant_actions():
  83. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  84. arguments = module.parse_arguments('--stats', '--files')
  85. assert 'prune' in arguments
  86. assert arguments['prune'].stats
  87. assert arguments['prune'].files
  88. assert 'create' in arguments
  89. assert arguments['create'].stats
  90. assert arguments['create'].files
  91. assert 'check' in arguments
  92. def test_parse_arguments_with_help_and_no_actions_shows_global_help(capsys):
  93. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  94. with pytest.raises(SystemExit) as exit:
  95. module.parse_arguments('--help')
  96. assert exit.value.code == 0
  97. captured = capsys.readouterr()
  98. assert 'global arguments:' in captured.out
  99. assert 'actions:' in captured.out
  100. def test_parse_arguments_with_help_and_action_shows_action_help(capsys):
  101. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  102. with pytest.raises(SystemExit) as exit:
  103. module.parse_arguments('create', '--help')
  104. assert exit.value.code == 0
  105. captured = capsys.readouterr()
  106. assert 'global arguments:' not in captured.out
  107. assert 'actions:' not in captured.out
  108. assert 'create arguments:' in captured.out
  109. def test_parse_arguments_with_action_before_global_options_parses_options():
  110. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  111. arguments = module.parse_arguments('prune', '--verbosity', '2')
  112. assert 'prune' in arguments
  113. assert arguments['global'].verbosity == 2
  114. def test_parse_arguments_with_global_options_before_action_parses_options():
  115. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  116. arguments = module.parse_arguments('--verbosity', '2', 'prune')
  117. assert 'prune' in arguments
  118. assert arguments['global'].verbosity == 2
  119. def test_parse_arguments_with_prune_action_leaves_other_actions_disabled():
  120. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  121. arguments = module.parse_arguments('prune')
  122. assert 'prune' in arguments
  123. assert 'create' not in arguments
  124. assert 'check' not in arguments
  125. def test_parse_arguments_with_dashed_prune_action_leaves_other_actions_disabled():
  126. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  127. arguments = module.parse_arguments('--prune')
  128. assert 'prune' in arguments
  129. assert 'create' not in arguments
  130. assert 'check' not in arguments
  131. def test_parse_arguments_with_multiple_actions_leaves_other_action_disabled():
  132. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  133. arguments = module.parse_arguments('create', 'check')
  134. assert 'prune' not in arguments
  135. assert 'create' in arguments
  136. assert 'check' in arguments
  137. def test_parse_arguments_with_multiple_dashed_actions_leaves_other_action_disabled():
  138. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  139. arguments = module.parse_arguments('--create', '--check')
  140. assert 'prune' not in arguments
  141. assert 'create' in arguments
  142. assert 'check' in arguments
  143. def test_parse_arguments_with_invalid_arguments_exits():
  144. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  145. with pytest.raises(SystemExit):
  146. module.parse_arguments('--posix-me-harder')
  147. def test_parse_arguments_disallows_deprecated_excludes_option():
  148. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  149. with pytest.raises(ValueError):
  150. module.parse_arguments('--config', 'myconfig', '--excludes', 'myexcludes')
  151. def test_parse_arguments_disallows_encryption_mode_without_init():
  152. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  153. with pytest.raises(SystemExit):
  154. module.parse_arguments('--config', 'myconfig', '--encryption', 'repokey')
  155. def test_parse_arguments_allows_encryption_mode_with_init():
  156. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  157. module.parse_arguments('--config', 'myconfig', 'init', '--encryption', 'repokey')
  158. def test_parse_arguments_allows_encryption_mode_with_dashed_init():
  159. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  160. module.parse_arguments('--config', 'myconfig', '--init', '--encryption', 'repokey')
  161. def test_parse_arguments_requires_encryption_mode_with_init():
  162. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  163. with pytest.raises(SystemExit):
  164. module.parse_arguments('--config', 'myconfig', 'init')
  165. def test_parse_arguments_disallows_append_only_without_init():
  166. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  167. with pytest.raises(SystemExit):
  168. module.parse_arguments('--config', 'myconfig', '--append-only')
  169. def test_parse_arguments_disallows_storage_quota_without_init():
  170. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  171. with pytest.raises(SystemExit):
  172. module.parse_arguments('--config', 'myconfig', '--storage-quota', '5G')
  173. def test_parse_arguments_allows_init_and_prune():
  174. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  175. module.parse_arguments('--config', 'myconfig', 'init', '--encryption', 'repokey', 'prune')
  176. def test_parse_arguments_allows_init_and_create():
  177. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  178. module.parse_arguments('--config', 'myconfig', 'init', '--encryption', 'repokey', 'create')
  179. def test_parse_arguments_disallows_init_and_dry_run():
  180. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  181. with pytest.raises(ValueError):
  182. module.parse_arguments(
  183. '--config', 'myconfig', 'init', '--encryption', 'repokey', '--dry-run'
  184. )
  185. def test_parse_arguments_disallows_glob_archives_with_successful():
  186. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  187. with pytest.raises(ValueError):
  188. module.parse_arguments(
  189. '--config', 'myconfig', 'list', '--glob-archives', '*glob*', '--successful'
  190. )
  191. def test_parse_arguments_disallows_repository_unless_action_consumes_it():
  192. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  193. with pytest.raises(SystemExit):
  194. module.parse_arguments('--config', 'myconfig', '--repository', 'test.borg')
  195. def test_parse_arguments_allows_repository_with_extract():
  196. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  197. module.parse_arguments(
  198. '--config', 'myconfig', 'extract', '--repository', 'test.borg', '--archive', 'test'
  199. )
  200. def test_parse_arguments_allows_repository_with_mount():
  201. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  202. module.parse_arguments(
  203. '--config',
  204. 'myconfig',
  205. 'mount',
  206. '--repository',
  207. 'test.borg',
  208. '--archive',
  209. 'test',
  210. '--mount-point',
  211. '/mnt',
  212. )
  213. def test_parse_arguments_allows_repository_with_list():
  214. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  215. module.parse_arguments('--config', 'myconfig', 'list', '--repository', 'test.borg')
  216. def test_parse_arguments_disallows_archive_unless_action_consumes_it():
  217. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  218. with pytest.raises(SystemExit):
  219. module.parse_arguments('--config', 'myconfig', '--archive', 'test')
  220. def test_parse_arguments_disallows_paths_unless_action_consumes_it():
  221. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  222. with pytest.raises(SystemExit):
  223. module.parse_arguments('--config', 'myconfig', '--path', 'test')
  224. def test_parse_arguments_allows_archive_with_extract():
  225. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  226. module.parse_arguments('--config', 'myconfig', 'extract', '--archive', 'test')
  227. def test_parse_arguments_allows_archive_with_mount():
  228. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  229. module.parse_arguments(
  230. '--config', 'myconfig', 'mount', '--archive', 'test', '--mount-point', '/mnt'
  231. )
  232. def test_parse_arguments_allows_archive_with_dashed_extract():
  233. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  234. module.parse_arguments('--config', 'myconfig', '--extract', '--archive', 'test')
  235. def test_parse_arguments_allows_archive_with_restore():
  236. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  237. module.parse_arguments('--config', 'myconfig', 'restore', '--archive', 'test')
  238. def test_parse_arguments_allows_archive_with_dashed_restore():
  239. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  240. module.parse_arguments('--config', 'myconfig', '--restore', '--archive', 'test')
  241. def test_parse_arguments_allows_archive_with_list():
  242. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  243. module.parse_arguments('--config', 'myconfig', 'list', '--archive', 'test')
  244. def test_parse_arguments_requires_archive_with_extract():
  245. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  246. with pytest.raises(SystemExit):
  247. module.parse_arguments('--config', 'myconfig', 'extract')
  248. def test_parse_arguments_requires_archive_with_restore():
  249. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  250. with pytest.raises(SystemExit):
  251. module.parse_arguments('--config', 'myconfig', 'restore')
  252. def test_parse_arguments_requires_mount_point_with_mount():
  253. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  254. with pytest.raises(SystemExit):
  255. module.parse_arguments('--config', 'myconfig', 'mount', '--archive', 'test')
  256. def test_parse_arguments_requires_mount_point_with_umount():
  257. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  258. with pytest.raises(SystemExit):
  259. module.parse_arguments('--config', 'myconfig', 'umount')
  260. def test_parse_arguments_allows_progress_before_create():
  261. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  262. module.parse_arguments('--progress', 'create', 'list')
  263. def test_parse_arguments_allows_progress_after_create():
  264. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  265. module.parse_arguments('create', '--progress', 'list')
  266. def test_parse_arguments_allows_progress_and_extract():
  267. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  268. module.parse_arguments('--progress', 'extract', '--archive', 'test', 'list')
  269. def test_parse_arguments_disallows_progress_without_create():
  270. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  271. with pytest.raises(SystemExit):
  272. module.parse_arguments('--progress', 'list')
  273. def test_parse_arguments_with_stats_and_create_flags_does_not_raise():
  274. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  275. module.parse_arguments('--stats', 'create', 'list')
  276. def test_parse_arguments_with_stats_and_prune_flags_does_not_raise():
  277. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  278. module.parse_arguments('--stats', 'prune', 'list')
  279. def test_parse_arguments_with_stats_flag_but_no_create_or_prune_flag_raises_value_error():
  280. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  281. with pytest.raises(SystemExit):
  282. module.parse_arguments('--stats', 'list')
  283. def test_parse_arguments_with_files_and_create_flags_does_not_raise():
  284. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  285. module.parse_arguments('--files', 'create', 'list')
  286. def test_parse_arguments_with_files_and_prune_flags_does_not_raise():
  287. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  288. module.parse_arguments('--files', 'prune', 'list')
  289. def test_parse_arguments_with_files_flag_but_no_create_or_prune_or_restore_flag_raises_value_error():
  290. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  291. with pytest.raises(SystemExit):
  292. module.parse_arguments('--files', 'list')
  293. def test_parse_arguments_allows_json_with_list_or_info():
  294. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  295. module.parse_arguments('list', '--json')
  296. module.parse_arguments('info', '--json')
  297. def test_parse_arguments_allows_json_with_dashed_info():
  298. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  299. module.parse_arguments('--info', '--json')
  300. def test_parse_arguments_disallows_json_with_both_list_and_info():
  301. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  302. with pytest.raises(ValueError):
  303. module.parse_arguments('list', 'info', '--json')
  304. def test_parse_arguments_check_only_extract_does_not_raise_extract_subparser_error():
  305. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  306. module.parse_arguments('check', '--only', 'extract')
  307. def test_parse_arguments_extract_archive_check_does_not_raise_check_subparser_error():
  308. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  309. module.parse_arguments('extract', '--archive', 'check')
  310. def test_parse_arguments_extract_with_check_only_extract_does_not_raise():
  311. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  312. module.parse_arguments('extract', '--archive', 'name', 'check', '--only', 'extract')