From 861bb6490d9ee9ddf79cdfce7fe677381ea9c945 Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Sat, 16 Sep 2017 16:58:18 +0800 Subject: Some optimizations about unit test Remove some "if/else". Rename a variables and a file. Split a testcase in two cases. Change-Id: Ic107845ac8eff41ba20e1501c93925586d6cbde9 Signed-off-by: Alex Yang --- tests/unit/config/test_schemas.py | 17 +++----- tests/unit/prepare/test_prepare_execure.py | 67 ------------------------------ tests/unit/prepare/test_prepare_execute.py | 67 ++++++++++++++++++++++++++++++ tests/unit/test_utils.py | 23 +++++----- 4 files changed, 86 insertions(+), 88 deletions(-) delete mode 100644 tests/unit/prepare/test_prepare_execure.py create mode 100644 tests/unit/prepare/test_prepare_execute.py (limited to 'tests/unit') diff --git a/tests/unit/config/test_schemas.py b/tests/unit/config/test_schemas.py index 7c7dab28..97bff208 100644 --- a/tests/unit/config/test_schemas.py +++ b/tests/unit/config/test_schemas.py @@ -21,16 +21,11 @@ def conf_file_dir(data_root): return os.path.join(data_root, 'lab_conf') -@pytest.mark.parametrize('deploy_file_name', [ - ('deploy_virtual1.yml'), - ('deploy_virtual_error.yml'), - ('deploy_baremetal.yml')]) -def test_deploy_schema_validate(conf_file_dir, deploy_file_name): +@pytest.mark.parametrize('deploy_file_name, tell_result', [ + ('deploy_virtual1.yml', lambda x: x == []), + ('deploy_virtual_error.yml', lambda x: x != []), + ('deploy_baremetal.yml', lambda x: x == [])]) +def test_deploy_schema_validate(conf_file_dir, deploy_file_name, tell_result): data = yaml.safe_load(open(os.path.join(conf_file_dir, deploy_file_name), 'r')) errors = deploy_schema_validate(data) - if deploy_file_name == 'deploy_virtual1.yml': - assert errors == [] - elif deploy_file_name == 'deploy_virtual_error.yml': - assert errors != [] - elif deploy_file_name == 'deploy_baremetal.yml': - assert errors == [] + assert tell_result(errors) diff --git a/tests/unit/prepare/test_prepare_execure.py b/tests/unit/prepare/test_prepare_execure.py deleted file mode 100644 index 9e72722d..00000000 --- a/tests/unit/prepare/test_prepare_execure.py +++ /dev/null @@ -1,67 +0,0 @@ -############################################################################## -# Copyright (c) 2017 ZTE Corp and others. -# -# All rights reserved. This program and the accompanying materials -# are made available under the terms of the Apache License, Version 2.0 -# which accompanies this distribution, and is available at -# http://www.apache.org/licenses/LICENSE-2.0 -############################################################################## - -import os -import pytest - -import deploy.prepare.execute -from deploy.prepare.execute import ( - _set_qemu_compute, - _set_default_floating_pool, - _set_trusts_auth -) - -deploy.prepare.execute.KOLLA_CONF_PATH = '/tmp' - - -@pytest.fixture(scope="module") -def kolla_conf_file_nov_path(): - return os.path.join(deploy.prepare.execute.KOLLA_CONF_PATH, 'nova') - - -@pytest.fixture(scope="module") -def kolla_conf_file_heat_dir(): - return os.path.join(deploy.prepare.execute.KOLLA_CONF_PATH, 'heat') - - -@pytest.fixture(scope="module") -def conf_file_dir(data_root): - return os.path.join(data_root, 'lab_conf') - - -def clear_tmp_dir(path): - filelist = os.listdir(path) - for file in filelist: - file_path = os.path.join(path, file) - if os.path.isfile(file_path): - os.remove(file_path) - os.rmdir(path) - - -def test__set_qemu_compute(kolla_conf_file_nov_path): - _set_qemu_compute() - exp_conf_file = os.path.join(kolla_conf_file_nov_path, 'nova-compute.conf') - assert os.path.isfile(exp_conf_file) - clear_tmp_dir(kolla_conf_file_nov_path) - - -def test__set_default_floating_pool(kolla_conf_file_nov_path, conf_file_dir): - network_conf_file = os.path.join(conf_file_dir, 'network_virtual1.yml') - _set_default_floating_pool(network_conf_file) - exp_conf_file = os.path.join(kolla_conf_file_nov_path, 'nova-api.conf') - assert os.path.isfile(exp_conf_file) - clear_tmp_dir(kolla_conf_file_nov_path) - - -def test__set_trusts_auth(kolla_conf_file_heat_dir): - _set_trusts_auth() - exp_conf_file_1 = os.path.join(kolla_conf_file_heat_dir, 'heat-api.conf') - exp_conf_file_2 = os.path.join(kolla_conf_file_heat_dir, 'heat-engine.conf') - assert (os.path.isfile(exp_conf_file_1) and os.path.isfile(exp_conf_file_2)) - clear_tmp_dir(kolla_conf_file_heat_dir) diff --git a/tests/unit/prepare/test_prepare_execute.py b/tests/unit/prepare/test_prepare_execute.py new file mode 100644 index 00000000..03259b93 --- /dev/null +++ b/tests/unit/prepare/test_prepare_execute.py @@ -0,0 +1,67 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corp and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +import os +import pytest + +import deploy.prepare.execute +from deploy.prepare.execute import ( + _set_qemu_compute, + _set_default_floating_pool, + _set_trusts_auth +) + +deploy.prepare.execute.KOLLA_CONF_PATH = '/tmp' + + +@pytest.fixture(scope="module") +def kolla_conf_file_nova_path(): + return os.path.join(deploy.prepare.execute.KOLLA_CONF_PATH, 'nova') + + +@pytest.fixture(scope="module") +def kolla_conf_file_heat_dir(): + return os.path.join(deploy.prepare.execute.KOLLA_CONF_PATH, 'heat') + + +@pytest.fixture(scope="module") +def conf_file_dir(data_root): + return os.path.join(data_root, 'lab_conf') + + +def clear_tmp_dir(path): + filelist = os.listdir(path) + for file in filelist: + file_path = os.path.join(path, file) + if os.path.isfile(file_path): + os.remove(file_path) + os.rmdir(path) + + +def test__set_qemu_compute(kolla_conf_file_nova_path): + _set_qemu_compute() + exp_conf_file = os.path.join(kolla_conf_file_nova_path, 'nova-compute.conf') + assert os.path.isfile(exp_conf_file) + clear_tmp_dir(kolla_conf_file_nova_path) + + +def test__set_default_floating_pool(kolla_conf_file_nova_path, conf_file_dir): + network_conf_file = os.path.join(conf_file_dir, 'network_virtual1.yml') + _set_default_floating_pool(network_conf_file) + exp_conf_file = os.path.join(kolla_conf_file_nova_path, 'nova-api.conf') + assert os.path.isfile(exp_conf_file) + clear_tmp_dir(kolla_conf_file_nova_path) + + +def test__set_trusts_auth(kolla_conf_file_heat_dir): + _set_trusts_auth() + exp_conf_file_1 = os.path.join(kolla_conf_file_heat_dir, 'heat-api.conf') + exp_conf_file_2 = os.path.join(kolla_conf_file_heat_dir, 'heat-engine.conf') + assert (os.path.isfile(exp_conf_file_1) and os.path.isfile(exp_conf_file_2)) + clear_tmp_dir(kolla_conf_file_heat_dir) diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index d6095c7f..22597dca 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -9,6 +9,7 @@ import os import pytest +import mock from deploy.utils import ( check_file_exists, @@ -76,14 +77,16 @@ def test_confirm_dir_exists(tmpdir, test_dir_name): @pytest.mark.parametrize('scenario', [ - ('os-nosdn-nofeature-ha'), + ('os-nosdn-nofeature-ha')]) +@mock.patch("deploy.utils.err_exit") +def test_check_scenario_supported(mock_err_exit, scenario): + check_scenario_valid(scenario) + mock_err_exit.assert_not_called() + + +@pytest.mark.parametrize('scenario', [ ('os-odl-kvm-ha')]) -def test_check_scenario_valid(scenario): - try: - check_scenario_valid(scenario) - except SystemExit: - if scenario == 'os-nosdn-nofeature-ha': - assert 0 - else: - if scenario == 'os-odl-kvm-ha': - assert 0 +@mock.patch("deploy.utils.err_exit") +def test_check_scenario_unsupported(mock_err_exit, scenario): + check_scenario_valid(scenario) + mock_err_exit.assert_called_once() -- cgit 1.2.3-korg