test_borgmatic.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import subprocess
  2. from flexmock import flexmock
  3. import pytest
  4. from borgmatic.commands import borgmatic as module
  5. def test_parse_arguments_with_no_arguments_uses_defaults():
  6. config_paths = ['default']
  7. flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)
  8. parser = module.parse_arguments()
  9. assert parser.config_paths == config_paths
  10. assert parser.excludes_filename is None
  11. assert parser.verbosity is 0
  12. assert parser.json is False
  13. def test_parse_arguments_with_multiple_config_paths_parses_as_list():
  14. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  15. parser = module.parse_arguments('--config', 'myconfig', 'otherconfig')
  16. assert parser.config_paths == ['myconfig', 'otherconfig']
  17. assert parser.verbosity is 0
  18. def test_parse_arguments_with_verbosity_overrides_default():
  19. config_paths = ['default']
  20. flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)
  21. parser = module.parse_arguments('--verbosity', '1')
  22. assert parser.config_paths == config_paths
  23. assert parser.excludes_filename is None
  24. assert parser.verbosity == 1
  25. def test_parse_arguments_with_json_overrides_default():
  26. parser = module.parse_arguments('--list', '--json')
  27. assert parser.json is True
  28. def test_parse_arguments_with_no_actions_defaults_to_all_actions_enabled():
  29. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  30. parser = module.parse_arguments()
  31. assert parser.prune is True
  32. assert parser.create is True
  33. assert parser.check is True
  34. def test_parse_arguments_with_prune_action_leaves_other_actions_disabled():
  35. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  36. parser = module.parse_arguments('--prune')
  37. assert parser.prune is True
  38. assert parser.create is False
  39. assert parser.check is False
  40. def test_parse_arguments_with_multiple_actions_leaves_other_action_disabled():
  41. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  42. parser = module.parse_arguments('--create', '--check')
  43. assert parser.prune is False
  44. assert parser.create is True
  45. assert parser.check is True
  46. def test_parse_arguments_with_invalid_arguments_exits():
  47. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  48. with pytest.raises(SystemExit):
  49. module.parse_arguments('--posix-me-harder')
  50. def test_parse_arguments_disallows_deprecated_excludes_option():
  51. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  52. with pytest.raises(ValueError):
  53. module.parse_arguments('--config', 'myconfig', '--excludes', 'myexcludes')
  54. def test_parse_arguments_disallows_encryption_mode_without_init():
  55. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  56. with pytest.raises(ValueError):
  57. module.parse_arguments('--config', 'myconfig', '--encryption', 'repokey')
  58. def test_parse_arguments_requires_encryption_mode_with_init():
  59. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  60. with pytest.raises(ValueError):
  61. module.parse_arguments('--config', 'myconfig', '--init')
  62. def test_parse_arguments_disallows_append_only_without_init():
  63. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  64. with pytest.raises(ValueError):
  65. module.parse_arguments('--config', 'myconfig', '--append-only')
  66. def test_parse_arguments_disallows_storage_quota_without_init():
  67. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  68. with pytest.raises(ValueError):
  69. module.parse_arguments('--config', 'myconfig', '--storage-quota', '5G')
  70. def test_parse_arguments_allows_init_and_prune():
  71. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  72. module.parse_arguments('--config', 'myconfig', '--init', '--encryption', 'repokey', '--prune')
  73. def test_parse_arguments_allows_init_and_create():
  74. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  75. module.parse_arguments('--config', 'myconfig', '--init', '--encryption', 'repokey', '--create')
  76. def test_parse_arguments_disallows_init_and_dry_run():
  77. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  78. with pytest.raises(ValueError):
  79. module.parse_arguments(
  80. '--config', 'myconfig', '--init', '--encryption', 'repokey', '--dry-run'
  81. )
  82. def test_parse_arguments_allows_progress_and_create():
  83. module.parse_arguments('--progress', '--create', '--list')
  84. def test_parse_arguments_disallows_progress_without_create():
  85. with pytest.raises(ValueError):
  86. module.parse_arguments('--progress', '--list')
  87. def test_parse_arguments_with_stats_and_create_flags_does_not_raise():
  88. module.parse_arguments('--stats', '--create', '--list')
  89. def test_parse_arguments_with_stats_and_prune_flags_does_not_raise():
  90. module.parse_arguments('--stats', '--prune', '--list')
  91. def test_parse_arguments_with_stats_flag_but_no_create_or_prune_flag_raises_value_error():
  92. with pytest.raises(ValueError):
  93. module.parse_arguments('--stats', '--list')
  94. def test_parse_arguments_with_just_stats_flag_does_not_raise():
  95. module.parse_arguments('--stats')
  96. def test_parse_arguments_allows_json_with_list_or_info():
  97. module.parse_arguments('--list', '--json')
  98. module.parse_arguments('--info', '--json')
  99. def test_parse_arguments_disallows_json_without_list_or_info():
  100. with pytest.raises(ValueError):
  101. module.parse_arguments('--json')
  102. def test_parse_arguments_disallows_json_with_both_list_and_info():
  103. with pytest.raises(ValueError):
  104. module.parse_arguments('--list', '--info', '--json')
  105. def test_borgmatic_version_matches_news_version():
  106. borgmatic_version = subprocess.check_output(('borgmatic', '--version')).decode('ascii')
  107. news_version = open('NEWS').readline()
  108. assert borgmatic_version == news_version