From 814729059a385d5edb0c19347b3dfa76930220a5 Mon Sep 17 00:00:00 2001 From: zhongjun Date: Thu, 31 Aug 2017 21:52:03 +0800 Subject: Add some unit test files test_netwrok.py 1.add the below unit test files: test_netwrok.py/test_schemas.py/test_query.py/test_utils.py 2.add the corresponding test conf file deploy_virtual_error.yml. Change-Id: I01292d4c94b9d6159597961a0cab515f57727ec3 Signed-off-by: zhongjun --- tests/data/lab_conf/deploy_virtual_error.yml | 31 +++++++++ tests/unit/common/test_query.py | 36 +++++++++++ tests/unit/config/test_network.py | 97 ++++++++++++++++++++++++++++ tests/unit/config/test_schemas.py | 33 ++++++++++ tests/unit/test_utils.py | 88 +++++++++++++++++++++++++ 5 files changed, 285 insertions(+) create mode 100644 tests/data/lab_conf/deploy_virtual_error.yml create mode 100644 tests/unit/common/test_query.py create mode 100644 tests/unit/config/test_network.py create mode 100644 tests/unit/config/test_schemas.py create mode 100644 tests/unit/test_utils.py diff --git a/tests/data/lab_conf/deploy_virtual_error.yml b/tests/data/lab_conf/deploy_virtual_error.yml new file mode 100644 index 00000000..ca9fba49 --- /dev/null +++ b/tests/data/lab_conf/deploy_virtual_error.yml @@ -0,0 +1,31 @@ +adapter: NULL +hosts: +- name: 'controller01' + roles: + - 'CONTROLLER_LB' + template: 'templates/virtual_environment/vms/controller.xml' +- name: 'controller02' + roles: + - 'CONTROLLER_LB' + template: 'templates/virtual_environment/vms/controller.xml' +- name: 'controller03' + roles: + - 'CONTROLLER_LB' + template: 'templates/virtual_environment/vms/controller.xml' +- name: 'computer01' + roles: + - 'COMPUTER' + template: 'templates/virtual_environment/vms/computer.xml' +- name: 'computer02' + roles: + - 'COMPUTER' + template: 'templates/virtual_environment/vms/computer.xml' +disks: + daisy: 50 + controller: 100 + compute: 100 + ceph: 100 +daisy_passwd: 'r00tme' +daisy_ip: '10.20.11.2' +daisy_gateway: '10.20.11.1' +ceph_disk_name: '/dev/sdb' \ No newline at end of file diff --git a/tests/unit/common/test_query.py b/tests/unit/common/test_query.py new file mode 100644 index 00000000..6e580dd7 --- /dev/null +++ b/tests/unit/common/test_query.py @@ -0,0 +1,36 @@ +############################################################################## +# 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 pytest + +from deploy.common.query import ( + find +) + + +@pytest.mark.parametrize('sequence, expected', [ + ([{'cidr': '10.20.11.0/24', 'gateway': '10.20.11.1', + 'ip_ranges': [{'start': '10.20.11.3', 'end': '10.20.11.10'}], + 'name': 'MANAGEMENT'}, + {'cidr': '10.20.11.0/24', 'gateway': '10.20.11.1', + 'ip_ranges': [{'start': '10.20.11.3', 'end': '10.20.11.10'}], + 'name': 'STORAGE'}], + {'cidr': '10.20.11.0/24', 'gateway': '10.20.11.1', + 'ip_ranges': [{'start': '10.20.11.3', 'end': '10.20.11.10'}], + 'name': 'STORAGE'}), + ([{'cidr': '10.20.11.0/24', 'gateway': '10.20.11.1', + 'ip_ranges': [{'start': '10.20.11.3', 'end': '10.20.11.10'}], + 'name': 'MANAGEMENT'}, + {'cidr': '10.20.11.0/24', 'gateway': '10.20.11.1', + 'ip_ranges': [{'start': '10.20.11.3', 'end': '10.20.11.10'}], + 'name': 'PUBLICAPI'}], + None)]) +def test_find(sequence, expected): + def sample_func(item): + return item['name'] == 'STORAGE' + assert find(sample_func, sequence) == expected diff --git a/tests/unit/config/test_network.py b/tests/unit/config/test_network.py new file mode 100644 index 00000000..98107929 --- /dev/null +++ b/tests/unit/config/test_network.py @@ -0,0 +1,97 @@ +############################################################################## +# 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 + +from deploy.config.network import NetworkConfig + + +@pytest.fixture(scope="session") +def conf_file_dir(data_root): + return os.path.join(data_root, 'lab_conf') + + +@pytest.mark.parametrize('network_file_name, metadatas, networks, interfaces, internal_vip, public_vip', [ + ('network_virtual1.yml', + {'title': 'zte-virtual1 network config', + 'version': '0.1', + 'created': 'Tue Apr 11 2017', + 'comment': 'five vm node deploy'}, + [{'cidr': '10.20.11.0/24', 'gateway': '10.20.11.1', + 'ip_ranges': [{'start': '10.20.11.3', 'end': '10.20.11.10'}], + 'name': 'MANAGEMENT'}, + {'cidr': '10.20.11.0/24', 'gateway': '10.20.11.1', + 'ip_ranges': [{'start': '10.20.11.3', 'end': '10.20.11.10'}], + 'name': 'STORAGE'}, + {'cidr': '172.10.101.0/24', 'gateway': '172.10.101.1', + 'ip_ranges': [{'start': '172.10.101.2', 'end': '172.10.101.20'}], + 'name': 'EXTERNAL', 'network_name': 'admin_external', 'mapping': 'physnet1'}, + {'cidr': '10.20.11.0/24', 'gateway': '10.20.11.1', + 'ip_ranges': [{'start': '10.20.11.3', 'end': '10.20.11.10'}], + 'name': 'PUBLICAPI'}, + {'cidr': '10.20.11.0/24', 'gateway': '10.20.11.1', + 'ip_ranges': [{'start': '10.20.11.3', 'end': '10.20.11.10'}], + 'name': 'TENANT'}, + {'cidr': '100.20.11.0/24', 'gateway': '100.20.11.1', + 'ip_ranges': [{'start': '100.20.11.3', 'end': '100.20.11.10'}], + 'name': 'HEARTBEAT'}], + [{'name': 'EXTERNAL', 'interface': 'ens8'}, + {'name': 'MANAGEMENT', 'interface': 'ens3'}, + {'name': 'PUBLICAPI', 'interface': 'ens3'}, + {'name': 'STORAGE', 'interface': 'ens3'}, + {'name': 'TENANT', 'interface': 'ens3'}, + {'name': 'HEARTBEAT', 'interface': 'ens9'}], + '10.20.11.11', + '10.20.11.11')]) +def test_create_NetworkConf_instance(conf_file_dir, network_file_name, metadatas, + networks, interfaces, internal_vip, public_vip): + type2name = { + 'EXTERNAL': 'ext', + 'MANAGEMENT': 'man', + 'STORAGE': 'stor', + 'PUBLICAPI': 'pub', + 'TENANT': 'tenant', + 'HEARTBEAT': 'hbt', + } + + network_file_path = os.path.join(conf_file_dir, network_file_name) + NetworkConfClient = NetworkConfig(network_file_path) + assert NetworkConfClient._file == network_file_path + + for key, val in metadatas.iteritems(): + assert getattr(NetworkConfClient, key) == val + for network in networks: + name = network['name'] + mapname = type2name[name] + assert getattr(NetworkConfClient, mapname) == network + for net_key, net_val in network.iteritems(): + net_name = '{}_{}'.format(mapname, net_key) + assert getattr(NetworkConfClient, net_name) == net_val + + for interface in interfaces: + name = interface['name'] + mapname = type2name[name] + interface_name = '{}_{}'.format(mapname, 'iterface') + assert getattr(NetworkConfClient, interface_name) == interface['interface'] + + assert getattr(NetworkConfClient, 'internal_vip') == internal_vip + assert getattr(NetworkConfClient, 'public_vip') == public_vip + + +@pytest.mark.parametrize('network_file_name, name, expected', [ + ('network_virtual1.yml', 'EXTERNAL', + {'cidr': '172.10.101.0/24', 'gateway': '172.10.101.1', + 'ip_ranges': [{'start': '172.10.101.2', 'end': '172.10.101.20'}], + 'name': 'EXTERNAL', 'network_name': 'admin_external', 'mapping': 'physnet1'}), + ('network_virtual1.yml', 'NO_EXIT', None)]) +def test__get_network_NetworkConf(conf_file_dir, network_file_name, name, expected): + network_file_path = os.path.join(conf_file_dir, network_file_name) + NetworkConfClient = NetworkConfig(network_file_path) + assert NetworkConfClient._get_network(name) == expected diff --git a/tests/unit/config/test_schemas.py b/tests/unit/config/test_schemas.py new file mode 100644 index 00000000..04705fda --- /dev/null +++ b/tests/unit/config/test_schemas.py @@ -0,0 +1,33 @@ +############################################################################## +# 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 yaml +import pytest + +from deploy.config.schemas import ( + deploy_schema_validate +) + + +@pytest.fixture(scope="session") +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')]) +def test_deploy_schema_validate(conf_file_dir, deploy_file_name): + 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 != [] diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py new file mode 100644 index 00000000..a6d84de0 --- /dev/null +++ b/tests/unit/test_utils.py @@ -0,0 +1,88 @@ +############################################################################## +# 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 + +from deploy.utils import ( + check_file_exists, + make_file_executable, + confirm_dir_exists, + check_scenario_valid +) + + +@pytest.mark.parametrize('test_file_name', [ + ('no_exist_file'), + ('exist_file')]) +def test_check_file_exists(tmpdir, test_file_name): + try: + file_path = os.path.join(tmpdir.dirname, test_file_name) + if test_file_name == 'exist_file': + os.mknod(file_path) + check_file_exists(file_path) + except SystemExit: + if test_file_name == 'exist_file': + assert 0 + else: + if test_file_name == 'no_exist_file': + assert 0 + finally: + tmpdir.remove() + + +@pytest.mark.parametrize('test_file_name', [ + ('no_exist_file'), + ('no_exe_file'), + ('exe_file')]) +def test_make_file_executable(tmpdir, test_file_name): + try: + file_path = os.path.join(tmpdir.dirname, test_file_name) + if test_file_name == 'no_exe_file': + os.mknod(file_path) + if test_file_name == 'exe_file': + os.mknod(file_path, 0700) + make_file_executable(file_path) + except SystemExit: + if test_file_name == 'no_exe_file' or test_file_name == 'exe_file': + assert 0 + else: + if test_file_name == 'no_exist_file': + assert 0 + finally: + if test_file_name == 'no_exe_file' or test_file_name == 'exe_file': + assert os.access(file_path, os.X_OK) + tmpdir.remove() + + +@pytest.mark.parametrize('test_dir_name', [ + ('no_exist_dir'), + ('exist_dir')]) +def test_confirm_dir_exists(tmpdir, test_dir_name): + if test_dir_name == 'no_exist_dir': + dir_path = os.path.join(tmpdir.dirname, 'no_exist_dir') + if test_dir_name == 'exist_dir': + dir_path = tmpdir.mkdir('exist_dir').dirname + confirm_dir_exists(dir_path) + assert os.path.isdir(dir_path) + tmpdir.remove() + + +@pytest.mark.parametrize('scenario', [ + ('os-nosdn-nofeature-ha'), + ('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 -- cgit 1.2.3-korg