diff options
author | zhongjun <zhong.jun@zte.com.cn> | 2017-09-14 19:30:05 +0800 |
---|---|---|
committer | zhongjun <zhong.jun@zte.com.cn> | 2017-09-18 15:48:50 +0800 |
commit | 42f3692a33504ed715eab9b1bef77e1737b1bfed (patch) | |
tree | d1fc60dac29f1accef5a0adc88863d309317d87d | |
parent | 81f5a26699ced0d7f254d05705e8c028471e44d1 (diff) |
Add the pytest file test_environment.py
1.Add the pytest file test_enviroment.py
2.Add the pytest file test_daisy_server.py
Change-Id: If679c3a7279a915d078dd3008bcfc56be6dccc3e
Signed-off-by: zhongjun <zhong.jun@zte.com.cn>
-rw-r--r-- | tests/unit/post/test_keystoneauth.py | 22 | ||||
-rw-r--r-- | tests/unit/test_daisy_server.py | 346 | ||||
-rw-r--r-- | tests/unit/test_environment.py | 385 | ||||
-rw-r--r-- | tests/unit/test_utils.py | 19 |
4 files changed, 771 insertions, 1 deletions
diff --git a/tests/unit/post/test_keystoneauth.py b/tests/unit/post/test_keystoneauth.py index afd9062a..1ae86452 100644 --- a/tests/unit/post/test_keystoneauth.py +++ b/tests/unit/post/test_keystoneauth.py @@ -80,3 +80,25 @@ def test_session(openrc_conf_file_dir): openrc = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') KeystoneClient = Keystoneauth(openrc) assert KeystoneClient.session + + +@pytest.mark.parametrize('openrc_file_name, expected', [ + ( + 'admin-openrc.sh', + { + 'OS_PROJECT_DOMAIN_NAME': 'Default', + 'OS_USER_DOMAIN_NAME': 'Default', + 'OS_PROJECT_NAME': 'admin', + 'OS_TENANT_NAME': 'admin', + 'OS_USERNAME': 'admin', + 'OS_PASSWORD': 'keystone', + 'OS_AUTH_URL': 'http://10.20.11.11:35357/v3', + 'OS_INTERFACE': 'internal', + 'OS_IDENTITY_API_VERSION': '3' + } + )]) +def test__parse_openrc(openrc_conf_file_dir, openrc_file_name, expected): + openrc = os.path.join(openrc_conf_file_dir, openrc_file_name) + KeystoneClient = Keystoneauth(openrc) + ret_openrc_dict = KeystoneClient._parse_openrc() + assert expected == ret_openrc_dict diff --git a/tests/unit/test_daisy_server.py b/tests/unit/test_daisy_server.py new file mode 100644 index 00000000..ada3c96d --- /dev/null +++ b/tests/unit/test_daisy_server.py @@ -0,0 +1,346 @@ +import os +import pytest +import mock + +from deploy import daisy_server +from deploy.daisy_server import ( + DaisyServer +) + + +@pytest.fixture(scope="session") +def conf_file_dir(data_root): + return os.path.join(data_root, 'lab_conf') + + +daisy_server_info = { + 'name': 'daisy', + 'image': 'daisy.qcow2', + 'address': '10.20.0.2', + 'gateway': '10.20.0.1', + 'password': 'r00tme', + 'disk_size': 50 +} +adapter = 'ipmi' +scenario = 'os-odl-nofeature-ha' +deploy_file_name = 'final_deploy.yml' +net_file_name = 'network_baremetal.yml' +remote_dir = '/home/daisy' +bin_file_name = 'opnfv.bin' + + +def test_create_DaisyServer_instance(tmpdir): + bin_file = os.path.join(tmpdir.dirname, tmpdir.basename, bin_file_name) + DaisyServerInst = DaisyServer(daisy_server_info['name'], + daisy_server_info['address'], + daisy_server_info['password'], + remote_dir, + bin_file, + adapter, + scenario, + deploy_file_name, + net_file_name) + assert (DaisyServerInst.name == daisy_server_info['name'] and + DaisyServerInst.address == daisy_server_info['address'] and + DaisyServerInst.password == daisy_server_info['password'] and + DaisyServerInst.remote_dir == remote_dir and + DaisyServerInst.bin_file == bin_file and + DaisyServerInst.adapter == adapter and + DaisyServerInst.scenario == scenario and + DaisyServerInst.deploy_file_name == deploy_file_name and + DaisyServerInst.net_file_name == net_file_name and + DaisyServerInst.ssh_client is None) + tmpdir.remove() + + +@mock.patch.object(daisy_server.DaisyServer, 'ssh_exec_cmd') +def test_create_dir_DaisyServer(mock_ssh_exec_cmd, tmpdir): + remote_dir_test = '/home/daisy/test' + cmd = 'mkdir -p ' + remote_dir_test + bin_file = os.path.join(tmpdir.dirname, tmpdir.basename, bin_file_name) + DaisyServerInst = DaisyServer(daisy_server_info['name'], + daisy_server_info['address'], + daisy_server_info['password'], + remote_dir, + bin_file, + adapter, + scenario, + deploy_file_name, + net_file_name) + DaisyServerInst.create_dir(remote_dir_test) + DaisyServerInst.ssh_exec_cmd.assert_called_once_with(cmd) + tmpdir.remove() + + +@mock.patch.object(daisy_server.DaisyServer, 'ssh_exec_cmd') +def test_delete_dir_DaisyServer(mock_ssh_exec_cmd, tmpdir): + remote_dir_test = '/home/daisy/test' + cmd = 'if [[ -f {DIR} || -d {DIR} ]]; then rm -fr {DIR}; fi'.format(DIR=remote_dir_test) + bin_file = os.path.join(tmpdir.dirname, tmpdir.basename, bin_file_name) + DaisyServerInst = DaisyServer(daisy_server_info['name'], + daisy_server_info['address'], + daisy_server_info['password'], + remote_dir, + bin_file, + adapter, + scenario, + deploy_file_name, + net_file_name) + DaisyServerInst.delete_dir(remote_dir_test) + DaisyServerInst.ssh_exec_cmd.assert_called_once_with(cmd) + tmpdir.remove() + + +@mock.patch.object(daisy_server.DaisyServer, 'delete_dir') +@mock.patch.object(daisy_server.DaisyServer, 'scp_put') +@mock.patch.object(daisy_server.DaisyServer, 'create_dir') +@mock.patch('deploy.daisy_server.update_config') +def test_prepare_files_DaisyServer(mock_update_config, + mock_create_dir, + mock_scp_put, + mock_delete_dir, + tmpdir): + bin_file = os.path.join(tmpdir.dirname, tmpdir.basename, bin_file_name) + DaisyServerInst = DaisyServer(daisy_server_info['name'], + daisy_server_info['address'], + daisy_server_info['password'], + remote_dir, + bin_file, + adapter, + scenario, + deploy_file_name, + net_file_name) + DaisyServerInst.prepare_files() + DaisyServerInst.delete_dir.assert_called_once_with(remote_dir) + DaisyServerInst.create_dir.assert_called_once_with('/home/daisy_install') + assert DaisyServerInst.scp_put.call_count == 3 + tmpdir.remove() + + +@mock.patch.object(daisy_server.DaisyServer, 'ssh_run') +@mock.patch.object(daisy_server.DaisyServer, 'prepare_files') +def test_install_daisy_DaisyServer(mock_prepare_files, mock_ssh_run, tmpdir): + bin_file = os.path.join(tmpdir.dirname, tmpdir.basename, bin_file_name) + DaisyServerInst = DaisyServer(daisy_server_info['name'], + daisy_server_info['address'], + daisy_server_info['password'], + remote_dir, + bin_file, + adapter, + scenario, + deploy_file_name, + net_file_name) + mock_ssh_run.return_value = 0 + cmd = '%s install' % os.path.join(remote_dir, 'opnfv.bin') + DaisyServerInst.install_daisy() + DaisyServerInst.ssh_run.assert_called_once_with(cmd) + DaisyServerInst.prepare_files.assert_called_once_with() + tmpdir.remove() + + +@mock.patch.object(daisy_server.DaisyServer, 'ssh_run') +def test_prepare_configurations_DaisyServer(mock_ssh_run, tmpdir): + bin_file = os.path.join(tmpdir.dirname, tmpdir.basename, bin_file_name) + adapter = 'libvirt' + DaisyServerInst = DaisyServer(daisy_server_info['name'], + daisy_server_info['address'], + daisy_server_info['password'], + remote_dir, + bin_file, + adapter, + scenario, + deploy_file_name, + net_file_name) + mock_ssh_run.return_value = 0 + cmd = 'export PYTHONPATH={python_path}; python {script} -nw {net_file} -b {is_bare}'.format( + python_path=remote_dir, + script=os.path.join(remote_dir, 'deploy/prepare/execute.py'), + net_file=os.path.join(remote_dir, net_file_name), + is_bare=1 if adapter == 'ipmi' else 0) + DaisyServerInst.prepare_configurations() + DaisyServerInst.ssh_run.assert_called_once_with(cmd) + tmpdir.remove() + + +@mock.patch.object(daisy_server.DaisyServer, 'scp_put') +@mock.patch.object(daisy_server.DaisyServer, 'prepare_configurations') +@mock.patch.object(daisy_server.DaisyServer, 'ssh_run') +def test_prepare_cluster_DaisyServer(mock_scp_put, + mock_prepare_configurations, + mock_ssh_run, + tmpdir): + bin_file = os.path.join(tmpdir.dirname, tmpdir.basename, bin_file_name) + DaisyServerInst = DaisyServer(daisy_server_info['name'], + daisy_server_info['address'], + daisy_server_info['password'], + remote_dir, + bin_file, + adapter, + scenario, + deploy_file_name, + net_file_name) + mock_ssh_run.return_value = 0 + cmd = "python {script} --dha {deploy_file} --network {net_file} --cluster \'yes\'".format( + script=os.path.join(remote_dir, 'deploy/tempest.py'), + deploy_file=os.path.join(remote_dir, deploy_file_name), + net_file=os.path.join(remote_dir, net_file_name)) + deploy_file = os.path.join(tmpdir.dirname, tmpdir.basename, deploy_file_name) + net_file = os.path.join(tmpdir.dirname, tmpdir.basename, net_file_name) + DaisyServerInst.prepare_cluster(deploy_file, net_file) + DaisyServerInst.ssh_run.assert_called_once_with(cmd, check=True) + DaisyServerInst.prepare_configurations.assert_called_once_with() + assert DaisyServerInst.scp_put.call_count == 2 + tmpdir.remove() + + +@mock.patch.object(daisy_server.DaisyServer, 'scp_put') +def test_copy_new_deploy_config_DaisyServer(mock_scp_put, tmpdir): + bin_file = os.path.join(tmpdir.dirname, tmpdir.basename, bin_file_name) + adapter = 'libvirt' + DaisyServerInst = DaisyServer(daisy_server_info['name'], + daisy_server_info['address'], + daisy_server_info['password'], + remote_dir, + bin_file, + adapter, + scenario, + deploy_file_name, + net_file_name) + mock_scp_put.return_value = 0 + data = { + 'adapter': 'ipmi', + 'hosts': [ + { + 'name': 'controller01', + 'roles': ['CONTROLLER_LB'], + 'ipmi_ip': '192.168.1.11', + 'ipmi_user': 'testuser', + 'ipmi_pass': 'testpass' + } + ], + 'disks': { + 'daisy': 50 + }, + 'daisy_passwd': 'r00tme' + } + DaisyServerInst.copy_new_deploy_config(data) + assert DaisyServerInst.scp_put.call_count == 1 + tmpdir.remove() + + +@mock.patch.object(daisy_server.DaisyServer, 'ssh_run') +def test_prepare_host_and_pxe_DaisyServer(mock_ssh_run, tmpdir): + bin_file = os.path.join(tmpdir.dirname, tmpdir.basename, bin_file_name) + adapter = 'libvirt' + DaisyServerInst = DaisyServer(daisy_server_info['name'], + daisy_server_info['address'], + daisy_server_info['password'], + remote_dir, + bin_file, + adapter, + scenario, + deploy_file_name, + net_file_name) + mock_ssh_run.return_value = 0 + cmd = "python {script} --dha {deploy_file} --network {net_file} --host \'yes\' --isbare {is_bare} --scenario {scenario}".format( + script=os.path.join(remote_dir, 'deploy/tempest.py'), + deploy_file=os.path.join(remote_dir, deploy_file_name), + net_file=os.path.join(remote_dir, net_file_name), + is_bare=1 if adapter == 'ipmi' else 0, + scenario=scenario) + DaisyServerInst.prepare_host_and_pxe() + DaisyServerInst.ssh_run.assert_called_once_with(cmd, check=True) + tmpdir.remove() + + +@mock.patch.object(daisy_server.DaisyServer, 'ssh_run') +def test_install_virtual_nodes_DaisyServer(mock_ssh_run, tmpdir): + bin_file = os.path.join(tmpdir.dirname, tmpdir.basename, bin_file_name) + adapter = 'libvirt' + DaisyServerInst = DaisyServer(daisy_server_info['name'], + daisy_server_info['address'], + daisy_server_info['password'], + remote_dir, + bin_file, + adapter, + scenario, + deploy_file_name, + net_file_name) + mock_ssh_run.return_value = 0 + cmd = "python {script} --dha {deploy_file} --network {net_file} --install \'yes\'".format( + script=os.path.join(remote_dir, 'deploy/tempest.py'), + deploy_file=os.path.join(remote_dir, deploy_file_name), + net_file=os.path.join(remote_dir, net_file_name)) + DaisyServerInst.install_virtual_nodes() + DaisyServerInst.ssh_run.assert_called_once_with(cmd, check=True) + tmpdir.remove() + + +@mock.patch.object(daisy_server.DaisyServer, 'ssh_run') +def test_check_os_installation_DaisyServer(mock_ssh_run, tmpdir): + bin_file = os.path.join(tmpdir.dirname, tmpdir.basename, bin_file_name) + adapter = 'libvirt' + DaisyServerInst = DaisyServer(daisy_server_info['name'], + daisy_server_info['address'], + daisy_server_info['password'], + remote_dir, + bin_file, + adapter, + scenario, + deploy_file_name, + net_file_name) + mock_ssh_run.return_value = 0 + nodes_num = 5 + cmd = '{script} -d {is_bare} -n {nodes_num}'.format( + script=os.path.join(remote_dir, 'deploy/check_os_progress.sh'), + is_bare=1 if adapter == 'ipmi' else 0, + nodes_num=nodes_num) + DaisyServerInst.check_os_installation(nodes_num) + DaisyServerInst.ssh_run.assert_called_once_with(cmd, check=True) + tmpdir.remove() + + +@mock.patch.object(daisy_server.DaisyServer, 'ssh_run') +def test_check_openstack_installation_DaisyServer(mock_ssh_run, tmpdir): + bin_file = os.path.join(tmpdir.dirname, tmpdir.basename, bin_file_name) + adapter = 'libvirt' + DaisyServerInst = DaisyServer(daisy_server_info['name'], + daisy_server_info['address'], + daisy_server_info['password'], + remote_dir, + bin_file, + adapter, + scenario, + deploy_file_name, + net_file_name) + mock_ssh_run.return_value = 0 + nodes_num = 5 + cmd = '{script} -n {nodes_num}'.format( + script=os.path.join(remote_dir, 'deploy/check_openstack_progress.sh'), + nodes_num=nodes_num) + DaisyServerInst.check_openstack_installation(nodes_num) + DaisyServerInst.ssh_run.assert_called_once_with(cmd, check=True) + tmpdir.remove() + + +@mock.patch.object(daisy_server.DaisyServer, 'ssh_run') +def test_post_deploy_DaisyServer(mock_ssh_run, tmpdir): + bin_file = os.path.join(tmpdir.dirname, tmpdir.basename, bin_file_name) + adapter = 'libvirt' + DaisyServerInst = DaisyServer(daisy_server_info['name'], + daisy_server_info['address'], + daisy_server_info['password'], + remote_dir, + bin_file, + adapter, + scenario, + deploy_file_name, + net_file_name) + mock_ssh_run.return_value = 0 + cmd = 'export PYTHONPATH={python_path}; python {script} -nw {net_file}'.format( + python_path=remote_dir, + script=os.path.join(remote_dir, 'deploy/post/execute.py'), + net_file=os.path.join(remote_dir, net_file_name)) + DaisyServerInst.post_deploy() + DaisyServerInst.ssh_run.assert_called_once_with(cmd, check=False) + tmpdir.remove() diff --git a/tests/unit/test_environment.py b/tests/unit/test_environment.py new file mode 100644 index 00000000..5545fc58 --- /dev/null +++ b/tests/unit/test_environment.py @@ -0,0 +1,385 @@ +import os +import copy +import mock +import yaml +from deepdiff import DeepDiff + +from deploy.utils import WORKSPACE +from deploy import environment +from deploy.environment import ( + DaisyEnvironmentBase, + BareMetalEnvironment, + VirtualEnvironment, + BMDEPLOY_DAISY_SERVER_VM, + VMDEPLOY_DAISY_SERVER_NET, + VMDEPLOY_DAISY_SERVER_VM, +) + + +def get_conf_file_dir(): + return os.path.join(WORKSPACE, 'tests/data/lab_conf') + + +def get_conf_info_from_file(file_dir, conf_file_name): + conf_file_path = os.path.join(file_dir, conf_file_name) + with open(conf_file_path) as f: + conf_info = yaml.safe_load(f) + return conf_info + + +deploy_struct = get_conf_info_from_file(get_conf_file_dir(), 'deploy_baremetal.yml') +deploy_virtual_struct = get_conf_info_from_file(get_conf_file_dir(), 'deploy_virtual1.yml') +net_struct = get_conf_info_from_file(get_conf_file_dir(), 'network_baremetal.yml') +adapter = 'ipmi' +adapter_virtual = 'libvirt' +pxe_bridge = 'br7' +pxe_bridge_virtual = 'daisy1' +daisy_server_info = { + 'name': 'daisy', + 'image': 'daisy.qcow2', + 'address': '10.20.0.2', + 'gateway': '10.20.0.1', + 'password': 'r00tme', + 'disk_size': 50 +} +work_dir_name = 'workdir' +storage_dir_name = 'vms' +scenario = 'os-odl-nofeature-ha' + + +def test_create_DaisyEnvironmentBase_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) + daisy_server = copy.deepcopy(daisy_server_info) + daisy_server['image'] = os.path.join(storage_dir, daisy_server['image']) + DaisyEnvBaseInst = DaisyEnvironmentBase( + deploy_struct, net_struct, adapter, pxe_bridge, + daisy_server, work_dir, storage_dir, scenario) + assert (DaisyEnvBaseInst.deploy_struct == deploy_struct and + DaisyEnvBaseInst.net_struct == net_struct and + DaisyEnvBaseInst.adapter == adapter and + DaisyEnvBaseInst.pxe_bridge == pxe_bridge and + DaisyEnvBaseInst.daisy_server_info == daisy_server and + DaisyEnvBaseInst.work_dir == work_dir and + DaisyEnvBaseInst.storage_dir == storage_dir and + DaisyEnvBaseInst.scenario == scenario) + tmpdir.remove() + + +def test_delete_daisy_server_DaisyEnvironmentBase(tmpdir, mocker): + 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']) + DaisyEnvBaseInst = DaisyEnvironmentBase( + deploy_struct, net_struct, adapter, pxe_bridge, + daisy_server, work_dir, storage_dir, scenario) + mocker.patch('deploy.environment.delete_vm_and_disk') + DaisyEnvBaseInst.delete_daisy_server() + environment.delete_vm_and_disk.assert_called_once_with('daisy') + tmpdir.remove() + + +def test_create_daisy_server_image_DaisyEnvironmentBase(tmpdir, monkeypatch): + 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']) + DaisyEnvBaseInst = DaisyEnvironmentBase( + deploy_struct, net_struct, adapter, pxe_bridge, + daisy_server, work_dir, storage_dir, scenario) + + 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']) + tmpdir.remove() + + +def test_create_BareMetalEnvironment_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) + daisy_server = copy.deepcopy(daisy_server_info) + daisy_server['image'] = os.path.join(storage_dir, daisy_server['image']) + BareMetalEnvironmentInst = BareMetalEnvironment( + deploy_struct, net_struct, adapter, pxe_bridge, + daisy_server, work_dir, storage_dir, scenario) + assert (BareMetalEnvironmentInst.deploy_struct == deploy_struct and + BareMetalEnvironmentInst.net_struct == net_struct and + BareMetalEnvironmentInst.adapter == adapter and + BareMetalEnvironmentInst.pxe_bridge == pxe_bridge and + BareMetalEnvironmentInst.daisy_server_info == daisy_server and + BareMetalEnvironmentInst.work_dir == work_dir and + BareMetalEnvironmentInst.storage_dir == storage_dir and + BareMetalEnvironmentInst.scenario == scenario) + tmpdir.remove() + + +@mock.patch.object(environment.DaisyEnvironmentBase, 'delete_daisy_server') +def test_delete_old_environment_BareMetalEnvironment(mock_delete_daisy_server, 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_delete_daisy_server.return_value = None + BareMetalEnvironmentInst = BareMetalEnvironment( + deploy_struct, net_struct, adapter, pxe_bridge, + daisy_server, work_dir, storage_dir, scenario) + BareMetalEnvironmentInst.delete_old_environment() + BareMetalEnvironmentInst.delete_daisy_server.assert_called_once_with() + tmpdir.remove() + + +def test_create_daisy_server_vm_BareMetalEnvironment(mocker, 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']) + mocker.patch('deploy.environment.create_vm') + BareMetalEnvironmentInst = BareMetalEnvironment( + deploy_struct, net_struct, adapter, pxe_bridge, + daisy_server, work_dir, storage_dir, scenario) + BareMetalEnvironmentInst.create_daisy_server_vm() + environment.create_vm.assert_called_once_with(BMDEPLOY_DAISY_SERVER_VM, + name=daisy_server['name'], + disks=[daisy_server['image']]) + tmpdir.remove() + + +@mock.patch('deploy.environment.ipmi_reboot_node') +def test_reboot_nodes_BareMetalEnvironment(mock_ipmi_reboot_node, 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 + BareMetalEnvironmentInst = BareMetalEnvironment( + deploy_struct, net_struct, adapter, pxe_bridge, + daisy_server, work_dir, storage_dir, scenario) + BareMetalEnvironmentInst.reboot_nodes() + assert environment.ipmi_reboot_node.call_count == 5 + tmpdir.remove() + + +@mock.patch.object(environment.DaisyEnvironmentBase, 'create_daisy_server_image') +@mock.patch.object(environment.BareMetalEnvironment, 'create_daisy_server_vm') +def test_create_daisy_server_BareMetalEnvironment(mock_create_daisy_server_vm, mock_create_daisy_server_image, 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_daisy_server_vm.return_value = None + mock_create_daisy_server_image.return_value = None + BareMetalEnvironmentInst = BareMetalEnvironment( + deploy_struct, net_struct, adapter, pxe_bridge, + daisy_server, work_dir, storage_dir, scenario) + BareMetalEnvironmentInst.create_daisy_server() + BareMetalEnvironmentInst.create_daisy_server_image.assert_called_once_with() + BareMetalEnvironmentInst.create_daisy_server_vm.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) + daisy_server = copy.deepcopy(daisy_server_info) + daisy_server['image'] = os.path.join(storage_dir, daisy_server['image']) + VirtualEnvironmentInst = VirtualEnvironment( + deploy_virtual_struct, net_struct, adapter_virtual, pxe_bridge_virtual, + daisy_server, work_dir, storage_dir, scenario) + assert (DeepDiff(VirtualEnvironmentInst.deploy_struct, deploy_virtual_struct, ignore_order=True) == {} and + VirtualEnvironmentInst.net_struct == net_struct and + VirtualEnvironmentInst.adapter == adapter_virtual and + VirtualEnvironmentInst.pxe_bridge == pxe_bridge_virtual and + VirtualEnvironmentInst.daisy_server_info == daisy_server and + VirtualEnvironmentInst.work_dir == work_dir and + VirtualEnvironmentInst.storage_dir == storage_dir and + VirtualEnvironmentInst.scenario == scenario and + VirtualEnvironmentInst._daisy_server_net is None and + VirtualEnvironmentInst._daisy_os_net is None and + VirtualEnvironmentInst._daisy_keepalived_net is None) + tmpdir.remove() + + +@mock.patch('deploy.environment.create_virtual_network') +def test_create_daisy_server_network_VirtualEnvironment(mock_create_virtual_network, 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 + 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 + tmpdir.remove() + + +@mock.patch('deploy.environment.create_vm') +def test_create_daisy_server_vm_VirtualEnvironment(mock_create_vm, 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_vm.return_value = True + VirtualEnvironmentInst = VirtualEnvironment( + deploy_virtual_struct, net_struct, adapter_virtual, pxe_bridge_virtual, + daisy_server, work_dir, storage_dir, scenario) + VirtualEnvironmentInst.create_daisy_server_vm() + environment.create_vm.assert_called_once_with(VMDEPLOY_DAISY_SERVER_VM, + name=daisy_server['name'], + disks=[daisy_server['image']]) + + +@mock.patch.object(environment.DaisyEnvironmentBase, 'create_daisy_server_image') +@mock.patch.object(environment.VirtualEnvironment, 'create_daisy_server_network') +@mock.patch.object(environment.VirtualEnvironment, 'create_daisy_server_vm') +def test_create_daisy_server_VirtualEnvironment(mock_create_daisy_server_vm, + mock_create_daisy_server_network, + mock_create_daisy_server_image, + 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']) + VirtualEnvironmentInst = VirtualEnvironment( + deploy_virtual_struct, net_struct, adapter_virtual, pxe_bridge_virtual, + daisy_server, work_dir, storage_dir, scenario) + VirtualEnvironmentInst.create_daisy_server() + VirtualEnvironmentInst.create_daisy_server_vm.assert_called_once_with() + VirtualEnvironmentInst.create_daisy_server_network.assert_called_once_with() + VirtualEnvironmentInst.create_daisy_server_image.assert_called_once_with() + tmpdir.remove() + + +@mock.patch('deploy.environment.create_vm') +@mock.patch('deploy.environment.create_virtual_disk') +def test_create_virtual_node_VirtualEnvironment(mock_create_virtual_disk, + mock_create_vm, + 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']) + VirtualEnvironmentInst = VirtualEnvironment( + deploy_virtual_struct, net_struct, adapter_virtual, pxe_bridge_virtual, + daisy_server, work_dir, storage_dir, scenario) + VirtualEnvironmentInst.create_virtual_node(deploy_virtual_struct['hosts'][0]) + environment.create_virtual_disk.call_count == 2 + name = deploy_virtual_struct['hosts'][0]['name'] + template = deploy_virtual_struct['hosts'][0]['template'] + files = [os.path.join(storage_dir, name + '.qcow2'), os.path.join(storage_dir, name + '_data.qcow2')] + environment.create_vm.assert_called_once_with(template, name, files) + tmpdir.remove() + + +@mock.patch('deploy.environment.create_virtual_network') +@mock.patch('deploy.environment.get_vm_mac_addresses') +@mock.patch.object(environment.VirtualEnvironment, 'create_virtual_node') +def test_create_nodes_VirtualEnvironment(mock_create_virtual_node, + mock_get_vm_mac_addresses, + mock_create_virtual_network, + tmpdir): + keepalived_net_name = 'daisy3' + 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 = keepalived_net_name + mock_get_vm_mac_addresses.return_value = '02:42:b6:90:7c:b2' + mock_create_virtual_node.return_value = 'test-domain' + + VirtualEnvironmentInst = VirtualEnvironment( + deploy_virtual_struct, net_struct, adapter_virtual, pxe_bridge_virtual, + daisy_server, work_dir, storage_dir, scenario) + VirtualEnvironmentInst.create_nodes() + assert (environment.create_virtual_network.call_count == 2 and + environment.get_vm_mac_addresses.call_count == 5 and + VirtualEnvironmentInst.create_virtual_node.call_count == 5) + environment.get_vm_mac_addresses.assert_called_with('test-domain') + assert VirtualEnvironmentInst._daisy_keepalived_net == keepalived_net_name + tmpdir.remove() + + +@mock.patch('deploy.environment.delete_vm_and_disk') +def test_delete_nodes_VirtualEnvironment(mock_delete_vm_and_disk, 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']) + VirtualEnvironmentInst = VirtualEnvironment( + deploy_virtual_struct, net_struct, adapter_virtual, pxe_bridge_virtual, + daisy_server, work_dir, storage_dir, scenario) + VirtualEnvironmentInst.delete_nodes() + assert environment.delete_vm_and_disk.call_count == 5 + + +@mock.patch('deploy.environment.reboot_vm') +def test_reboot_nodes_VirtualEnvironment(mock_reboot_vm, 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']) + VirtualEnvironmentInst = VirtualEnvironment( + deploy_virtual_struct, net_struct, adapter_virtual, pxe_bridge_virtual, + daisy_server, work_dir, storage_dir, scenario) + VirtualEnvironmentInst.reboot_nodes() + assert environment.reboot_vm.call_count == 5 + + +@mock.patch('deploy.environment.delete_virtual_network') +def test_delete_networks_VirtualEnvironment(mock_delete_virtual_network, 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']) + VirtualEnvironmentInst = VirtualEnvironment( + deploy_virtual_struct, net_struct, adapter_virtual, pxe_bridge_virtual, + daisy_server, work_dir, storage_dir, scenario) + VirtualEnvironmentInst.delete_networks() + assert environment.delete_virtual_network.call_count == 3 + + +@mock.patch.object(environment.DaisyEnvironmentBase, 'delete_daisy_server') +@mock.patch.object(environment.VirtualEnvironment, 'delete_networks') +@mock.patch.object(environment.VirtualEnvironment, 'delete_nodes') +def test_delete_old_environment_VirtualEnvironment(mock_delete_daisy_server, + mock_delete_networks, + mock_delete_nodes, + 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']) + VirtualEnvironmentInst = VirtualEnvironment( + deploy_virtual_struct, net_struct, adapter_virtual, pxe_bridge_virtual, + daisy_server, work_dir, storage_dir, scenario) + VirtualEnvironmentInst.delete_old_environment() + VirtualEnvironmentInst.delete_daisy_server.assert_called_once_with() + VirtualEnvironmentInst.delete_networks.assert_called_once_with() + VirtualEnvironmentInst.delete_nodes.assert_called_once_with() + tmpdir.remove() + + +@mock.patch('deploy.environment.commands.getstatusoutput') +def test_post_deploy_VirtualEnvironment(mock_getstatusoutput, 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_getstatusoutput.return_value = (0, 'sucess') + VirtualEnvironmentInst = VirtualEnvironment( + deploy_virtual_struct, net_struct, adapter_virtual, pxe_bridge_virtual, + daisy_server, work_dir, storage_dir, scenario) + VirtualEnvironmentInst._post_deploy() + VirtualEnvironmentInst._daisy_server_net = 'daisy1' + VirtualEnvironmentInst._daisy_os_net = 'daisy2' + assert environment.commands.getstatusoutput.call_count == 4 diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index d6095c7f..d5cd6104 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -7,10 +7,13 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## import os - +import mock import pytest +from deploy import utils from deploy.utils import ( + err_exit, + check_sudo_privilege, check_file_exists, make_file_executable, confirm_dir_exists, @@ -18,6 +21,20 @@ from deploy.utils import ( ) +def test_err_exit(): + message = 'test error msg!' + with pytest.raises(SystemExit): + err_exit(message) + + +@mock.patch('deploy.utils.err_exit') +@mock.patch('deploy.utils.os.getuid') +def test_check_sudo_privilege(mock_getuid, mock_err_exit): + mock_getuid.return_value = 1 + check_sudo_privilege() + utils.err_exit.assert_called_once_with('You need run this script with sudo privilege') + + @pytest.mark.parametrize('test_file_name', [ ('no_exist_file'), ('exist_file')]) |