From a6c4debf78af9428d979f75d09df8d79fa47f7ff Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Thu, 13 Jun 2019 10:01:55 -0700 Subject: [PATCH] Additional test coverage, and upgrade test requirements. --- borgmatic/borg/init.py | 5 ++- test_requirements.txt | 33 ++++++++++---------- tests/integration/commands/test_borgmatic.py | 19 +++++++++-- tests/unit/borg/test_info.py | 22 +++++++++++++ tests/unit/borg/test_init.py | 14 +++++++-- tests/unit/borg/test_list.py | 18 +++++++++++ tox.ini | 4 +-- 7 files changed, 91 insertions(+), 24 deletions(-) diff --git a/borgmatic/borg/init.py b/borgmatic/borg/init.py index 938cb76..ff91f73 100644 --- a/borgmatic/borg/init.py +++ b/borgmatic/borg/init.py @@ -7,6 +7,9 @@ from borgmatic.logger import get_logger logger = get_logger(__name__) +INFO_REPOSITORY_NOT_FOUND_EXIT_CODE = 2 + + def initialize_repository( repository, encryption_mode, @@ -28,7 +31,7 @@ def initialize_repository( logger.info('Repository already exists. Skipping initialization.') return except subprocess.CalledProcessError as error: - if error.returncode != 2: + if error.returncode != INFO_REPOSITORY_NOT_FOUND_EXIT_CODE: raise init_command = ( diff --git a/test_requirements.txt b/test_requirements.txt index a9e67a2..cc0df6b 100644 --- a/test_requirements.txt +++ b/test_requirements.txt @@ -1,25 +1,24 @@ appdirs==1.4.3 -atomicwrites==1.2.1 -attrs==18.2.0 -black==18.9b0; python_version >= '3.6' -Click==7.0 +atomicwrites==1.3.0 +attrs==19.1.0 +black==19.3b0; python_version >= '3.6' +click==7.0 colorama==0.4.1 -coverage==4.5.1 +coverage==4.5.3 docopt==0.6.2 -flake8==3.5.0 +flake8==3.7.7 flexmock==0.10.4 -isort==4.3.19 +isort==4.3.20 mccabe==0.6.1 -more-itertools==4.3.0 -pluggy==0.7.1 -py==1.6.0 -pycodestyle==2.3.1 -pyflakes==2.0.0 +more-itertools==7.0.0 +pluggy==0.12.0 +py==1.8.0 +pycodestyle==2.5.0 +pyflakes==2.1.1 pykwalify==1.7.0 -pytest==3.8.2 -pytest-cov==2.6.0 -python-dateutil==2.7.3 -PyYAML==3.13 +pytest==4.6.3 +pytest-cov==2.7.1 +python-dateutil==2.8.0 +PyYAML==5.1.1 ruamel.yaml>0.15.0,<0.16.0 -six==1.11.0 toml==0.10.0 diff --git a/tests/integration/commands/test_borgmatic.py b/tests/integration/commands/test_borgmatic.py index 9232a9b..6043ee9 100644 --- a/tests/integration/commands/test_borgmatic.py +++ b/tests/integration/commands/test_borgmatic.py @@ -14,7 +14,8 @@ def test_parse_arguments_with_no_arguments_uses_defaults(): assert parser.config_paths == config_paths 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 @@ -24,7 +25,8 @@ def test_parse_arguments_with_multiple_config_paths_parses_as_list(): parser = module.parse_arguments('--config', '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(): @@ -36,6 +38,19 @@ def test_parse_arguments_with_verbosity_overrides_default(): assert parser.config_paths == config_paths assert parser.excludes_filename is None 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(): diff --git a/tests/unit/borg/test_info.py b/tests/unit/borg/test_info.py index beb9070..9bf0dae 100644 --- a/tests/unit/borg/test_info.py +++ b/tests/unit/borg/test_info.py @@ -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={}) +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(): flexmock(module).should_receive('execute_command').with_args( 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={}) +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(): flexmock(module).should_receive('execute_command').with_args( INFO_COMMAND + ('--json',), output_log_level=None diff --git a/tests/unit/borg/test_init.py b/tests/unit/borg/test_init.py index 7487361..19b26c2 100644 --- a/tests/unit/borg/test_init.py +++ b/tests/unit/borg/test_init.py @@ -1,13 +1,14 @@ import logging import subprocess +import pytest from flexmock import flexmock from borgmatic.borg import init as module 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') @@ -17,7 +18,7 @@ def insert_info_command_found_mock(): def insert_info_command_not_found_mock(): 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') +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(): insert_info_command_not_found_mock() insert_init_command_mock(INIT_COMMAND + ('--append-only',)) diff --git a/tests/unit/borg/test_list.py b/tests/unit/borg/test_list.py index f47350f..2e6beb9 100644 --- a/tests/unit/borg/test_list.py +++ b/tests/unit/borg/test_list.py @@ -26,6 +26,15 @@ def test_list_archives_with_log_info_calls_borg_with_info_parameter(): 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(): flexmock(module).should_receive('execute_command').with_args( 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={}) +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(): storage_config = {'lock_wait': 5} flexmock(module).should_receive('execute_command').with_args( diff --git a/tox.ini b/tox.ini index b6d005c..66bfbda 100644 --- a/tox.ini +++ b/tox.ini @@ -18,7 +18,7 @@ commands = pytest {posargs} py36,py37: black --check . isort --recursive --check-only --settings-path setup.cfg . - flake8 . + flake8 borgmatic tests [testenv:black] basepython = python3.7 @@ -32,7 +32,7 @@ commands = [testenv:end-to-end] deps = -rtest_requirements.txt commands = - pytest {posargs} tests/end-to-end + pytest {posargs} --no-cov tests/end-to-end [testenv:isort] deps = {[testenv]deps}