Additional test coverage, and upgrade test requirements.

This commit is contained in:
Dan Helfman 2019-06-13 10:01:55 -07:00
parent a74ad5475e
commit a6c4debf78
7 changed files with 91 additions and 24 deletions

View File

@ -7,6 +7,9 @@ from borgmatic.logger import get_logger
logger = get_logger(__name__) logger = get_logger(__name__)
INFO_REPOSITORY_NOT_FOUND_EXIT_CODE = 2
def initialize_repository( def initialize_repository(
repository, repository,
encryption_mode, encryption_mode,
@ -28,7 +31,7 @@ def initialize_repository(
logger.info('Repository already exists. Skipping initialization.') logger.info('Repository already exists. Skipping initialization.')
return return
except subprocess.CalledProcessError as error: except subprocess.CalledProcessError as error:
if error.returncode != 2: if error.returncode != INFO_REPOSITORY_NOT_FOUND_EXIT_CODE:
raise raise
init_command = ( init_command = (

View File

@ -1,25 +1,24 @@
appdirs==1.4.3 appdirs==1.4.3
atomicwrites==1.2.1 atomicwrites==1.3.0
attrs==18.2.0 attrs==19.1.0
black==18.9b0; python_version >= '3.6' black==19.3b0; python_version >= '3.6'
Click==7.0 click==7.0
colorama==0.4.1 colorama==0.4.1
coverage==4.5.1 coverage==4.5.3
docopt==0.6.2 docopt==0.6.2
flake8==3.5.0 flake8==3.7.7
flexmock==0.10.4 flexmock==0.10.4
isort==4.3.19 isort==4.3.20
mccabe==0.6.1 mccabe==0.6.1
more-itertools==4.3.0 more-itertools==7.0.0
pluggy==0.7.1 pluggy==0.12.0
py==1.6.0 py==1.8.0
pycodestyle==2.3.1 pycodestyle==2.5.0
pyflakes==2.0.0 pyflakes==2.1.1
pykwalify==1.7.0 pykwalify==1.7.0
pytest==3.8.2 pytest==4.6.3
pytest-cov==2.6.0 pytest-cov==2.7.1
python-dateutil==2.7.3 python-dateutil==2.8.0
PyYAML==3.13 PyYAML==5.1.1
ruamel.yaml>0.15.0,<0.16.0 ruamel.yaml>0.15.0,<0.16.0
six==1.11.0
toml==0.10.0 toml==0.10.0

View File

@ -14,7 +14,8 @@ def test_parse_arguments_with_no_arguments_uses_defaults():
assert parser.config_paths == config_paths assert parser.config_paths == config_paths
assert parser.excludes_filename is None assert parser.excludes_filename is None
assert parser.verbosity is 0 assert parser.verbosity == 0
assert parser.syslog_verbosity == 1
assert parser.json is False assert parser.json is False
@ -24,7 +25,8 @@ def test_parse_arguments_with_multiple_config_paths_parses_as_list():
parser = module.parse_arguments('--config', 'myconfig', 'otherconfig') parser = module.parse_arguments('--config', 'myconfig', 'otherconfig')
assert parser.config_paths == ['myconfig', 'otherconfig'] assert parser.config_paths == ['myconfig', 'otherconfig']
assert parser.verbosity is 0 assert parser.verbosity == 0
assert parser.syslog_verbosity == 1
def test_parse_arguments_with_verbosity_overrides_default(): def test_parse_arguments_with_verbosity_overrides_default():
@ -36,6 +38,19 @@ def test_parse_arguments_with_verbosity_overrides_default():
assert parser.config_paths == config_paths assert parser.config_paths == config_paths
assert parser.excludes_filename is None assert parser.excludes_filename is None
assert parser.verbosity == 1 assert parser.verbosity == 1
assert parser.syslog_verbosity == 1
def test_parse_arguments_with_syslog_verbosity_overrides_default():
config_paths = ['default']
flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)
parser = module.parse_arguments('--syslog-verbosity', '2')
assert parser.config_paths == config_paths
assert parser.excludes_filename is None
assert parser.verbosity == 0
assert parser.syslog_verbosity == 2
def test_parse_arguments_with_json_overrides_default(): def test_parse_arguments_with_json_overrides_default():

View File

@ -25,6 +25,17 @@ def test_display_archives_info_with_log_info_calls_borg_with_info_parameter():
module.display_archives_info(repository='repo', storage_config={}) module.display_archives_info(repository='repo', storage_config={})
def test_display_archives_info_with_log_info_and_json_suppresses_most_borg_output():
flexmock(module).should_receive('execute_command').with_args(
INFO_COMMAND + ('--json',), output_log_level=None
).and_return('[]')
insert_logging_mock(logging.INFO)
json_output = module.display_archives_info(repository='repo', storage_config={}, json=True)
assert json_output == '[]'
def test_display_archives_info_with_log_debug_calls_borg_with_debug_parameter(): def test_display_archives_info_with_log_debug_calls_borg_with_debug_parameter():
flexmock(module).should_receive('execute_command').with_args( flexmock(module).should_receive('execute_command').with_args(
INFO_COMMAND + ('--debug', '--show-rc'), output_log_level=logging.WARNING INFO_COMMAND + ('--debug', '--show-rc'), output_log_level=logging.WARNING
@ -34,6 +45,17 @@ def test_display_archives_info_with_log_debug_calls_borg_with_debug_parameter():
module.display_archives_info(repository='repo', storage_config={}) module.display_archives_info(repository='repo', storage_config={})
def test_display_archives_info_with_log_debug_and_json_suppresses_most_borg_output():
flexmock(module).should_receive('execute_command').with_args(
INFO_COMMAND + ('--json',), output_log_level=None
).and_return('[]')
insert_logging_mock(logging.DEBUG)
json_output = module.display_archives_info(repository='repo', storage_config={}, json=True)
assert json_output == '[]'
def test_display_archives_info_with_json_calls_borg_with_json_parameter(): def test_display_archives_info_with_json_calls_borg_with_json_parameter():
flexmock(module).should_receive('execute_command').with_args( flexmock(module).should_receive('execute_command').with_args(
INFO_COMMAND + ('--json',), output_log_level=None INFO_COMMAND + ('--json',), output_log_level=None

View File

@ -1,13 +1,14 @@
import logging import logging
import subprocess import subprocess
import pytest
from flexmock import flexmock from flexmock import flexmock
from borgmatic.borg import init as module from borgmatic.borg import init as module
from ..test_verbosity import insert_logging_mock from ..test_verbosity import insert_logging_mock
INFO_REPOSITORY_NOT_FOUND_RESPONSE_CODE = 2 INFO_SOME_UNKNOWN_EXIT_CODE = -999
INIT_COMMAND = ('borg', 'init', 'repo', '--encryption', 'repokey') INIT_COMMAND = ('borg', 'init', 'repo', '--encryption', 'repokey')
@ -17,7 +18,7 @@ def insert_info_command_found_mock():
def insert_info_command_not_found_mock(): def insert_info_command_not_found_mock():
flexmock(module).should_receive('execute_command').and_raise( flexmock(module).should_receive('execute_command').and_raise(
subprocess.CalledProcessError(INFO_REPOSITORY_NOT_FOUND_RESPONSE_CODE, []) subprocess.CalledProcessError(module.INFO_REPOSITORY_NOT_FOUND_EXIT_CODE, [])
) )
@ -41,6 +42,15 @@ def test_initialize_repository_skips_initialization_when_repository_already_exis
module.initialize_repository(repository='repo', encryption_mode='repokey') module.initialize_repository(repository='repo', encryption_mode='repokey')
def test_initialize_repository_raises_for_unknown_info_command_error():
flexmock(module).should_receive('execute_command').and_raise(
subprocess.CalledProcessError(INFO_SOME_UNKNOWN_EXIT_CODE, [])
)
with pytest.raises(subprocess.CalledProcessError):
module.initialize_repository(repository='repo', encryption_mode='repokey')
def test_initialize_repository_with_append_only_calls_borg_with_append_only_parameter(): def test_initialize_repository_with_append_only_calls_borg_with_append_only_parameter():
insert_info_command_not_found_mock() insert_info_command_not_found_mock()
insert_init_command_mock(INIT_COMMAND + ('--append-only',)) insert_init_command_mock(INIT_COMMAND + ('--append-only',))

View File

@ -26,6 +26,15 @@ def test_list_archives_with_log_info_calls_borg_with_info_parameter():
module.list_archives(repository='repo', storage_config={}) module.list_archives(repository='repo', storage_config={})
def test_list_archives_with_log_info_and_json_suppresses_most_borg_output():
flexmock(module).should_receive('execute_command').with_args(
LIST_COMMAND + ('--json',), output_log_level=None
)
insert_logging_mock(logging.INFO)
module.list_archives(repository='repo', storage_config={}, json=True)
def test_list_archives_with_log_debug_calls_borg_with_debug_parameter(): def test_list_archives_with_log_debug_calls_borg_with_debug_parameter():
flexmock(module).should_receive('execute_command').with_args( flexmock(module).should_receive('execute_command').with_args(
LIST_COMMAND + ('--debug', '--show-rc'), output_log_level=logging.WARNING LIST_COMMAND + ('--debug', '--show-rc'), output_log_level=logging.WARNING
@ -35,6 +44,15 @@ def test_list_archives_with_log_debug_calls_borg_with_debug_parameter():
module.list_archives(repository='repo', storage_config={}) module.list_archives(repository='repo', storage_config={})
def test_list_archives_with_log_debug_and_json_suppresses_most_borg_output():
flexmock(module).should_receive('execute_command').with_args(
LIST_COMMAND + ('--json',), output_log_level=None
)
insert_logging_mock(logging.DEBUG)
module.list_archives(repository='repo', storage_config={}, json=True)
def test_list_archives_with_lock_wait_calls_borg_with_lock_wait_parameters(): def test_list_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
storage_config = {'lock_wait': 5} storage_config = {'lock_wait': 5}
flexmock(module).should_receive('execute_command').with_args( flexmock(module).should_receive('execute_command').with_args(

View File

@ -18,7 +18,7 @@ commands =
pytest {posargs} pytest {posargs}
py36,py37: black --check . py36,py37: black --check .
isort --recursive --check-only --settings-path setup.cfg . isort --recursive --check-only --settings-path setup.cfg .
flake8 . flake8 borgmatic tests
[testenv:black] [testenv:black]
basepython = python3.7 basepython = python3.7
@ -32,7 +32,7 @@ commands =
[testenv:end-to-end] [testenv:end-to-end]
deps = -rtest_requirements.txt deps = -rtest_requirements.txt
commands = commands =
pytest {posargs} tests/end-to-end pytest {posargs} --no-cov tests/end-to-end
[testenv:isort] [testenv:isort]
deps = {[testenv]deps} deps = {[testenv]deps}