From 81f5a26699ced0d7f254d05705e8c028471e44d1 Mon Sep 17 00:00:00 2001 From: zhongjun Date: Fri, 8 Sep 2017 17:27:26 +0800 Subject: Add the pytest file test_deploy.py 1.add the pytest file test_deploy.py 2.fix the tmpfile clearup issue. Change-Id: I3a7e0f9199f1bf518b332fd3e9884c8f084575ae Signed-off-by: zhongjun --- tests/unit/post/test_post_execute.py | 6 +- tests/unit/test_deploy.py | 112 +++++++++++++++++++++++++++++++++++ tests/unit/test_utils.py | 9 +-- 3 files changed, 120 insertions(+), 7 deletions(-) create mode 100644 tests/unit/test_deploy.py (limited to 'tests/unit') diff --git a/tests/unit/post/test_post_execute.py b/tests/unit/post/test_post_execute.py index d614360c..c06bc085 100644 --- a/tests/unit/post/test_post_execute.py +++ b/tests/unit/post/test_post_execute.py @@ -110,14 +110,14 @@ def openrc_conf_file_dir(data_root): ('globals.yml'), ('globals_odl.yml')]) def test__config_kolla_admin_openrc(globals_file_name, openrc_conf_file_dir, tmpdir): src_globals_file_path = os.path.join(openrc_conf_file_dir, globals_file_name) - dst_globals_file_path = os.path.join(tmpdir.dirname, 'globals.yml') + dst_globals_file_path = os.path.join(tmpdir.dirname, tmpdir.basename, 'globals.yml') shutil.copyfile(src_globals_file_path, dst_globals_file_path) src_openrc_file_path = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') - dst_openrc_file_path = os.path.join(tmpdir.dirname, 'admin-openrc.sh') + dst_openrc_file_path = os.path.join(tmpdir.dirname, tmpdir.basename, 'admin-openrc.sh') shutil.copyfile(src_openrc_file_path, dst_openrc_file_path) - _config_kolla_admin_openrc(tmpdir.dirname) + _config_kolla_admin_openrc(os.path.join(tmpdir.dirname, tmpdir.basename)) src_openrc_lines = open(src_openrc_file_path, 'r').readlines() dst_openrc_lines = open(dst_openrc_file_path, 'r').readlines() if globals_file_name == 'globals.yml': diff --git a/tests/unit/test_deploy.py b/tests/unit/test_deploy.py new file mode 100644 index 00000000..fe796800 --- /dev/null +++ b/tests/unit/test_deploy.py @@ -0,0 +1,112 @@ +############################################################################## +# 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 +import argparse +import os +import sys +import yaml +import mock +sys.modules['libvirt'] = mock.Mock() + +from deploy.deploy import ( + config_arg_parser, + DaisyDeployment +) # noqa: ignore=E402 + + +def test_config_arg_parser(): + parser = config_arg_parser() + assert isinstance(parser, argparse.ArgumentParser) + + +@pytest.fixture(scope="session") +def conf_file_dir(data_root): + return os.path.join(data_root, 'lab_conf') + + +@pytest.mark.parametrize('kwargs, expect_dasiy_info', [ + ({'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': 'pxebr', + 'deploy_log': 'deploy.log', + 'scenario': 'os-nosdn-nofeature-ha'}, + {'name': 'daisy', + 'image': 'daisy.qcow2', + 'address': '10.20.11.2', + 'gateway': '10.20.11.1', + 'password': 'r00tme', + 'disk_size': 50}), + ({'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'}, + {'name': 'daisy', + 'image': 'daisy.qcow2', + 'address': '10.20.0.2', + 'gateway': '10.20.0.1', + 'password': 'r00tme', + 'disk_size': 50})]) +def test_create_DaisyDeployment_instance(kwargs, expect_dasiy_info, conf_file_dir, tmpdir): + 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) + + deploy = DaisyDeployment(**kwargs) + assert (deploy.lab_name, deploy.pod_name, deploy.src_deploy_file, deploy.net_file, deploy.bin_file, + deploy.daisy_only, deploy.cleanup_only, deploy.remote_dir, deploy.work_dir, deploy.storage_dir, + deploy.deploy_log, deploy.scenario) == \ + (kwargs['lab_name'], kwargs['pod_name'], kwargs['deploy_file'], kwargs['net_file'], + kwargs['bin_file'], kwargs['daisy_only'], kwargs['cleanup_only'], kwargs['remote_dir'], + kwargs['work_dir'], kwargs['storage_dir'], kwargs['deploy_log'], kwargs['scenario']) + + assert deploy.deploy_file_name == 'final_deploy.yml' + assert deploy.deploy_file == os.path.join(deploy.work_dir, 'final_deploy.yml') + assert os.path.isfile(os.path.join(deploy.work_dir, 'final_deploy.yml')) + + if not deploy.cleanup_only: + assert deploy.net_file_name == os.path.basename(kwargs['net_file']) + with open(deploy.net_file) as yaml_file: + expected_net_struct = yaml.safe_load(yaml_file) + assert expected_net_struct == deploy.net_struct + else: + assert deploy.net_struct is None + + if 'virtual' in kwargs['deploy_file']: + assert (deploy.adapter == 'libvirt' and deploy.pxe_bridge == 'daisy1') + + else: + assert (deploy.adapter == 'ipmi' and deploy.pxe_bridge == 'br7') + + expect_dasiy_info['image'] = os.path.join(kwargs['storage_dir'], expect_dasiy_info['image']) + assert deploy.daisy_server_info == expect_dasiy_info + + tmpdir.remove() diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index a6d84de0..d6095c7f 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -23,7 +23,7 @@ from deploy.utils import ( ('exist_file')]) def test_check_file_exists(tmpdir, test_file_name): try: - file_path = os.path.join(tmpdir.dirname, test_file_name) + file_path = os.path.join(tmpdir.dirname, tmpdir.basename, test_file_name) if test_file_name == 'exist_file': os.mknod(file_path) check_file_exists(file_path) @@ -43,7 +43,7 @@ def test_check_file_exists(tmpdir, test_file_name): ('exe_file')]) def test_make_file_executable(tmpdir, test_file_name): try: - file_path = os.path.join(tmpdir.dirname, test_file_name) + file_path = os.path.join(tmpdir.dirname, tmpdir.basename, test_file_name) if test_file_name == 'no_exe_file': os.mknod(file_path) if test_file_name == 'exe_file': @@ -66,9 +66,10 @@ def test_make_file_executable(tmpdir, test_file_name): ('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') + dir_path = os.path.join(tmpdir.dirname, tmpdir.basename, 'no_exist_dir') if test_dir_name == 'exist_dir': - dir_path = tmpdir.mkdir('exist_dir').dirname + tmpsubdir = tmpdir.mkdir('exist_dir') + dir_path = os.path.join(tmpsubdir.dirname, tmpsubdir.basename) confirm_dir_exists(dir_path) assert os.path.isdir(dir_path) tmpdir.remove() -- cgit 1.2.3-korg