diff options
Diffstat (limited to 'functest/tests/unit/utils')
-rw-r--r-- | functest/tests/unit/utils/test_decorators.py | 126 | ||||
-rw-r--r-- | functest/tests/unit/utils/test_env.py | 57 | ||||
-rw-r--r-- | functest/tests/unit/utils/test_functest_utils.py | 581 | ||||
-rw-r--r-- | functest/tests/unit/utils/test_openstack_utils.py | 1811 |
4 files changed, 338 insertions, 2237 deletions
diff --git a/functest/tests/unit/utils/test_decorators.py b/functest/tests/unit/utils/test_decorators.py deleted file mode 100644 index 82291fa2d..000000000 --- a/functest/tests/unit/utils/test_decorators.py +++ /dev/null @@ -1,126 +0,0 @@ -#!/usr/bin/env python - -# Copyright (c) 2017 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 - -"""Define the class required to fully cover decorators.""" - -from datetime import datetime -import errno -import json -import logging -import os -import unittest - -import mock - -from functest.utils import decorators -from functest.utils import functest_utils -from functest.utils.constants import CONST - -__author__ = "Cedric Ollivier <cedric.ollivier@orange.com>" - -VERSION = 'master' -DIR = '/dev' -FILE = '{}/null'.format(DIR) -URL = 'file://{}'.format(FILE) - - -class DecoratorsTesting(unittest.TestCase): - # pylint: disable=missing-docstring - - _case_name = 'base' - _project_name = 'functest' - _start_time = 1.0 - _stop_time = 2.0 - _result = 'PASS' - _build_tag = VERSION - _node_name = 'bar' - _deploy_scenario = 'foo' - _installer_type = 'debian' - - def setUp(self): - os.environ['INSTALLER_TYPE'] = self._installer_type - os.environ['DEPLOY_SCENARIO'] = self._deploy_scenario - os.environ['NODE_NAME'] = self._node_name - os.environ['BUILD_TAG'] = self._build_tag - - def test_wraps(self): - self.assertEqual(functest_utils.push_results_to_db.__name__, - "push_results_to_db") - - def _get_json(self): - stop_time = datetime.fromtimestamp(self._stop_time).strftime( - '%Y-%m-%d %H:%M:%S') - start_time = datetime.fromtimestamp(self._start_time).strftime( - '%Y-%m-%d %H:%M:%S') - data = {'project_name': self._project_name, - 'stop_date': stop_time, 'start_date': start_time, - 'case_name': self._case_name, 'build_tag': self._build_tag, - 'pod_name': self._node_name, 'installer': self._installer_type, - 'scenario': self._deploy_scenario, 'version': VERSION, - 'details': {}, 'criteria': self._result} - return json.dumps(data, sort_keys=True) - - @mock.patch('{}.get_version'.format(functest_utils.__name__), - return_value=VERSION) - @mock.patch('requests.post') - def test_http_shema(self, *args): - CONST.__setattr__('results_test_db_url', 'http://127.0.0.1') - self.assertTrue(functest_utils.push_results_to_db( - self._project_name, self._case_name, self._start_time, - self._stop_time, self._result, {})) - args[1].assert_called_once_with() - args[0].assert_called_once_with( - 'http://127.0.0.1', data=self._get_json(), - headers={'Content-Type': 'application/json'}) - - def test_wrong_shema(self): - CONST.__setattr__('results_test_db_url', '/dev/null') - self.assertFalse(functest_utils.push_results_to_db( - self._project_name, self._case_name, self._start_time, - self._stop_time, self._result, {})) - - @mock.patch('{}.get_version'.format(functest_utils.__name__), - return_value=VERSION) - def _test_dump(self, *args): - CONST.__setattr__('results_test_db_url', URL) - with mock.patch.object(decorators, 'open', mock.mock_open(), - create=True) as mock_open: - self.assertTrue(functest_utils.push_results_to_db( - self._project_name, self._case_name, self._start_time, - self._stop_time, self._result, {})) - mock_open.assert_called_once_with(FILE, 'a') - handle = mock_open() - call_args, _ = handle.write.call_args - self.assertIn('POST', call_args[0]) - self.assertIn(self._get_json(), call_args[0]) - args[0].assert_called_once_with() - - @mock.patch('os.makedirs') - def test_default_dump(self, mock_method=None): - self._test_dump() - mock_method.assert_called_once_with(DIR) - - @mock.patch('os.makedirs', side_effect=OSError(errno.EEXIST, '')) - def test_makedirs_dir_exists(self, mock_method=None): - self._test_dump() - mock_method.assert_called_once_with(DIR) - - @mock.patch('os.makedirs', side_effect=OSError) - def test_makedirs_exc(self, *args): - CONST.__setattr__('results_test_db_url', URL) - self.assertFalse( - functest_utils.push_results_to_db( - self._project_name, self._case_name, self._start_time, - self._stop_time, self._result, {})) - args[0].assert_called_once_with(DIR) - - -if __name__ == "__main__": - logging.disable(logging.CRITICAL) - unittest.main(verbosity=2) diff --git a/functest/tests/unit/utils/test_env.py b/functest/tests/unit/utils/test_env.py new file mode 100644 index 000000000..49d2d974c --- /dev/null +++ b/functest/tests/unit/utils/test_env.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python + +# Copyright (c) 2018 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 + +# pylint: disable=missing-docstring + +import logging +import os +import unittest + +from six.moves import reload_module + +from functest.utils import env + + +class EnvTesting(unittest.TestCase): + # pylint: disable=missing-docstring + + def setUp(self): + os.environ['FOO'] = 'foo' + os.environ['BUILD_TAG'] = 'master' + os.environ['CI_LOOP'] = 'weekly' + + def test_get_unset_unknown_env(self): + del os.environ['FOO'] + self.assertEqual(env.get('FOO'), None) + + def test_get_unknown_env(self): + self.assertEqual(env.get('FOO'), 'foo') + reload_module(env) + + def test_get_unset_env(self): + del os.environ['CI_LOOP'] + self.assertEqual( + env.get('CI_LOOP'), env.INPUTS['CI_LOOP']) + + def test_get_env(self): + self.assertEqual( + env.get('CI_LOOP'), 'weekly') + + def test_get_unset_env2(self): + del os.environ['BUILD_TAG'] + self.assertEqual( + env.get('BUILD_TAG'), env.INPUTS['BUILD_TAG']) + + def test_get_env2(self): + self.assertEqual(env.get('BUILD_TAG'), 'master') + + +if __name__ == "__main__": + logging.disable(logging.CRITICAL) + unittest.main(verbosity=2) diff --git a/functest/tests/unit/utils/test_functest_utils.py b/functest/tests/unit/utils/test_functest_utils.py index 7a77d25b9..4b642ff9d 100644 --- a/functest/tests/unit/utils/test_functest_utils.py +++ b/functest/tests/unit/utils/test_functest_utils.py @@ -7,22 +7,24 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 +# pylint: disable=missing-docstring + import logging -import pkg_resources -import os import time import unittest import mock -import requests -from six.moves import urllib +import pkg_resources +import six -from functest.tests.unit import test_utils from functest.utils import functest_utils -from functest.utils.constants import CONST class FunctestUtilsTesting(unittest.TestCase): + # pylint: disable=too-many-instance-attributes,too-many-public-methods + + readline = 0 + test_ip = ['10.1.23.4', '10.1.14.15', '10.1.16.15'] def setUp(self): self.url = 'http://www.opnfv.org/' @@ -53,8 +55,6 @@ class FunctestUtilsTesting(unittest.TestCase): self.cmd = 'test_cmd' self.output_file = 'test_output_file' self.testname = 'testname' - self.testcase_dict = {'case_name': 'testname', - 'criteria': self.criteria} self.parameter = 'general.openstack.image_name' self.config_yaml = pkg_resources.resource_filename( 'functest', 'ci/config_functest.yaml') @@ -63,56 +63,6 @@ class FunctestUtilsTesting(unittest.TestCase): self.file_yaml = {'general': {'openstack': {'image_name': 'test_image_name'}}} - @mock.patch('six.moves.urllib.request.urlopen', - side_effect=urllib.error.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('six.moves.urllib.request.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('six.moves.urllib.request.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('six.moves.urllib.request.urlopen', - side_effect=urllib.error.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('six.moves.urllib.request.urlopen') - def test_download_url_default(self, mock_url): - with mock.patch("six.moves.builtins.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_version_daily_job(self): - CONST.__setattr__('BUILD_TAG', self.build_tag) - self.assertEqual(functest_utils.get_version(), self.version) - - def test_get_version_weekly_job(self): - CONST.__setattr__('BUILD_TAG', self.build_tag_week) - self.assertEqual(functest_utils.get_version(), self.version) - - def test_get_version_with_dummy_build_tag(self): - CONST.__setattr__('BUILD_TAG', 'whatever') - self.assertEqual(functest_utils.get_version(), 'unknown') - - def test_get_version_unknown(self): - CONST.__setattr__('BUILD_TAG', 'unknown_build_tag') - self.assertEqual(functest_utils.get_version(), "unknown") - def _get_env_dict(self, var): dic = {'INSTALLER_TYPE': self.installer, 'DEPLOY_SCENARIO': self.scenario, @@ -121,87 +71,6 @@ class FunctestUtilsTesting(unittest.TestCase): dic.pop(var, None) return dic - def _test_push_results_to_db_missing_env(self, env_var): - dic = self._get_env_dict(env_var) - CONST.__setattr__('results_test_db_url', self.db_url) - with 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.result, 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_request_post_failed(self): - dic = self._get_env_dict(None) - CONST.__setattr__('results_test_db_url', self.db_url) - with 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.result, 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) - CONST.__setattr__('results_test_db_url', self.db_url) - with 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.result, self.details)) - self.assertTrue(mock_logger_error.called) - - def test_push_results_to_db_default(self): - dic = self._get_env_dict(None) - CONST.__setattr__('results_test_db_url', self.db_url) - with 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.result, 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 == \ @@ -210,173 +79,96 @@ class FunctestUtilsTesting(unittest.TestCase): 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("six.moves.builtins.open") as mo: - mo.return_value = m - self.assertEqual(functest_utils.get_resolvconf_ns(), - self.test_ip[1:]) - - def _get_environ(self, var): + def _get_environ(self, var, *args): # pylint: disable=unused-argument if var == 'INSTALLER_TYPE': return self.installer - elif var == 'DEPLOY_SCENARIO': + if 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): + @staticmethod + def cmd_readline(): 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): + @mock.patch('functest.utils.functest_utils.LOGGER.error') + @mock.patch('functest.utils.functest_utils.LOGGER.info') + def test_exec_cmd_args_present_ko(self, mock_logger_info, + mock_logger_error): with mock.patch('functest.utils.functest_utils.subprocess.Popen') \ as mock_subproc_open, \ mock.patch('six.moves.builtins.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) + stream = six.BytesIO() + stream.write(self.cmd_readline().encode("utf-8")) + attrs = { + 'return_value.__enter__.return_value.stdout': stream, + 'return_value.__enter__.return_value.wait.return_value': 1} + mock_subproc_open.configure_mock(**attrs) + 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) + msg_exec = f"Executing command: '{self.cmd}'" mock_logger_info.assert_called_once_with(msg_exec) - mopen.assert_called_once_with(self.output_file, "w") + mopen.assert_called_once_with( + self.output_file, "w", encoding='utf-8') 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, - ): + @mock.patch('functest.utils.functest_utils.LOGGER.info') + def test_exec_cmd_args_present_ok(self, mock_logger_info): with mock.patch('functest.utils.functest_utils.subprocess.Popen') \ as mock_subproc_open, \ mock.patch('six.moves.builtins.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) + stream = six.BytesIO() + stream.write(self.cmd_readline().encode("utf-8")) + attrs = { + 'return_value.__enter__.return_value.stdout': stream, + 'return_value.__enter__.return_value.wait.return_value': 0} + mock_subproc_open.configure_mock(**attrs) + 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) + msg_exec = (f"Executing command: '{self.cmd}'") mock_logger_info.assert_called_once_with(msg_exec) - mopen.assert_called_once_with(self.output_file, "w") + mopen.assert_called_once_with( + self.output_file, "w", encoding='utf-8') @mock.patch('sys.stdout') - def test_execute_command_args_missing_with_success(self, stdout=None): + def test_exec_cmd_args_missing_ok(self, stdout=None): + # pylint: disable=unused-argument 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) + stream = six.BytesIO() + stream.write(self.cmd_readline().encode("utf-8")) + attrs = { + 'return_value.__enter__.return_value.stdout': stream, + 'return_value.__enter__.return_value.wait.return_value': 0} + mock_subproc_open.configure_mock(**attrs) + resp = functest_utils.execute_command( + self.cmd, info=False, error_msg="", verbose=False, + output_file=None) self.assertEqual(resp, 0) @mock.patch('sys.stdout') - def test_execute_command_args_missing_with_error(self, stdout=None): + def test_exec_cmd_args_missing_ko(self, stdout=None): + # pylint: disable=unused-argument 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) + attrs = {} + stream = six.BytesIO() + stream.write(self.cmd_readline().encode("utf-8")) + attrs = { + 'return_value.__enter__.return_value.stdout': stream, + 'return_value.__enter__.return_value.wait.return_value': 1} + mock_subproc_open.configure_mock(**attrs) + 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('six.moves.builtins.open', mock.mock_open()), \ - mock.patch('functest.utils.functest_utils.yaml.safe_load') \ - as mock_yaml: - 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): + def test_get_param_from_yaml_failed(self): self.file_yaml['general'] = None with mock.patch('six.moves.builtins.open', mock.mock_open()), \ mock.patch('functest.utils.functest_utils.yaml.safe_load') \ @@ -385,11 +177,11 @@ class FunctestUtilsTesting(unittest.TestCase): 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) + self.assertTrue((f"The parameter {self.parameter} is not" + " defined in config_functest.yaml" + ) in excep.exception) - def test_get_parameter_from_yaml_default(self): + def test_get_param_from_yaml_def(self): with mock.patch('six.moves.builtins.open', mock.mock_open()), \ mock.patch('functest.utils.functest_utils.yaml.safe_load') \ as mock_yaml: @@ -399,28 +191,217 @@ class FunctestUtilsTesting(unittest.TestCase): 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_get_functest_yaml(self): - with mock.patch('six.moves.builtins.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("=======================" - "=======================") + def test_nova_version_exc1(self): + # pylint: disable=protected-access + cloud = mock.Mock() + cloud._compute_client.request.return_value = None + self.assertEqual(functest_utils.get_nova_version(cloud), None) + cloud._compute_client.request.assert_called_once_with('/', 'GET') + + def test_nova_version_exc2(self): + # pylint: disable=protected-access + cloud = mock.Mock() + cloud._compute_client.request.return_value = {"version": None} + self.assertEqual(functest_utils.get_nova_version(cloud), None) + cloud._compute_client.request.assert_called_once_with('/', 'GET') + + def test_nova_version_exc3(self): + # pylint: disable=protected-access + cloud = mock.Mock() + cloud._compute_client.request.return_value = { + "version": {"version": None}} + self.assertEqual(functest_utils.get_nova_version(cloud), None) + cloud._compute_client.request.assert_called_once_with('/', 'GET') + + def test_nova_version_exc4(self): + # pylint: disable=protected-access + cloud = mock.Mock() + cloud._compute_client.request.return_value = { + "version": {"version": "a.b"}} + self.assertEqual(functest_utils.get_nova_version(cloud), None) + cloud._compute_client.request.assert_called_once_with('/', 'GET') + + def test_nova_version(self): + # pylint: disable=protected-access + cloud = mock.Mock() + cloud._compute_client.request.return_value = { + "version": {"version": "2.1"}} + self.assertEqual(functest_utils.get_nova_version(cloud), (2, 1)) + cloud._compute_client.request.assert_called_once_with('/', 'GET') + + @mock.patch('functest.utils.functest_utils.get_nova_version', + return_value=(2, 61)) + def test_openstack_version1(self, *args): + cloud = mock.Mock() + self.assertEqual(functest_utils.get_openstack_version( + cloud), "Rocky") + args[0].assert_called_once_with(cloud) + + @mock.patch('functest.utils.functest_utils.get_nova_version', + return_value=(2, 60)) + def test_openstack_version2(self, *args): + cloud = mock.Mock() + self.assertEqual(functest_utils.get_openstack_version(cloud), "Queens") + args[0].assert_called_once_with(cloud) + + @mock.patch('functest.utils.functest_utils.get_nova_version', + return_value=(2, 43)) + def test_openstack_version3(self, *args): + cloud = mock.Mock() + self.assertEqual(functest_utils.get_openstack_version(cloud), "Pike") + args[0].assert_called_once_with(cloud) + + @mock.patch('functest.utils.functest_utils.get_nova_version', + return_value=(2, 39)) + def test_openstack_version4(self, *args): + cloud = mock.Mock() + self.assertEqual(functest_utils.get_openstack_version(cloud), "Ocata") + args[0].assert_called_once_with(cloud) + + @mock.patch('functest.utils.functest_utils.get_nova_version', + return_value=(2, 26)) + def test_openstack_version5(self, *args): + cloud = mock.Mock() + self.assertEqual(functest_utils.get_openstack_version(cloud), "Newton") + args[0].assert_called_once_with(cloud) + + @mock.patch('functest.utils.functest_utils.get_nova_version', + return_value=(2, 13)) + def test_openstack_version6(self, *args): + cloud = mock.Mock() + self.assertEqual(functest_utils.get_openstack_version(cloud), "Mitaka") + args[0].assert_called_once_with(cloud) + + @mock.patch('functest.utils.functest_utils.get_nova_version', + return_value=(2, 4)) + def test_openstack_version7(self, *args): + cloud = mock.Mock() + self.assertEqual( + functest_utils.get_openstack_version(cloud), "Liberty") + args[0].assert_called_once_with(cloud) + + @mock.patch('functest.utils.functest_utils.get_nova_version', + return_value=(2, 1)) + def test_openstack_version8(self, *args): + cloud = mock.Mock() + self.assertEqual(functest_utils.get_openstack_version(cloud), "Kilo") + args[0].assert_called_once_with(cloud) + + @mock.patch('functest.utils.functest_utils.get_nova_version', + return_value=(1, 9)) + def test_openstack_version9(self, *args): + cloud = mock.Mock() + self.assertEqual( + functest_utils.get_openstack_version(cloud), "Unknown") + args[0].assert_called_once_with(cloud) + + @mock.patch('functest.utils.functest_utils.get_nova_version', + return_value=(3, 1)) + def test_openstack_version10(self, *args): + cloud = mock.Mock() + self.assertEqual( + functest_utils.get_openstack_version(cloud), "Master") + args[0].assert_called_once_with(cloud) + + @mock.patch('functest.utils.functest_utils.get_nova_version', + return_value=(2, 66)) + def test_openstack_version11(self, *args): + cloud = mock.Mock() + self.assertEqual(functest_utils.get_openstack_version( + cloud), "Stein") + args[0].assert_called_once_with(cloud) + + @mock.patch('functest.utils.functest_utils.get_nova_version', + return_value=(2, 78)) + def test_openstack_version12(self, *args): + cloud = mock.Mock() + self.assertEqual(functest_utils.get_openstack_version( + cloud), "Train") + args[0].assert_called_once_with(cloud) + + @mock.patch('functest.utils.functest_utils.get_nova_version', + return_value=(2, 87)) + def test_openstack_version13(self, *args): + cloud = mock.Mock() + self.assertEqual(functest_utils.get_openstack_version( + cloud), "Ussuri") + args[0].assert_called_once_with(cloud) + + @mock.patch('functest.utils.functest_utils.get_nova_version', + return_value=(2, 88)) + def test_openstack_version14(self, *args): + cloud = mock.Mock() + self.assertEqual(functest_utils.get_openstack_version( + cloud), "Wallaby") + args[0].assert_called_once_with(cloud) + + @mock.patch('functest.utils.functest_utils.get_nova_version', + return_value=(2, 89)) + def test_openstack_version15(self, *args): + cloud = mock.Mock() + self.assertEqual(functest_utils.get_openstack_version( + cloud), "Xena") + args[0].assert_called_once_with(cloud) + + @mock.patch('functest.utils.functest_utils.get_nova_version', + return_value=(2, 92)) + def test_openstack_version16(self, *args): + cloud = mock.Mock() + self.assertEqual(functest_utils.get_openstack_version( + cloud), "Zed") + args[0].assert_called_once_with(cloud) + + @mock.patch('functest.utils.functest_utils.get_nova_version', + return_value=None) + def test_openstack_version_exc(self, *args): + cloud = mock.Mock() + self.assertEqual( + functest_utils.get_openstack_version(cloud), "Unknown") + args[0].assert_called_once_with(cloud) + + def test_convert_dict_to_ini(self): + self.assertEqual( + functest_utils.convert_dict_to_ini({}), "") + self.assertEqual( + functest_utils.convert_dict_to_ini({"a": "b"}), "a:b") + value = functest_utils.convert_dict_to_ini({"a": "b", "c": "d"}) + self.assertTrue(value in ('a:b,c:d', 'c:d,a:b')) + with self.assertRaises(AssertionError): + functest_utils.convert_list_to_ini("") + + def test_convert_list_to_ini(self): + self.assertEqual( + functest_utils.convert_list_to_ini([]), "") + self.assertEqual( + functest_utils.convert_list_to_ini(["a"]), "a") + self.assertEqual( + functest_utils.convert_list_to_ini(["a", "b"]), "a,b") + with self.assertRaises(AssertionError): + functest_utils.convert_list_to_ini("") + + def test_convert_ini_to_dict(self): + self.assertEqual( + functest_utils.convert_ini_to_dict(""), {}) + self.assertEqual( + functest_utils.convert_ini_to_dict("a:b"), {"a": "b"}) + self.assertEqual( + functest_utils.convert_ini_to_dict( + "a:b,c:d"), {"a": "b", "c": "d"}) + self.assertEqual( + functest_utils.convert_ini_to_dict( + "a:b:c,d:e:f"), {"a:b": "c", "d:e": "f"}) + with self.assertRaises(AssertionError): + functest_utils.convert_list_to_ini({}) + + def test_convert_ini_to_list(self): + self.assertEqual( + functest_utils.convert_ini_to_list(""), []) + self.assertEqual( + functest_utils.convert_ini_to_list("a"), ["a"]) + self.assertEqual( + functest_utils.convert_ini_to_list("a,b"), ["a", "b"]) + with self.assertRaises(AssertionError): + functest_utils.convert_ini_to_list([]) if __name__ == "__main__": diff --git a/functest/tests/unit/utils/test_openstack_utils.py b/functest/tests/unit/utils/test_openstack_utils.py deleted file mode 100644 index 01085bb7d..000000000 --- a/functest/tests/unit/utils/test_openstack_utils.py +++ /dev/null @@ -1,1811 +0,0 @@ -#!/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 os -import unittest - -import mock - -from functest.utils import openstack_utils -from functest.utils.constants import CONST - - -class OSUtilsTesting(unittest.TestCase): - - 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', - 'OS_CACERT': os_prefix + 'https_cacert', - 'OS_INSECURE': os_prefix + 'https_insecure'} - - 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', - 'https_cacert': 'test_https_cacert', - 'https_insecure': 'test_https_insecure'} - - 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 = {'floating_network_id': 'floating_id', - 'floating_ip_address': 'test_floating_ip'} - 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, - '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], - '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 - - self.cinder_client = mock.Mock() - attrs = {'volumes.list.return_value': [self.volume], - '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) - - self.resource = mock.Mock() - attrs = {'id': 'resource_test_id', - 'name': 'resource_test_name' - } - - self.heat_client = mock.Mock() - attrs = {'resources.get.return_value': self.resource} - self.heat_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 - - mock_obj = mock.Mock() - attrs = {'id': 'domain_id', - 'name': 'test_domain'} - mock_obj.configure_mock(**attrs) - self.domain = 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], - 'domains.list.return_value': [self.domain], - '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.sec_group_rule = {'id': 'sec_group_rule_id', - 'direction': 'direction', - 'protocol': 'protocol', - 'port_range_max': 'port_max', - 'security_group_id': self.sec_group['id'], - 'port_range_min': 'port_min'} - 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(), - 'list_security_groups.return_value': {'security_groups': - [self.sec_group]}, - 'list_security_group_rules.' - 'return_value': {'security_group_rules': - [self.sec_group_rule]}, - '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(), - 'list_floatingips.return_value': {'floatingips': - [self.floating_ip]}, - 'delete_floatingip.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, msg, key='OS_TENANT_NAME', - value='admin'): - try: - del os.environ[key] - except: - pass - f = 'rc_file' - with mock.patch('six.moves.builtins.open', - mock.mock_open(read_data=msg), - create=True) as m: - m.return_value.__iter__ = lambda self: iter(self.readline, '') - openstack_utils.source_credentials(f) - m.assert_called_once_with(f, 'r') - self.assertEqual(os.environ[key], value) - - def test_source_credentials(self): - self._test_source_credentials('OS_TENANT_NAME=admin') - self._test_source_credentials('OS_TENANT_NAME= admin') - self._test_source_credentials('OS_TENANT_NAME = admin') - self._test_source_credentials('OS_TENANT_NAME = "admin"') - self._test_source_credentials('export OS_TENANT_NAME=admin') - self._test_source_credentials('export OS_TENANT_NAME =admin') - self._test_source_credentials('export OS_TENANT_NAME = admin') - self._test_source_credentials('export OS_TENANT_NAME = "admin"') - # This test will fail as soon as rc_file is fixed - self._test_source_credentials( - 'export "\'OS_TENANT_NAME\'" = "\'admin\'"') - - @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') - - @mock.patch('functest.utils.openstack_utils.get_session') - @mock.patch('functest.utils.openstack_utils.keystoneclient.Client') - @mock.patch('functest.utils.openstack_utils.get_keystone_client_version', - return_value='3') - @mock.patch('functest.utils.openstack_utils.os.getenv', - return_value='public') - def test_get_keystone_client_with_interface(self, mock_os_getenv, - mock_keystoneclient_version, - mock_key_client, - mock_get_session): - mock_keystone_obj = mock.Mock() - mock_session_obj = mock.Mock() - mock_key_client.return_value = mock_keystone_obj - mock_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, - interface='public') - - @mock.patch('functest.utils.openstack_utils.get_session') - @mock.patch('functest.utils.openstack_utils.keystoneclient.Client') - @mock.patch('functest.utils.openstack_utils.get_keystone_client_version', - return_value='3') - @mock.patch('functest.utils.openstack_utils.os.getenv', - return_value='admin') - def test_get_keystone_client_no_interface(self, mock_os_getenv, - mock_keystoneclient_version, - mock_key_client, - mock_get_session): - mock_keystone_obj = mock.Mock() - mock_session_obj = mock.Mock() - mock_key_client.return_value = mock_keystone_obj - mock_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, - interface='admin') - - @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) - - @mock.patch('functest.utils.openstack_utils.os.getenv', - return_value=None) - def test_get_heat_client_version_missing_env(self, mock_os_getenv): - self.assertEqual(openstack_utils.get_heat_client_version(), - openstack_utils.DEFAULT_HEAT_API_VERSION) - - @mock.patch('functest.utils.openstack_utils.logger.info') - @mock.patch('functest.utils.openstack_utils.os.getenv', return_value='1') - def test_get_heat_client_version_default(self, mock_os_getenv, - mock_logger_info): - self.assertEqual(openstack_utils.get_heat_client_version(), '1') - mock_logger_info.assert_called_once_with( - "OS_ORCHESTRATION_API_VERSION is set in env as '%s'", '1') - - def test_get_heat_client(self): - mock_heat_obj = mock.Mock() - mock_session_obj = mock.Mock() - with mock.patch('functest.utils.openstack_utils' - '.get_heat_client_version', return_value='1'), \ - mock.patch('functest.utils.openstack_utils' - '.heatclient.Client', - return_value=mock_heat_obj) \ - as mock_heat_client, \ - mock.patch('functest.utils.openstack_utils.get_session', - return_value=mock_session_obj): - self.assertEqual(openstack_utils.get_heat_client(), - mock_heat_obj) - mock_heat_client.assert_called_once_with('1', - 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.neutron_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.neutron_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_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_get_security_group_rules_default(self): - self.assertEqual(openstack_utils. - get_security_group_rules(self.neutron_client, - self.sec_group['id']), - [self.sec_group_rule]) - - @mock.patch('functest.utils.openstack_utils.logger.error') - def test_get_security_group_rules_exception(self, mock_logger_error): - self.assertEqual(openstack_utils. - get_security_group_rules(Exception, - 'sec_group_id'), - None) - self.assertTrue(mock_logger_error.called) - - def test_check_security_group_rules_not_exists(self): - self.assertEqual(openstack_utils. - check_security_group_rules(self.neutron_client, - 'sec_group_id_2', - 'direction', - 'protocol', - 'port_min', - 'port_max'), - True) - - def test_check_security_group_rules_exists(self): - self.assertEqual(openstack_utils. - check_security_group_rules(self.neutron_client, - self.sec_group['id'], - 'direction', - 'protocol', - 'port_min', - 'port_max'), - False) - - @mock.patch('functest.utils.openstack_utils.logger.error') - def test_check_security_group_rules_exception(self, mock_logger_error): - self.assertEqual(openstack_utils. - check_security_group_rules(Exception, - 'sec_group_id', - 'direction', - 'protocol', - 'port_max', - 'port_min'), - None) - self.assertTrue(mock_logger_error.called) - - 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.glance_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('six.moves.builtins.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_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_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_get_domain_id_default(self): - self.assertEqual(openstack_utils. - get_domain_id(self.keystone_client, - 'test_domain'), - 'domain_id') - - def test_create_tenant_default(self): - with mock.patch('functest.utils.openstack_utils.' - 'is_keystone_v3', return_value=True): - CONST.__setattr__('OS_PROJECT_DOMAIN_NAME', 'Default') - 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) - - def test_get_resource_default(self): - with mock.patch('functest.utils.openstack_utils.' - 'is_keystone_v3', return_value=True): - self.assertEqual(openstack_utils. - get_resource(self.heat_client, - 'stack_id', - 'resource'), - self.resource) - - @mock.patch('functest.utils.openstack_utils.logger.error') - def test_get_resource_exception(self, mock_logger_error): - self.assertEqual(openstack_utils. - get_resource(Exception, - 'stack_id', - 'resource'), - None) - self.assertTrue(mock_logger_error.called) - - def test_get_or_create_user_for_vnf_get(self): - with mock.patch('functest.utils.openstack_utils.' - 'get_user_id', - return_value='user_id'), \ - mock.patch('functest.utils.openstack_utils.get_tenant_id', - return_value='tenant_id'): - self.assertFalse(openstack_utils. - get_or_create_user_for_vnf(self.keystone_client, - 'my_vnf')) - - def test_get_or_create_user_for_vnf_create(self): - with mock.patch('functest.utils.openstack_utils.' - 'get_user_id', - return_value=None), \ - mock.patch('functest.utils.openstack_utils.get_tenant_id', - return_value='tenant_id'): - self.assertTrue(openstack_utils. - get_or_create_user_for_vnf(self.keystone_client, - 'my_vnf')) - - def test_get_or_create_user_for_vnf_error_get_user_id(self): - with mock.patch('functest.utils.openstack_utils.' - 'get_user_id', - side_effect=Exception): - self.assertRaises(Exception) - - def test_get_or_create_user_for_vnf_error_get_tenant_id(self): - with mock.patch('functest.utils.openstack_utils.' - 'get_user_id', - return_value='user_id'), \ - mock.patch('functest.utils.openstack_utils.get_tenant_id', - side_effect='Exception'): - self.assertRaises(Exception) - - def test_get_or_create_tenant_for_vnf_get(self): - with mock.patch('functest.utils.openstack_utils.' - 'get_tenant_id', - return_value='tenant_id'): - self.assertFalse( - openstack_utils.get_or_create_tenant_for_vnf( - self.keystone_client, 'tenant_name', 'tenant_description')) - - def test_get_or_create_tenant_for_vnf_create(self): - with mock.patch('functest.utils.openstack_utils.get_tenant_id', - return_value=None): - self.assertTrue( - openstack_utils.get_or_create_tenant_for_vnf( - self.keystone_client, 'tenant_name', 'tenant_description')) - - def test_get_or_create_tenant_for_vnf_error_get_tenant_id(self): - with mock.patch('functest.utils.openstack_utils.' - 'get_tenant_id', - side_effect=Exception): - self.assertRaises(Exception) - - def test_download_and_add_image_on_glance_image_creation_failure(self): - with mock.patch('functest.utils.openstack_utils.' - 'os.makedirs'), \ - mock.patch('functest.utils.openstack_utils.' - 'ft_utils.download_url', - return_value=True), \ - mock.patch('functest.utils.openstack_utils.' - 'create_glance_image', - return_value=''): - resp = openstack_utils.download_and_add_image_on_glance( - self.glance_client, - 'image_name', - 'http://url', - 'data_dir') - self.assertEqual(resp, False) - - -if __name__ == "__main__": - logging.disable(logging.CRITICAL) - unittest.main(verbosity=2) |