test_borgmatic.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import subprocess
  2. import pytest
  3. from flexmock import flexmock
  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_allows_encryption_mode_with_init():
  59. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  60. module.parse_arguments('--config', 'myconfig', '--init', '--encryption', 'repokey')
  61. def test_parse_arguments_requires_encryption_mode_with_init():
  62. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  63. with pytest.raises(ValueError):
  64. module.parse_arguments('--config', 'myconfig', '--init')
  65. def test_parse_arguments_disallows_append_only_without_init():
  66. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  67. with pytest.raises(ValueError):
  68. module.parse_arguments('--config', 'myconfig', '--append-only')
  69. def test_parse_arguments_disallows_storage_quota_without_init():
  70. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  71. with pytest.raises(ValueError):
  72. module.parse_arguments('--config', 'myconfig', '--storage-quota', '5G')
  73. def test_parse_arguments_allows_init_and_prune():
  74. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  75. module.parse_arguments('--config', 'myconfig', '--init', '--encryption', 'repokey', '--prune')
  76. def test_parse_arguments_allows_init_and_create():
  77. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  78. module.parse_arguments('--config', 'myconfig', '--init', '--encryption', 'repokey', '--create')
  79. def test_parse_arguments_disallows_init_and_dry_run():
  80. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  81. with pytest.raises(ValueError):
  82. module.parse_arguments(
  83. '--config', 'myconfig', '--init', '--encryption', 'repokey', '--dry-run'
  84. )
  85. def test_parse_arguments_disallows_repository_without_extract_or_list():
  86. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  87. with pytest.raises(ValueError):
  88. module.parse_arguments('--config', 'myconfig', '--repository', 'test.borg')
  89. def test_parse_arguments_allows_repository_with_extract():
  90. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  91. module.parse_arguments(
  92. '--config', 'myconfig', '--extract', '--repository', 'test.borg', '--archive', 'test'
  93. )
  94. def test_parse_arguments_allows_repository_with_list():
  95. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  96. module.parse_arguments('--config', 'myconfig', '--list', '--repository', 'test.borg')
  97. def test_parse_arguments_disallows_archive_without_extract_or_list():
  98. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  99. with pytest.raises(ValueError):
  100. module.parse_arguments('--config', 'myconfig', '--archive', 'test')
  101. def test_parse_arguments_disallows_restore_paths_without_extract():
  102. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  103. with pytest.raises(ValueError):
  104. module.parse_arguments('--config', 'myconfig', '--restore-path', 'test')
  105. def test_parse_arguments_allows_archive_with_extract():
  106. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  107. module.parse_arguments('--config', 'myconfig', '--extract', '--archive', 'test')
  108. def test_parse_arguments_allows_archive_with_list():
  109. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  110. module.parse_arguments('--config', 'myconfig', '--list', '--archive', 'test')
  111. def test_parse_arguments_requires_archive_with_extract():
  112. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  113. with pytest.raises(ValueError):
  114. module.parse_arguments('--config', 'myconfig', '--extract')
  115. def test_parse_arguments_allows_progress_and_create():
  116. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  117. module.parse_arguments('--progress', '--create', '--list')
  118. def test_parse_arguments_allows_progress_and_extract():
  119. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  120. module.parse_arguments('--progress', '--extract', '--archive', 'test', '--list')
  121. def test_parse_arguments_disallows_progress_without_create():
  122. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  123. with pytest.raises(ValueError):
  124. module.parse_arguments('--progress', '--list')
  125. def test_parse_arguments_with_stats_and_create_flags_does_not_raise():
  126. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  127. module.parse_arguments('--stats', '--create', '--list')
  128. def test_parse_arguments_with_stats_and_prune_flags_does_not_raise():
  129. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  130. module.parse_arguments('--stats', '--prune', '--list')
  131. def test_parse_arguments_with_stats_flag_but_no_create_or_prune_flag_raises_value_error():
  132. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  133. with pytest.raises(ValueError):
  134. module.parse_arguments('--stats', '--list')
  135. def test_parse_arguments_with_just_stats_flag_does_not_raise():
  136. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  137. module.parse_arguments('--stats')
  138. def test_parse_arguments_allows_json_with_list_or_info():
  139. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  140. module.parse_arguments('--list', '--json')
  141. module.parse_arguments('--info', '--json')
  142. def test_parse_arguments_disallows_json_without_list_or_info():
  143. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  144. with pytest.raises(ValueError):
  145. module.parse_arguments('--json')
  146. def test_parse_arguments_disallows_json_with_both_list_and_info():
  147. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  148. with pytest.raises(ValueError):
  149. module.parse_arguments('--list', '--info', '--json')
  150. def test_borgmatic_version_matches_news_version():
  151. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  152. borgmatic_version = subprocess.check_output(('borgmatic', '--version')).decode('ascii')
  153. news_version = open('NEWS').readline()
  154. assert borgmatic_version == news_version