test_borgmatic.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 == 0
  12. assert parser.syslog_verbosity == 1
  13. assert parser.json is False
  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. parser = module.parse_arguments('--config', 'myconfig', 'otherconfig')
  17. assert parser.config_paths == ['myconfig', 'otherconfig']
  18. assert parser.verbosity == 0
  19. assert parser.syslog_verbosity == 1
  20. def test_parse_arguments_with_verbosity_overrides_default():
  21. config_paths = ['default']
  22. flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)
  23. parser = module.parse_arguments('--verbosity', '1')
  24. assert parser.config_paths == config_paths
  25. assert parser.excludes_filename is None
  26. assert parser.verbosity == 1
  27. assert parser.syslog_verbosity == 1
  28. def test_parse_arguments_with_syslog_verbosity_overrides_default():
  29. config_paths = ['default']
  30. flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)
  31. parser = module.parse_arguments('--syslog-verbosity', '2')
  32. assert parser.config_paths == config_paths
  33. assert parser.excludes_filename is None
  34. assert parser.verbosity == 0
  35. assert parser.syslog_verbosity == 2
  36. def test_parse_arguments_with_json_overrides_default():
  37. parser = module.parse_arguments('--list', '--json')
  38. assert parser.json is True
  39. def test_parse_arguments_with_no_actions_defaults_to_all_actions_enabled():
  40. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  41. parser = module.parse_arguments()
  42. assert parser.prune is True
  43. assert parser.create is True
  44. assert parser.check is True
  45. def test_parse_arguments_with_prune_action_leaves_other_actions_disabled():
  46. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  47. parser = module.parse_arguments('--prune')
  48. assert parser.prune is True
  49. assert parser.create is False
  50. assert parser.check is False
  51. def test_parse_arguments_with_multiple_actions_leaves_other_action_disabled():
  52. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  53. parser = module.parse_arguments('--create', '--check')
  54. assert parser.prune is False
  55. assert parser.create is True
  56. assert parser.check is True
  57. def test_parse_arguments_with_invalid_arguments_exits():
  58. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  59. with pytest.raises(SystemExit):
  60. module.parse_arguments('--posix-me-harder')
  61. def test_parse_arguments_disallows_deprecated_excludes_option():
  62. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  63. with pytest.raises(ValueError):
  64. module.parse_arguments('--config', 'myconfig', '--excludes', 'myexcludes')
  65. def test_parse_arguments_disallows_encryption_mode_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', '--encryption', 'repokey')
  69. def test_parse_arguments_allows_encryption_mode_with_init():
  70. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  71. module.parse_arguments('--config', 'myconfig', '--init', '--encryption', 'repokey')
  72. def test_parse_arguments_requires_encryption_mode_with_init():
  73. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  74. with pytest.raises(ValueError):
  75. module.parse_arguments('--config', 'myconfig', '--init')
  76. def test_parse_arguments_disallows_append_only_without_init():
  77. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  78. with pytest.raises(ValueError):
  79. module.parse_arguments('--config', 'myconfig', '--append-only')
  80. def test_parse_arguments_disallows_storage_quota_without_init():
  81. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  82. with pytest.raises(ValueError):
  83. module.parse_arguments('--config', 'myconfig', '--storage-quota', '5G')
  84. def test_parse_arguments_allows_init_and_prune():
  85. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  86. module.parse_arguments('--config', 'myconfig', '--init', '--encryption', 'repokey', '--prune')
  87. def test_parse_arguments_allows_init_and_create():
  88. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  89. module.parse_arguments('--config', 'myconfig', '--init', '--encryption', 'repokey', '--create')
  90. def test_parse_arguments_disallows_init_and_dry_run():
  91. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  92. with pytest.raises(ValueError):
  93. module.parse_arguments(
  94. '--config', 'myconfig', '--init', '--encryption', 'repokey', '--dry-run'
  95. )
  96. def test_parse_arguments_disallows_repository_without_extract_or_list():
  97. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  98. with pytest.raises(ValueError):
  99. module.parse_arguments('--config', 'myconfig', '--repository', 'test.borg')
  100. def test_parse_arguments_allows_repository_with_extract():
  101. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  102. module.parse_arguments(
  103. '--config', 'myconfig', '--extract', '--repository', 'test.borg', '--archive', 'test'
  104. )
  105. def test_parse_arguments_allows_repository_with_list():
  106. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  107. module.parse_arguments('--config', 'myconfig', '--list', '--repository', 'test.borg')
  108. def test_parse_arguments_disallows_archive_without_extract_or_list():
  109. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  110. with pytest.raises(ValueError):
  111. module.parse_arguments('--config', 'myconfig', '--archive', 'test')
  112. def test_parse_arguments_disallows_restore_paths_without_extract():
  113. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  114. with pytest.raises(ValueError):
  115. module.parse_arguments('--config', 'myconfig', '--restore-path', 'test')
  116. def test_parse_arguments_allows_archive_with_extract():
  117. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  118. module.parse_arguments('--config', 'myconfig', '--extract', '--archive', 'test')
  119. def test_parse_arguments_allows_archive_with_list():
  120. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  121. module.parse_arguments('--config', 'myconfig', '--list', '--archive', 'test')
  122. def test_parse_arguments_requires_archive_with_extract():
  123. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  124. with pytest.raises(ValueError):
  125. module.parse_arguments('--config', 'myconfig', '--extract')
  126. def test_parse_arguments_allows_progress_and_create():
  127. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  128. module.parse_arguments('--progress', '--create', '--list')
  129. def test_parse_arguments_allows_progress_and_extract():
  130. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  131. module.parse_arguments('--progress', '--extract', '--archive', 'test', '--list')
  132. def test_parse_arguments_disallows_progress_without_create():
  133. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  134. with pytest.raises(ValueError):
  135. module.parse_arguments('--progress', '--list')
  136. def test_parse_arguments_with_stats_and_create_flags_does_not_raise():
  137. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  138. module.parse_arguments('--stats', '--create', '--list')
  139. def test_parse_arguments_with_stats_and_prune_flags_does_not_raise():
  140. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  141. module.parse_arguments('--stats', '--prune', '--list')
  142. def test_parse_arguments_with_stats_flag_but_no_create_or_prune_flag_raises_value_error():
  143. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  144. with pytest.raises(ValueError):
  145. module.parse_arguments('--stats', '--list')
  146. def test_parse_arguments_with_just_stats_flag_does_not_raise():
  147. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  148. module.parse_arguments('--stats')
  149. def test_parse_arguments_allows_json_with_list_or_info():
  150. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  151. module.parse_arguments('--list', '--json')
  152. module.parse_arguments('--info', '--json')
  153. def test_parse_arguments_disallows_json_without_list_or_info():
  154. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  155. with pytest.raises(ValueError):
  156. module.parse_arguments('--json')
  157. def test_parse_arguments_disallows_json_with_both_list_and_info():
  158. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  159. with pytest.raises(ValueError):
  160. module.parse_arguments('--list', '--info', '--json')
  161. def test_borgmatic_version_matches_news_version():
  162. flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
  163. borgmatic_version = subprocess.check_output(('borgmatic', '--version')).decode('ascii')
  164. news_version = open('NEWS').readline()
  165. assert borgmatic_version == news_version