From c5dfe52ced4f9180933188a9460fead36f92f653 Mon Sep 17 00:00:00 2001 From: zhongjun Date: Tue, 26 Sep 2017 15:35:34 +0800 Subject: Add more test cases in test_deploy.py Add more test cases in test_deploy.py and test_environment.py Change-Id: I6adf3f51aa7a436387c2da8a8dc8c67f6e5a70db Signed-off-by: zhongjun --- tests/data/lab_conf/deploy_bare_odl-ha.yml | 43 ++++++ .../lab_conf/deploy_baremetal_invalid_ipmi.yml | 29 ++++ tests/data/lab_conf/deploy_vir1_nosdn-ha.yml | 32 ++++ .../lab_conf/deploy_virtual_invalid_template.yml | 31 ++++ tests/unit/test_deploy.py | 67 ++++++++- tests/unit/test_environment.py | 165 +++++++++++++++++++-- 6 files changed, 349 insertions(+), 18 deletions(-) create mode 100644 tests/data/lab_conf/deploy_bare_odl-ha.yml create mode 100644 tests/data/lab_conf/deploy_baremetal_invalid_ipmi.yml create mode 100644 tests/data/lab_conf/deploy_vir1_nosdn-ha.yml create mode 100644 tests/data/lab_conf/deploy_virtual_invalid_template.yml diff --git a/tests/data/lab_conf/deploy_bare_odl-ha.yml b/tests/data/lab_conf/deploy_bare_odl-ha.yml new file mode 100644 index 00000000..28e8c195 --- /dev/null +++ b/tests/data/lab_conf/deploy_bare_odl-ha.yml @@ -0,0 +1,43 @@ +adapter: 'ipmi' +hosts: +- name: 'controller01' + roles: + - 'CONTROLLER_LB' + ipmi_ip: '192.168.1.11' + ipmi_user: 'testuser' + ipmi_pass: 'testpass' +- name: 'controller02' + roles: + - 'CONTROLLER_LB' + ipmi_ip: '192.168.1.12' + ipmi_user: 'testuser' + ipmi_pass: 'testpass' +- name: 'controller03' + roles: + - 'CONTROLLER_LB' + ipmi_ip: '192.168.1.13' + ipmi_user: 'testuser' + ipmi_pass: 'testpass' +- name: 'computer01' + roles: + - 'COMPUTER' + ipmi_ip: '192.168.1.14' + ipmi_user: 'testuser' + ipmi_pass: 'testpass' +- name: 'computer02' + roles: + - 'COMPUTER' + ipmi_ip: '192.168.1.15' + ipmi_user: 'testuser' + ipmi_pass: 'testpass' +disks: + daisy: 50 +daisy_passwd: 'r00tme' +daisy_ip: '10.20.0.2' +daisy_gateway: '10.20.0.1' +ceph_disk_name: '/dev/sdb' +modules: + - module: opendaylight + module-config: + - enable_l3_odl: + value: true diff --git a/tests/data/lab_conf/deploy_baremetal_invalid_ipmi.yml b/tests/data/lab_conf/deploy_baremetal_invalid_ipmi.yml new file mode 100644 index 00000000..2e6de5a3 --- /dev/null +++ b/tests/data/lab_conf/deploy_baremetal_invalid_ipmi.yml @@ -0,0 +1,29 @@ +adapter: 'ipmi' +hosts: +- name: 'controller01' + roles: + - 'CONTROLLER_LB' +- name: 'controller02' + roles: + - 'CONTROLLER_LB' +- name: 'controller03' + roles: + - 'CONTROLLER_LB' +- name: 'computer01' + roles: + - 'COMPUTER' + ipmi_ip: '192.168.1.14' + ipmi_user: 'testuser' + ipmi_pass: 'testpass' +- name: 'computer02' + roles: + - 'COMPUTER' + ipmi_ip: '192.168.1.15' + ipmi_user: 'testuser' + ipmi_pass: 'testpass' +disks: + daisy: 50 +daisy_passwd: 'r00tme' +daisy_ip: '10.20.0.2' +daisy_gateway: '10.20.0.1' +ceph_disk_name: '/dev/sdb' \ No newline at end of file diff --git a/tests/data/lab_conf/deploy_vir1_nosdn-ha.yml b/tests/data/lab_conf/deploy_vir1_nosdn-ha.yml new file mode 100644 index 00000000..688b4f4f --- /dev/null +++ b/tests/data/lab_conf/deploy_vir1_nosdn-ha.yml @@ -0,0 +1,32 @@ +adapter: libvirt +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: 110 + compute: 110 + ceph: 110 +daisy_passwd: 'r00tme' +daisy_ip: '10.20.11.2' +daisy_gateway: '10.20.11.1' +ceph_disk_name: '/dev/sdb' +modules: diff --git a/tests/data/lab_conf/deploy_virtual_invalid_template.yml b/tests/data/lab_conf/deploy_virtual_invalid_template.yml new file mode 100644 index 00000000..483532cb --- /dev/null +++ b/tests/data/lab_conf/deploy_virtual_invalid_template.yml @@ -0,0 +1,31 @@ +adapter: libvirt +hosts: +- name: 'controller01' + roles: + - 'CONTROLLER_LB' + template: 'controller.xml' +- name: 'controller02' + roles: + - 'CONTROLLER_LB' + template: 'controller.xml' +- name: 'controller03' + roles: + - 'CONTROLLER_LB' + template: 'controller.xml' +- name: 'computer01' + roles: + - 'COMPUTER' + template: 'computer.xml' +- name: 'computer02' + roles: + - 'COMPUTER' + template: 'computer.xml' +disks: + daisy: 50 + controller: 110 + compute: 110 + ceph: 110 +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/test_deploy.py b/tests/unit/test_deploy.py index 0c4ebb6f..142a7f45 100644 --- a/tests/unit/test_deploy.py +++ b/tests/unit/test_deploy.py @@ -16,7 +16,7 @@ from deploy.utils import WORKSPACE import mock sys.modules['libvirt'] = mock.Mock() -from deploy import environment # noqa: ignore=E402 +from deploy import environment # noqa: ignore=E402 from deploy.deploy import ( config_arg_parser, DaisyDeployment, @@ -128,6 +128,66 @@ def test_create_DaisyDeployment_instance(mock_err_exit, mock_deploy_schema_valid tmpdir.remove() +@pytest.mark.parametrize('kwargs, is_use_pdf, exp_deploy_file', [ + ( + { + 'lab_name': 'zte', + 'pod_name': 'virtual1', + 'deploy_file': 'deploy_virtual1.yml', + 'net_file': 'network_virtual1.yml', + 'bin_file': 'opnfv.bin', + 'daisy_only': False, + 'cleanup_only': False, + 'remote_dir': '/home/daisy', + 'work_dir': 'workdir', + 'storage_dir': 'vms', + 'pxe_bridge': 'libvirt', + 'deploy_log': 'deploy.log', + 'scenario': 'os-nosdn-nofeature-ha' + }, False, 'deploy_vir1_nosdn-ha.yml' + ), + ( + { + 'lab_name': 'zte', + 'pod_name': 'pod1', + 'deploy_file': 'deploy_baremetal.yml', + 'net_file': 'network_baremetal.yml', + 'bin_file': 'opnfv.bin', + 'daisy_only': False, + 'cleanup_only': False, + 'remote_dir': '/home/daisy', + 'work_dir': 'workdir', + 'storage_dir': 'vms', + 'pxe_bridge': 'pxebr', + 'deploy_log': 'deploy.log', + 'scenario': 'os-odl-nofeature-ha' + }, True, 'deploy_bare_odl-ha.yml' + )]) +@mock.patch.object(DaisyDeployment, '_use_pod_descriptor_file') +def test__construct_final_deploy_conf_in_DaisyDeployment(mock__use_pod_descriptor_file, + conf_file_dir, tmpdir, + kwargs, is_use_pdf, exp_deploy_file): + kwargs['deploy_file'] = os.path.join(conf_file_dir, kwargs['deploy_file']) + kwargs['net_file'] = os.path.join(conf_file_dir, kwargs['net_file']) + tmpdir.join(kwargs['bin_file']).write('testdata') + kwargs['bin_file'] = os.path.join(tmpdir.dirname, tmpdir.basename, kwargs['bin_file']) + kwargs['deploy_log'] = os.path.join(tmpdir.dirname, tmpdir.basename, kwargs['deploy_log']) + tmpsubdir = tmpdir.mkdir(kwargs['work_dir']) + kwargs['work_dir'] = os.path.join(tmpsubdir.dirname, tmpsubdir.basename) + tmpsubdir = tmpdir.mkdir(kwargs['storage_dir']) + kwargs['storage_dir'] = os.path.join(tmpsubdir.dirname, tmpsubdir.basename) + exp_deploy_file_path = os.path.join(conf_file_dir, exp_deploy_file) + pdf_deploy_file = None if not is_use_pdf else kwargs['deploy_file'] + + mock__use_pod_descriptor_file.return_value = pdf_deploy_file + daisy_deploy = DaisyDeployment(**kwargs) + mock__use_pod_descriptor_file.asser_called_once_with() + with open(exp_deploy_file_path) as yaml_file: + exp_deploy_struct = yaml.safe_load(yaml_file) + assert daisy_deploy.deploy_struct == exp_deploy_struct + tmpdir.remove() + + @pytest.mark.parametrize('kwargs', [ ( { @@ -213,6 +273,8 @@ def test_run_in_DaisyDeployment(mock_deploy, mock_install_daisy, tmpdir.remove() +@pytest.mark.parametrize('cleanup_only', [ + (False), (True)]) @mock.patch('deploy.deploy.argparse.ArgumentParser.parse_args') @mock.patch('deploy.deploy.check_sudo_privilege') @mock.patch('deploy.deploy.save_log_to_file') @@ -223,7 +285,7 @@ def test_run_in_DaisyDeployment(mock_deploy, mock_install_daisy, def test_parse_arguments(mock_confirm_dir_exists, mock_make_file_executable, mock_check_file_exists, mock_check_scenario_valid, mock_save_log_to_file, mock_check_sudo_privilege, - mock_parse_args, tmpdir): + mock_parse_args, cleanup_only, tmpdir): class MockArg(): def __init__(self, lab_name, pod_name, bin_file, daisy_only, cleanup_only, remote_dir, work_dir, storage_dir, pxe_bridge, @@ -245,7 +307,6 @@ def test_parse_arguments(mock_confirm_dir_exists, mock_make_file_executable, conf_base_dir = os.path.join(WORKSPACE, 'labs', 'zte', 'pod2') deploy_file = os.path.join(conf_base_dir, 'daisy/config/deploy.yml') net_file = os.path.join(conf_base_dir, 'daisy/config/network.yml') - cleanup_only = False expected = { 'lab_name': 'zte', 'pod_name': 'pod2', diff --git a/tests/unit/test_environment.py b/tests/unit/test_environment.py index 250a80a9..32ab14bd 100644 --- a/tests/unit/test_environment.py +++ b/tests/unit/test_environment.py @@ -2,10 +2,13 @@ import os import copy import mock import yaml +import pytest from deepdiff import DeepDiff from deploy.utils import WORKSPACE from deploy import environment +from deploy import daisy_server +from deploy.daisy_server import DaisyServer from deploy.environment import ( DaisyEnvironmentBase, BareMetalEnvironment, @@ -80,7 +83,18 @@ def test_delete_daisy_server_DaisyEnvironmentBase(tmpdir, mocker): tmpdir.remove() -def test_create_daisy_server_image_DaisyEnvironmentBase(tmpdir, monkeypatch): +@pytest.mark.parametrize('ret_run_shell, ret_access', [ + (0, 1), + (1, 0)]) +@mock.patch('deploy.environment.os.access') +@mock.patch('deploy.environment.os.remove') +@mock.patch('deploy.environment.shutil.move') +@mock.patch('deploy.environment.err_exit') +@mock.patch('deploy.environment.run_shell') +def test_create_daisy_server_image_DaisyEnvironmentBase(mock_run_shell, mock_err_exit, + mock_move, mock_remove, + mock_access, tmpdir, + ret_run_shell, ret_access): work_dir = os.path.join(tmpdir.dirname, tmpdir.basename, work_dir_name) os.makedirs(work_dir, 0755) storage_dir = os.path.join(tmpdir.dirname, tmpdir.basename, storage_dir_name) @@ -90,15 +104,40 @@ def test_create_daisy_server_image_DaisyEnvironmentBase(tmpdir, monkeypatch): DaisyEnvBaseInst = DaisyEnvironmentBase( deploy_struct, net_struct, adapter, pxe_bridge, daisy_server, work_dir, storage_dir, scenario) + mock_run_shell.return_value = ret_run_shell + mock_access.return_value = ret_access + mock_err_exit.return_value = 0 - def create_server_image_sucess(cmd): - os.makedirs(os.path.join(work_dir, 'daisy')) - with open(os.path.join(work_dir, 'daisy', 'centos7.qcow2'), 'w') as f: - f.write('image-data') - return 0 - monkeypatch.setattr(environment, 'run_shell', create_server_image_sucess) DaisyEnvBaseInst.create_daisy_server_image() - assert os.path.isfile(DaisyEnvBaseInst.daisy_server_info['image']) + if ret_run_shell: + mock_err_exit.assert_called_once_with('Failed to create Daisy Server image') + else: + if ret_access: + mock_remove.assert_called_once_with(DaisyEnvBaseInst.daisy_server_info['image']) + else: + mock_move.assert_called_once() + tmpdir.remove() + + +@mock.patch.object(daisy_server.DaisyServer, 'connect') +@mock.patch.object(daisy_server.DaisyServer, 'install_daisy') +def test_install_daisy_DaisyEnvironmentBase(mock_install_daisy, mock_connect, tmpdir): + work_dir = os.path.join(tmpdir.dirname, tmpdir.basename, work_dir_name) + os.makedirs(work_dir, 0755) + storage_dir = os.path.join(tmpdir.dirname, tmpdir.basename, storage_dir_name) + os.makedirs(storage_dir, 0755) + daisy_server = copy.deepcopy(daisy_server_info) + daisy_server['image'] = os.path.join(storage_dir, daisy_server['image']) + remote_dir = '/home/daisy' + bin_file = os.path.join(tmpdir.dirname, tmpdir.basename, 'opnfv.bin') + deploy_file_name = 'final_deploy.yml' + net_file_name = 'network_baremetal.yml' + DaisyEnvBaseInst = DaisyEnvironmentBase( + deploy_struct, net_struct, adapter, pxe_bridge, + daisy_server, work_dir, storage_dir, scenario) + DaisyEnvBaseInst.install_daisy(remote_dir, bin_file, deploy_file_name, net_file_name) + mock_install_daisy.assert_called_once_with() + mock_connect.assert_called_once_with() tmpdir.remove() @@ -153,18 +192,23 @@ def test_create_daisy_server_vm_BareMetalEnvironment(mocker, tmpdir): tmpdir.remove() +@pytest.mark.parametrize('deploy_struct_info', [ + (deploy_struct)]) +@mock.patch('deploy.environment.err_exit') @mock.patch('deploy.environment.ipmi_reboot_node') -def test_reboot_nodes_BareMetalEnvironment(mock_ipmi_reboot_node, tmpdir): +def test_reboot_nodes_BareMetalEnvironment(mock_ipmi_reboot_node, mock_err_exit, + deploy_struct_info, tmpdir): work_dir = os.path.join(tmpdir.dirname, tmpdir.basename, work_dir_name) storage_dir = os.path.join(tmpdir.dirname, tmpdir.basename, storage_dir_name) daisy_server = copy.deepcopy(daisy_server_info) daisy_server['image'] = os.path.join(storage_dir, daisy_server['image']) mock_ipmi_reboot_node.return_value = True + mock_err_exit.return_value = 0 BareMetalEnvironmentInst = BareMetalEnvironment( - deploy_struct, net_struct, adapter, pxe_bridge, + deploy_struct_info, net_struct, adapter, pxe_bridge, daisy_server, work_dir, storage_dir, scenario) BareMetalEnvironmentInst.reboot_nodes() - assert environment.ipmi_reboot_node.call_count == 5 + assert mock_ipmi_reboot_node.call_count == 5 tmpdir.remove() @@ -186,6 +230,49 @@ def test_create_daisy_server_BareMetalEnvironment(mock_create_daisy_server_vm, m tmpdir.remove() +@mock.patch.object(daisy_server.DaisyServer, 'prepare_cluster') +@mock.patch.object(environment.BareMetalEnvironment, 'reboot_nodes') +@mock.patch.object(daisy_server.DaisyServer, 'prepare_host_and_pxe') +@mock.patch.object(daisy_server.DaisyServer, 'check_os_installation') +@mock.patch.object(daisy_server.DaisyServer, 'check_openstack_installation') +@mock.patch.object(daisy_server.DaisyServer, 'post_deploy') +def test_deploy_BareMetalEnvironment(mock_post_deploy, mock_check_openstack_installation, + mock_check_os_installation, mock_prepare_host_and_pxe, + mock_reboot_nodes, mock_prepare_cluster, + tmpdir): + work_dir = os.path.join(tmpdir.dirname, tmpdir.basename, work_dir_name) + storage_dir = os.path.join(tmpdir.dirname, tmpdir.basename, storage_dir_name) + daisy_server = copy.deepcopy(daisy_server_info) + daisy_server['image'] = os.path.join(storage_dir, daisy_server['image']) + deploy_file = os.path.join(get_conf_file_dir(), 'deploy_baremetal.yml') + net_file = os.path.join(get_conf_file_dir(), 'network_baremetal.yml') + remote_dir = '/home/daisy' + bin_file = os.path.join(tmpdir.dirname, tmpdir.basename, 'opnfv.bin') + deploy_file_name = 'final_deploy.yml' + net_file_name = 'network_baremetal.yml' + BareMetalEnvironmentInst = BareMetalEnvironment( + deploy_struct, net_struct, adapter, pxe_bridge, + daisy_server, work_dir, storage_dir, scenario) + BareMetalEnvironmentInst.server = DaisyServer( + daisy_server['name'], + daisy_server['address'], + daisy_server['password'], + remote_dir, + bin_file, + adapter, + scenario, + deploy_file_name, + net_file_name) + BareMetalEnvironmentInst.deploy(deploy_file, net_file) + mock_prepare_cluster.assert_called_once_with(deploy_file, net_file) + mock_reboot_nodes.assert_called_once_with(boot_dev='pxe') + mock_prepare_host_and_pxe.assert_called_once_with() + mock_check_os_installation.assert_called_once_with(len(BareMetalEnvironmentInst.deploy_struct['hosts'])) + mock_check_openstack_installation.assert_called_once_with(len(BareMetalEnvironmentInst.deploy_struct['hosts'])) + mock_post_deploy.assert_called_once_with() + tmpdir.remove() + + def test_create_VirtualEnvironment_instance(tmpdir): work_dir = os.path.join(tmpdir.dirname, tmpdir.basename, work_dir_name) storage_dir = os.path.join(tmpdir.dirname, tmpdir.basename, storage_dir_name) @@ -208,19 +295,67 @@ def test_create_VirtualEnvironment_instance(tmpdir): tmpdir.remove() +@mock.patch.object(environment.VirtualEnvironment, 'check_nodes_template') +def test_check_configuration_VirtualEnvironment(mock_check_nodes_template, tmpdir): + work_dir = os.path.join(tmpdir.dirname, tmpdir.basename, work_dir_name) + storage_dir = os.path.join(tmpdir.dirname, tmpdir.basename, storage_dir_name) + daisy_server = copy.deepcopy(daisy_server_info) + daisy_server['image'] = os.path.join(storage_dir, daisy_server['image']) + VirtualEnvironment( + deploy_virtual_struct, net_struct, adapter_virtual, pxe_bridge_virtual, + daisy_server, work_dir, storage_dir, scenario) + mock_check_nodes_template.assert_called_once_with() + tmpdir.remove() + + +deploy_virtual_invalid_struct = get_conf_info_from_file(get_conf_file_dir(), 'deploy_virtual_invalid_template.yml') + + +@pytest.mark.parametrize('deploy_struct_info', [ + (deploy_struct), + (deploy_virtual_struct), + (deploy_virtual_invalid_struct)]) +@mock.patch('deploy.environment.err_exit') +def test_check_nodes_template_VirtualEnvironment(mock_err_exit, deploy_struct_info, tmpdir): + work_dir = os.path.join(tmpdir.dirname, tmpdir.basename, work_dir_name) + storage_dir = os.path.join(tmpdir.dirname, tmpdir.basename, storage_dir_name) + daisy_server = copy.deepcopy(daisy_server_info) + daisy_server['image'] = os.path.join(storage_dir, daisy_server['image']) + mock_err_exit.return_value = 0 + VirtualEnvironment( + deploy_struct_info, net_struct, adapter_virtual, pxe_bridge_virtual, + daisy_server, work_dir, storage_dir, scenario) + if deploy_struct_info == deploy_struct: + mock_err_exit.assert_not_called() + elif deploy_struct_info == deploy_virtual_struct: + mock_err_exit.assert_not_called() + elif deploy_struct_info == deploy_virtual_invalid_struct: + assert mock_err_exit.call_count == 5 + tmpdir.remove() + + +@pytest.mark.parametrize('net_name', [ + (pxe_bridge_virtual)]) @mock.patch('deploy.environment.create_virtual_network') -def test_create_daisy_server_network_VirtualEnvironment(mock_create_virtual_network, tmpdir): +@mock.patch('deploy.environment.err_exit') +def test_create_daisy_server_network_VirtualEnvironment(mock_err_exit, mock_create_virtual_network, + net_name, tmpdir): work_dir = os.path.join(tmpdir.dirname, tmpdir.basename, work_dir_name) storage_dir = os.path.join(tmpdir.dirname, tmpdir.basename, storage_dir_name) daisy_server = copy.deepcopy(daisy_server_info) daisy_server['image'] = os.path.join(storage_dir, daisy_server['image']) - mock_create_virtual_network.return_value = pxe_bridge_virtual + mock_create_virtual_network.return_value = net_name + mock_err_exit.return_value = 0 VirtualEnvironmentInst = VirtualEnvironment( deploy_virtual_struct, net_struct, adapter_virtual, pxe_bridge_virtual, daisy_server, work_dir, storage_dir, scenario) VirtualEnvironmentInst.create_daisy_server_network() - environment.create_virtual_network.assert_called_once_with(VMDEPLOY_DAISY_SERVER_NET) - assert VirtualEnvironmentInst._daisy_server_net == pxe_bridge_virtual + mock_create_virtual_network.assert_called_once_with(VMDEPLOY_DAISY_SERVER_NET) + if net_name == pxe_bridge_virtual: + mock_err_exit.assert_not_called() + assert VirtualEnvironmentInst._daisy_server_net == pxe_bridge_virtual + elif net_name == pxe_bridge: + mock_err_exit.assert_called_once() tmpdir.remove() -- cgit 1.2.3-korg