diff options
29 files changed, 4657 insertions, 150 deletions
diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 000000000..fe258c6c6 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,3 @@ +[report] +exclude_lines = + if __name__ == .__main__.: @@ -12,7 +12,7 @@ Repository: functest Committers: yaohelan@huawei.com -serena.feng.711@gmail.com +feng.xiaowei@zte.com.cn ollivier.cedric@gmail.com jose.lausuch@ericsson.com morgan.richomme@orange.com diff --git a/functest/ci/check_os.sh b/functest/ci/check_os.sh index 053796d98..e2471026c 100755 --- a/functest/ci/check_os.sh +++ b/functest/ci/check_os.sh @@ -24,7 +24,7 @@ fi echo "Checking OpenStack endpoints:" -publicURL=$OS_AUTH_URL +publicURL=$(openstack catalog show identity |awk '/public/ {print $4}') publicIP=$(echo $publicURL|sed 's/^.*http\:\/\///'|sed 's/.[^:]*$//') publicPort=$(echo $publicURL|sed 's/^.*://'|sed 's/\/.*$//') echo ">>Verifying connectivity to the public endpoint $publicIP:$publicPort..." diff --git a/functest/ci/run_tests.py b/functest/ci/run_tests.py index 722929d20..10f1ac9a1 100755 --- a/functest/ci/run_tests.py +++ b/functest/ci/run_tests.py @@ -179,7 +179,7 @@ def run_test(test, tier_name, testcases=None): if result != 0: logger.error("The test case '%s' failed. " % test_name) - OVERALL_RESULT = -1 + GlobalVariables.OVERALL_RESULT = -1 result_str = "FAIL" if test.is_blocking(): @@ -189,8 +189,9 @@ def run_test(test, tier_name, testcases=None): # if it is a single test we don't print the whole results table update_test_info(test_name, result_str, duration_str) generate_report.main(GlobalVariables.EXECUTED_TEST_CASES) - logger.info("Execution exit value: %s" % OVERALL_RESULT) - sys.exit(OVERALL_RESULT) + logger.info("Execution exit value: %s" % + GlobalVariables.OVERALL_RESULT) + sys.exit(GlobalVariables.OVERALL_RESULT) update_test_info(test_name, result_str, duration_str) diff --git a/functest/ci/testcases.yaml b/functest/ci/testcases.yaml index 6f57c7030..446a3b851 100755 --- a/functest/ci/testcases.yaml +++ b/functest/ci/testcases.yaml @@ -65,7 +65,7 @@ tiers: Tempest automatically and depends on the parameters of the OpenStack deplopyment. dependencies: - installer: '' + installer: '^((?!netvirt).)*$' scenario: '' run: module: 'functest.opnfv_tests.openstack.tempest.tempest' @@ -120,7 +120,7 @@ tiers: the cloud's private network. dependencies: - installer: '' + installer: '^((?!netvirt).)*$' scenario: '' run: module: 'functest.opnfv_tests.openstack.snaps.connection_check' @@ -138,7 +138,7 @@ tiers: the cloud's private network. dependencies: - installer: '' + installer: '^((?!netvirt).)*$' scenario: '' run: module: 'functest.opnfv_tests.openstack.snaps.api_check' @@ -158,7 +158,7 @@ tiers: the cloud's private network. dependencies: - installer: '' + installer: '^((?!netvirt).)*$' scenario: '' run: module: 'functest.opnfv_tests.openstack.snaps.smoke' @@ -199,7 +199,7 @@ tiers: description: >- Test suite from SDNVPN project. dependencies: - installer: '(fuel)|(apex)' + installer: '(fuel)|(apex)|(netvirt)' scenario: 'bgpvpn' run: module: 'functest.opnfv_tests.features.sdnvpn' @@ -298,7 +298,7 @@ tiers: Tempest automatically and depends on the parameters of the OpenStack deplopyment. dependencies: - installer: '' + installer: '^((?!netvirt).)*$' scenario: '' run: module: 'functest.opnfv_tests.openstack.tempest.tempest' @@ -312,7 +312,7 @@ tiers: This test case runs the full suite of scenarios of the OpenStack Rally suite using several threads and iterations. dependencies: - installer: '' + installer: '^((?!netvirt).)*$' scenario: '' - diff --git a/functest/opnfv_tests/features/copper.py b/functest/opnfv_tests/features/copper.py index 8d5393c95..204fa3374 100755 --- a/functest/opnfv_tests/features/copper.py +++ b/functest/opnfv_tests/features/copper.py @@ -22,4 +22,4 @@ class Copper(base.FeatureBase): super(Copper, self).__init__(project='copper', case='copper-notification', repo='dir_repo_copper') - self.cmd = "%s/tests/run.sh %s/tests" % (self.repo, self.repo) + self.cmd = 'cd %s/tests && ./run.sh' % self.repo diff --git a/functest/opnfv_tests/openstack/healthcheck/healthcheck.sh b/functest/opnfv_tests/openstack/healthcheck/healthcheck.sh index e27cf4b4b..57aa0c707 100755 --- a/functest/opnfv_tests/openstack/healthcheck/healthcheck.sh +++ b/functest/opnfv_tests/openstack/healthcheck/healthcheck.sh @@ -228,10 +228,11 @@ sleep ${wait_time} # Check if flavor exists -if [[ -z $(nova flavor-list|grep $flavor) ]]; then +if [[ -z $(openstack flavor list -f value -c Name | fgrep -x $flavor) ]]; then # if given flavor doesn't exist, we create one debug "Flavor $flavor doesn't exist. Creating a new flavor." - nova flavor-create --is-public false ${flavor} auto 512 1 1 --is-public True + openstack flavor create ${flavor} --id auto --ram 512 --disk 1 --vcpus 1 + openstack flavor set ${flavor} --property hw:mem_page_size=any fi debug "Using flavor $flavor to boot the instances." diff --git a/functest/opnfv_tests/openstack/tempest/conf_utils.py b/functest/opnfv_tests/openstack/tempest/conf_utils.py index 5295ff373..6aa39ea94 100644 --- a/functest/opnfv_tests/openstack/tempest/conf_utils.py +++ b/functest/opnfv_tests/openstack/tempest/conf_utils.py @@ -15,6 +15,7 @@ import shutil import opnfv.utils.constants as releng_constants import functest.utils.functest_utils as ft_utils +import functest.utils.openstack_utils as os_utils from functest.utils.constants import CONST IMAGE_ID_ALT = None @@ -129,9 +130,11 @@ def configure_tempest_multisite(logger, deployment_dir): config.read(tempest_conf_file) config.set('service_available', 'kingbird', 'true') - cmd = ("openstack endpoint show kingbird | grep publicurl |" - "awk '{print $4}' | awk -F '/' '{print $4}'") - kingbird_api_version = os.popen(cmd).read() + # cmd = ("openstack endpoint show kingbird | grep publicurl |" + # "awk '{print $4}' | awk -F '/' '{print $4}'") + # kingbird_api_version = os.popen(cmd).read() + kingbird_api_version = os_utils.get_endpoint(service_type='kingbird') + if CI_INSTALLER_TYPE == 'fuel': # For MOS based setup, the service is accessible # via bind host @@ -172,9 +175,10 @@ def configure_tempest_multisite(logger, deployment_dir): bind_details)[0] kingbird_endpoint_url = "http://%s:%s/" % (bind_host, bind_port) else: - cmd = "openstack endpoint show kingbird | grep publicurl |\ - awk '{print $4}' | awk -F '/' '{print $3}'" - kingbird_endpoint_url = os.popen(cmd).read() + # cmd = "openstack endpoint show kingbird | grep publicurl |\ + # awk '{print $4}' | awk -F '/' '{print $3}'" + # kingbird_endpoint_url = os.popen(cmd).read() + kingbird_endpoint_url = os_utils.get_endpoint(service_type='kingbird') try: config.add_section("kingbird") diff --git a/functest/opnfv_tests/sdn/odl/odl.py b/functest/opnfv_tests/sdn/odl/odl.py index 0905e55cc..606d59a11 100755 --- a/functest/opnfv_tests/sdn/odl/odl.py +++ b/functest/opnfv_tests/sdn/odl/odl.py @@ -15,7 +15,7 @@ import re import sys import urlparse -from robot.api import ExecutionResult, ResultVisitor +import robot.api from robot.errors import RobotError import robot.run from robot.utils.robottime import timestamp_to_secs @@ -25,7 +25,7 @@ import functest.utils.functest_logger as ft_logger import functest.utils.openstack_utils as op_utils -class ODLResultVisitor(ResultVisitor): +class ODLResultVisitor(robot.api.ResultVisitor): def __init__(self): self._data = [] @@ -79,7 +79,7 @@ class ODLTests(testcase_base.TestcaseBase): def parse_results(self): xml_file = os.path.join(self.res_dir, 'output.xml') - result = ExecutionResult(xml_file) + result = robot.api.ExecutionResult(xml_file) visitor = ODLResultVisitor() result.visit(visitor) self.criteria = result.suite.status @@ -161,7 +161,7 @@ class ODLTests(testcase_base.TestcaseBase): kwargs['odlwebport'] = '8282' elif installer_type == 'apex': kwargs['odlip'] = os.environ['SDN_CONTROLLER_IP'] - kwargs['odlwebport'] = '8181' + kwargs['odlwebport'] = '8081' elif installer_type == 'joid': kwargs['odlip'] = os.environ['SDN_CONTROLLER'] elif installer_type == 'compass': @@ -180,44 +180,52 @@ class ODLTests(testcase_base.TestcaseBase): return self.main(**kwargs) +class ODLParser(): + + def __init__(self): + self.parser = argparse.ArgumentParser() + self.parser.add_argument( + '-k', '--keystoneip', help='Keystone IP', + default='127.0.0.1') + self.parser.add_argument( + '-n', '--neutronip', help='Neutron IP', + default='127.0.0.1') + self.parser.add_argument( + '-a', '--osusername', help='Username for OpenStack', + default='admin') + self.parser.add_argument( + '-b', '--ostenantname', help='Tenantname for OpenStack', + default='admin') + self.parser.add_argument( + '-c', '--ospassword', help='Password for OpenStack', + default='admin') + self.parser.add_argument( + '-o', '--odlip', help='OpenDaylight IP', + default='127.0.0.1') + self.parser.add_argument( + '-w', '--odlwebport', help='OpenDaylight Web Portal Port', + default='8080') + self.parser.add_argument( + '-r', '--odlrestconfport', help='OpenDaylight RESTConf Port', + default='8181') + self.parser.add_argument( + '-d', '--odlusername', help='Username for ODL', + default='admin') + self.parser.add_argument( + '-e', '--odlpassword', help='Password for ODL', + default='admin') + self.parser.add_argument( + '-p', '--pushtodb', help='Push results to DB', + action='store_true') + + def parse_args(self, argv=[]): + return vars(self.parser.parse_args(argv)) + + if __name__ == '__main__': - parser = argparse.ArgumentParser() - parser.add_argument('-k', '--keystoneip', - help='Keystone IP', - default='127.0.0.1') - parser.add_argument('-n', '--neutronip', - help='Neutron IP', - default='127.0.0.1') - parser.add_argument('-a', '--osusername', - help='Username for OpenStack', - default='admin') - parser.add_argument('-b', '--ostenantname', - help='Tenantname for OpenStack', - default='admin') - parser.add_argument('-c', '--ospassword', - help='Password for OpenStack', - default='admin') - parser.add_argument('-o', '--odlip', - help='OpenDaylight IP', - default='127.0.0.1') - parser.add_argument('-w', '--odlwebport', - help='OpenDaylight Web Portal Port', - default='8080') - parser.add_argument('-r', '--odlrestconfport', - help='OpenDaylight RESTConf Port', - default='8181') - parser.add_argument('-d', '--odlusername', - help='Username for ODL', - default='admin') - parser.add_argument('-e', '--odlpassword', - help='Password for ODL', - default='admin') - parser.add_argument('-p', '--pushtodb', - help='Push results to DB', - action='store_true') - - args = vars(parser.parse_args()) odl = ODLTests() + parser = ODLParser() + args = parser.parse_args(sys.argv[1:]) try: result = odl.main(**args) if result != testcase_base.TestcaseBase.EX_OK: diff --git a/functest/opnfv_tests/sdn/onos/teston/onos.py b/functest/opnfv_tests/sdn/onos/teston/onos.py index 300f56d18..213bdb7dc 100755 --- a/functest/opnfv_tests/sdn/onos/teston/onos.py +++ b/functest/opnfv_tests/sdn/onos/teston/onos.py @@ -18,14 +18,15 @@ import datetime import os import re import time +import urlparse import argparse from neutronclient.v2_0 import client as neutronclient +import functest.utils.functest_constants as ft_constants import functest.utils.functest_logger as ft_logger import functest.utils.functest_utils as ft_utils import functest.utils.openstack_utils as openstack_utils -import functest.utils.functest_constants as ft_constants parser = argparse.ArgumentParser() @@ -135,9 +136,9 @@ def GetResult(): def SetOnosIp(): - cmd = "openstack catalog show network | grep publicURL" - cmd_output = os.popen(cmd).read() - OC1 = re.search(r"\d+\.\d+\.\d+\.\d+", cmd_output).group() + # cmd = "openstack catalog show network | grep publicURL" + neutron_url = openstack_utils.get_endpoint(service_type='network') + OC1 = urlparse.urlparse(neutron_url).hostname os.environ['OC1'] = OC1 time.sleep(2) logger.debug("ONOS IP is " + OC1) @@ -180,10 +181,9 @@ def SfcTest(): def GetIp(type): - cmd = "openstack catalog show " + type + " | grep publicURL" - cmd_output = os.popen(cmd).read() - ip = re.search(r"\d+\.\d+\.\d+\.\d+", cmd_output).group() - return ip + # cmd = "openstack catalog show " + type + " | grep publicURL" + url = openstack_utils.get_endpoint(service_type=type) + return urlparse.urlparse(url).hostname def Replace(before, after): diff --git a/functest/tests/unit/cli/__init__.py b/functest/tests/unit/cli/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/functest/tests/unit/cli/__init__.py diff --git a/functest/tests/unit/cli/commands/__init__.py b/functest/tests/unit/cli/commands/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/functest/tests/unit/cli/commands/__init__.py diff --git a/functest/tests/unit/cli/commands/test_cli_env.py b/functest/tests/unit/cli/commands/test_cli_env.py new file mode 100644 index 000000000..f70761dcd --- /dev/null +++ b/functest/tests/unit/cli/commands/test_cli_env.py @@ -0,0 +1,131 @@ +#!/usr/bin/env python + +# 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 logging +import unittest + +from git.exc import NoSuchPathError +import mock + +mock.patch('logging.FileHandler').start() # noqa +from functest.cli.commands import cli_env +from functest.utils.constants import CONST +from functest.tests.unit import test_utils + + +class CliEnvTesting(unittest.TestCase): + + logging.disable(logging.CRITICAL) + + def setUp(self): + self.cli_environ = cli_env.CliEnv() + + @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile', + return_value=False) + @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command') + def test_prepare_default(self, mock_ft_utils, mock_os): + cmd = ("python %s/functest/ci/prepare_env.py start" % + CONST.dir_repo_functest) + self.cli_environ.prepare() + mock_ft_utils.assert_called_with(cmd) + + @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile', + return_value=True) + @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command') + def test_prepare_missing_status(self, mock_ft_utils, mock_os): + with mock.patch('__builtin__.raw_input', return_value="y"), \ + mock.patch('functest.cli.commands.cli_testcase.os.remove') \ + as mock_os_remove: + cmd = ("python %s/functest/ci/prepare_env.py start" % + CONST.dir_repo_functest) + self.cli_environ.prepare() + mock_os_remove.assert_called_once_with(CONST.env_active) + mock_ft_utils.assert_called_with(cmd) + + def _test_show_missing_env_var(self, var, *args): + if var == 'INSTALLER_TYPE': + CONST.INSTALLER_TYPE = None + reg_string = "| INSTALLER: Unknown, \S+\s*|" + elif var == 'INSTALLER_IP': + CONST.INSTALLER_IP = None + reg_string = "| INSTALLER: \S+, Unknown\s*|" + elif var == 'SCENARIO': + CONST.DEPLOY_SCENARIO = None + reg_string = "| SCENARIO: Unknown\s*|" + elif var == 'NODE': + CONST.NODE_NAME = None + reg_string = "| POD: Unknown\s*|" + elif var == 'BUILD_TAG': + CONST.BUILD_TAG = None + reg_string = "| BUILD TAG: None|" + elif var == 'DEBUG': + CONST.CI_DEBUG = None + reg_string = "| DEBUG FLAG: false\s*|" + elif var == 'STATUS': + reg_string = "| STATUS: not ready\s*|" + + with mock.patch('functest.cli.commands.cli_env.click.echo') \ + as mock_click_echo: + self.cli_environ.show() + mock_click_echo.assert_called_with(test_utils. + RegexMatch(reg_string)) + + @mock.patch('functest.cli.commands.cli_env.git.Repo') + def test_show_missing_ci_installer_type(self, *args): + self._test_show_missing_env_var('INSTALLER_TYPE', *args) + + @mock.patch('functest.cli.commands.cli_env.git.Repo') + def test_show_missing_ci_installer_ip(self, *args): + self._test_show_missing_env_var('INSTALLER_IP', *args) + + @mock.patch('functest.cli.commands.cli_env.git.Repo') + def test_show_missing_ci_scenario(self, *args): + self._test_show_missing_env_var('SCENARIO', *args) + + @mock.patch('functest.cli.commands.cli_env.git.Repo') + def test_show_missing_ci_node(self, *args): + self._test_show_missing_env_var('NODE', *args) + + @mock.patch('functest.cli.commands.cli_env.git.Repo') + def test_show_missing_ci_build_tag(self, *args): + self._test_show_missing_env_var('BUILD_TAG', *args) + + @mock.patch('functest.cli.commands.cli_env.git.Repo') + def test_show_missing_ci_debug(self, *args): + self._test_show_missing_env_var('DEBUG', *args) + + @mock.patch('functest.cli.commands.cli_env.git.Repo') + @mock.patch('functest.cli.commands.cli_env.os.path.isfile', + return_value=False) + def test_show_missing_environment(self, *args): + self._test_show_missing_env_var('STATUS', *args) + + @mock.patch('functest.cli.commands.cli_env.os.path.exists', + return_value=False) + def test_show_missing_git_repo_dir(self, *args): + CONST.dir_repo_functest = None + self.assertRaises(NoSuchPathError, lambda: self.cli_environ.show()) + + @mock.patch('functest.cli.commands.cli_env.click.echo') + @mock.patch('functest.cli.commands.cli_env.os.path.isfile', + return_value=True) + def test_status_environment_present(self, mock_path, mock_click_echo): + self.assertEqual(self.cli_environ.status(), 0) + mock_click_echo.assert_called_with("Functest environment" + " ready to run tests.\n") + + @mock.patch('functest.cli.commands.cli_env.click.echo') + @mock.patch('functest.cli.commands.cli_env.os.path.isfile', + return_value=False) + def test_status_environment_absent(self, mock_path, mock_click_echo): + self.assertEqual(self.cli_environ.status(), 1) + mock_click_echo.assert_called_with("Functest environment" + " is not installed.\n") + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/functest/tests/unit/cli/commands/test_cli_os.py b/functest/tests/unit/cli/commands/test_cli_os.py new file mode 100644 index 000000000..f0e58c676 --- /dev/null +++ b/functest/tests/unit/cli/commands/test_cli_os.py @@ -0,0 +1,238 @@ +#!/usr/bin/env python +# +# jose.lausuch@ericsson.com +# 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 logging +import unittest +import os + +import mock + +from functest.cli.commands import cli_os +from functest.utils.constants import CONST + + +class CliOpenStackTesting(unittest.TestCase): + logging.disable(logging.CRITICAL) + + def setUp(self): + self.endpoint_ip = 'test_ip' + self.os_auth_url = 'http://test_ip:test_port/v2.0' + self.installer_type = 'test_installer_type' + self.installer_ip = 'test_installer_ip' + self.openstack_creds = 'test_openstack_creds' + self.dir_repo_functest = 'test_dir_repo_functest' + self.snapshot_file = 'test_snapshot_file' + self.cli_os = cli_os.CliOpenStack() + + def test_ping_endpoint_default(self): + self.cli_os.os_auth_url = self.os_auth_url + self.cli_os.endpoint_ip = self.endpoint_ip + with mock.patch('functest.cli.commands.cli_os.os.system', + return_value=0): + self.assertEqual(self.cli_os.ping_endpoint(), 0) + + @mock.patch('functest.cli.commands.cli_os.exit', side_effect=Exception) + @mock.patch('functest.cli.commands.cli_os.click.echo') + def test_ping_endpoint_missing_auth_url(self, mock_click_echo, + mock_exit): + with self.assertRaises(Exception): + self.cli_os.os_auth_url = None + self.cli_os.ping_endpoint() + mock_click_echo.assert_called_once_with("Source the OpenStack " + "credentials first '. " + "$creds'") + + @mock.patch('functest.cli.commands.cli_os.exit') + @mock.patch('functest.cli.commands.cli_os.click.echo') + def test_ping_endpoint_os_system_fails(self, mock_click_echo, + mock_exit): + self.cli_os.os_auth_url = self.os_auth_url + self.cli_os.endpoint_ip = self.endpoint_ip + with mock.patch('functest.cli.commands.cli_os.os.system', + return_value=1): + self.cli_os.ping_endpoint() + mock_click_echo.assert_called_once_with("Cannot talk to the " + "endpoint %s\n" % + self.endpoint_ip) + mock_exit.assert_called_once_with(0) + + @mock.patch('functest.cli.commands.cli_os.ft_utils.execute_command') + @mock.patch('functest.cli.commands.cli_os.os.path.isfile', + return_value=False) + @mock.patch('functest.cli.commands.cli_os.click.echo') + def test_fetch_credentials_default(self, mock_click_echo, + mock_os_path, + mock_ftutils_execute): + CONST.INSTALLER_TYPE = self.installer_type + CONST.INSTALLER_IP = self.installer_ip + cmd = ("%s/releng/utils/fetch_os_creds.sh -d %s -i %s -a %s" + % (CONST.dir_repos, + self.openstack_creds, + self.installer_type, + self.installer_ip)) + self.cli_os.openstack_creds = self.openstack_creds + self.cli_os.fetch_credentials() + mock_click_echo.assert_called_once_with("Fetching credentials from " + "installer node '%s' with " + "IP=%s.." % + (self.installer_type, + self.installer_ip)) + mock_ftutils_execute.assert_called_once_with(cmd, verbose=False) + + @mock.patch('functest.cli.commands.cli_os.ft_utils.execute_command') + @mock.patch('functest.cli.commands.cli_os.os.path.isfile', + return_value=False) + @mock.patch('functest.cli.commands.cli_os.click.echo') + def test_fetch_credentials_missing_installer_type(self, mock_click_echo, + mock_os_path, + mock_ftutils_execute): + installer_type = None + installer_ip = self.installer_ip + CONST.INSTALLER_TYPE = installer_type + CONST.INSTALLER_IP = installer_ip + cmd = ("%s/releng/utils/fetch_os_creds.sh -d %s -i %s -a %s" + % (CONST.dir_repos, + self.openstack_creds, + installer_type, + installer_ip)) + self.cli_os.openstack_creds = self.openstack_creds + self.cli_os.fetch_credentials() + mock_click_echo.assert_any_call("The environment variable " + "'INSTALLER_TYPE' is not" + "defined. Please export it") + mock_click_echo.assert_any_call("Fetching credentials from " + "installer node '%s' with " + "IP=%s.." % + (installer_type, + installer_ip)) + mock_ftutils_execute.assert_called_once_with(cmd, verbose=False) + + @mock.patch('functest.cli.commands.cli_os.ft_utils.execute_command') + @mock.patch('functest.cli.commands.cli_os.os.path.isfile', + return_value=False) + @mock.patch('functest.cli.commands.cli_os.click.echo') + def test_fetch_credentials_missing_installer_ip(self, mock_click_echo, + mock_os_path, + mock_ftutils_execute): + installer_type = self.installer_type + installer_ip = None + CONST.INSTALLER_TYPE = installer_type + CONST.INSTALLER_IP = installer_ip + cmd = ("%s/releng/utils/fetch_os_creds.sh -d %s -i %s -a %s" + % (CONST.dir_repos, + self.openstack_creds, + installer_type, + installer_ip)) + self.cli_os.openstack_creds = self.openstack_creds + self.cli_os.fetch_credentials() + mock_click_echo.assert_any_call("The environment variable " + "'INSTALLER_IP' is not" + "defined. Please export it") + mock_click_echo.assert_any_call("Fetching credentials from " + "installer node '%s' with " + "IP=%s.." % + (installer_type, + installer_ip)) + mock_ftutils_execute.assert_called_once_with(cmd, verbose=False) + + @mock.patch('functest.cli.commands.cli_os.ft_utils.execute_command') + def test_check(self, mock_ftutils_execute): + with mock.patch.object(self.cli_os, 'ping_endpoint'): + CONST.dir_repo_functest = self.dir_repo_functest + cmd = CONST.dir_repo_functest + "/functest/ci/check_os.sh" + self.cli_os.check() + mock_ftutils_execute.assert_called_once_with(cmd, verbose=False) + + @mock.patch('functest.cli.commands.cli_os.os.path.isfile', + return_value=False) + @mock.patch('functest.cli.commands.cli_os.click.echo') + def test_snapshot_create(self, mock_click_echo, mock_os_path): + with mock.patch.object(self.cli_os, 'ping_endpoint'), \ + mock.patch('functest.cli.commands.cli_os.os_snapshot.main') \ + as mock_os_snapshot: + self.cli_os.snapshot_create() + mock_click_echo.assert_called_once_with("Generating Openstack " + "snapshot...") + self.assertTrue(mock_os_snapshot.called) + + @mock.patch('functest.cli.commands.cli_os.os.path.isfile', + return_value=True) + @mock.patch('functest.cli.commands.cli_os.click.echo') + def test_snapshot_create_overwrite(self, mock_click_echo, mock_os_path): + with mock.patch('__builtin__.raw_input', return_value="y") \ + as mock_raw_input, \ + mock.patch.object(self.cli_os, 'ping_endpoint'), \ + mock.patch('functest.cli.commands.cli_os.os_snapshot.main') \ + as mock_os_snapshot: + self.cli_os.snapshot_create() + mock_click_echo.assert_called_once_with("Generating Openstack " + "snapshot...") + mock_raw_input.assert_any_call("It seems there is already an " + "OpenStack snapshot. Do you want " + "to overwrite it with the current " + "OpenStack status? [y|n]\n") + self.assertTrue(mock_os_snapshot.called) + + @mock.patch('functest.cli.commands.cli_os.os.path.isfile', + return_value=False) + @mock.patch('functest.cli.commands.cli_os.click.echo') + def test_snapshot_show_missing_snap(self, mock_click_echo, mock_os_path): + self.cli_os.snapshot_show() + mock_click_echo.assert_called_once_with("There is no OpenStack " + "snapshot created. To create " + "one run the command " + "'functest openstack " + "snapshot-create'") + + @mock.patch('functest.cli.commands.cli_os.os.path.isfile', + return_value=True) + @mock.patch('functest.cli.commands.cli_os.click.echo') + def test_snapshot_show_default(self, mock_click_echo, mock_os_path): + with mock.patch('__builtin__.open', mock.mock_open(read_data='0')) \ + as m: + self.cli_os.snapshot_file = self.snapshot_file + self.cli_os.snapshot_show() + m.assert_called_once_with(self.snapshot_file, 'r') + mock_click_echo.assert_called_once_with("\n0") + + @mock.patch('functest.cli.commands.cli_os.os.path.isfile', + return_value=True) + @mock.patch('functest.cli.commands.cli_os.click.echo') + def test_clean(self, mock_click_echo, mock_os_path): + with mock.patch.object(self.cli_os, 'ping_endpoint'), \ + mock.patch('functest.cli.commands.cli_os.os_clean.main') \ + as mock_os_clean: + self.cli_os.clean() + self.assertTrue(mock_os_clean.called) + + @mock.patch('functest.cli.commands.cli_os.os.path.isfile', + return_value=False) + @mock.patch('functest.cli.commands.cli_os.click.echo') + def test_clean_missing_file(self, mock_click_echo, mock_os_path): + with mock.patch.object(self.cli_os, 'ping_endpoint'): + self.cli_os.clean() + mock_click_echo.assert_called_once_with("Not possible to clean " + "OpenStack without a " + "snapshot. This could " + "cause problems. " + "Run first the command " + "'functest openstack " + "snapshot-create'") + + @mock.patch('functest.cli.commands.cli_os.click.echo') + def test_show_credentials(self, mock_click_echo): + key = 'OS_KEY' + value = 'OS_VALUE' + with mock.patch.dict(os.environ, {key: value}): + self.cli_os.show_credentials() + mock_click_echo.assert_any_call("{}={}".format(key, value)) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/functest/tests/unit/cli/commands/test_cli_testcase.py b/functest/tests/unit/cli/commands/test_cli_testcase.py new file mode 100644 index 000000000..39c8139d7 --- /dev/null +++ b/functest/tests/unit/cli/commands/test_cli_testcase.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python + +# 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 logging +import unittest + +import mock + +from functest.cli.commands import cli_testcase +from functest.utils.constants import CONST + + +class CliTestCasesTesting(unittest.TestCase): + + logging.disable(logging.CRITICAL) + + def setUp(self): + self.testname = 'testname' + with mock.patch('functest.cli.commands.cli_testcase.tb'): + self.cli_tests = cli_testcase.CliTestcase() + + @mock.patch('functest.cli.commands.cli_testcase.vacation.main') + def test_run_vacation(self, mock_method): + self.cli_tests.run('vacation') + self.assertTrue(mock_method.called) + + @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile', + return_value=False) + @mock.patch('functest.cli.commands.cli_testcase.click.echo') + def test_run_missing_env_file(self, mock_click_echo, mock_os): + self.cli_tests.run(self.testname) + mock_click_echo.assert_called_with("Functest environment is not ready." + " Run first 'functest env prepare'") + + @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile', + return_value=True) + @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command') + def test_run_default(self, mock_ft_utils, mock_os): + cmd = ("python %s/functest/ci/run_tests.py " + "%s -t %s" % (CONST.dir_repo_functest, "-n -r ", self.testname)) + self.cli_tests.run(self.testname, noclean=True, report=True) + mock_ft_utils.assert_called_with(cmd) + + @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile', + return_value=True) + @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command') + def test_run_noclean_missing_report(self, mock_ft_utils, mock_os): + cmd = ("python %s/functest/ci/run_tests.py " + "%s -t %s" % (CONST.dir_repo_functest, "-n ", self.testname)) + self.cli_tests.run(self.testname, noclean=True, report=False) + mock_ft_utils.assert_called_with(cmd) + + @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile', + return_value=True) + @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command') + def test_run_report_missing_noclean(self, mock_ft_utils, mock_os): + cmd = ("python %s/functest/ci/run_tests.py " + "%s -t %s" % (CONST.dir_repo_functest, "-r ", self.testname)) + self.cli_tests.run(self.testname, noclean=False, report=True) + mock_ft_utils.assert_called_with(cmd) + + @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile', + return_value=True) + @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command') + def test_run_missing_noclean_report(self, mock_ft_utils, mock_os): + cmd = ("python %s/functest/ci/run_tests.py " + "%s -t %s" % (CONST.dir_repo_functest, "", self.testname)) + self.cli_tests.run(self.testname, noclean=False, report=False) + mock_ft_utils.assert_called_with(cmd) + + @mock.patch('functest.cli.commands.cli_testcase.click.echo') + def test_list(self, mock_click_echo): + with mock.patch.object(self.cli_tests.tiers, 'get_tiers', + return_value=[]): + self.cli_tests.list() + mock_click_echo.assert_called_with("") + + @mock.patch('functest.cli.commands.cli_testcase.click.echo') + def test_show_default_desc_none(self, mock_click_echo): + with mock.patch.object(self.cli_tests.tiers, 'get_test', + return_value=None): + self.cli_tests.show(self.testname) + mock_click_echo.assert_any_call("The test case '%s' " + "does not exist or is" + " not supported." + % self.testname) + + @mock.patch('functest.cli.commands.cli_testcase.click.echo') + def test_show_default(self, mock_click_echo): + mock_obj = mock.Mock() + with mock.patch.object(self.cli_tests.tiers, 'get_test', + return_value=mock_obj): + self.cli_tests.show(self.testname) + mock_click_echo.assert_called_with(mock_obj) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/functest/tests/unit/cli/commands/test_cli_tier.py b/functest/tests/unit/cli/commands/test_cli_tier.py new file mode 100644 index 000000000..802359f16 --- /dev/null +++ b/functest/tests/unit/cli/commands/test_cli_tier.py @@ -0,0 +1,130 @@ +#!/usr/bin/env python + +# 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 logging +import unittest + +import mock + +from functest.cli.commands import cli_tier +from functest.utils.constants import CONST + + +class CliTierTesting(unittest.TestCase): + + logging.disable(logging.CRITICAL) + + def setUp(self): + self.tiername = 'tiername' + self.testnames = 'testnames' + with mock.patch('functest.cli.commands.cli_tier.tb'): + self.cli_tier = cli_tier.CliTier() + + @mock.patch('functest.cli.commands.cli_tier.click.echo') + def test_list(self, mock_click_echo): + with mock.patch.object(self.cli_tier.tiers, 'get_tiers', + return_value=[]): + self.cli_tier.list() + mock_click_echo.assert_called_with("") + + @mock.patch('functest.cli.commands.cli_tier.click.echo') + def test_show_default(self, mock_click_echo): + with mock.patch.object(self.cli_tier.tiers, 'get_tier', + return_value=self.tiername): + self.cli_tier.show(self.tiername) + mock_click_echo.assert_called_with(self.tiername) + + @mock.patch('functest.cli.commands.cli_tier.click.echo') + def test_show_missing_tier(self, mock_click_echo): + with mock.patch.object(self.cli_tier.tiers, 'get_tier', + return_value=None), \ + mock.patch.object(self.cli_tier.tiers, 'get_tier_names', + return_value='tiernames'): + self.cli_tier.show(self.tiername) + mock_click_echo.assert_called_with("The tier with name '%s' does " + "not exist. Available tiers are" + ":\n %s\n" % (self.tiername, + 'tiernames')) + + @mock.patch('functest.cli.commands.cli_tier.click.echo') + def test_gettests_default(self, mock_click_echo): + mock_obj = mock.Mock() + attrs = {'get_test_names.return_value': self.testnames} + mock_obj.configure_mock(**attrs) + + with mock.patch.object(self.cli_tier.tiers, 'get_tier', + return_value=mock_obj): + self.cli_tier.gettests(self.tiername) + mock_click_echo.assert_called_with("Test cases in tier " + "'%s':\n %s\n" % (self.tiername, + self.testnames + )) + + @mock.patch('functest.cli.commands.cli_tier.click.echo') + def test_gettests_missing_tier(self, mock_click_echo): + with mock.patch.object(self.cli_tier.tiers, 'get_tier', + return_value=None), \ + mock.patch.object(self.cli_tier.tiers, 'get_tier_names', + return_value='tiernames'): + self.cli_tier.gettests(self.tiername) + mock_click_echo.assert_called_with("The tier with name '%s' does " + "not exist. Available tiers are" + ":\n %s\n" % (self.tiername, + 'tiernames')) + + @mock.patch('functest.cli.commands.cli_tier.os.path.isfile', + return_value=False) + @mock.patch('functest.cli.commands.cli_tier.click.echo') + def test_run_missing_env_file(self, mock_click_echo, mock_os): + self.cli_tier.run(self.tiername) + mock_click_echo.assert_called_with("Functest environment is not ready." + " Run first 'functest env prepare'") + + @mock.patch('functest.cli.commands.cli_tier.os.path.isfile', + return_value=True) + @mock.patch('functest.cli.commands.cli_tier.ft_utils.execute_command') + def test_run_default(self, mock_ft_utils, mock_os): + cmd = ("python %s/functest/ci/run_tests.py " + "%s -t %s" % (CONST.dir_repo_functest, "-n -r ", + self.tiername)) + self.cli_tier.run(self.tiername, noclean=True, report=True) + mock_ft_utils.assert_called_with(cmd) + + @mock.patch('functest.cli.commands.cli_tier.os.path.isfile', + return_value=True) + @mock.patch('functest.cli.commands.cli_tier.ft_utils.execute_command') + def test_run_report_missing_noclean(self, mock_ft_utils, mock_os): + cmd = ("python %s/functest/ci/run_tests.py " + "%s -t %s" % (CONST.dir_repo_functest, "-r ", + self.tiername)) + self.cli_tier.run(self.tiername, noclean=False, report=True) + mock_ft_utils.assert_called_with(cmd) + + @mock.patch('functest.cli.commands.cli_tier.os.path.isfile', + return_value=True) + @mock.patch('functest.cli.commands.cli_tier.ft_utils.execute_command') + def test_run_noclean_missing_report(self, mock_ft_utils, mock_os): + cmd = ("python %s/functest/ci/run_tests.py " + "%s -t %s" % (CONST.dir_repo_functest, "-n ", + self.tiername)) + self.cli_tier.run(self.tiername, noclean=True, report=False) + mock_ft_utils.assert_called_with(cmd) + + @mock.patch('functest.cli.commands.cli_tier.os.path.isfile', + return_value=True) + @mock.patch('functest.cli.commands.cli_tier.ft_utils.execute_command') + def test_run_missing_noclean_report(self, mock_ft_utils, mock_os): + cmd = ("python %s/functest/ci/run_tests.py " + "%s -t %s" % (CONST.dir_repo_functest, "", + self.tiername)) + self.cli_tier.run(self.tiername, noclean=False, report=False) + mock_ft_utils.assert_called_with(cmd) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/functest/tests/unit/cli/test_cli_base.py b/functest/tests/unit/cli/test_cli_base.py new file mode 100644 index 000000000..fe065c2aa --- /dev/null +++ b/functest/tests/unit/cli/test_cli_base.py @@ -0,0 +1,138 @@ +#!/usr/bin/env python + +# Copyright (c) 2016 Orange 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 logging +import unittest + +import mock +from click.testing import CliRunner + +with mock.patch('functest.cli.commands.cli_testcase.CliTestcase.__init__', + mock.Mock(return_value=None)), \ + mock.patch('functest.cli.commands.cli_tier.CliTier.__init__', + mock.Mock(return_value=None)): + from functest.cli import cli_base + + +class CliBaseTesting(unittest.TestCase): + + logging.disable(logging.CRITICAL) + + def setUp(self): + self.runner = CliRunner() + self._openstack = cli_base._openstack + self._env = cli_base._env + self._testcase = cli_base._testcase + self._tier = cli_base._tier + + def test_os_check(self): + with mock.patch.object(self._openstack, 'check') as mock_method: + result = self.runner.invoke(cli_base.os_check) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_os_snapshot_create(self): + with mock.patch.object(self._openstack, 'snapshot_create') \ + as mock_method: + result = self.runner.invoke(cli_base.os_snapshot_create) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_os_snapshot_show(self): + with mock.patch.object(self._openstack, 'snapshot_show') \ + as mock_method: + result = self.runner.invoke(cli_base.os_snapshot_show) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_os_clean(self): + with mock.patch.object(self._openstack, 'clean') as mock_method: + result = self.runner.invoke(cli_base.os_clean) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_os_show_credentials(self): + with mock.patch.object(self._openstack, 'show_credentials') \ + as mock_method: + result = self.runner.invoke(cli_base.os_show_credentials) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_os_fetch_rc(self): + with mock.patch.object(self._openstack, 'fetch_credentials') \ + as mock_method: + result = self.runner.invoke(cli_base.os_fetch_rc) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_env_prepare(self): + with mock.patch.object(self._env, 'prepare') as mock_method: + result = self.runner.invoke(cli_base.env_prepare) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_env_show(self): + with mock.patch.object(self._env, 'show') as mock_method: + result = self.runner.invoke(cli_base.env_show) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_env_status(self): + with mock.patch.object(self._env, 'status') as mock_method: + result = self.runner.invoke(cli_base.env_status) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_testcase_list(self): + with mock.patch.object(self._testcase, 'list') as mock_method: + result = self.runner.invoke(cli_base.testcase_list) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_testcase_show(self): + with mock.patch.object(self._testcase, 'show') as mock_method: + result = self.runner.invoke(cli_base.testcase_show, ['testname']) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_testcase_run(self): + with mock.patch.object(self._testcase, 'run') as mock_method: + result = self.runner.invoke(cli_base.testcase_run, + ['testname', '--noclean']) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_tier_list(self): + with mock.patch.object(self._tier, 'list') as mock_method: + result = self.runner.invoke(cli_base.tier_list) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_tier_show(self): + with mock.patch.object(self._tier, 'show') as mock_method: + result = self.runner.invoke(cli_base.tier_show, ['tiername']) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_tier_gettests(self): + with mock.patch.object(self._tier, 'gettests') as mock_method: + result = self.runner.invoke(cli_base.tier_gettests, ['tiername']) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + def test_tier_run(self): + with mock.patch.object(self._tier, 'run') as mock_method: + result = self.runner.invoke(cli_base.tier_run, + ['tiername', '--noclean']) + self.assertEqual(result.exit_code, 0) + self.assertTrue(mock_method.called) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/functest/tests/unit/core/test_testcase_base.py b/functest/tests/unit/core/test_testcase_base.py index b7c81d87c..b6efa40dc 100644 --- a/functest/tests/unit/core/test_testcase_base.py +++ b/functest/tests/unit/core/test_testcase_base.py @@ -11,6 +11,7 @@ import logging import mock import unittest +mock.patch('logging.FileHandler').start() # noqa from functest.core import testcase_base diff --git a/functest/tests/unit/odl/test_odl.py b/functest/tests/unit/odl/test_odl.py index d8c7f84ec..8a52a9f63 100644 --- a/functest/tests/unit/odl/test_odl.py +++ b/functest/tests/unit/odl/test_odl.py @@ -11,12 +11,15 @@ import errno import logging import mock import os +import StringIO import unittest from keystoneauth1.exceptions import auth_plugins -from robot.errors import RobotError +from robot.errors import DataError, RobotError from robot.result import testcase +from robot.utils.robottime import timestamp_to_secs +mock.patch('logging.FileHandler').start() # noqa from functest.core import testcase_base from functest.opnfv_tests.sdn.odl import odl @@ -44,6 +47,17 @@ class ODLTesting(unittest.TestCase): os.environ["OS_PASSWORD"] = self._os_password os.environ["OS_TENANT_NAME"] = self._os_tenantname self.test = odl.ODLTests() + self.defaultargs = {'odlusername': self._odl_username, + 'odlpassword': self._odl_password, + 'keystoneip': self._keystone_ip, + 'neutronip': self._keystone_ip, + 'osusername': self._os_username, + 'ostenantname': self._os_tenantname, + 'ospassword': self._os_password, + 'odlip': self._keystone_ip, + 'odlwebport': self._odl_webport, + 'odlrestconfport': self._odl_restconfport, + 'pushtodb': False} def test_empty_visitor(self): visitor = odl.ODLResultVisitor() @@ -72,14 +86,65 @@ class ODLTesting(unittest.TestCase): visitor.visit_test(test) self.assertEqual(visitor.get_data(), [data]) + @mock.patch('robot.api.ExecutionResult', side_effect=DataError) + def test_parse_results_raises_exceptions(self, *args): + with self.assertRaises(DataError): + self.test.parse_results() + + def test_parse_results(self, *args): + config = {'name': 'dummy', 'starttime': '20161216 16:00:00.000', + 'endtime': '20161216 16:00:01.000', 'status': 'PASS'} + suite = mock.Mock() + suite.configure_mock(**config) + with mock.patch('robot.api.ExecutionResult', + return_value=mock.Mock(suite=suite)): + self.test.parse_results() + self.assertEqual(self.test.criteria, config['status']) + self.assertEqual(self.test.start_time, + timestamp_to_secs(config['starttime'])) + self.assertEqual(self.test.stop_time, + timestamp_to_secs(config['endtime'])) + self.assertEqual(self.test.details, + {'description': config['name'], 'tests': []}) + @mock.patch('fileinput.input', side_effect=Exception()) def test_set_robotframework_vars_failed(self, *args): self.assertFalse(self.test.set_robotframework_vars()) @mock.patch('fileinput.input', return_value=[]) - def test_set_robotframework_vars(self, args): + def test_set_robotframework_vars_empty(self, args): self.assertTrue(self.test.set_robotframework_vars()) + @mock.patch('sys.stdout', new_callable=StringIO.StringIO) + def _test_set_robotframework_vars(self, msg1, msg2, *args): + line = mock.MagicMock() + line.__iter__.return_value = [msg1] + with mock.patch('fileinput.input', return_value=line) as mock_method: + self.assertTrue(self.test.set_robotframework_vars()) + mock_method.assert_called_once_with( + os.path.join(odl.ODLTests.odl_test_repo, + 'csit/variables/Variables.py'), inplace=True) + self.assertEqual(args[0].getvalue(), "{}\n".format(msg2)) + + def test_set_robotframework_vars_auth_default(self): + self._test_set_robotframework_vars("AUTH = []", + "AUTH = [u'admin', u'admin']") + + def test_set_robotframework_vars_auth1(self): + self._test_set_robotframework_vars("AUTH1 = []", "AUTH1 = []") + + @mock.patch('sys.stdout', new_callable=StringIO.StringIO) + def test_set_robotframework_vars_auth_foo(self, *args): + line = mock.MagicMock() + line.__iter__.return_value = ["AUTH = []"] + with mock.patch('fileinput.input', return_value=line) as mock_method: + self.assertTrue(self.test.set_robotframework_vars('foo', 'bar')) + mock_method.assert_called_once_with( + os.path.join(odl.ODLTests.odl_test_repo, + 'csit/variables/Variables.py'), inplace=True) + self.assertEqual(args[0].getvalue(), + "AUTH = [u'{}', u'{}']\n".format('foo', 'bar')) + @classmethod def _fake_url_for(cls, service_type='identity', **kwargs): if service_type == 'identity': @@ -194,6 +259,8 @@ class ODLTesting(unittest.TestCase): def test_main_robot_run_failed(self, *args): with mock.patch.object(self.test, 'set_robotframework_vars', return_value=True), \ + mock.patch.object(odl, 'open', mock.mock_open(), + create=True), \ self.assertRaises(RobotError): self._test_main(testcase_base.TestcaseBase.EX_RUN_ERROR, *args) @@ -202,6 +269,8 @@ class ODLTesting(unittest.TestCase): def test_main_parse_results_failed(self, *args): with mock.patch.object(self.test, 'set_robotframework_vars', return_value=True), \ + mock.patch.object(odl, 'open', mock.mock_open(), + create=True), \ mock.patch.object(self.test, 'parse_results', side_effect=RobotError): self._test_main(testcase_base.TestcaseBase.EX_RUN_ERROR, *args) @@ -222,6 +291,8 @@ class ODLTesting(unittest.TestCase): def test_main(self, *args): with mock.patch.object(self.test, 'set_robotframework_vars', return_value=True), \ + mock.patch.object(odl, 'open', mock.mock_open(), + create=True), \ mock.patch.object(self.test, 'parse_results'): self._test_main(testcase_base.TestcaseBase.EX_OK, *args) @@ -231,6 +302,8 @@ class ODLTesting(unittest.TestCase): def test_main_makedirs_oserror17(self, *args): with mock.patch.object(self.test, 'set_robotframework_vars', return_value=True), \ + mock.patch.object(odl, 'open', mock.mock_open(), + create=True), \ mock.patch.object(self.test, 'parse_results'): self._test_main(testcase_base.TestcaseBase.EX_OK, *args) @@ -240,6 +313,8 @@ class ODLTesting(unittest.TestCase): def test_main_testcases_in_failure(self, *args): with mock.patch.object(self.test, 'set_robotframework_vars', return_value=True), \ + mock.patch.object(odl, 'open', mock.mock_open(), + create=True), \ mock.patch.object(self.test, 'parse_results'): self._test_main(testcase_base.TestcaseBase.EX_OK, *args) @@ -249,6 +324,8 @@ class ODLTesting(unittest.TestCase): def test_main_remove_oserror(self, *args): with mock.patch.object(self.test, 'set_robotframework_vars', return_value=True), \ + mock.patch.object(odl, 'open', mock.mock_open(), + create=True), \ mock.patch.object(self.test, 'parse_results'): self._test_main(testcase_base.TestcaseBase.EX_OK, *args) @@ -333,7 +410,7 @@ class ODLTesting(unittest.TestCase): os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip os.environ["INSTALLER_TYPE"] = "apex" self._test_run(testcase_base.TestcaseBase.EX_OK, - odlip=self._sdn_controller_ip, odlwebport='8181') + odlip=self._sdn_controller_ip, odlwebport='8081') def test_run_joid_missing_sdn_controller(self): with mock.patch('functest.utils.openstack_utils.get_endpoint', @@ -353,6 +430,77 @@ class ODLTesting(unittest.TestCase): self._test_run(testcase_base.TestcaseBase.EX_OK, odlip=self._neutron_ip, odlwebport='8181') + def test_argparser_default(self): + parser = odl.ODLParser() + self.assertEqual(parser.parse_args(), self.defaultargs) + + def test_argparser_basic(self): + self.defaultargs['neutronip'] = self._neutron_ip + self.defaultargs['odlip'] = self._sdn_controller_ip + parser = odl.ODLParser() + self.assertEqual(parser.parse_args( + ["--neutronip={}".format(self._neutron_ip), + "--odlip={}".format(self._sdn_controller_ip) + ]), self.defaultargs) + + @mock.patch('sys.stderr', new_callable=StringIO.StringIO) + def test_argparser_fail(self, *args): + self.defaultargs['foo'] = 'bar' + parser = odl.ODLParser() + with self.assertRaises(SystemExit): + parser.parse_args(["--foo=bar"]) + + def _test_argparser(self, arg, value): + self.defaultargs[arg] = value + parser = odl.ODLParser() + self.assertEqual(parser.parse_args(["--{}={}".format(arg, value)]), + self.defaultargs) + + def test_argparser_odlusername(self): + self._test_argparser('odlusername', 'foo') + + def test_argparser_odlpassword(self): + self._test_argparser('odlpassword', 'foo') + + def test_argparser_keystoneip(self): + self._test_argparser('keystoneip', '127.0.0.4') + + def test_argparser_neutronip(self): + self._test_argparser('neutronip', '127.0.0.4') + + def test_argparser_osusername(self): + self._test_argparser('osusername', 'foo') + + def test_argparser_ostenantname(self): + self._test_argparser('ostenantname', 'foo') + + def test_argparser_ospassword(self): + self._test_argparser('ospassword', 'foo') + + def test_argparser_odlip(self): + self._test_argparser('odlip', '127.0.0.4') + + def test_argparser_odlwebport(self): + self._test_argparser('odlwebport', '80') + + def test_argparser_odlrestconfport(self): + self._test_argparser('odlrestconfport', '80') + + def test_argparser_pushtodb(self): + self.defaultargs['pushtodb'] = True + parser = odl.ODLParser() + self.assertEqual(parser.parse_args(["--{}".format('pushtodb')]), + self.defaultargs) + + def test_argparser_multiple_args(self): + self.defaultargs['neutronip'] = self._neutron_ip + self.defaultargs['odlip'] = self._sdn_controller_ip + parser = odl.ODLParser() + self.assertEqual(parser.parse_args( + ["--neutronip={}".format(self._neutron_ip), + "--odlip={}".format(self._sdn_controller_ip) + ]), self.defaultargs) + if __name__ == "__main__": unittest.main(verbosity=2) diff --git a/functest/tests/unit/test_utils.py b/functest/tests/unit/test_utils.py new file mode 100644 index 000000000..e171db022 --- /dev/null +++ b/functest/tests/unit/test_utils.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python + +# 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 re + + +class RegexMatch(str): + def __eq__(self, other): + match = re.search(self, other) + if match: + return True + return False + + +class SubstrMatch(str): + def __eq__(self, other): + if self in other: + return True + return False diff --git a/functest/tests/unit/utils/test_functest_utils.py b/functest/tests/unit/utils/test_functest_utils.py new file mode 100644 index 000000000..c4b566608 --- /dev/null +++ b/functest/tests/unit/utils/test_functest_utils.py @@ -0,0 +1,600 @@ +#!/usr/bin/env python + +# Copyright (c) 2016 Orange 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 logging +import os +import time +import unittest +import urllib2 + +from git.exc import NoSuchPathError +import mock +import requests + +from functest.tests.unit import test_utils +mock.patch('logging.FileHandler').start() # noqa +from functest.utils import functest_utils + + +class FunctestUtilsTesting(unittest.TestCase): + + logging.disable(logging.CRITICAL) + + def setUp(self): + self.url = 'http://www.opnfv.org/' + self.timeout = 5 + self.dest_path = 'test_path' + self.repo_path = 'test_repo_path' + self.installer = 'test_installer' + self.scenario = 'test_scenario' + self.build_tag = 'jenkins-functest-fuel-opnfv-jump-2-daily-master-190' + self.version = 'master' + self.node_name = 'test_node_name' + self.project = 'test_project' + self.case_name = 'test_case_name' + self.status = 'test_status' + self.details = 'test_details' + self.db_url = 'test_db_url' + self.success_rate = 2.0 + self.criteria = 'test_criteria==2.0' + self.start_date = 1482624000 + self.stop_date = 1482624000 + self.start_time = time.time() + self.stop_time = time.time() + self.readline = -1 + self.test_ip = ['10.1.23.4', '10.1.14.15', '10.1.16.15'] + self.test_file = 'test_file' + self.error_msg = 'test_error_msg' + self.cmd = 'test_cmd' + self.output_file = 'test_output_file' + self.testname = 'testname' + self.testcase_dict = {'name': 'testname', 'criteria': self.criteria} + self.parameter = 'general.openstack.image_name' + self.config_yaml = 'test_config_yaml-' + self.file_yaml = {'general': {'openstack': {'image_name': + 'test_image_name'}}} + + @mock.patch('urllib2.urlopen', + side_effect=urllib2.URLError('no host given')) + def test_check_internet_connectivity_failed(self, mock_method): + self.assertFalse(functest_utils.check_internet_connectivity()) + mock_method.assert_called_once_with(self.url, timeout=self.timeout) + + @mock.patch('urllib2.urlopen') + def test_check_internet_connectivity_default(self, mock_method): + self.assertTrue(functest_utils.check_internet_connectivity()) + mock_method.assert_called_once_with(self.url, timeout=self.timeout) + + @mock.patch('urllib2.urlopen') + def test_check_internet_connectivity_debian(self, mock_method): + self.url = "https://www.debian.org/" + self.assertTrue(functest_utils.check_internet_connectivity(self.url)) + mock_method.assert_called_once_with(self.url, timeout=self.timeout) + + @mock.patch('urllib2.urlopen', + side_effect=urllib2.URLError('no host given')) + def test_download_url_failed(self, mock_url): + self.assertFalse(functest_utils.download_url(self.url, self.dest_path)) + + @mock.patch('urllib2.urlopen') + def test_download_url_default(self, mock_url): + with mock.patch("__builtin__.open", mock.mock_open()) as m, \ + mock.patch('functest.utils.functest_utils.shutil.copyfileobj')\ + as mock_sh: + name = self.url.rsplit('/')[-1] + dest = self.dest_path + "/" + name + self.assertTrue(functest_utils.download_url(self.url, + self.dest_path)) + m.assert_called_once_with(dest, 'wb') + self.assertTrue(mock_sh.called) + + def test_get_git_branch(self): + with mock.patch('functest.utils.functest_utils.Repo') as mock_repo: + mock_obj2 = mock.Mock() + attrs = {'name': 'test_branch'} + mock_obj2.configure_mock(**attrs) + + mock_obj = mock.Mock() + attrs = {'active_branch': mock_obj2} + mock_obj.configure_mock(**attrs) + + mock_repo.return_value = mock_obj + self.assertEqual(functest_utils.get_git_branch(self.repo_path), + 'test_branch') + + @mock.patch('functest.utils.functest_utils.Repo', + side_effect=NoSuchPathError) + def test_get_git_branch_failed(self, mock_repo): + self.assertRaises(NoSuchPathError, + lambda: functest_utils.get_git_branch(self.repo_path + )) + + @mock.patch('functest.utils.functest_utils.logger.error') + def test_get_installer_type_failed(self, mock_logger_error): + with mock.patch.dict(os.environ, + {}, + clear=True): + self.assertEqual(functest_utils.get_installer_type(), + "Unknown_installer") + mock_logger_error.assert_called_once_with("Impossible to retrieve" + " the installer type") + + def test_get_installer_type_default(self): + with mock.patch.dict(os.environ, + {'INSTALLER_TYPE': 'test_installer'}, + clear=True): + self.assertEqual(functest_utils.get_installer_type(), + self.installer) + + @mock.patch('functest.utils.functest_utils.logger.error') + def test_get_scenario_failed(self, mock_logger_error): + with mock.patch.dict(os.environ, + {}, + clear=True): + self.assertEqual(functest_utils.get_scenario(), + "Unknown_scenario") + mock_logger_error.assert_called_once_with("Impossible to retrieve" + " the scenario") + + def test_get_scenario_default(self): + with mock.patch.dict(os.environ, + {'DEPLOY_SCENARIO': 'test_scenario'}, + clear=True): + self.assertEqual(functest_utils.get_scenario(), + self.scenario) + + @mock.patch('functest.utils.functest_utils.get_build_tag') + def test_get_version_default(self, mock_get_build_tag): + mock_get_build_tag.return_value = self.build_tag + self.assertEqual(functest_utils.get_version(), self.version) + + @mock.patch('functest.utils.functest_utils.get_build_tag') + def test_get_version_unknown(self, mock_get_build_tag): + mock_get_build_tag.return_value = "unknown_build_tag" + self.assertEqual(functest_utils.get_version(), "unknown") + + @mock.patch('functest.utils.functest_utils.logger.error') + def test_get_pod_name_failed(self, mock_logger_error): + with mock.patch.dict(os.environ, + {}, + clear=True): + self.assertEqual(functest_utils.get_pod_name(), + "unknown-pod") + mock_logger_error.assert_called_once_with("Unable to retrieve " + "the POD name from " + "environment. Using " + "pod name 'unknown-pod'") + + def test_get_pod_name_default(self): + with mock.patch.dict(os.environ, + {'NODE_NAME': 'test_node_name'}, + clear=True): + self.assertEqual(functest_utils.get_pod_name(), + self.node_name) + + @mock.patch('functest.utils.functest_utils.logger.error') + def test_get_build_tag_failed(self, mock_logger_error): + with mock.patch.dict(os.environ, + {}, + clear=True): + self.assertEqual(functest_utils.get_build_tag(), + "unknown_build_tag") + mock_logger_error.assert_called_once_with("Impossible to retrieve" + " the build tag") + + def test_get_build_tag_default(self): + with mock.patch.dict(os.environ, + {'BUILD_TAG': self.build_tag}, + clear=True): + self.assertEqual(functest_utils.get_build_tag(), + self.build_tag) + + @mock.patch('functest.utils.functest_utils.get_functest_config') + def test_get_db_url(self, mock_get_functest_config): + mock_get_functest_config.return_value = self.db_url + self.assertEqual(functest_utils.get_db_url(), self.db_url) + mock_get_functest_config.assert_called_once_with('results.test_db_url') + + @mock.patch('functest.utils.functest_utils.logger.info') + def test_logger_test_results(self, mock_logger_info): + with mock.patch('functest.utils.functest_utils.get_pod_name', + return_value=self.node_name), \ + mock.patch('functest.utils.functest_utils.get_scenario', + return_value=self.scenario), \ + mock.patch('functest.utils.functest_utils.get_version', + return_value=self.version), \ + mock.patch('functest.utils.functest_utils.get_build_tag', + return_value=self.build_tag), \ + mock.patch('functest.utils.functest_utils.get_db_url', + return_value=self.db_url): + functest_utils.logger_test_results(self.project, self.case_name, + self.status, self.details) + mock_logger_info.assert_called_once_with( + "\n" + "****************************************\n" + "\t %(p)s/%(n)s results \n\n" + "****************************************\n" + "DB:\t%(db)s\n" + "pod:\t%(pod)s\n" + "version:\t%(v)s\n" + "scenario:\t%(s)s\n" + "status:\t%(c)s\n" + "build tag:\t%(b)s\n" + "details:\t%(d)s\n" + % {'p': self.project, + 'n': self.case_name, + 'db': self.db_url, + 'pod': self.node_name, + 'v': self.version, + 's': self.scenario, + 'c': self.status, + 'b': self.build_tag, + 'd': self.details}) + + def _get_env_dict(self, var): + dic = {'INSTALLER_TYPE': self.installer, + 'DEPLOY_SCENARIO': self.scenario, + 'NODE_NAME': self.node_name, + 'BUILD_TAG': self.build_tag} + dic.pop(var, None) + return dic + + def _test_push_results_to_db_missing_env(self, env_var): + dic = self._get_env_dict(env_var) + with mock.patch('functest.utils.functest_utils.get_db_url', + return_value=self.db_url), \ + mock.patch.dict(os.environ, + dic, + clear=True), \ + mock.patch('functest.utils.functest_utils.logger.error') \ + as mock_logger_error: + functest_utils.push_results_to_db(self.project, self.case_name, + self.start_date, self.stop_date, + self.criteria, self.details) + mock_logger_error.assert_called_once_with("Please set env var: " + + str("\'" + env_var + + "\'")) + + def test_push_results_to_db_missing_installer(self): + self._test_push_results_to_db_missing_env('INSTALLER_TYPE') + + def test_push_results_to_db_missing_scenario(self): + self._test_push_results_to_db_missing_env('DEPLOY_SCENARIO') + + def test_push_results_to_db_missing_nodename(self): + self._test_push_results_to_db_missing_env('NODE_NAME') + + def test_push_results_to_db_missing_buildtag(self): + self._test_push_results_to_db_missing_env('BUILD_TAG') + + def test_push_results_to_db_incorrect_buildtag(self): + dic = self._get_env_dict(None) + dic['BUILD_TAG'] = 'incorrect_build_tag' + with mock.patch('functest.utils.functest_utils.get_db_url', + return_value=self.db_url), \ + mock.patch.dict(os.environ, + dic, + clear=True), \ + mock.patch('functest.utils.functest_utils.logger.error') \ + as mock_logger_error: + self.assertFalse(functest_utils. + push_results_to_db(self.project, self.case_name, + self.start_date, + self.stop_date, + self.criteria, self.details)) + mock_logger_error.assert_called_once_with("Please fix BUILD_TAG" + " env var: incorrect_" + "build_tag") + + def test_push_results_to_db_request_post_failed(self): + dic = self._get_env_dict(None) + with mock.patch('functest.utils.functest_utils.get_db_url', + return_value=self.db_url), \ + mock.patch.dict(os.environ, + dic, + clear=True), \ + mock.patch('functest.utils.functest_utils.logger.error') \ + as mock_logger_error, \ + mock.patch('functest.utils.functest_utils.requests.post', + side_effect=requests.RequestException): + self.assertFalse(functest_utils. + push_results_to_db(self.project, self.case_name, + self.start_date, + self.stop_date, + self.criteria, self.details)) + mock_logger_error.assert_called_once_with(test_utils. + RegexMatch("Pushing " + "Result to" + " DB" + "(\S+\s*) " + "failed:")) + + def test_push_results_to_db_request_post_exception(self): + dic = self._get_env_dict(None) + with mock.patch('functest.utils.functest_utils.get_db_url', + return_value=self.db_url), \ + mock.patch.dict(os.environ, + dic, + clear=True), \ + mock.patch('functest.utils.functest_utils.logger.error') \ + as mock_logger_error, \ + mock.patch('functest.utils.functest_utils.requests.post', + side_effect=Exception): + self.assertFalse(functest_utils. + push_results_to_db(self.project, self.case_name, + self.start_date, + self.stop_date, + self.criteria, self.details)) + self.assertTrue(mock_logger_error.called) + + def test_push_results_to_db_default(self): + dic = self._get_env_dict(None) + with mock.patch('functest.utils.functest_utils.get_db_url', + return_value=self.db_url), \ + mock.patch.dict(os.environ, + dic, + clear=True), \ + mock.patch('functest.utils.functest_utils.requests.post'): + self.assertTrue(functest_utils. + push_results_to_db(self.project, self.case_name, + self.start_date, + self.stop_date, + self.criteria, self.details)) + readline = 0 + test_ip = ['10.1.23.4', '10.1.14.15', '10.1.16.15'] + + @staticmethod + def readline_side(): + if FunctestUtilsTesting.readline == \ + len(FunctestUtilsTesting.test_ip) - 1: + return False + FunctestUtilsTesting.readline += 1 + return FunctestUtilsTesting.test_ip[FunctestUtilsTesting.readline] + + # TODO: get_resolvconf_ns + @mock.patch('functest.utils.functest_utils.dns.resolver.Resolver') + def test_get_resolvconf_ns_default(self, mock_dns_resolve): + attrs = {'query.return_value': ["test"]} + mock_dns_resolve.configure_mock(**attrs) + + m = mock.Mock() + attrs = {'readline.side_effect': self.readline_side} + m.configure_mock(**attrs) + + with mock.patch("__builtin__.open") as mo: + mo.return_value = m + self.assertEqual(functest_utils.get_resolvconf_ns(), + self.test_ip[1:]) + + def _get_environ(self, var): + if var == 'INSTALLER_TYPE': + return self.installer + elif var == 'DEPLOY_SCENARIO': + return self.scenario + return var + + def test_get_ci_envvars_default(self): + with mock.patch('os.environ.get', + side_effect=self._get_environ): + dic = {"installer": self.installer, + "scenario": self.scenario} + self.assertDictEqual(functest_utils.get_ci_envvars(), dic) + + def cmd_readline(self): + return 'test_value\n' + + @mock.patch('functest.utils.functest_utils.logger.error') + @mock.patch('functest.utils.functest_utils.logger.info') + def test_execute_command_args_present_with_error(self, mock_logger_info, + mock_logger_error): + with mock.patch('functest.utils.functest_utils.subprocess.Popen') \ + as mock_subproc_open, \ + mock.patch('__builtin__.open', mock.mock_open()) as mopen: + + FunctestUtilsTesting.readline = 0 + + mock_obj = mock.Mock() + attrs = {'readline.side_effect': self.cmd_readline()} + mock_obj.configure_mock(**attrs) + + mock_obj2 = mock.Mock() + attrs = {'stdout': mock_obj, 'wait.return_value': 1} + mock_obj2.configure_mock(**attrs) + + mock_subproc_open.return_value = mock_obj2 + + resp = functest_utils.execute_command(self.cmd, info=True, + error_msg=self.error_msg, + verbose=True, + output_file=self.output_file) + self.assertEqual(resp, 1) + msg_exec = ("Executing command: '%s'" % self.cmd) + mock_logger_info.assert_called_once_with(msg_exec) + mopen.assert_called_once_with(self.output_file, "w") + mock_logger_error.assert_called_once_with(self.error_msg) + + @mock.patch('functest.utils.functest_utils.logger.info') + def test_execute_command_args_present_with_success(self, mock_logger_info, + ): + with mock.patch('functest.utils.functest_utils.subprocess.Popen') \ + as mock_subproc_open, \ + mock.patch('__builtin__.open', mock.mock_open()) as mopen: + + FunctestUtilsTesting.readline = 0 + + mock_obj = mock.Mock() + attrs = {'readline.side_effect': self.cmd_readline()} + mock_obj.configure_mock(**attrs) + + mock_obj2 = mock.Mock() + attrs = {'stdout': mock_obj, 'wait.return_value': 0} + mock_obj2.configure_mock(**attrs) + + mock_subproc_open.return_value = mock_obj2 + + resp = functest_utils.execute_command(self.cmd, info=True, + error_msg=self.error_msg, + verbose=True, + output_file=self.output_file) + self.assertEqual(resp, 0) + msg_exec = ("Executing command: '%s'" % self.cmd) + mock_logger_info.assert_called_once_with(msg_exec) + mopen.assert_called_once_with(self.output_file, "w") + + @mock.patch('functest.utils.functest_utils.logger.info') + def test_execute_command_args_missing_with_success(self, mock_logger_info, + ): + with mock.patch('functest.utils.functest_utils.subprocess.Popen') \ + as mock_subproc_open: + + FunctestUtilsTesting.readline = 2 + + mock_obj = mock.Mock() + attrs = {'readline.side_effect': self.cmd_readline()} + mock_obj.configure_mock(**attrs) + + mock_obj2 = mock.Mock() + attrs = {'stdout': mock_obj, 'wait.return_value': 0} + mock_obj2.configure_mock(**attrs) + + mock_subproc_open.return_value = mock_obj2 + + resp = functest_utils.execute_command(self.cmd, info=False, + error_msg="", + verbose=False, + output_file=None) + self.assertEqual(resp, 0) + + @mock.patch('functest.utils.functest_utils.logger.error') + def test_execute_command_args_missing_with_error(self, mock_logger_error, + ): + with mock.patch('functest.utils.functest_utils.subprocess.Popen') \ + as mock_subproc_open: + + FunctestUtilsTesting.readline = 2 + mock_obj = mock.Mock() + attrs = {'readline.side_effect': self.cmd_readline()} + mock_obj.configure_mock(**attrs) + + mock_obj2 = mock.Mock() + attrs = {'stdout': mock_obj, 'wait.return_value': 1} + mock_obj2.configure_mock(**attrs) + + mock_subproc_open.return_value = mock_obj2 + + resp = functest_utils.execute_command(self.cmd, info=False, + error_msg="", + verbose=False, + output_file=None) + self.assertEqual(resp, 1) + + def _get_functest_config(self, var): + return var + + @mock.patch('functest.utils.functest_utils.logger.error') + def test_get_dict_by_test(self, mock_logger_error): + with mock.patch('__builtin__.open', mock.mock_open()), \ + mock.patch('functest.utils.functest_utils.yaml.safe_load') \ + as mock_yaml, \ + mock.patch('functest.utils.functest_utils.get_testcases_' + 'file_dir'): + mock_obj = mock.Mock() + attrs = {'get.return_value': [{'testcases': [self.testcase_dict]}]} + mock_obj.configure_mock(**attrs) + + mock_yaml.return_value = mock_obj + + self.assertDictEqual(functest_utils. + get_dict_by_test(self.testname), + self.testcase_dict) + + @mock.patch('functest.utils.functest_utils.get_dict_by_test') + def test_get_criteria_by_test_default(self, mock_get_dict_by_test): + mock_get_dict_by_test.return_value = self.testcase_dict + self.assertEqual(functest_utils.get_criteria_by_test(self.testname), + self.criteria) + + @mock.patch('functest.utils.functest_utils.get_dict_by_test') + def test_get_criteria_by_test_failed(self, mock_get_dict_by_test): + mock_get_dict_by_test.return_value = None + self.assertIsNone(functest_utils.get_criteria_by_test(self.testname)) + + def test_get_parameter_from_yaml_failed(self): + self.file_yaml['general'] = None + with mock.patch('__builtin__.open', mock.mock_open()), \ + mock.patch('functest.utils.functest_utils.yaml.safe_load') \ + as mock_yaml, \ + self.assertRaises(ValueError) as excep: + mock_yaml.return_value = self.file_yaml + functest_utils.get_parameter_from_yaml(self.parameter, + self.test_file) + self.assertTrue(("The parameter %s is not" + " defined in config_functest.yaml" % + self.parameter) in excep.exception) + + def test_get_parameter_from_yaml_default(self): + with mock.patch('__builtin__.open', mock.mock_open()), \ + mock.patch('functest.utils.functest_utils.yaml.safe_load') \ + as mock_yaml: + mock_yaml.return_value = self.file_yaml + self.assertEqual(functest_utils. + get_parameter_from_yaml(self.parameter, + self.test_file), + 'test_image_name') + + @mock.patch('functest.utils.functest_utils.get_parameter_from_yaml') + def test_get_functest_config_default(self, mock_get_parameter_from_yaml): + with mock.patch.dict(os.environ, + {'CONFIG_FUNCTEST_YAML': self.config_yaml}): + functest_utils.get_functest_config(self.parameter) + mock_get_parameter_from_yaml. \ + assert_called_once_with(self.parameter, + self.config_yaml) + + def test_check_success_rate_default(self): + with mock.patch('functest.utils.functest_utils.get_criteria_by_test') \ + as mock_criteria: + mock_criteria.return_value = self.criteria + resp = functest_utils.check_success_rate(self.case_name, + self.success_rate) + self.assertEqual(resp, 'PASS') + + def test_check_success_rate_failed(self): + with mock.patch('functest.utils.functest_utils.get_criteria_by_test') \ + as mock_criteria: + mock_criteria.return_value = self.criteria + resp = functest_utils.check_success_rate(self.case_name, + 3.0) + self.assertEqual(resp, 'FAIL') + + # TODO: merge_dicts + + def test_get_testcases_file_dir(self): + resp = functest_utils.get_testcases_file_dir() + self.assertEqual(resp, + "/home/opnfv/repos/functest/" + "functest/ci/testcases.yaml") + + def test_get_functest_yaml(self): + with mock.patch('__builtin__.open', mock.mock_open()), \ + mock.patch('functest.utils.functest_utils.yaml.safe_load') \ + as mock_yaml: + mock_yaml.return_value = self.file_yaml + resp = functest_utils.get_functest_yaml() + self.assertEqual(resp, self.file_yaml) + + @mock.patch('functest.utils.functest_utils.logger.info') + def test_print_separator(self, mock_logger_info): + functest_utils.print_separator() + mock_logger_info.assert_called_once_with("=======================" + "=======================") + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/functest/tests/unit/utils/test_openstack_clean.py b/functest/tests/unit/utils/test_openstack_clean.py new file mode 100644 index 000000000..28eab4f8a --- /dev/null +++ b/functest/tests/unit/utils/test_openstack_clean.py @@ -0,0 +1,671 @@ +#!/usr/bin/env python + +# 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 logging +import mock +import unittest + +from functest.utils import openstack_clean +from functest.tests.unit import test_utils + + +class OSCleanTesting(unittest.TestCase): + + logging.disable(logging.CRITICAL) + + def _get_instance(self, key): + mock_obj = mock.Mock() + attrs = {'id': 'id' + str(key), 'name': 'name' + str(key), + 'ip': 'ip' + str(key)} + mock_obj.configure_mock(**attrs) + return mock_obj + + def setUp(self): + self.client = mock.Mock() + self.test_list = [self._get_instance(1), self._get_instance(2)] + self.update_list = {'id1': 'name1', 'id2': 'name2'} + self.remove_list = {'id3': 'name3', 'id4': 'name4'} + self.test_dict_list = [{'id': 'id1', 'name': 'name1', 'ip': 'ip1', + 'router:external': False, + 'external_gateway_info': None}, + {'id': 'id2', 'name': 'name2', 'ip': 'ip2', + 'router:external': False, + 'external_gateway_info': None}] + self.routers = [mock.Mock()] + self.ports = [mock.Mock()] + + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_separator(self, mock_logger_debug): + openstack_clean.separator() + mock_logger_debug.assert_called_once_with("-----------------" + "-----------------" + "---------") + + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_instances(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_instances', return_value=self.test_list): + openstack_clean.remove_instances(self.client, self.update_list) + mock_logger_debug.assert_any_call("Removing Nova instances...") + mock_logger_debug.assert_any_call(" > this is a default " + "instance and will " + "NOT be deleted.") + + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_instances_missing_instances(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_instances', return_value=[]): + openstack_clean.remove_instances(self.client, self.update_list) + mock_logger_debug.assert_any_call("Removing Nova instances...") + mock_logger_debug.assert_any_call("No instances found.") + + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_instances_delete_success(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_instances', return_value=self.test_list), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.delete_instance', return_value=True): + openstack_clean.remove_instances(self.client, self.remove_list) + mock_logger_debug.assert_any_call("Removing Nova instances...") + mock_logger_debug.assert_any_call(" > Request sent.") + mock_logger_debug.assert_any_call(test_utils.RegexMatch("Removing" + " instance" + " '\s*\S+'" + " ...")) + + @mock.patch('functest.utils.openstack_clean.logger.error') + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_instances_delete_failed(self, mock_logger_debug, + mock_logger_error): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_instances', return_value=self.test_list), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.delete_instance', return_value=False): + openstack_clean.remove_instances(self.client, self.remove_list) + mock_logger_debug.assert_any_call("Removing Nova instances...") + mock_logger_error.assert_any_call(test_utils. + RegexMatch("There has been a " + "problem removing " + "the instance \s*\S+" + "...")) + mock_logger_debug.assert_any_call(test_utils.RegexMatch("Removing" + " instance" + " '\s*\S+'" + " ...")) + + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_images(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_images', return_value=self.test_list): + openstack_clean.remove_images(self.client, self.update_list) + mock_logger_debug.assert_any_call("Removing Glance images...") + mock_logger_debug.assert_any_call(" > this is a default " + "image and will " + "NOT be deleted.") + + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_images_missing_images(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_images', return_value=[]): + openstack_clean.remove_images(self.client, self.update_list) + mock_logger_debug.assert_any_call("Removing Glance images...") + mock_logger_debug.assert_any_call("No images found.") + + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_images_delete_success(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_images', return_value=self.test_list), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.delete_glance_image', return_value=True): + openstack_clean.remove_images(self.client, self.remove_list) + mock_logger_debug.assert_any_call("Removing Glance images...") + mock_logger_debug.assert_any_call(" > Done!") + mock_logger_debug.assert_any_call(test_utils. + RegexMatch("Removing image " + "\s*\S+," + " ID=\s*\S+ ...")) + + @mock.patch('functest.utils.openstack_clean.logger.error') + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_images_delete_failed(self, mock_logger_debug, + mock_logger_error): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_images', return_value=self.test_list), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.delete_glance_image', return_value=False): + openstack_clean.remove_images(self.client, self.remove_list) + mock_logger_debug.assert_any_call("Removing Glance images...") + mock_logger_error.assert_any_call(test_utils. + RegexMatch("There has been a " + "problem removing the" + "image \s*\S+...")) + mock_logger_debug.assert_any_call(test_utils. + RegexMatch("Removing image " + "\s*\S+," + " ID=\s*\S+ ...")) + + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_volumes(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_volumes', return_value=self.test_list): + openstack_clean.remove_volumes(self.client, self.update_list) + mock_logger_debug.assert_any_call("Removing Cinder volumes...") + mock_logger_debug.assert_any_call(" > this is a default " + "volume and will " + "NOT be deleted.") + + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_volumes_missing_volumes(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_volumes', return_value=[]): + openstack_clean.remove_volumes(self.client, self.update_list) + mock_logger_debug.assert_any_call("Removing Cinder volumes...") + mock_logger_debug.assert_any_call("No volumes found.") + + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_volumes_delete_success(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_volumes', return_value=self.test_list), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.delete_volume', return_value=True): + openstack_clean.remove_volumes(self.client, self.remove_list) + mock_logger_debug.assert_any_call("Removing Cinder volumes...") + mock_logger_debug.assert_any_call(" > Done!") + mock_logger_debug.assert_any_call(test_utils. + RegexMatch("Removing cinder " + "volume \s*\S+ ...")) + + @mock.patch('functest.utils.openstack_clean.logger.error') + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_volumes_delete_failed(self, mock_logger_debug, + mock_logger_error): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_volumes', return_value=self.test_list), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.delete_volume', return_value=False): + openstack_clean.remove_volumes(self.client, self.remove_list) + mock_logger_debug.assert_any_call("Removing Cinder volumes...") + mock_logger_error.assert_any_call(test_utils. + RegexMatch("There has been a " + "problem removing " + "the " + "volume \s*\S+...")) + mock_logger_debug.assert_any_call(test_utils. + RegexMatch("Removing cinder " + "volume \s*\S+ ...")) + + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_floatingips(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_floating_ips', return_value=self.test_list): + openstack_clean.remove_floatingips(self.client, self.update_list) + mock_logger_debug.assert_any_call("Removing floating IPs...") + mock_logger_debug.assert_any_call(" > this is a default " + "floating IP and will " + "NOT be deleted.") + + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_floatingips_missing_floatingips(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_floating_ips', return_value=[]): + openstack_clean.remove_floatingips(self.client, self.update_list) + mock_logger_debug.assert_any_call("Removing floating IPs...") + mock_logger_debug.assert_any_call("No floating IPs found.") + + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_floatingips_delete_success(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_floating_ips', return_value=self.test_list), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.delete_volume', return_value=True): + openstack_clean.remove_floatingips(self.client, self.remove_list) + mock_logger_debug.assert_any_call("Removing floating IPs...") + mock_logger_debug.assert_any_call(" > Done!") + mock_logger_debug.assert_any_call(test_utils. + RegexMatch("Removing floating " + "IP \s*\S+ ...")) + + @mock.patch('functest.utils.openstack_clean.logger.error') + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_floatingips_delete_failed(self, mock_logger_debug, + mock_logger_error): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_floating_ips', return_value=self.test_list), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.delete_floating_ip', return_value=False): + openstack_clean.remove_floatingips(self.client, self.remove_list) + mock_logger_debug.assert_any_call("Removing floating IPs...") + mock_logger_error.assert_any_call(test_utils. + RegexMatch("There has been a " + "problem removing " + "the floating IP " + "\s*\S+...")) + mock_logger_debug.assert_any_call(test_utils. + RegexMatch("Removing floating " + "IP \s*\S+ ...")) + + @mock.patch('functest.utils.openstack_clean.remove_routers') + @mock.patch('functest.utils.openstack_clean.remove_ports') + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_networks(self, mock_logger_debug, + mock_remove_ports, + mock_remove_routers): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_network_list', + return_value=self.test_dict_list), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.get_port_list', return_value=self.ports), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.get_router_list', return_value=self.routers): + openstack_clean.remove_networks(self.client, self.update_list, + self.update_list) + mock_logger_debug.assert_any_call("Removing Neutron objects") + mock_logger_debug.assert_any_call(" > this is a default " + "network and will " + "NOT be deleted.") + mock_remove_ports.assert_called_once_with(self.client, self.ports, + []) + mock_remove_routers.assert_called_once_with(self.client, + self.routers, + self.update_list) + + @mock.patch('functest.utils.openstack_clean.remove_routers') + @mock.patch('functest.utils.openstack_clean.remove_ports') + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_networks_missing_networks(self, mock_logger_debug, + mock_remove_ports, + mock_remove_routers): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_network_list', return_value=None), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.get_port_list', return_value=self.ports), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.get_router_list', return_value=self.routers): + openstack_clean.remove_networks(self.client, self.update_list, + self.update_list) + mock_logger_debug.assert_any_call("Removing Neutron objects") + mock_logger_debug.assert_any_call("There are no networks in the" + " deployment. ") + mock_remove_ports.assert_called_once_with(self.client, self.ports, + []) + mock_remove_routers.assert_called_once_with(self.client, + self.routers, + self.update_list) + + @mock.patch('functest.utils.openstack_clean.remove_routers') + @mock.patch('functest.utils.openstack_clean.remove_ports') + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_networks_delete_success(self, mock_logger_debug, + mock_remove_ports, + mock_remove_routers): + + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_network_list', + return_value=self.test_dict_list), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.delete_neutron_net', return_value=True), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.get_port_list', return_value=self.ports), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.get_router_list', return_value=self.routers): + openstack_clean.remove_networks(self.client, self.remove_list, + self.remove_list) + mock_logger_debug.assert_any_call("Removing Neutron objects") + mock_logger_debug.assert_any_call(" > this network will be " + "deleted.") + mock_logger_debug.assert_any_call(" > Done!") + mock_logger_debug.assert_any_call(test_utils. + RegexMatch("Removing network " + "\s*\S+ ...")) + mock_remove_ports.assert_called_once_with(self.client, self.ports, + ['id1', 'id2']) + mock_remove_routers.assert_called_once_with(self.client, + self.routers, + self.remove_list) + + @mock.patch('functest.utils.openstack_clean.remove_routers') + @mock.patch('functest.utils.openstack_clean.remove_ports') + @mock.patch('functest.utils.openstack_clean.logger.error') + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_networks_delete_failed(self, mock_logger_debug, + mock_logger_error, + mock_remove_ports, + mock_remove_routers): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_network_list', + return_value=self.test_dict_list), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.delete_neutron_net', return_value=False), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.get_port_list', return_value=self.ports), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.get_router_list', return_value=self.routers): + openstack_clean.remove_networks(self.client, self.remove_list, + self.remove_list) + mock_logger_debug.assert_any_call("Removing Neutron objects") + mock_logger_error.assert_any_call(test_utils. + RegexMatch("There has been a" + " problem removing" + " the network \s*\S+" + "...")) + mock_logger_debug.assert_any_call(test_utils. + RegexMatch("Removing network " + "\s*\S+ ...")) + mock_remove_ports.assert_called_once_with(self.client, self.ports, + ['id1', 'id2']) + mock_remove_routers.assert_called_once_with(self.client, + self.routers, + self.remove_list) + + # TODO: ports + @mock.patch('functest.utils.openstack_clean.os_utils.update_neutron_port') + @mock.patch('functest.utils.openstack_clean.logger.error') + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_force_remove_port(self, mock_logger_debug, + mock_logger_error, + mock_update_neutron_port): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.delete_neutron_port', + return_value=True): + openstack_clean.force_remove_port(self.client, 'id') + mock_logger_debug.assert_any_call(" > Done!") + mock_logger_debug.assert_any_call(test_utils. + RegexMatch("Clearing device_" + "owner for port " + "\s*\S+ ...")) + + @mock.patch('functest.utils.openstack_clean.os_utils.update_neutron_port') + @mock.patch('functest.utils.openstack_clean.logger.error') + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_force_remove_port_failed(self, mock_logger_debug, + mock_logger_error, + mock_update_neutron_port): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.delete_neutron_port', + return_value=False): + openstack_clean.force_remove_port(self.client, 'id') + mock_logger_error.assert_any_call("There has been a " + "problem removing " + "the port id...") + mock_logger_debug.assert_any_call(test_utils. + RegexMatch("Clearing device_" + "owner for port " + "\s*\S+ ...")) + + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_routers_missing_routers(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.delete_neutron_router', + return_value=True): + openstack_clean.remove_routers(self.client, self.test_dict_list, + self.remove_list) + mock_logger_debug.assert_any_call("Router is not connected" + " to anything." + "Ready to remove...") + mock_logger_debug.assert_any_call(" > Done!") + mock_logger_debug.assert_any_call(test_utils. + RegexMatch("Removing router " + "\s*\S+(\s*\S+) ...")) + + @mock.patch('functest.utils.openstack_clean.logger.error') + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_routers_failed(self, mock_logger_debug, + mock_logger_error): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.delete_neutron_router', + return_value=False): + openstack_clean.remove_routers(self.client, self.test_dict_list, + self.remove_list) + mock_logger_debug.assert_any_call("Router is not connected" + " to anything." + "Ready to remove...") + mock_logger_debug.assert_any_call(test_utils. + RegexMatch("Removing router " + "\s*\S+(\s*\S+) ...")) + mock_logger_error.assert_any_call(test_utils. + RegexMatch("There has been " + "a problem" + " removing the " + "router \s*\S+(" + "\s*\S+)...")) + + @mock.patch('functest.utils.openstack_clean.logger.error') + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_missing_external_gateway(self, mock_logger_debug, + mock_logger_error): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.delete_neutron_router', + return_value=False), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.remove_gateway_router', + return_value=False): + self.test_dict_list[0]['external_gateway_info'] = mock.Mock() + openstack_clean.remove_routers(self.client, self.test_dict_list, + self.remove_list) + mock_logger_debug.assert_any_call("Router has gateway to external" + " network.Removing link...") + mock_logger_error.assert_any_call("There has been a problem " + "removing the gateway...") + mock_logger_debug.assert_any_call(test_utils. + RegexMatch("Removing router " + "\s*\S+(\s*\S+) ...")) + mock_logger_error.assert_any_call(test_utils. + RegexMatch("There has been " + "a problem" + " removing the " + "router \s*\S+(" + "\s*\S+)...")) + + @mock.patch('functest.utils.openstack_clean.logger.debug') + def remove_security_groups(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_security_groups', + return_value=self.test_dict_list): + openstack_clean.remove_security_groups(self.client, + self.update_list) + mock_logger_debug.assert_any_call("Removing Security groups...") + mock_logger_debug.assert_any_call(" > this is a default " + "security group and will NOT " + "be deleted.") + + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_security_groups_missing_sec_group(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_security_groups', return_value=[]): + openstack_clean.remove_security_groups(self.client, + self.update_list) + mock_logger_debug.assert_any_call("Removing Security groups...") + mock_logger_debug.assert_any_call("No security groups found.") + + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_security_groups_delete_success(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_security_groups', + return_value=self.test_dict_list), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.delete_security_group', return_value=True): + openstack_clean.remove_security_groups(self.client, + self.remove_list) + mock_logger_debug.assert_any_call("Removing Security groups...") + mock_logger_debug.assert_any_call(" > Done!") + mock_logger_debug.assert_any_call(test_utils. + RegexMatch(" Removing \s*\S+" + "...")) + + @mock.patch('functest.utils.openstack_clean.logger.error') + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_security_groups_delete_failed(self, mock_logger_debug, + mock_logger_error): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_security_groups', + return_value=self.test_dict_list), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.delete_security_group', return_value=False): + openstack_clean.remove_security_groups(self.client, + self.remove_list) + mock_logger_debug.assert_any_call("Removing Security groups...") + mock_logger_error.assert_any_call(test_utils. + RegexMatch("There has been a " + "problem removing " + "the security group" + " \s*\S+...")) + mock_logger_debug.assert_any_call(test_utils. + RegexMatch(" Removing \s*\S+" + "...")) + + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_users(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_users', return_value=self.test_list): + openstack_clean.remove_users(self.client, self.update_list) + mock_logger_debug.assert_any_call("Removing Users...") + mock_logger_debug.assert_any_call(" > this is a default " + "user and will " + "NOT be deleted.") + + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_users_missing_users(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_users', return_value=None): + openstack_clean.remove_users(self.client, self.update_list) + mock_logger_debug.assert_any_call("Removing Users...") + mock_logger_debug.assert_any_call("There are no users in" + " the deployment. ") + + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_users_delete_success(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_users', return_value=self.test_list), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.delete_user', return_value=True): + openstack_clean.remove_users(self.client, self.remove_list) + mock_logger_debug.assert_any_call("Removing Users...") + mock_logger_debug.assert_any_call(" > Done!") + mock_logger_debug.assert_any_call(test_utils. + RegexMatch(" Removing " + "\s*\S+...")) + + @mock.patch('functest.utils.openstack_clean.logger.error') + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_users_delete_failed(self, mock_logger_debug, + mock_logger_error): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_users', return_value=self.test_list), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.delete_user', return_value=False): + openstack_clean.remove_users(self.client, self.remove_list) + mock_logger_debug.assert_any_call("Removing Users...") + mock_logger_error.assert_any_call(test_utils. + RegexMatch("There has been a " + "problem removing " + "the user \s*\S+" + "...")) + mock_logger_debug.assert_any_call(test_utils. + RegexMatch(" Removing " + "\s*\S+...")) + + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_tenants(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_tenants', return_value=self.test_list): + openstack_clean.remove_tenants(self.client, self.update_list) + mock_logger_debug.assert_any_call("Removing Tenants...") + mock_logger_debug.assert_any_call(" > this is a default" + " tenant and will " + "NOT be deleted.") + + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_tenants_missing_tenants(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_tenants', return_value=None): + openstack_clean.remove_tenants(self.client, self.update_list) + mock_logger_debug.assert_any_call("Removing Tenants...") + mock_logger_debug.assert_any_call("There are no tenants in" + " the deployment. ") + + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_tenants_delete_success(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_tenants', return_value=self.test_list), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.delete_tenant', return_value=True): + openstack_clean.remove_tenants(self.client, self.remove_list) + mock_logger_debug.assert_any_call("Removing Tenants...") + mock_logger_debug.assert_any_call(" > Done!") + mock_logger_debug.assert_any_call(test_utils. + RegexMatch(" Removing " + "\s*\S+...")) + + @mock.patch('functest.utils.openstack_clean.logger.error') + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_remove_tenants_delete_failed(self, mock_logger_debug, + mock_logger_error): + with mock.patch('functest.utils.openstack_clean.os_utils' + '.get_tenants', return_value=self.test_list), \ + mock.patch('functest.utils.openstack_clean.os_utils' + '.delete_tenant', return_value=False): + openstack_clean.remove_tenants(self.client, self.remove_list) + mock_logger_debug.assert_any_call("Removing Tenants...") + mock_logger_error.assert_any_call(test_utils. + RegexMatch("There has been a " + "problem removing " + "the tenant \s*\S+" + "...")) + mock_logger_debug.assert_any_call(test_utils. + RegexMatch(" Removing " + "\s*\S+...")) + + @mock.patch('functest.utils.openstack_clean.os_utils.get_cinder_client') + @mock.patch('functest.utils.openstack_clean.os_utils' + '.get_keystone_client') + @mock.patch('functest.utils.openstack_clean.os_utils' + '.get_neutron_client') + @mock.patch('functest.utils.openstack_clean.os_utils.get_nova_client') + @mock.patch('functest.utils.openstack_clean.os_utils.check_credentials', + return_value=True) + @mock.patch('functest.utils.openstack_clean.logger.info') + @mock.patch('functest.utils.openstack_clean.logger.debug') + def test_main_default(self, mock_logger_debug, mock_logger_info, + mock_creds, mock_nova, mock_neutron, + mock_keystone, mock_cinder): + + with mock.patch('functest.utils.openstack_clean.remove_instances') \ + as mock_remove_instances, \ + mock.patch('functest.utils.openstack_clean.remove_images') \ + as mock_remove_images, \ + mock.patch('functest.utils.openstack_clean.remove_volumes') \ + as mock_remove_volumes, \ + mock.patch('functest.utils.openstack_clean.remove_floatingips') \ + as mock_remove_floatingips, \ + mock.patch('functest.utils.openstack_clean.remove_networks') \ + as mock_remove_networks, \ + mock.patch('functest.utils.openstack_clean.' + 'remove_security_groups') \ + as mock_remove_security_groups, \ + mock.patch('functest.utils.openstack_clean.remove_users') \ + as mock_remove_users, \ + mock.patch('functest.utils.openstack_clean.remove_tenants') \ + as mock_remove_tenants, \ + mock.patch('functest.utils.openstack_clean.yaml.safe_load', + return_value=mock.Mock()), \ + mock.patch('__builtin__.open', mock.mock_open()) as m: + openstack_clean.main() + self.assertTrue(mock_remove_instances) + self.assertTrue(mock_remove_images) + self.assertTrue(mock_remove_volumes) + self.assertTrue(mock_remove_floatingips) + self.assertTrue(mock_remove_networks) + self.assertTrue(mock_remove_security_groups) + self.assertTrue(mock_remove_users) + self.assertTrue(mock_remove_tenants) + m.assert_called_once_with(openstack_clean.OS_SNAPSHOT_FILE) + mock_logger_info.assert_called_once_with("Cleaning OpenStack " + "resources...") + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/functest/tests/unit/utils/test_openstack_snapshot.py b/functest/tests/unit/utils/test_openstack_snapshot.py new file mode 100644 index 000000000..52744db1c --- /dev/null +++ b/functest/tests/unit/utils/test_openstack_snapshot.py @@ -0,0 +1,235 @@ +#!/usr/bin/env python + +# 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 logging +import mock +import unittest + +from functest.utils import openstack_snapshot + + +class OSTackerTesting(unittest.TestCase): + + logging.disable(logging.CRITICAL) + + def _get_instance(self, key): + mock_obj = mock.Mock() + attrs = {'id': 'id' + str(key), 'name': 'name' + str(key), + 'ip': 'ip' + str(key)} + mock_obj.configure_mock(**attrs) + return mock_obj + + def setUp(self): + self.client = mock.Mock() + self.test_list = [self._get_instance(1), self._get_instance(2)] + self.update_list = {'id1': 'name1', 'id2': 'name2'} + self.update_floatingips = {'id1': 'ip1', 'id2': 'ip2'} + self.test_dict_list = [{'id': 'id1', 'name': 'name1', 'ip': 'ip1'}, + {'id': 'id2', 'name': 'name2', 'ip': 'ip2'}] + + @mock.patch('functest.utils.openstack_snapshot.logger.info') + def test_separator(self, mock_logger_info): + openstack_snapshot.separator() + mock_logger_info.assert_called_once_with("-----------------" + "-----------------" + "---------") + + @mock.patch('functest.utils.openstack_snapshot.logger.debug') + def test_get_instances(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_snapshot.os_utils' + '.get_instances', return_value=self.test_list): + resp = openstack_snapshot.get_instances(self.client) + mock_logger_debug.assert_called_once_with("Getting instances...") + self.assertDictEqual(resp, {'instances': self.update_list}) + + @mock.patch('functest.utils.openstack_snapshot.logger.debug') + def test_get_instances_missing_instances(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_snapshot.os_utils' + '.get_instances', return_value=[]): + resp = openstack_snapshot.get_instances(self.client) + mock_logger_debug.assert_called_once_with("Getting instances...") + self.assertDictEqual(resp, {'instances': {}}) + + @mock.patch('functest.utils.openstack_snapshot.logger.debug') + def test_get_images(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_snapshot.os_utils' + '.get_images', return_value=self.test_list): + resp = openstack_snapshot.get_images(self.client) + mock_logger_debug.assert_called_once_with("Getting images...") + self.assertDictEqual(resp, {'images': self.update_list}) + + @mock.patch('functest.utils.openstack_snapshot.logger.debug') + def test_get_images_missing_images(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_snapshot.os_utils' + '.get_images', return_value=[]): + resp = openstack_snapshot.get_images(self.client) + mock_logger_debug.assert_called_once_with("Getting images...") + self.assertDictEqual(resp, {'images': {}}) + + @mock.patch('functest.utils.openstack_snapshot.logger.debug') + def test_get_volumes(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_snapshot.os_utils' + '.get_volumes', return_value=self.test_list): + resp = openstack_snapshot.get_volumes(self.client) + mock_logger_debug.assert_called_once_with("Getting volumes...") + self.assertDictEqual(resp, {'volumes': self.update_list}) + + @mock.patch('functest.utils.openstack_snapshot.logger.debug') + def test_get_volumes_missing_volumes(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_snapshot.os_utils' + '.get_volumes', return_value=[]): + resp = openstack_snapshot.get_volumes(self.client) + mock_logger_debug.assert_called_once_with("Getting volumes...") + self.assertDictEqual(resp, {'volumes': {}}) + + @mock.patch('functest.utils.openstack_snapshot.logger.debug') + def test_get_networks(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_snapshot.os_utils' + '.get_network_list', return_value=self.test_dict_list): + resp = openstack_snapshot.get_networks(self.client) + mock_logger_debug.assert_called_once_with("Getting networks") + self.assertDictEqual(resp, {'networks': self.update_list}) + + @mock.patch('functest.utils.openstack_snapshot.logger.debug') + def test_get_networks_missing_networks(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_snapshot.os_utils' + '.get_network_list', return_value=[]): + resp = openstack_snapshot.get_networks(self.client) + mock_logger_debug.assert_called_once_with("Getting networks") + self.assertDictEqual(resp, {'networks': {}}) + + @mock.patch('functest.utils.openstack_snapshot.logger.debug') + def test_get_routers(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_snapshot.os_utils' + '.get_router_list', return_value=self.test_dict_list): + resp = openstack_snapshot.get_routers(self.client) + mock_logger_debug.assert_called_once_with("Getting routers") + self.assertDictEqual(resp, {'routers': self.update_list}) + + @mock.patch('functest.utils.openstack_snapshot.logger.debug') + def test_get_routers_missing_routers(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_snapshot.os_utils' + '.get_router_list', return_value=[]): + resp = openstack_snapshot.get_routers(self.client) + mock_logger_debug.assert_called_once_with("Getting routers") + self.assertDictEqual(resp, {'routers': {}}) + + @mock.patch('functest.utils.openstack_snapshot.logger.debug') + def test_get_secgroups(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_snapshot.os_utils' + '.get_security_groups', + return_value=self.test_dict_list): + resp = openstack_snapshot.get_security_groups(self.client) + mock_logger_debug.assert_called_once_with("Getting Security " + "groups...") + self.assertDictEqual(resp, {'secgroups': self.update_list}) + + @mock.patch('functest.utils.openstack_snapshot.logger.debug') + def test_get_secgroups_missing_secgroups(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_snapshot.os_utils' + '.get_security_groups', return_value=[]): + resp = openstack_snapshot.get_security_groups(self.client) + mock_logger_debug.assert_called_once_with("Getting Security " + "groups...") + self.assertDictEqual(resp, {'secgroups': {}}) + + @mock.patch('functest.utils.openstack_snapshot.logger.debug') + def test_get_floatingips(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_snapshot.os_utils' + '.get_floating_ips', return_value=self.test_list): + resp = openstack_snapshot.get_floatinips(self.client) + mock_logger_debug.assert_called_once_with("Getting Floating " + "IPs...") + self.assertDictEqual(resp, {'floatingips': + self.update_floatingips}) + + @mock.patch('functest.utils.openstack_snapshot.logger.debug') + def test_get_floatingips_missing_floatingips(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_snapshot.os_utils' + '.get_floating_ips', return_value=[]): + resp = openstack_snapshot.get_floatinips(self.client) + mock_logger_debug.assert_called_once_with("Getting Floating " + "IPs...") + self.assertDictEqual(resp, {'floatingips': {}}) + + @mock.patch('functest.utils.openstack_snapshot.logger.debug') + def test_get_users(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_snapshot.os_utils' + '.get_users', return_value=self.test_list): + resp = openstack_snapshot.get_users(self.client) + mock_logger_debug.assert_called_once_with("Getting users...") + self.assertDictEqual(resp, {'users': self.update_list}) + + @mock.patch('functest.utils.openstack_snapshot.logger.debug') + def test_get_users_missing_users(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_snapshot.os_utils' + '.get_users', return_value=[]): + resp = openstack_snapshot.get_users(self.client) + mock_logger_debug.assert_called_once_with("Getting users...") + self.assertDictEqual(resp, {'users': {}}) + + @mock.patch('functest.utils.openstack_snapshot.logger.debug') + def test_get_tenants(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_snapshot.os_utils' + '.get_tenants', return_value=self.test_list): + resp = openstack_snapshot.get_tenants(self.client) + mock_logger_debug.assert_called_once_with("Getting tenants...") + self.assertDictEqual(resp, {'tenants': self.update_list}) + + @mock.patch('functest.utils.openstack_snapshot.logger.debug') + def test_get_tenants_missing_tenants(self, mock_logger_debug): + with mock.patch('functest.utils.openstack_snapshot.os_utils' + '.get_tenants', return_value=[]): + resp = openstack_snapshot.get_tenants(self.client) + mock_logger_debug.assert_called_once_with("Getting tenants...") + self.assertDictEqual(resp, {'tenants': {}}) + + @mock.patch('functest.utils.openstack_snapshot.os_utils.get_cinder_client') + @mock.patch('functest.utils.openstack_snapshot.os_utils' + '.get_keystone_client') + @mock.patch('functest.utils.openstack_snapshot.os_utils' + '.get_neutron_client') + @mock.patch('functest.utils.openstack_snapshot.os_utils.get_nova_client') + @mock.patch('functest.utils.openstack_snapshot.os_utils.check_credentials') + @mock.patch('functest.utils.openstack_snapshot.logger.info') + @mock.patch('functest.utils.openstack_snapshot.logger.debug') + def test_main_default(self, mock_logger_debug, mock_logger_info, + mock_creds, mock_nova, mock_neutron, + mock_keystone, mock_cinder): + with mock.patch('functest.utils.openstack_snapshot.get_instances', + return_value=self.update_list), \ + mock.patch('functest.utils.openstack_snapshot.get_images', + return_value=self.update_list), \ + mock.patch('functest.utils.openstack_snapshot.get_images', + return_value=self.update_list), \ + mock.patch('functest.utils.openstack_snapshot.get_volumes', + return_value=self.update_list), \ + mock.patch('functest.utils.openstack_snapshot.get_networks', + return_value=self.update_list), \ + mock.patch('functest.utils.openstack_snapshot.get_routers', + return_value=self.update_list), \ + mock.patch('functest.utils.openstack_snapshot.get_security_groups', + return_value=self.update_list), \ + mock.patch('functest.utils.openstack_snapshot.get_floatinips', + return_value=self.update_floatingips), \ + mock.patch('functest.utils.openstack_snapshot.get_users', + return_value=self.update_list), \ + mock.patch('functest.utils.openstack_snapshot.get_tenants', + return_value=self.update_list), \ + mock.patch('__builtin__.open', mock.mock_open()) as m: + openstack_snapshot.main() + mock_logger_info.assert_called_once_with("Generating OpenStack " + "snapshot...") + m.assert_called_once_with(openstack_snapshot.OS_SNAPSHOT_FILE, + 'w+') + mock_logger_debug.assert_any_call("NOTE: These objects will " + "NOT be deleted after " + + "running the test.") + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/functest/tests/unit/utils/test_openstack_tacker.py b/functest/tests/unit/utils/test_openstack_tacker.py new file mode 100644 index 000000000..a8330c0ed --- /dev/null +++ b/functest/tests/unit/utils/test_openstack_tacker.py @@ -0,0 +1,455 @@ +#!/usr/bin/env python + +# 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 logging +import mock +import unittest + +from functest.utils import openstack_tacker +from functest.tests.unit import test_utils + + +class OSTackerTesting(unittest.TestCase): + + logging.disable(logging.CRITICAL) + + def setUp(self): + self.tacker_client = mock.Mock() + self.getresponse = {'vnfds': [{'id': 'test_id'}], + 'vnfs': [{'id': 'test_id'}], + 'sfcs': [{'id': 'test_id'}]} + self.vnfdlist = {'vnfds': [{'id': 'test_vnfd1'}, {'id': 'test_vnfd2'}]} + self.vnflist = {'vnfs': [{'id': 'test_vnf1'}, {'id': 'test_vnf2'}]} + self.sfclist = {'sfcs': [{'id': 'test_sfc1'}, {'id': 'test_sfc2'}]} + self.sfc_classifierlist = {'sfc_classifiers': [{'id': 'test_sfc_cl1'}, + {'id': 'test_sfc_cl2'}]} + + self.createvnfd = {"vnfd": {"attributes": {"vnfd": 'vnfd_body'}}} + self.createvnf = {"vnf": {"attributes": {"vnf": 'vnf_body'}}} + self.createsfc = {"sfc": {"attributes": {"sfc": 'sfc_body'}}} + self.createsfc_clf = {"sfc_classifier": {"attributes": + {"sfc_clf": 'sfc_clf_body'}}} + + self.resource_type = 'vnfd' + self.resource_name = 'resource_name' + self.tosca_file = 'test_tosca_file' + self.vnfd = 'test_vnfd' + self.vnf = 'test_vnf' + self.sfc = 'test_sfc' + self.sfc_clf = 'test_sfc_clf' + + def _get_creds(self): + cred_dict = { + 'OS_USERNAME': 'username', + 'OS_PASSWORD': 'password', + 'OS_AUTH_URL': 'auth_url', + 'OS_TENANT_NAME': 'tenant_name', + 'OS_USER_DOMAIN_NAME': 'user_domain_name', + 'OS_PROJECT_DOMAIN_NAME': 'project_domain_name', + 'OS_PROJECT_NAME': 'project_name', + 'OS_ENDPOINT_TYPE': 'endpoint_type', + 'OS_REGION_NAME': 'region_name' + } + return cred_dict + + def test_get_id_from_name(self): + with mock.patch.object(self.tacker_client, 'get', + return_value=self.getresponse): + resp = openstack_tacker.get_id_from_name(self.tacker_client, + self.resource_type, + self.resource_name) + self.assertEqual(resp, 'test_id') + + @mock.patch('functest.utils.openstack_tacker.logger.error') + def test_get_id_from_name_exception(self, mock_logger_error): + with mock.patch.object(self.tacker_client, 'get', + side_effect=Exception): + resp = openstack_tacker.get_id_from_name(self.tacker_client, + self.resource_type, + self.resource_name) + self.assertIsNone(resp) + mock_logger_error.assert_called_once_with(test_utils. + SubstrMatch("Error [get" + "_id_from_" + "name(tacker" + "_client" + ", resource_" + "type, " + "resource_" + "name)]:")) + + @mock.patch('functest.utils.openstack_tacker.get_id_from_name') + def test_get_vnfd_id(self, mock_get_id_from_name): + openstack_tacker.get_vnfd_id(self.tacker_client, self.resource_name) + mock_get_id_from_name.assert_called_once_with(self.tacker_client, + 'vnfd', + self.resource_name) + + @mock.patch('functest.utils.openstack_tacker.get_id_from_name') + def test_get_vnf_id(self, mock_get_id_from_name): + openstack_tacker.get_vnf_id(self.tacker_client, self.resource_name) + mock_get_id_from_name.assert_called_once_with(self.tacker_client, + 'vnf', + self.resource_name) + + @mock.patch('functest.utils.openstack_tacker.get_id_from_name') + def test_get_sfc_id(self, mock_get_id_from_name): + openstack_tacker.get_sfc_id(self.tacker_client, self.resource_name) + mock_get_id_from_name.assert_called_once_with(self.tacker_client, + 'sfc', + self.resource_name) + + @mock.patch('functest.utils.openstack_tacker.get_id_from_name') + def test_get_sfc_classifier_id(self, mock_get_id_from_name): + openstack_tacker.get_sfc_classifier_id(self.tacker_client, + self.resource_name) + mock_get_id_from_name.assert_called_once_with(self.tacker_client, + 'sfc-classifier', + self.resource_name) + + def test_list_vnfds(self): + with mock.patch.object(self.tacker_client, 'list_vnfds', + return_value=self.vnfdlist): + resp = openstack_tacker.list_vnfds(self.tacker_client, + verbose=False) + self.assertEqual(resp, ['test_vnfd1', 'test_vnfd2']) + + def test_list_vnfds_verbose(self): + with mock.patch.object(self.tacker_client, 'list_vnfds', + return_value=self.vnfdlist): + resp = openstack_tacker.list_vnfds(self.tacker_client, + verbose=True) + self.assertEqual(resp, self.vnfdlist) + + @mock.patch('functest.utils.openstack_tacker.logger.error') + def test_list_vnfds_exception(self, mock_logger_error): + with mock.patch.object(self.tacker_client, 'list_vnfds', + side_effect=Exception): + resp = openstack_tacker.list_vnfds(self.tacker_client, + verbose=False) + mock_logger_error.assert_called_once_with(test_utils. + SubstrMatch("Error" + " [list" + "_vnfds(" + "tacker_" + "client)]:")) + self.assertIsNone(resp) + + def test_create_vnfd_missing_file(self): + with mock.patch.object(self.tacker_client, 'create_vnfd', + return_value=self.createvnfd): + resp = openstack_tacker.create_vnfd(self.tacker_client, + tosca_file=None) + self.assertEqual(resp, self.createvnfd) + + @mock.patch('functest.utils.openstack_tacker.logger.error') + def test_create_vnfd_default(self, mock_logger_error): + with mock.patch.object(self.tacker_client, 'create_vnfd', + return_value=self.createvnfd), \ + mock.patch('__builtin__.open', mock.mock_open(read_data='1')) \ + as m: + resp = openstack_tacker.create_vnfd(self.tacker_client, + tosca_file=self.tosca_file) + m.assert_called_once_with(self.tosca_file) + mock_logger_error.assert_called_once_with('1') + self.assertEqual(resp, self.createvnfd) + + @mock.patch('functest.utils.openstack_tacker.logger.exception') + def test_create_vnfd_exception(self, mock_logger_excep): + with mock.patch.object(self.tacker_client, 'create_vnfd', + side_effect=Exception): + resp = openstack_tacker.create_vnfd(self.tacker_client, + tosca_file=self.tosca_file) + mock_logger_excep.assert_called_once_with(test_utils. + SubstrMatch("Error" + " [create" + "_vnfd(" + "tacker_" + "client, " + "'%s')]" + % self. + tosca_file)) + self.assertIsNone(resp) + + def test_delete_vnfd(self): + with mock.patch('functest.utils.openstack_tacker.get_vnfd_id', + return_value=self.vnfd), \ + mock.patch.object(self.tacker_client, 'delete_vnfd', + return_value=self.vnfd): + resp = openstack_tacker.delete_vnfd(self.tacker_client, + vnfd_id='vnfd_id', + vnfd_name=self.vnfd) + self.assertEqual(resp, self.vnfd) + + # TODO: Exception('You need to provide an VNFD' + # 'id or name') AssertionError + + @mock.patch('functest.utils.openstack_tacker.logger.error') + def test_delete_vnfd_exception(self, mock_logger_error): + with mock.patch('functest.utils.openstack_tacker.get_vnfd_id', + return_value=self.vnfd), \ + mock.patch.object(self.tacker_client, 'delete_vnfd', + side_effect=Exception): + resp = openstack_tacker.delete_vnfd(self.tacker_client, + vnfd_id=None, + vnfd_name=None) + self.assertIsNone(resp) + self.assertTrue(mock_logger_error.called) + + def test_list_vnfs(self): + with mock.patch.object(self.tacker_client, 'list_vnfs', + return_value=self.vnflist): + resp = openstack_tacker.list_vnfs(self.tacker_client, + verbose=False) + self.assertEqual(resp, ['test_vnf1', 'test_vnf2']) + + def test_list_vnfs_verbose(self): + with mock.patch.object(self.tacker_client, 'list_vnfs', + return_value=self.vnflist): + resp = openstack_tacker.list_vnfs(self.tacker_client, + verbose=True) + self.assertEqual(resp, self.vnflist) + + @mock.patch('functest.utils.openstack_tacker.logger.error') + def test_list_vnfs_exception(self, mock_logger_error): + with mock.patch.object(self.tacker_client, 'list_vnfs', + side_effect=Exception): + resp = openstack_tacker.list_vnfs(self.tacker_client, + verbose=False) + mock_logger_error.assert_called_once_with(test_utils. + SubstrMatch("Error" + " [list" + "_vnfs(" + "tacker_" + "client)]:")) + self.assertIsNone(resp) + + def test_create_vnf_default(self): + with mock.patch.object(self.tacker_client, 'create_vnf', + return_value=self.createvnf), \ + mock.patch('functest.utils.openstack_tacker.get_vnfd_id', + return_value=self.vnf): + resp = openstack_tacker.create_vnf(self.tacker_client, + vnf_name=self.vnf, + vnfd_id='vnfd_id', + vnfd_name=self.vnfd) + self.assertEqual(resp, self.createvnf) + + @mock.patch('functest.utils.openstack_tacker.logger.error') + def test_create_vnf_exception(self, mock_logger_error): + with mock.patch.object(self.tacker_client, 'create_vnf', + side_effect=Exception): + resp = openstack_tacker.create_vnf(self.tacker_client, + vnf_name=self.vnf, + vnfd_id='vnfd_id', + vnfd_name=self.vnfd) + mock_logger_error.assert_called_once_with(test_utils. + SubstrMatch("error" + " [create" + "_vnf(" + "tacker_" + "client")) + self.assertIsNone(resp) + + # TODO: wait_for_vnf + + def test_delete_vnf(self): + with mock.patch('functest.utils.openstack_tacker.get_vnf_id', + return_value=self.vnf), \ + mock.patch.object(self.tacker_client, 'delete_vnf', + return_value=self.vnf): + resp = openstack_tacker.delete_vnf(self.tacker_client, + vnf_id='vnf_id', + vnf_name=self.vnf) + self.assertEqual(resp, self.vnf) + + # TODO: Exception('You need to provide an VNF' + # 'classifier id or name') AssertionError + + @mock.patch('functest.utils.openstack_tacker.logger.error') + def test_delete_vnf_exception(self, mock_logger_error): + with mock.patch('functest.utils.openstack_tacker.get_vnf_id', + return_value=self.vnf), \ + mock.patch.object(self.tacker_client, 'delete_vnf', + side_effect=Exception): + resp = openstack_tacker.delete_vnf(self.tacker_client, + vnf_id=None, + vnf_name=None) + self.assertIsNone(resp) + self.assertTrue(mock_logger_error.called) + + def test_list_sfcs(self): + with mock.patch.object(self.tacker_client, 'list_sfcs', + return_value=self.sfclist): + resp = openstack_tacker.list_sfcs(self.tacker_client, + verbose=False) + self.assertEqual(resp, ['test_sfc1', 'test_sfc2']) + + def test_list_sfcs_verbose(self): + with mock.patch.object(self.tacker_client, 'list_sfcs', + return_value=self.sfclist): + resp = openstack_tacker.list_sfcs(self.tacker_client, + verbose=True) + self.assertEqual(resp, self.sfclist) + + @mock.patch('functest.utils.openstack_tacker.logger.error') + def test_list_sfcs_exception(self, mock_logger_error): + with mock.patch.object(self.tacker_client, 'list_sfcs', + side_effect=Exception): + resp = openstack_tacker.list_sfcs(self.tacker_client, + verbose=False) + mock_logger_error.assert_called_once_with(test_utils. + SubstrMatch("Error" + " [list" + "_sfcs(" + "tacker_" + "client)]:")) + self.assertIsNone(resp) + + def test_create_sfc_default(self): + with mock.patch.object(self.tacker_client, 'create_sfc', + return_value=self.createsfc), \ + mock.patch('functest.utils.openstack_tacker.get_vnf_id', + return_value=self.vnf): + resp = openstack_tacker.create_sfc(self.tacker_client, + sfc_name=self.sfc, + chain_vnf_ids=['chain_vnf_id'], + chain_vnf_names=[self.vnf]) + self.assertEqual(resp, self.createsfc) + + @mock.patch('functest.utils.openstack_tacker.logger.error') + def test_create_sfc_exception(self, mock_logger_error): + with mock.patch.object(self.tacker_client, 'create_sfc', + side_effect=Exception): + resp = openstack_tacker.create_sfc(self.tacker_client, + sfc_name=self.sfc, + chain_vnf_ids=['chain_vnf_id'], + chain_vnf_names=[self.vnf]) + mock_logger_error.assert_called_once_with(test_utils. + SubstrMatch("error" + " [create" + "_sfc(" + "tacker_" + "client")) + self.assertIsNone(resp) + + def test_delete_sfc(self): + with mock.patch('functest.utils.openstack_tacker.get_sfc_id', + return_value=self.sfc), \ + mock.patch.object(self.tacker_client, 'delete_sfc', + return_value=self.sfc): + resp = openstack_tacker.delete_sfc(self.tacker_client, + sfc_id='sfc_id', + sfc_name=self.sfc) + self.assertEqual(resp, self.sfc) + + # TODO: Exception('You need to provide an SFC' + # 'id or name') AssertionError + + @mock.patch('functest.utils.openstack_tacker.logger.error') + def test_delete_sfc_exception(self, mock_logger_error): + with mock.patch('functest.utils.openstack_tacker.get_sfc_id', + return_value=self.sfc), \ + mock.patch.object(self.tacker_client, 'delete_sfc', + side_effect=Exception): + resp = openstack_tacker.delete_sfc(self.tacker_client, + sfc_id=None, + sfc_name=None) + self.assertIsNone(resp) + self.assertTrue(mock_logger_error.called) + + def test_list_sfc_classifiers(self): + with mock.patch.object(self.tacker_client, 'list_sfc_classifiers', + return_value=self.sfc_classifierlist): + resp = openstack_tacker.list_sfc_classifiers(self.tacker_client, + verbose=False) + self.assertEqual(resp, ['test_sfc_cl1', 'test_sfc_cl2']) + + def test_list_sfc_classifiers_verbose(self): + with mock.patch.object(self.tacker_client, 'list_sfc_classifiers', + return_value=self.sfc_classifierlist): + resp = openstack_tacker.list_sfc_classifiers(self.tacker_client, + verbose=True) + self.assertEqual(resp, self.sfc_classifierlist) + + @mock.patch('functest.utils.openstack_tacker.logger.error') + def test_list_sfc_classifiers_exception(self, mock_logger_error): + with mock.patch.object(self.tacker_client, 'list_sfc_classifiers', + side_effect=Exception): + resp = openstack_tacker.list_sfc_classifiers(self.tacker_client, + verbose=False) + mock_logger_error.assert_called_once_with(test_utils. + SubstrMatch("Error" + " [list" + "_sfc_cl" + "assifiers(" + "tacker_" + "client)]:")) + self.assertIsNone(resp) + + def test_create_sfc_classifier_default(self): + with mock.patch.object(self.tacker_client, 'create_sfc_classifier', + return_value=self.createsfc_clf), \ + mock.patch('functest.utils.openstack_tacker.get_sfc_id', + return_value=self.sfc): + cl = self.sfc_clf + resp = openstack_tacker.create_sfc_classifier(self.tacker_client, + sfc_clf_name=cl, + sfc_id='sfc_id', + sfc_name=self.sfc) + self.assertEqual(resp, self.createsfc_clf) + + @mock.patch('functest.utils.openstack_tacker.logger.error') + def test_sfc_classifier_exception(self, mock_logger_error): + with mock.patch.object(self.tacker_client, 'create_sfc_classifier', + side_effect=Exception): + cl = self.sfc_clf + resp = openstack_tacker.create_sfc_classifier(self.tacker_client, + sfc_clf_name=cl, + sfc_id='sfc_id', + sfc_name=self.sfc) + mock_logger_error.assert_called_once_with(test_utils. + SubstrMatch("error" + " [create" + "_sfc_cl" + "assifier(" + "tacker_" + "client")) + self.assertIsNone(resp) + + def test_delete_sfc_classifier(self): + with mock.patch('functest.utils.openstack_tacker.get_sfc_' + 'classifier_id', + return_value=self.sfc_clf), \ + mock.patch.object(self.tacker_client, 'delete_sfc_classifier', + return_value=self.sfc_clf): + cl = self.sfc_clf + resp = openstack_tacker.delete_sfc_classifier(self.tacker_client, + sfc_clf_id='sfc_id', + sfc_clf_name=cl) + self.assertEqual(resp, cl) + + # TODO: Exception('You need to provide an SFC' + # 'classifier id or name') AssertionError + + @mock.patch('functest.utils.openstack_tacker.logger.error') + def test_delete_sfc_classifier_exception(self, mock_logger_error): + with mock.patch('functest.utils.openstack_tacker.get_sfc_' + 'classifier_id', + return_value=self.sfc_clf), \ + mock.patch.object(self.tacker_client, 'delete_sfc_classifier', + side_effect=Exception): + cl = self.sfc_clf + resp = openstack_tacker.delete_sfc_classifier(self.tacker_client, + sfc_clf_id='sfc_id', + sfc_clf_name=cl) + self.assertIsNone(resp) + self.assertTrue(mock_logger_error.called) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/functest/tests/unit/utils/test_openstack_utils.py b/functest/tests/unit/utils/test_openstack_utils.py new file mode 100644 index 000000000..0f510414b --- /dev/null +++ b/functest/tests/unit/utils/test_openstack_utils.py @@ -0,0 +1,1688 @@ +#!/usr/bin/env python + +# 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 copy +import logging +import unittest + +import mock + +from functest.utils import openstack_utils + + +class OSUtilsTesting(unittest.TestCase): + + logging.disable(logging.CRITICAL) + + def _get_env_cred_dict(self, os_prefix=''): + return {'OS_USERNAME': os_prefix + 'username', + 'OS_PASSWORD': os_prefix + 'password', + 'OS_AUTH_URL': os_prefix + 'auth_url', + 'OS_TENANT_NAME': os_prefix + 'tenant_name', + 'OS_USER_DOMAIN_NAME': os_prefix + 'user_domain_name', + 'OS_PROJECT_DOMAIN_NAME': os_prefix + 'project_domain_name', + 'OS_PROJECT_NAME': os_prefix + 'project_name', + 'OS_ENDPOINT_TYPE': os_prefix + 'endpoint_type', + 'OS_REGION_NAME': os_prefix + 'region_name'} + + def _get_os_env_vars(self): + return {'username': 'test_username', 'password': 'test_password', + 'auth_url': 'test_auth_url', 'tenant_name': 'test_tenant_name', + 'user_domain_name': 'test_user_domain_name', + 'project_domain_name': 'test_project_domain_name', + 'project_name': 'test_project_name', + 'endpoint_type': 'test_endpoint_type', + 'region_name': 'test_region_name'} + + def setUp(self): + self.env_vars = ['OS_AUTH_URL', 'OS_USERNAME', 'OS_PASSWORD'] + self.tenant_name = 'test_tenant_name' + self.env_cred_dict = self._get_env_cred_dict() + self.os_environs = self._get_env_cred_dict(os_prefix='test_') + self.os_env_vars = self._get_os_env_vars() + + mock_obj = mock.Mock() + attrs = {'name': 'test_flavor', + 'id': 'flavor_id', + 'ram': 2} + mock_obj.configure_mock(**attrs) + self.flavor = mock_obj + + mock_obj = mock.Mock() + attrs = {'name': 'test_aggregate', + 'id': 'aggregate_id', + 'hosts': ['host_name']} + mock_obj.configure_mock(**attrs) + self.aggregate = mock_obj + + mock_obj = mock.Mock() + attrs = {'id': 'instance_id', + 'name': 'test_instance', + 'status': 'ok'} + mock_obj.configure_mock(**attrs) + self.instance = mock_obj + + mock_obj = mock.Mock() + attrs = {'id': 'azone_id', + 'zoneName': 'test_azone', + 'status': 'ok'} + mock_obj.configure_mock(**attrs) + self.availability_zone = mock_obj + + mock_obj = mock.Mock() + attrs = {'id': 'floating_id', + 'zoneName': 'test_floating_ip', + 'status': 'ok'} + mock_obj.configure_mock(**attrs) + self.floating_ip = mock_obj + + mock_obj = mock.Mock() + attrs = {'id': 'hypervisor_id', + 'hypervisor_hostname': 'test_hostname', + 'state': 'up'} + mock_obj.configure_mock(**attrs) + self.hypervisor = mock_obj + + mock_obj = mock.Mock() + attrs = {'id': 'image_id', + 'name': 'test_image'} + mock_obj.configure_mock(**attrs) + self.image = mock_obj + + mock_obj = mock.Mock() + self.mock_return = mock_obj + + self.nova_client = mock.Mock() + attrs = {'servers.list.return_value': [self.instance], + 'servers.get.return_value': self.instance, + 'servers.find.return_value': self.instance, + 'servers.create.return_value': self.instance, + 'flavors.list.return_value': [self.flavor], + 'flavors.find.return_value': self.flavor, + 'flavors.list.return_value': [self.flavor], + 'servers.add_floating_ip.return_value': mock.Mock(), + 'servers.force_delete.return_value': mock.Mock(), + 'aggregates.list.return_value': [self.aggregate], + 'aggregates.add_host.return_value': mock.Mock(), + 'aggregates.remove_host.return_value': mock.Mock(), + 'aggregates.get.return_value': self.aggregate, + 'aggregates.delete.return_value': mock.Mock(), + 'availability_zones.list.return_value': + [self.availability_zone], + 'floating_ips.list.return_value': [self.floating_ip], + 'floating_ips.delete.return_value': mock.Mock(), + 'hypervisors.list.return_value': [self.hypervisor], + 'create.return_value': mock.Mock(), + 'add_security_group.return_value': mock.Mock(), + 'images.list.return_value': [self.image], + 'images.delete.return_value': mock.Mock(), + } + self.nova_client.configure_mock(**attrs) + + self.glance_client = mock.Mock() + attrs = {'images.list.return_value': [self.image], + 'images.create.return_value': self.image, + 'images.upload.return_value': mock.Mock()} + self.glance_client.configure_mock(**attrs) + + mock_obj = mock.Mock() + attrs = {'id': 'volume_id', + 'name': 'test_volume'} + mock_obj.configure_mock(**attrs) + self.volume = mock_obj + + mock_obj = mock.Mock() + attrs = {'id': 'volume_type_id', + 'name': 'test_volume_type', + 'is_public': True} + mock_obj.configure_mock(**attrs) + self.volume_types = [mock_obj] + + mock_obj = mock.Mock() + attrs = {'id': 'volume_type_id', + 'name': 'test_volume_type', + 'is_public': False} + mock_obj.configure_mock(**attrs) + self.volume_types.append(mock_obj) + + self.cinder_client = mock.Mock() + attrs = {'volumes.list.return_value': [self.volume], + 'volume_types.list.return_value': self.volume_types, + 'volume_types.create.return_value': self.volume_types[0], + 'volume_types.delete.return_value': mock.Mock(), + 'quotas.update.return_value': mock.Mock(), + 'volumes.detach.return_value': mock.Mock(), + 'volumes.force_delete.return_value': mock.Mock(), + 'volumes.delete.return_value': mock.Mock() + } + self.cinder_client.configure_mock(**attrs) + + mock_obj = mock.Mock() + attrs = {'id': 'tenant_id', + 'name': 'test_tenant'} + mock_obj.configure_mock(**attrs) + self.tenant = mock_obj + + mock_obj = mock.Mock() + attrs = {'id': 'user_id', + 'name': 'test_user'} + mock_obj.configure_mock(**attrs) + self.user = mock_obj + + mock_obj = mock.Mock() + attrs = {'id': 'role_id', + 'name': 'test_role'} + mock_obj.configure_mock(**attrs) + self.role = mock_obj + + self.keystone_client = mock.Mock() + attrs = {'projects.list.return_value': [self.tenant], + 'tenants.list.return_value': [self.tenant], + 'users.list.return_value': [self.user], + 'roles.list.return_value': [self.role], + 'projects.create.return_value': self.tenant, + 'tenants.create.return_value': self.tenant, + 'users.create.return_value': self.user, + 'roles.grant.return_value': mock.Mock(), + 'roles.add_user_role.return_value': mock.Mock(), + 'projects.delete.return_value': mock.Mock(), + 'tenants.delete.return_value': mock.Mock(), + 'users.delete.return_value': mock.Mock(), + } + self.keystone_client.configure_mock(**attrs) + + self.router = {'id': 'router_id', + 'name': 'test_router'} + + self.subnet = {'id': 'subnet_id', + 'name': 'test_subnet'} + + self.networks = [{'id': 'network_id', + 'name': 'test_network', + 'router:external': False, + 'shared': True, + 'subnets': [self.subnet]}, + {'id': 'network_id1', + 'name': 'test_network1', + 'router:external': True, + 'shared': True, + 'subnets': [self.subnet]}] + + self.port = {'id': 'port_id', + 'name': 'test_port'} + + self.sec_group = {'id': 'sec_group_id', + 'name': 'test_sec_group'} + + self.neutron_floatingip = {'id': 'fip_id', + 'floating_ip_address': 'test_ip'} + self.neutron_client = mock.Mock() + attrs = {'list_networks.return_value': {'networks': self.networks}, + 'list_routers.return_value': {'routers': [self.router]}, + 'list_ports.return_value': {'ports': [self.port]}, + 'list_subnets.return_value': {'subnets': [self.subnet]}, + 'create_network.return_value': {'network': self.networks[0]}, + 'create_subnet.return_value': {'subnets': [self.subnet]}, + 'create_router.return_value': {'router': self.router}, + 'create_port.return_value': {'port': self.port}, + 'create_floatingip.return_value': {'floatingip': + self.neutron_floatingip}, + 'update_network.return_value': mock.Mock(), + 'update_port.return_value': {'port': self.port}, + 'add_interface_router.return_value': mock.Mock(), + 'add_gateway_router.return_value': mock.Mock(), + 'delete_network.return_value': mock.Mock(), + 'delete_subnet.return_value': mock.Mock(), + 'delete_router.return_value': mock.Mock(), + 'delete_port.return_value': mock.Mock(), + 'remove_interface_router.return_value': mock.Mock(), + 'remove_gateway_router.return_value': mock.Mock(), + 'create_bgpvpn.return_value': self.mock_return, + 'create_network_association.return_value': self.mock_return, + 'create_router_association.return_value': self.mock_return, + 'update_bgpvpn.return_value': self.mock_return, + 'delete_bgpvpn.return_value': self.mock_return, + 'show_bgpvpn.return_value': self.mock_return, + 'list_security_groups.return_value': {'security_groups': + [self.sec_group]}, + 'create_security_group_rule.return_value': mock.Mock(), + 'create_security_group.return_value': {'security_group': + self.sec_group}, + 'update_quota.return_value': mock.Mock(), + 'delete_security_group.return_value': mock.Mock() + } + self.neutron_client.configure_mock(**attrs) + + self.empty_client = mock.Mock() + attrs = {'list_networks.return_value': {'networks': []}, + 'list_routers.return_value': {'routers': []}, + 'list_ports.return_value': {'ports': []}, + 'list_subnets.return_value': {'subnets': []}} + self.empty_client.configure_mock(**attrs) + + @mock.patch('functest.utils.openstack_utils.os.getenv', + return_value=None) + def test_is_keystone_v3_missing_identity(self, mock_os_getenv): + self.assertEqual(openstack_utils.is_keystone_v3(), False) + + @mock.patch('functest.utils.openstack_utils.os.getenv', + return_value='3') + def test_is_keystone_v3_default(self, mock_os_getenv): + self.assertEqual(openstack_utils.is_keystone_v3(), True) + + @mock.patch('functest.utils.openstack_utils.is_keystone_v3', + return_value=False) + def test_get_rc_env_vars_missing_identity(self, mock_get_rc_env): + exp_resp = self.env_vars + exp_resp.extend(['OS_TENANT_NAME']) + self.assertEqual(openstack_utils.get_rc_env_vars(), exp_resp) + + @mock.patch('functest.utils.openstack_utils.is_keystone_v3', + return_value=True) + def test_get_rc_env_vars_default(self, mock_get_rc_env): + exp_resp = self.env_vars + exp_resp.extend(['OS_PROJECT_NAME', + 'OS_USER_DOMAIN_NAME', + 'OS_PROJECT_DOMAIN_NAME']) + self.assertEqual(openstack_utils.get_rc_env_vars(), exp_resp) + + @mock.patch('functest.utils.openstack_utils.get_rc_env_vars') + def test_check_credentials_missing_env(self, mock_get_rc_env): + exp_resp = self.env_vars + exp_resp.extend(['OS_TENANT_NAME']) + mock_get_rc_env.return_value = exp_resp + with mock.patch.dict('functest.utils.openstack_utils.os.environ', {}, + clear=True): + self.assertEqual(openstack_utils.check_credentials(), False) + + @mock.patch('functest.utils.openstack_utils.get_rc_env_vars') + def test_check_credentials_default(self, mock_get_rc_env): + exp_resp = ['OS_TENANT_NAME'] + mock_get_rc_env.return_value = exp_resp + with mock.patch.dict('functest.utils.openstack_utils.os.environ', + {'OS_TENANT_NAME': self.tenant_name}, + clear=True): + self.assertEqual(openstack_utils.check_credentials(), True) + + def test_get_env_cred_dict(self): + self.assertDictEqual(openstack_utils.get_env_cred_dict(), + self.env_cred_dict) + + @mock.patch('functest.utils.openstack_utils.get_rc_env_vars') + def test_get_credentials_default(self, mock_get_rc_env): + mock_get_rc_env.return_value = self.env_cred_dict.keys() + with mock.patch.dict('functest.utils.openstack_utils.os.environ', + self.os_environs, + clear=True): + self.assertDictEqual(openstack_utils.get_credentials(), + self.os_env_vars) + + def _get_credentials_missing_env(self, var): + dic = copy.deepcopy(self.os_environs) + dic.pop(var) + with mock.patch('functest.utils.openstack_utils.get_rc_env_vars', + return_value=self.env_cred_dict.keys()), \ + mock.patch.dict('functest.utils.openstack_utils.os.environ', + dic, + clear=True): + self.assertRaises(openstack_utils.MissingEnvVar, + lambda: openstack_utils.get_credentials()) + + def test_get_credentials_missing_username(self): + self._get_credentials_missing_env('OS_USERNAME') + + def test_get_credentials_missing_password(self): + self._get_credentials_missing_env('OS_PASSWORD') + + def test_get_credentials_missing_auth_url(self): + self._get_credentials_missing_env('OS_AUTH_URL') + + def test_get_credentials_missing_tenantname(self): + self._get_credentials_missing_env('OS_TENANT_NAME') + + def test_get_credentials_missing_domainname(self): + self._get_credentials_missing_env('OS_USER_DOMAIN_NAME') + + def test_get_credentials_missing_projectname(self): + self._get_credentials_missing_env('OS_PROJECT_NAME') + + def test_get_credentials_missing_endpoint_type(self): + self._get_credentials_missing_env('OS_ENDPOINT_TYPE') + + def test_source_credentials(self): + with mock.patch('functest.utils.openstack_utils.subprocess.Popen') \ + as mock_subproc_popen, \ + mock.patch('functest.utils.openstack_utils.os.environ'): + process_mock = mock.Mock() + attrs = {'communicate.return_value': ('OS_USER_NAME=test_name', + 'success')} + process_mock.configure_mock(**attrs) + mock_subproc_popen.return_value = process_mock + + self.assertDictEqual(openstack_utils.source_credentials('rc_file'), + {'OS_USER_NAME': 'test_name'}) + + @mock.patch('functest.utils.openstack_utils.os.getenv', + return_value=None) + def test_get_keystone_client_version_missing_env(self, mock_os_getenv): + self.assertEqual(openstack_utils.get_keystone_client_version(), + openstack_utils.DEFAULT_API_VERSION) + + @mock.patch('functest.utils.openstack_utils.logger.info') + @mock.patch('functest.utils.openstack_utils.os.getenv', + return_value='3') + def test_get_keystone_client_version_default(self, mock_os_getenv, + mock_logger_info): + self.assertEqual(openstack_utils.get_keystone_client_version(), + '3') + mock_logger_info.assert_called_once_with("OS_IDENTITY_API_VERSION is " + "set in env as '%s'", '3') + + def test_get_keystone_client(self): + mock_keystone_obj = mock.Mock() + mock_session_obj = mock.Mock() + with mock.patch('functest.utils.openstack_utils' + '.get_keystone_client_version', return_value='3'), \ + mock.patch('functest.utils.openstack_utils' + '.keystoneclient.Client', + return_value=mock_keystone_obj) \ + as mock_key_client, \ + mock.patch('functest.utils.openstack_utils.get_session', + return_value=mock_session_obj): + self.assertEqual(openstack_utils.get_keystone_client(), + mock_keystone_obj) + mock_key_client.assert_called_once_with('3', + session=mock_session_obj) + + @mock.patch('functest.utils.openstack_utils.os.getenv', + return_value=None) + def test_get_nova_client_version_missing_env(self, mock_os_getenv): + self.assertEqual(openstack_utils.get_nova_client_version(), + openstack_utils.DEFAULT_API_VERSION) + + @mock.patch('functest.utils.openstack_utils.logger.info') + @mock.patch('functest.utils.openstack_utils.os.getenv', + return_value='3') + def test_get_nova_client_version_default(self, mock_os_getenv, + mock_logger_info): + self.assertEqual(openstack_utils.get_nova_client_version(), + '3') + mock_logger_info.assert_called_once_with("OS_COMPUTE_API_VERSION is " + "set in env as '%s'", '3') + + def test_get_nova_client(self): + mock_nova_obj = mock.Mock() + mock_session_obj = mock.Mock() + with mock.patch('functest.utils.openstack_utils' + '.get_nova_client_version', return_value='3'), \ + mock.patch('functest.utils.openstack_utils' + '.novaclient.Client', + return_value=mock_nova_obj) \ + as mock_nova_client, \ + mock.patch('functest.utils.openstack_utils.get_session', + return_value=mock_session_obj): + self.assertEqual(openstack_utils.get_nova_client(), + mock_nova_obj) + mock_nova_client.assert_called_once_with('3', + session=mock_session_obj) + + @mock.patch('functest.utils.openstack_utils.os.getenv', + return_value=None) + def test_get_cinder_client_version_missing_env(self, mock_os_getenv): + self.assertEqual(openstack_utils.get_cinder_client_version(), + openstack_utils.DEFAULT_API_VERSION) + + @mock.patch('functest.utils.openstack_utils.logger.info') + @mock.patch('functest.utils.openstack_utils.os.getenv', + return_value='3') + def test_get_cinder_client_version_default(self, mock_os_getenv, + mock_logger_info): + self.assertEqual(openstack_utils.get_cinder_client_version(), + '3') + mock_logger_info.assert_called_once_with("OS_VOLUME_API_VERSION is " + "set in env as '%s'", '3') + + def test_get_cinder_client(self): + mock_cinder_obj = mock.Mock() + mock_session_obj = mock.Mock() + with mock.patch('functest.utils.openstack_utils' + '.get_cinder_client_version', return_value='3'), \ + mock.patch('functest.utils.openstack_utils' + '.cinderclient.Client', + return_value=mock_cinder_obj) \ + as mock_cind_client, \ + mock.patch('functest.utils.openstack_utils.get_session', + return_value=mock_session_obj): + self.assertEqual(openstack_utils.get_cinder_client(), + mock_cinder_obj) + mock_cind_client.assert_called_once_with('3', + session=mock_session_obj) + + @mock.patch('functest.utils.openstack_utils.os.getenv', + return_value=None) + def test_get_neutron_client_version_missing_env(self, mock_os_getenv): + self.assertEqual(openstack_utils.get_neutron_client_version(), + openstack_utils.DEFAULT_API_VERSION) + + @mock.patch('functest.utils.openstack_utils.logger.info') + @mock.patch('functest.utils.openstack_utils.os.getenv', + return_value='3') + def test_get_neutron_client_version_default(self, mock_os_getenv, + mock_logger_info): + self.assertEqual(openstack_utils.get_neutron_client_version(), + '3') + mock_logger_info.assert_called_once_with("OS_NETWORK_API_VERSION is " + "set in env as '%s'", '3') + + def test_get_neutron_client(self): + mock_neutron_obj = mock.Mock() + mock_session_obj = mock.Mock() + with mock.patch('functest.utils.openstack_utils' + '.get_neutron_client_version', return_value='3'), \ + mock.patch('functest.utils.openstack_utils' + '.neutronclient.Client', + return_value=mock_neutron_obj) \ + as mock_neut_client, \ + mock.patch('functest.utils.openstack_utils.get_session', + return_value=mock_session_obj): + self.assertEqual(openstack_utils.get_neutron_client(), + mock_neutron_obj) + mock_neut_client.assert_called_once_with('3', + session=mock_session_obj) + + @mock.patch('functest.utils.openstack_utils.os.getenv', + return_value=None) + def test_get_glance_client_version_missing_env(self, mock_os_getenv): + self.assertEqual(openstack_utils.get_glance_client_version(), + openstack_utils.DEFAULT_API_VERSION) + + @mock.patch('functest.utils.openstack_utils.logger.info') + @mock.patch('functest.utils.openstack_utils.os.getenv', + return_value='3') + def test_get_glance_client_version_default(self, mock_os_getenv, + mock_logger_info): + self.assertEqual(openstack_utils.get_glance_client_version(), + '3') + mock_logger_info.assert_called_once_with("OS_IMAGE_API_VERSION is " + "set in env as '%s'", '3') + + def test_get_glance_client(self): + mock_glance_obj = mock.Mock() + mock_session_obj = mock.Mock() + with mock.patch('functest.utils.openstack_utils' + '.get_glance_client_version', return_value='3'), \ + mock.patch('functest.utils.openstack_utils' + '.glanceclient.Client', + return_value=mock_glance_obj) \ + as mock_glan_client, \ + mock.patch('functest.utils.openstack_utils.get_session', + return_value=mock_session_obj): + self.assertEqual(openstack_utils.get_glance_client(), + mock_glance_obj) + mock_glan_client.assert_called_once_with('3', + session=mock_session_obj) + + def test_get_instances_default(self): + self.assertEqual(openstack_utils.get_instances(self.nova_client), + [self.instance]) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_get_instances_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + get_instances(Exception), + None) + self.assertTrue(mock_logger_error.called) + + def test_get_instance_status_default(self): + self.assertEqual(openstack_utils.get_instance_status(self.nova_client, + self.instance), + 'ok') + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_get_instance_status_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + get_instance_status(Exception, + self.instance), + None) + self.assertTrue(mock_logger_error.called) + + def test_get_instance_by_name_default(self): + self.assertEqual(openstack_utils. + get_instance_by_name(self.nova_client, + 'test_instance'), + self.instance) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_get_instance_by_name_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + get_instance_by_name(Exception, + 'test_instance'), + None) + self.assertTrue(mock_logger_error.called) + + def test_get_flavor_id_default(self): + self.assertEqual(openstack_utils. + get_flavor_id(self.nova_client, + 'test_flavor'), + self.flavor.id) + + def test_get_flavor_id_by_ram_range_default(self): + self.assertEqual(openstack_utils. + get_flavor_id_by_ram_range(self.nova_client, + 1, 3), + self.flavor.id) + + def test_get_aggregates_default(self): + self.assertEqual(openstack_utils. + get_aggregates(self.nova_client), + [self.aggregate]) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_get_aggregates_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + get_aggregates(Exception), + None) + self.assertTrue(mock_logger_error.called) + + def test_get_aggregate_id_default(self): + with mock.patch('functest.utils.openstack_utils.get_aggregates', + return_value=[self.aggregate]): + self.assertEqual(openstack_utils. + get_aggregate_id(self.nova_client, + 'test_aggregate'), + 'aggregate_id') + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_get_aggregate_id_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + get_aggregate_id(Exception, + 'test_aggregate'), + None) + self.assertTrue(mock_logger_error.called) + + def test_get_availability_zone_names_default(self): + with mock.patch('functest.utils.openstack_utils' + '.get_availability_zones', + return_value=[self.availability_zone]): + self.assertEqual(openstack_utils. + get_availability_zone_names(self.nova_client), + ['test_azone']) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_get_availability_zone_names_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + get_availability_zone_names(Exception), + None) + self.assertTrue(mock_logger_error.called) + + def test_get_availability_zones_default(self): + self.assertEqual(openstack_utils. + get_availability_zones(self.nova_client), + [self.availability_zone]) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_get_availability_zones_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + get_availability_zones(Exception), + None) + self.assertTrue(mock_logger_error.called) + + def test_get_floating_ips_default(self): + self.assertEqual(openstack_utils. + get_floating_ips(self.nova_client), + [self.floating_ip]) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_get_floating_ips_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + get_floating_ips(Exception), + None) + self.assertTrue(mock_logger_error.called) + + def test_get_hypervisors_default(self): + self.assertEqual(openstack_utils. + get_hypervisors(self.nova_client), + ['test_hostname']) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_get_hypervisors_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + get_hypervisors(Exception), + None) + self.assertTrue(mock_logger_error.called) + + def test_create_aggregate_default(self): + self.assertTrue(openstack_utils. + create_aggregate(self.nova_client, + 'test_aggregate', + 'azone')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_create_aggregate_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + create_aggregate(Exception, + 'test_aggregate', + 'azone'), + None) + self.assertTrue(mock_logger_error.called) + + def test_add_host_to_aggregate_default(self): + with mock.patch('functest.utils.openstack_utils.get_aggregate_id'): + self.assertTrue(openstack_utils. + add_host_to_aggregate(self.nova_client, + 'test_aggregate', + 'test_hostname')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_add_host_to_aggregate_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + add_host_to_aggregate(Exception, + 'test_aggregate', + 'test_hostname'), + None) + self.assertTrue(mock_logger_error.called) + + def test_create_aggregate_with_host_default(self): + with mock.patch('functest.utils.openstack_utils.create_aggregate'), \ + mock.patch('functest.utils.openstack_utils.' + 'add_host_to_aggregate'): + self.assertTrue(openstack_utils. + create_aggregate_with_host(self.nova_client, + 'test_aggregate', + 'test_azone', + 'test_hostname')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_create_aggregate_with_host_exception(self, mock_logger_error): + with mock.patch('functest.utils.openstack_utils.create_aggregate', + side_effect=Exception): + self.assertEqual(openstack_utils. + create_aggregate_with_host(Exception, + 'test_aggregate', + 'test_azone', + 'test_hostname'), + None) + self.assertTrue(mock_logger_error.called) + + def test_create_instance_default(self): + with mock.patch('functest.utils.openstack_utils.' + 'get_nova_client', + return_value=self.nova_client): + self.assertEqual(openstack_utils. + create_instance('test_flavor', + 'image_id', + 'network_id'), + self.instance) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_create_instance_exception(self, mock_logger_error): + with mock.patch('functest.utils.openstack_utils.' + 'get_nova_client', + return_value=self.nova_client): + self.nova_client.flavors.find.side_effect = Exception + self.assertEqual(openstack_utils. + create_instance('test_flavor', + 'image_id', + 'network_id'), + None) + self.assertTrue(mock_logger_error) + + def test_create_floating_ip_default(self): + with mock.patch('functest.utils.openstack_utils.' + 'get_external_net_id', + return_value='external_net_id'): + exp_resp = {'fip_addr': 'test_ip', 'fip_id': 'fip_id'} + self.assertEqual(openstack_utils. + create_floating_ip(self.neutron_client), + exp_resp) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_create_floating_ip_exception(self, mock_logger_error): + with mock.patch('functest.utils.openstack_utils.' + 'get_external_net_id', + return_value='external_net_id'): + self.assertEqual(openstack_utils. + create_floating_ip(Exception), + None) + self.assertTrue(mock_logger_error) + + def test_add_floating_ip_default(self): + with mock.patch('functest.utils.openstack_utils.get_aggregate_id'): + self.assertTrue(openstack_utils. + add_floating_ip(self.nova_client, + 'test_serverid', + 'test_floatingip_addr')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_add_floating_ip_exception(self, mock_logger_error): + self.assertFalse(openstack_utils. + add_floating_ip(Exception, + 'test_serverid', + 'test_floatingip_addr')) + self.assertTrue(mock_logger_error.called) + + def test_delete_instance_default(self): + self.assertTrue(openstack_utils. + delete_instance(self.nova_client, + 'instance_id')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_delete_instance_exception(self, mock_logger_error): + self.assertFalse(openstack_utils. + delete_instance(Exception, + 'instance_id')) + self.assertTrue(mock_logger_error.called) + + def test_delete_floating_ip_default(self): + self.assertTrue(openstack_utils. + delete_floating_ip(self.nova_client, + 'floating_ip_id')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_delete_floating_ip_exception(self, mock_logger_error): + self.assertFalse(openstack_utils. + delete_floating_ip(Exception, + 'floating_ip_id')) + self.assertTrue(mock_logger_error.called) + + def test_remove_host_from_aggregate_default(self): + with mock.patch('functest.utils.openstack_utils.' + 'get_aggregate_id'): + self.assertTrue(openstack_utils. + remove_host_from_aggregate(self.nova_client, + 'agg_name', + 'host_name')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_remove_host_from_aggregate_exception(self, mock_logger_error): + with mock.patch('functest.utils.openstack_utils.' + 'get_aggregate_id', side_effect=Exception): + self.assertFalse(openstack_utils. + remove_host_from_aggregate(self.nova_client, + 'agg_name', + 'host_name')) + self.assertTrue(mock_logger_error.called) + + def test_remove_hosts_from_aggregate_default(self): + with mock.patch('functest.utils.openstack_utils.' + 'get_aggregate_id'), \ + mock.patch('functest.utils.openstack_utils.' + 'remove_host_from_aggregate', + return_value=True) \ + as mock_method: + openstack_utils.remove_hosts_from_aggregate(self.nova_client, + 'test_aggregate') + mock_method.assert_any_call(self.nova_client, + 'test_aggregate', + 'host_name') + + def test_delete_aggregate_default(self): + with mock.patch('functest.utils.openstack_utils.' + 'remove_hosts_from_aggregate'): + self.assertTrue(openstack_utils. + delete_aggregate(self.nova_client, + 'agg_name')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_delete_aggregate_exception(self, mock_logger_error): + with mock.patch('functest.utils.openstack_utils.' + 'remove_hosts_from_aggregate', side_effect=Exception): + self.assertFalse(openstack_utils. + delete_aggregate(self.nova_client, + 'agg_name')) + self.assertTrue(mock_logger_error.called) + + def test_get_network_list_default(self): + self.assertEqual(openstack_utils. + get_network_list(self.neutron_client), + self.networks) + + def test_get_network_list_missing_network(self): + self.assertEqual(openstack_utils. + get_network_list(self.empty_client), + None) + + def test_get_router_list_default(self): + self.assertEqual(openstack_utils. + get_router_list(self.neutron_client), + [self.router]) + + def test_get_router_list_missing_router(self): + self.assertEqual(openstack_utils. + get_router_list(self.empty_client), + None) + + def test_get_port_list_default(self): + self.assertEqual(openstack_utils. + get_port_list(self.neutron_client), + [self.port]) + + def test_get_port_list_missing_port(self): + self.assertEqual(openstack_utils. + get_port_list(self.empty_client), + None) + + def test_get_network_id_default(self): + self.assertEqual(openstack_utils. + get_network_id(self.neutron_client, + 'test_network'), + 'network_id') + + def test_get_subnet_id_default(self): + self.assertEqual(openstack_utils. + get_subnet_id(self.neutron_client, + 'test_subnet'), + 'subnet_id') + + def test_get_router_id_default(self): + self.assertEqual(openstack_utils. + get_router_id(self.neutron_client, + 'test_router'), + 'router_id') + + def test_get_private_net_default(self): + self.assertEqual(openstack_utils. + get_private_net(self.neutron_client), + self.networks[0]) + + def test_get_private_net_missing_net(self): + self.assertEqual(openstack_utils. + get_private_net(self.empty_client), + None) + + def test_get_external_net_default(self): + self.assertEqual(openstack_utils. + get_external_net(self.neutron_client), + 'test_network1') + + def test_get_external_net_missing_net(self): + self.assertEqual(openstack_utils. + get_external_net(self.empty_client), + None) + + def test_get_external_net_id_default(self): + self.assertEqual(openstack_utils. + get_external_net_id(self.neutron_client), + 'network_id1') + + def test_get_external_net_id_missing_net(self): + self.assertEqual(openstack_utils. + get_external_net_id(self.empty_client), + None) + + def test_check_neutron_net_default(self): + self.assertTrue(openstack_utils. + check_neutron_net(self.neutron_client, + 'test_network')) + + def test_check_neutron_net_missing_net(self): + self.assertFalse(openstack_utils. + check_neutron_net(self.empty_client, + 'test_network')) + + def test_create_neutron_net_default(self): + self.assertEqual(openstack_utils. + create_neutron_net(self.neutron_client, + 'test_network'), + 'network_id') + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_create_neutron_net_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + create_neutron_net(Exception, + 'test_network'), + None) + self.assertTrue(mock_logger_error.called) + + def test_create_neutron_subnet_default(self): + self.assertEqual(openstack_utils. + create_neutron_subnet(self.neutron_client, + 'test_subnet', + 'test_cidr', + 'network_id'), + 'subnet_id') + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_create_neutron_subnet_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + create_neutron_subnet(Exception, + 'test_subnet', + 'test_cidr', + 'network_id'), + None) + self.assertTrue(mock_logger_error.called) + + def test_create_neutron_router_default(self): + self.assertEqual(openstack_utils. + create_neutron_router(self.neutron_client, + 'test_router'), + 'router_id') + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_create_neutron_router_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + create_neutron_router(Exception, + 'test_router'), + None) + self.assertTrue(mock_logger_error.called) + + def test_create_neutron_port_default(self): + self.assertEqual(openstack_utils. + create_neutron_port(self.neutron_client, + 'test_port', + 'network_id', + 'test_ip'), + 'port_id') + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_create_neutron_port_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + create_neutron_port(Exception, + 'test_port', + 'network_id', + 'test_ip'), + None) + self.assertTrue(mock_logger_error.called) + + def test_update_neutron_net_default(self): + self.assertTrue(openstack_utils. + update_neutron_net(self.neutron_client, + 'network_id')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_update_neutron_net_exception(self, mock_logger_error): + self.assertFalse(openstack_utils. + update_neutron_net(Exception, + 'network_id')) + self.assertTrue(mock_logger_error.called) + + def test_update_neutron_port_default(self): + self.assertEqual(openstack_utils. + update_neutron_port(self.neutron_client, + 'port_id', + 'test_owner'), + 'port_id') + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_update_neutron_port_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + update_neutron_port(Exception, + 'port_id', + 'test_owner'), + None) + self.assertTrue(mock_logger_error.called) + + def test_add_interface_router_default(self): + self.assertTrue(openstack_utils. + add_interface_router(self.neutron_client, + 'router_id', + 'subnet_id')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_add_interface_router_exception(self, mock_logger_error): + self.assertFalse(openstack_utils. + add_interface_router(Exception, + 'router_id', + 'subnet_id')) + self.assertTrue(mock_logger_error.called) + + def test_add_gateway_router_default(self): + with mock.patch('functest.utils.openstack_utils.' + 'get_external_net_id', + return_value='network_id'): + self.assertTrue(openstack_utils. + add_gateway_router(self.neutron_client, + 'router_id')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_add_gateway_router_exception(self, mock_logger_error): + with mock.patch('functest.utils.openstack_utils.' + 'get_external_net_id', + return_value='network_id'): + self.assertFalse(openstack_utils. + add_gateway_router(Exception, + 'router_id')) + self.assertTrue(mock_logger_error.called) + + def test_delete_neutron_net_default(self): + self.assertTrue(openstack_utils. + delete_neutron_net(self.neutron_client, + 'network_id')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_delete_neutron_net_exception(self, mock_logger_error): + self.assertFalse(openstack_utils. + delete_neutron_net(Exception, + 'network_id')) + self.assertTrue(mock_logger_error.called) + + def test_delete_neutron_subnet_default(self): + self.assertTrue(openstack_utils. + delete_neutron_subnet(self.neutron_client, + 'subnet_id')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_delete_neutron_subnet_exception(self, mock_logger_error): + self.assertFalse(openstack_utils. + delete_neutron_subnet(Exception, + 'subnet_id')) + self.assertTrue(mock_logger_error.called) + + def test_delete_neutron_router_default(self): + self.assertTrue(openstack_utils. + delete_neutron_router(self.neutron_client, + 'router_id')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_delete_neutron_router_exception(self, mock_logger_error): + self.assertFalse(openstack_utils. + delete_neutron_router(Exception, + 'router_id')) + self.assertTrue(mock_logger_error.called) + + def test_delete_neutron_port_default(self): + self.assertTrue(openstack_utils. + delete_neutron_port(self.neutron_client, + 'port_id')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_delete_neutron_port_exception(self, mock_logger_error): + self.assertFalse(openstack_utils. + delete_neutron_port(Exception, + 'port_id')) + self.assertTrue(mock_logger_error.called) + + def test_remove_interface_router_default(self): + self.assertTrue(openstack_utils. + remove_interface_router(self.neutron_client, + 'router_id', + 'subnet_id')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_remove_interface_router_exception(self, mock_logger_error): + self.assertFalse(openstack_utils. + remove_interface_router(Exception, + 'router_id', + 'subnet_id')) + self.assertTrue(mock_logger_error.called) + + def test_remove_gateway_router_default(self): + self.assertTrue(openstack_utils. + remove_gateway_router(self.neutron_client, + 'router_id')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_remove_gateway_router_exception(self, mock_logger_error): + self.assertFalse(openstack_utils. + remove_gateway_router(Exception, + 'router_id')) + self.assertTrue(mock_logger_error.called) + + def test_create_bgpvpn(self): + self.assertEqual(openstack_utils. + create_bgpvpn(self.neutron_client), + self.mock_return) + + def test_create_network_association(self): + self.assertEqual(openstack_utils. + create_network_association(self.neutron_client, + 'bgpvpn_id', + 'network_id'), + self.mock_return) + + def test_create_router_association(self): + self.assertEqual(openstack_utils. + create_router_association(self.neutron_client, + 'bgpvpn_id', + 'router_id'), + self.mock_return) + + def test_update_bgpvpn(self): + self.assertEqual(openstack_utils. + update_bgpvpn(self.neutron_client, + 'bgpvpn_id'), + self.mock_return) + + def test_delete_bgpvpn(self): + self.assertEqual(openstack_utils. + delete_bgpvpn(self.neutron_client, + 'bgpvpn_id'), + self.mock_return) + + def test_get_bgpvpn(self): + self.assertEqual(openstack_utils. + get_bgpvpn(self.neutron_client, + 'bgpvpn_id'), + self.mock_return) + + def test_get_bgpvpn_routers(self): + with mock.patch('functest.utils.openstack_utils.' + 'get_bgpvpn', + return_value={'bgpvpn': + {'routers': [self.router]}}): + self.assertEqual(openstack_utils. + get_bgpvpn_routers(self.neutron_client, + 'bgpvpn_id'), + [self.router]) + + def test_get_security_groups_default(self): + self.assertEqual(openstack_utils. + get_security_groups(self.neutron_client), + [self.sec_group]) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_get_security_groups_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + get_security_groups(Exception), + None) + self.assertTrue(mock_logger_error.called) + + def test_get_security_group_id_default(self): + with mock.patch('functest.utils.openstack_utils.' + 'get_security_groups', + return_value=[self.sec_group]): + self.assertEqual(openstack_utils. + get_security_group_id(self.neutron_client, + 'test_sec_group'), + 'sec_group_id') + + def test_create_security_group_default(self): + self.assertEqual(openstack_utils. + create_security_group(self.neutron_client, + 'test_sec_group', + 'sec_group_desc'), + self.sec_group) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_create_security_group_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + create_security_group(Exception, + 'test_sec_group', + 'sec_group_desc'), + None) + self.assertTrue(mock_logger_error.called) + + def test_create_secgroup_rule_default(self): + self.assertTrue(openstack_utils. + create_secgroup_rule(self.neutron_client, + 'sg_id', + 'direction', + 'protocol', + 80, + 80)) + self.assertTrue(openstack_utils. + create_secgroup_rule(self.neutron_client, + 'sg_id', + 'direction', + 'protocol')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_create_secgroup_rule_invalid_port_range(self, mock_logger_error): + self.assertFalse(openstack_utils. + create_secgroup_rule(self.neutron_client, + 'sg_id', + 'direction', + 'protocol', + 80)) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_create_secgroup_rule_exception(self, mock_logger_error): + self.assertFalse(openstack_utils. + create_secgroup_rule(Exception, + 'sg_id', + 'direction', + 'protocol')) + + @mock.patch('functest.utils.openstack_utils.logger.info') + def test_create_security_group_full_default(self, mock_logger_info): + with mock.patch('functest.utils.openstack_utils.' + 'get_security_group_id', + return_value='sg_id'): + self.assertEqual(openstack_utils. + create_security_group_full(self.neutron_client, + 'sg_name', + 'sg_desc'), + 'sg_id') + self.assertTrue(mock_logger_info) + + @mock.patch('functest.utils.openstack_utils.logger.info') + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_create_security_group_full_sec_group_fail(self, + mock_logger_error, + mock_logger_info): + with mock.patch('functest.utils.openstack_utils.' + 'get_security_group_id', + return_value=''), \ + mock.patch('functest.utils.openstack_utils.' + 'create_security_group', + return_value=False): + self.assertEqual(openstack_utils. + create_security_group_full(self.neutron_client, + 'sg_name', + 'sg_desc'), + None) + self.assertTrue(mock_logger_error) + self.assertTrue(mock_logger_info) + + @mock.patch('functest.utils.openstack_utils.logger.debug') + @mock.patch('functest.utils.openstack_utils.logger.info') + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_create_security_group_full_secgroup_rule_fail(self, + mock_logger_error, + mock_logger_info, + mock_logger_debug): + with mock.patch('functest.utils.openstack_utils.' + 'get_security_group_id', + return_value=''), \ + mock.patch('functest.utils.openstack_utils.' + 'create_security_group', + return_value={'id': 'sg_id', + 'name': 'sg_name'}), \ + mock.patch('functest.utils.openstack_utils.' + 'create_secgroup_rule', + return_value=False): + self.assertEqual(openstack_utils. + create_security_group_full(self.neutron_client, + 'sg_name', + 'sg_desc'), + None) + self.assertTrue(mock_logger_error) + self.assertTrue(mock_logger_info) + self.assertTrue(mock_logger_debug) + + def test_add_secgroup_to_instance_default(self): + self.assertTrue(openstack_utils. + add_secgroup_to_instance(self.nova_client, + 'instance_id', + 'sec_group_id')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_add_secgroup_to_instance_exception(self, mock_logger_error): + self.assertFalse(openstack_utils. + add_secgroup_to_instance(Exception, + 'instance_id', + 'sec_group_id')) + self.assertTrue(mock_logger_error.called) + + def test_update_sg_quota_default(self): + self.assertTrue(openstack_utils. + update_sg_quota(self.neutron_client, + 'tenant_id', + 'sg_quota', + 'sg_rule_quota')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_update_sg_quota_exception(self, mock_logger_error): + self.assertFalse(openstack_utils. + update_sg_quota(Exception, + 'tenant_id', + 'sg_quota', + 'sg_rule_quota')) + self.assertTrue(mock_logger_error.called) + + def test_delete_security_group_default(self): + self.assertTrue(openstack_utils. + delete_security_group(self.neutron_client, + 'sec_group_id')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_delete_security_group_exception(self, mock_logger_error): + self.assertFalse(openstack_utils. + delete_security_group(Exception, + 'sec_group_id')) + self.assertTrue(mock_logger_error.called) + + def test_get_images_default(self): + self.assertEqual(openstack_utils. + get_images(self.nova_client), + [self.image]) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_get_images_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + get_images(Exception), + None) + self.assertTrue(mock_logger_error.called) + + def test_get_image_id_default(self): + self.assertEqual(openstack_utils. + get_image_id(self.glance_client, + 'test_image'), + 'image_id') + + # create_glance_image, get_or_create_image + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_create_glance_image_file_present(self, mock_logger_error): + with mock.patch('functest.utils.openstack_utils.' + 'os.path.isfile', + return_value=False): + self.assertEqual(openstack_utils. + create_glance_image(self.glance_client, + 'test_image', + 'file_path'), + None) + self.assertTrue(mock_logger_error.called) + + @mock.patch('functest.utils.openstack_utils.logger.info') + def test_create_glance_image_already_exist(self, mock_logger_info): + with mock.patch('functest.utils.openstack_utils.' + 'os.path.isfile', + return_value=True), \ + mock.patch('functest.utils.openstack_utils.get_image_id', + return_value='image_id'): + self.assertEqual(openstack_utils. + create_glance_image(self.glance_client, + 'test_image', + 'file_path'), + 'image_id') + self.assertTrue(mock_logger_info.called) + + @mock.patch('functest.utils.openstack_utils.logger.info') + def test_create_glance_image_default(self, mock_logger_info): + with mock.patch('functest.utils.openstack_utils.' + 'os.path.isfile', + return_value=True), \ + mock.patch('functest.utils.openstack_utils.get_image_id', + return_value=''), \ + mock.patch('__builtin__.open', + mock.mock_open(read_data='1')) as m: + self.assertEqual(openstack_utils. + create_glance_image(self.glance_client, + 'test_image', + 'file_path'), + 'image_id') + m.assert_called_once_with('file_path') + self.assertTrue(mock_logger_info.called) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_create_glance_image_exception(self, mock_logger_error): + with mock.patch('functest.utils.openstack_utils.' + 'os.path.isfile', + return_value=True), \ + mock.patch('functest.utils.openstack_utils.get_image_id', + side_effect=Exception): + self.assertEqual(openstack_utils. + create_glance_image(self.glance_client, + 'test_image', + 'file_path'), + None) + self.assertTrue(mock_logger_error.called) + + def test_delete_glance_image_default(self): + self.assertTrue(openstack_utils. + delete_glance_image(self.nova_client, + 'image_id')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_delete_glance_image_exception(self, mock_logger_error): + self.assertFalse(openstack_utils. + delete_glance_image(Exception, + 'image_id')) + self.assertTrue(mock_logger_error.called) + + def test_get_volumes_default(self): + self.assertEqual(openstack_utils. + get_volumes(self.cinder_client), + [self.volume]) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_get_volumes_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + get_volumes(Exception), + None) + self.assertTrue(mock_logger_error.called) + + def test_list_volume_types_default_private(self): + self.assertEqual(openstack_utils. + list_volume_types(self.cinder_client, + public=False, + private=True), + [self.volume_types[1]]) + + def test_list_volume_types_default_public(self): + self.assertEqual(openstack_utils. + list_volume_types(self.cinder_client, + public=True, + private=False), + [self.volume_types[0]]) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_list_volume_types_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + list_volume_types(Exception), + None) + self.assertTrue(mock_logger_error.called) + + def test_create_volume_type_default(self): + self.assertEqual(openstack_utils. + create_volume_type(self.cinder_client, + 'test_volume_type'), + self.volume_types[0]) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_create_volume_type_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + create_volume_type(Exception, + 'test_volume_type'), + None) + self.assertTrue(mock_logger_error.called) + + def test_update_cinder_quota_default(self): + self.assertTrue(openstack_utils. + update_cinder_quota(self.cinder_client, + 'tenant_id', + 'vols_quota', + 'snap_quota', + 'giga_quota')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_update_cinder_quota_exception(self, mock_logger_error): + self.assertFalse(openstack_utils. + update_cinder_quota(Exception, + 'tenant_id', + 'vols_quota', + 'snap_quota', + 'giga_quota')) + self.assertTrue(mock_logger_error.called) + + def test_delete_volume_default(self): + self.assertTrue(openstack_utils. + delete_volume(self.cinder_client, + 'volume_id', + forced=False)) + + self.assertTrue(openstack_utils. + delete_volume(self.cinder_client, + 'volume_id', + forced=True)) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_delete_volume_exception(self, mock_logger_error): + self.assertFalse(openstack_utils. + delete_volume(Exception, + 'volume_id', + forced=True)) + self.assertTrue(mock_logger_error.called) + + def test_delete_volume_type_default(self): + self.assertTrue(openstack_utils. + delete_volume_type(self.cinder_client, + self.volume_types[0])) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_delete_volume_type_exception(self, mock_logger_error): + self.assertFalse(openstack_utils. + delete_volume_type(Exception, + self.volume_types[0])) + self.assertTrue(mock_logger_error.called) + + def test_get_tenants_default(self): + with mock.patch('functest.utils.openstack_utils.' + 'is_keystone_v3', return_value=True): + self.assertEqual(openstack_utils. + get_tenants(self.keystone_client), + [self.tenant]) + with mock.patch('functest.utils.openstack_utils.' + 'is_keystone_v3', return_value=False): + self.assertEqual(openstack_utils. + get_tenants(self.keystone_client), + [self.tenant]) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_get_tenants_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + get_tenants(Exception), + None) + self.assertTrue(mock_logger_error.called) + + def test_get_users_default(self): + self.assertEqual(openstack_utils. + get_users(self.keystone_client), + [self.user]) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_get_users_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + get_users(Exception), + None) + self.assertTrue(mock_logger_error.called) + + def test_get_tenant_id_default(self): + self.assertEqual(openstack_utils. + get_tenant_id(self.keystone_client, + 'test_tenant'), + 'tenant_id') + + def test_get_user_id_default(self): + self.assertEqual(openstack_utils. + get_user_id(self.keystone_client, + 'test_user'), + 'user_id') + + def test_get_role_id_default(self): + self.assertEqual(openstack_utils. + get_role_id(self.keystone_client, + 'test_role'), + 'role_id') + + def test_create_tenant_default(self): + with mock.patch('functest.utils.openstack_utils.' + 'is_keystone_v3', return_value=True): + self.assertEqual(openstack_utils. + create_tenant(self.keystone_client, + 'test_tenant', + 'tenant_desc'), + 'tenant_id') + with mock.patch('functest.utils.openstack_utils.' + 'is_keystone_v3', return_value=False): + self.assertEqual(openstack_utils. + create_tenant(self.keystone_client, + 'test_tenant', + 'tenant_desc'), + 'tenant_id') + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_create_tenant_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + create_tenant(Exception, + 'test_tenant', + 'tenant_desc'), + None) + self.assertTrue(mock_logger_error.called) + + def test_create_user_default(self): + with mock.patch('functest.utils.openstack_utils.' + 'is_keystone_v3', return_value=True): + self.assertEqual(openstack_utils. + create_user(self.keystone_client, + 'test_user', + 'password', + 'email', + 'tenant_id'), + 'user_id') + with mock.patch('functest.utils.openstack_utils.' + 'is_keystone_v3', return_value=False): + self.assertEqual(openstack_utils. + create_user(self.keystone_client, + 'test_user', + 'password', + 'email', + 'tenant_id'), + 'user_id') + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_create_user_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + create_user(Exception, + 'test_user', + 'password', + 'email', + 'tenant_id'), + None) + self.assertTrue(mock_logger_error.called) + + def test_add_role_user_default(self): + with mock.patch('functest.utils.openstack_utils.' + 'is_keystone_v3', return_value=True): + self.assertTrue(openstack_utils. + add_role_user(self.keystone_client, + 'user_id', + 'role_id', + 'tenant_id')) + + with mock.patch('functest.utils.openstack_utils.' + 'is_keystone_v3', return_value=False): + self.assertTrue(openstack_utils. + add_role_user(self.keystone_client, + 'user_id', + 'role_id', + 'tenant_id')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_add_role_user_exception(self, mock_logger_error): + self.assertFalse(openstack_utils. + add_role_user(Exception, + 'user_id', + 'role_id', + 'tenant_id')) + self.assertTrue(mock_logger_error.called) + + def test_delete_tenant_default(self): + with mock.patch('functest.utils.openstack_utils.' + 'is_keystone_v3', return_value=True): + self.assertTrue(openstack_utils. + delete_tenant(self.keystone_client, + 'tenant_id')) + + with mock.patch('functest.utils.openstack_utils.' + 'is_keystone_v3', return_value=False): + self.assertTrue(openstack_utils. + delete_tenant(self.keystone_client, + 'tenant_id')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_delete_tenant_exception(self, mock_logger_error): + self.assertFalse(openstack_utils. + delete_tenant(Exception, + 'tenant_id')) + self.assertTrue(mock_logger_error.called) + + def test_delete_user_default(self): + self.assertTrue(openstack_utils. + delete_user(self.keystone_client, + 'user_id')) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_delete_user_exception(self, mock_logger_error): + self.assertFalse(openstack_utils. + delete_user(Exception, + 'user_id')) + self.assertTrue(mock_logger_error.called) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/functest/tests/unit/utils/test_utils.py b/functest/tests/unit/utils/test_utils.py deleted file mode 100644 index 8b6c5e1b9..000000000 --- a/functest/tests/unit/utils/test_utils.py +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env python - -# Copyright (c) 2016 Orange 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 logging -import mock -import unittest -import urllib2 - -from functest.utils import functest_utils - - -class FunctestUtilsTesting(unittest.TestCase): - - logging.disable(logging.CRITICAL) - - def setUp(self): - self.url = 'http://www.opnfv.org/' - self.timeout = 5 - - @mock.patch('urllib2.urlopen', - side_effect=urllib2.URLError('no host given')) - def test_check_internet_connectivity_failed(self, mock_method): - self.assertFalse(functest_utils.check_internet_connectivity()) - mock_method.assert_called_once_with(self.url, timeout=self.timeout) - - @mock.patch('urllib2.urlopen') - def test_check_internet_connectivity_default(self, mock_method): - self.assertTrue(functest_utils.check_internet_connectivity()) - mock_method.assert_called_once_with(self.url, timeout=self.timeout) - - @mock.patch('urllib2.urlopen') - def test_check_internet_connectivity_debian(self, mock_method): - self.url = "https://www.debian.org/" - self.assertTrue(functest_utils.check_internet_connectivity(self.url)) - mock_method.assert_called_once_with(self.url, timeout=self.timeout) - - -if __name__ == "__main__": - unittest.main(verbosity=2) diff --git a/functest/utils/functest_logger.py b/functest/utils/functest_logger.py index b154f563d..c0fba082a 100644 --- a/functest/utils/functest_logger.py +++ b/functest/utils/functest_logger.py @@ -40,8 +40,10 @@ class Logger: ch.setFormatter(formatter) if CI_DEBUG is not None and CI_DEBUG.lower() == "true": ch.setLevel(logging.DEBUG) + self.logger.parent.level = logging.DEBUG else: ch.setLevel(logging.INFO) + self.logger.parent.level = logging.INFO self.logger.addHandler(ch) hdlr = logging.FileHandler('/home/opnfv/functest/results/functest.log') diff --git a/run_unit_tests.sh b/run_unit_tests.sh index b098f37b2..79d05d3d1 100755 --- a/run_unit_tests.sh +++ b/run_unit_tests.sh @@ -2,32 +2,6 @@ set -o errexit set -o pipefail -function clean_results_dir { - if [ -d "/home/opnfv/functest/results" ] - then - sudo rm -rf /home/opnfv/functest/results - fi -} - -# ****************************** -# prepare the env for the tests -# ****************************** -# clean useless results dir -# should be done at the end -# but in case of crash during unit test -# clean it anyway -clean_results_dir - -# TODO clean that... -# Create log dir if needed -# log shall be disabled during unit tests -# fix to be done in Logger -echo "Create dummy log file...." -sudo mkdir -p /home/opnfv/functest/results/odl -sudo touch /home/opnfv/functest/results/functest.log -sudo touch /home/opnfv/functest/results/odl/stdout.txt -sudo chmod -Rf a+rw /home/opnfv - # Either Workspace is set (CI) if [ -z $WORKSPACE ] then @@ -58,8 +32,10 @@ nosetests --with-xunit \ --with-coverage \ --cover-erase \ --cover-tests \ + --cover-package=functest.cli \ --cover-package=functest.core.testcase_base \ --cover-package=functest.opnfv_tests.sdn.odl.odl \ + --cover-package=functest.utils \ --cover-xml \ --cover-html \ functest/tests/unit @@ -67,10 +43,4 @@ rc=$? deactivate -# ******* -# clean -# ******* -# Clean useless logs -clean_results_dir - exit $rc diff --git a/test-requirements.txt b/test-requirements.txt index 8be8e2033..2bf297ba8 100755 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -5,6 +5,7 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 # +click==6.6 coverage==4.1 dnspython==1.15.0 gitpython==1.0.1 @@ -15,9 +16,10 @@ python-congressclient==1.5.0 python-keystoneclient==3.5.0 python-neutronclient==6.0.0 python-openstackclient==2.3.0 +python-tackerclient==0.7.0 pyyaml==3.10 requests==2.8.0 robotframework==2.9.1 robotframework-requests==0.3.8 robotframework-sshlibrary==2.1.1 -virtualenv==15.1.0
\ No newline at end of file +virtualenv==15.1.0 |