diff options
Diffstat (limited to 'functest/tests/unit')
4 files changed, 203 insertions, 83 deletions
diff --git a/functest/tests/unit/opnfv_tests/openstack/refstack_client/__init__.py b/functest/tests/unit/opnfv_tests/openstack/refstack_client/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/functest/tests/unit/opnfv_tests/openstack/refstack_client/__init__.py diff --git a/functest/tests/unit/opnfv_tests/openstack/refstack_client/test_refstack_client.py b/functest/tests/unit/opnfv_tests/openstack/refstack_client/test_refstack_client.py new file mode 100644 index 00000000..4eb5a250 --- /dev/null +++ b/functest/tests/unit/opnfv_tests/openstack/refstack_client/test_refstack_client.py @@ -0,0 +1,115 @@ +#!/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 os +import unittest + +from functest.core import testcase_base +from functest.opnfv_tests.openstack.refstack_client import refstack_client +from functest.utils.constants import CONST + + +class OSRefstackClientTesting(unittest.TestCase): + + logging.disable(logging.CRITICAL) + _config = \ + os.path.join(CONST.dir_functest_test, CONST.refstack_tempest_conf_path) + _testlist = \ + os.path.join(CONST.dir_functest_test, CONST.refstack_defcore_list) + + def setUp(self): + self.defaultargs = {'config': self._config, + 'testlist': self._testlist} + self.refstackclient = refstack_client.RefstackClient() + + def test_source_venv(self): + CONST.dir_refstack_client = 'test_repo_dir' + with mock.patch('functest.opnfv_tests.openstack.refstack_client.' + 'refstack_client.ft_utils.execute_command') as m: + cmd = ("cd {0};" + ". .venv/bin/activate;" + "cd -;".format(CONST.dir_refstack_client)) + self.refstackclient.source_venv() + m.assert_any_call(cmd) + + def test_run_defcore_default(self): + with mock.patch('functest.opnfv_tests.openstack.refstack_client.' + 'refstack_client.ft_utils.execute_command') as m: + cmd = ("cd {0};" + "./refstack-client test -c {1} -v --test-list {2};" + "cd -;".format(CONST.dir_refstack_client, + self._config, + self._testlist)) + self.refstackclient.run_defcore_default() + m.assert_any_call(cmd) + + def test_run_defcore(self): + config = 'tempest.conf' + testlist = 'testlist' + with mock.patch('functest.opnfv_tests.openstack.refstack_client.' + 'refstack_client.ft_utils.execute_command') as m: + cmd = ("cd {0};" + "./refstack-client test -c {1} -v --test-list {2};" + "cd -;".format(CONST.dir_refstack_client, + config, + testlist)) + self.refstackclient.run_defcore(config, testlist) + m.assert_any_call(cmd) + + def _get_main_kwargs(self, key=None): + kwargs = {'config': self._config, + 'testlist': self._testlist} + if key: + del kwargs[key] + return kwargs + + def _test_main(self, status, *args): + kwargs = self._get_main_kwargs() + self.assertEqual(self.refstackclient.main(**kwargs), status) + if len(args) > 0: + args[0].assert_called_once_with( + refstack_client.RefstackClient.result_dir) + if len(args) > 1: + args + + def _test_main_missing_keyword(self, key): + kwargs = self._get_main_kwargs(key) + self.assertEqual(self.refstackclient.main(**kwargs), + testcase_base.TestcaseBase.EX_RUN_ERROR) + + def test_main_missing_conf(self): + self._test_main_missing_keyword('config') + + def test_main_missing_testlist(self): + self._test_main_missing_keyword('testlist') + + def _test_argparser(self, arg, value): + self.defaultargs[arg] = value + parser = refstack_client.RefstackClientParser() + self.assertEqual(parser.parse_args(["--{}={}".format(arg, value)]), + self.defaultargs) + + def test_argparser_conf(self): + self._test_argparser('config', self._config) + + def test_argparser_testlist(self): + self._test_argparser('testlist', self._testlist) + + def test_argparser_multiple_args(self): + self.defaultargs['config'] = self._config + self.defaultargs['testlist'] = self._testlist + parser = refstack_client.RefstackClientParser() + self.assertEqual(parser.parse_args( + ["--config={}".format(self._config), + "--testlist={}".format(self._testlist) + ]), self.defaultargs) + + +if __name__ == "__main__": + unittest.main(verbosity=2) diff --git a/functest/tests/unit/opnfv_tests/openstack/tempest/test_conf_utils.py b/functest/tests/unit/opnfv_tests/openstack/tempest/test_conf_utils.py index 6121f4eb..caf21925 100644 --- a/functest/tests/unit/opnfv_tests/openstack/tempest/test_conf_utils.py +++ b/functest/tests/unit/opnfv_tests/openstack/tempest/test_conf_utils.py @@ -18,6 +18,72 @@ class OSTempestConfUtilsTesting(unittest.TestCase): logging.disable(logging.CRITICAL) + def test_create_tempest_resources_missing_network_dic(self): + with mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.' + 'os_utils.get_keystone_client', + return_value=mock.Mock()), \ + mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.' + 'os_utils.create_tenant', + return_value='test_tenant_id'), \ + mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.' + 'os_utils.create_user', + return_value='test_user_id'), \ + mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.' + 'os_utils.create_shared_network_full', + return_value=None), \ + self.assertRaises(Exception) as context: + conf_utils.create_tempest_resources() + msg = 'Failed to create private network' + self.assertTrue(msg in context) + + def test_create_tempest_resources_missing_image(self): + CONST.tempest_use_custom_images = 'test_image' + with mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.' + 'os_utils.get_keystone_client', + return_value=mock.Mock()), \ + mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.' + 'os_utils.create_tenant', + return_value='test_tenant_id'), \ + mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.' + 'os_utils.create_user', + return_value='test_user_id'), \ + mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.' + 'os_utils.create_shared_network_full', + return_value=mock.Mock()), \ + mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.' + 'os_utils.get_or_create_image', + return_value=(mock.Mock(), None)), \ + self.assertRaises(Exception) as context: + conf_utils.create_tempest_resources() + msg = 'Failed to create image' + self.assertTrue(msg in context) + + def test_create_tempest_resources_missing_flavor(self): + CONST.tempest_use_custom_images = 'test_image' + CONST.tempest_use_custom_flavors = 'test_flavour' + with mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.' + 'os_utils.get_keystone_client', + return_value=mock.Mock()), \ + mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.' + 'os_utils.create_tenant', + return_value='test_tenant_id'), \ + mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.' + 'os_utils.create_user', + return_value='test_user_id'), \ + mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.' + 'os_utils.create_shared_network_full', + return_value=mock.Mock()), \ + mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.' + 'os_utils.get_or_create_image', + return_value=(mock.Mock(), 'image_id')), \ + mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.' + 'os_utils.get_or_create_flavor', + return_value=(mock.Mock(), None)), \ + self.assertRaises(Exception) as context: + conf_utils.create_tempest_resources() + msg = 'Failed to create flavor' + self.assertTrue(msg in context) + def test_get_verifier_id_missing_verifier(self): CONST.tempest_deployment_name = 'test_deploy_name' with mock.patch('functest.opnfv_tests.openstack.tempest.' diff --git a/functest/tests/unit/opnfv_tests/openstack/tempest/test_tempest.py b/functest/tests/unit/opnfv_tests/openstack/tempest/test_tempest.py index 8ae7b4db..856cd143 100644 --- a/functest/tests/unit/opnfv_tests/openstack/tempest/test_tempest.py +++ b/functest/tests/unit/opnfv_tests/openstack/tempest/test_tempest.py @@ -13,7 +13,6 @@ import mock from functest.core import testcase_base from functest.opnfv_tests.openstack.tempest import tempest from functest.opnfv_tests.openstack.tempest import conf_utils -from functest.utils.constants import CONST class OSTempestTesting(unittest.TestCase): @@ -35,72 +34,6 @@ class OSTempestTesting(unittest.TestCase): return_value='test_verifier_deploy_dir'): self.tempestcommon = tempest.TempestCommon() - def test_create_tempest_resources_missing_network_dic(self): - with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os_utils.get_keystone_client', - return_value=mock.Mock()), \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os_utils.create_tenant', - return_value='test_tenant_id'), \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os_utils.create_user', - return_value='test_user_id'), \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os_utils.create_shared_network_full', - return_value=None), \ - self.assertRaises(Exception) as context: - self.tempestcommon.create_tempest_resources() - msg = 'Failed to create private network' - self.assertTrue(msg in context) - - def test_create_tempest_resources_missing_image(self): - CONST.tempest_use_custom_images = 'test_image' - with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os_utils.get_keystone_client', - return_value=mock.Mock()), \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os_utils.create_tenant', - return_value='test_tenant_id'), \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os_utils.create_user', - return_value='test_user_id'), \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os_utils.create_shared_network_full', - return_value=mock.Mock()), \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os_utils.get_or_create_image', - return_value=(mock.Mock(), None)), \ - self.assertRaises(Exception) as context: - self.tempestcommon.create_tempest_resources() - msg = 'Failed to create image' - self.assertTrue(msg in context) - - def test_create_tempest_resources_missing_flavor(self): - CONST.tempest_use_custom_images = 'test_image' - CONST.tempest_use_custom_flavors = 'test_flavour' - with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os_utils.get_keystone_client', - return_value=mock.Mock()), \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os_utils.create_tenant', - return_value='test_tenant_id'), \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os_utils.create_user', - return_value='test_user_id'), \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os_utils.create_shared_network_full', - return_value=mock.Mock()), \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os_utils.get_or_create_image', - return_value=(mock.Mock(), 'image_id')), \ - mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' - 'os_utils.get_or_create_flavor', - return_value=(mock.Mock(), None)), \ - self.assertRaises(Exception) as context: - self.tempestcommon.create_tempest_resources() - msg = 'Failed to create flavor' - self.assertTrue(msg in context) - @mock.patch('functest.opnfv_tests.openstack.tempest.tempest.logger.debug') def test_generate_test_list_defcore_mode(self, mock_logger_debug): self.tempestcommon.MODE = 'defcore' @@ -191,8 +124,9 @@ class OSTempestTesting(unittest.TestCase): 'os.path.exists', return_value=False), \ mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' 'os.makedirs') as mock_os_makedirs, \ - mock.patch.object(self.tempestcommon, 'create_tempest_resources', - return_value=ret): + mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' + 'conf_utils.create_tempest_resources', + return_value="image_and_flavor"): self.assertEqual(self.tempestcommon.run(), ret) self.assertTrue(mock_os_makedirs.called) @@ -204,9 +138,9 @@ class OSTempestTesting(unittest.TestCase): 'os.path.exists', return_value=False), \ mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' 'os.makedirs') as mock_os_makedirs, \ - mock.patch.object(self.tempestcommon, - 'create_tempest_resources', - return_value=ret_ok), \ + mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' + 'conf_utils.create_tempest_resources', + return_value=ret_ok), \ mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' 'conf_utils.configure_tempest', return_value=ret): @@ -221,8 +155,9 @@ class OSTempestTesting(unittest.TestCase): 'os.path.exists', return_value=False), \ mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' 'os.makedirs') as mock_os_makedirs, \ - mock.patch.object(self.tempestcommon, 'create_tempest_resources', - return_value=ret_ok), \ + mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' + 'conf_utils.create_tempest_resources', + return_value=ret_ok), \ mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' 'conf_utils.configure_tempest', return_value=ret_ok), \ @@ -239,8 +174,9 @@ class OSTempestTesting(unittest.TestCase): 'os.path.exists', return_value=False), \ mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' 'os.makedirs') as mock_os_makedirs, \ - mock.patch.object(self.tempestcommon, 'create_tempest_resources', - return_value=ret_ok), \ + mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' + 'conf_utils.create_tempest_resources', + return_value=ret_ok), \ mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' 'conf_utils.configure_tempest', return_value=ret_ok), \ @@ -252,15 +188,16 @@ class OSTempestTesting(unittest.TestCase): ret) self.assertTrue(mock_os_makedirs.called) - def test_run_missing_default_criteria_pass(self): + def test_run_missing_parse_verifier_result(self): + ret = testcase_base.TestcaseBase.EX_RUN_ERROR ret_ok = testcase_base.TestcaseBase.EX_OK - self.tempestcommon.criteria = "PASS" with mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' 'os.path.exists', return_value=False), \ mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' 'os.makedirs') as mock_os_makedirs, \ - mock.patch.object(self.tempestcommon, 'create_tempest_resources', - return_value=ret_ok), \ + mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' + 'conf_utils.create_tempest_resources', + return_value=ret_ok), \ mock.patch('functest.opnfv_tests.openstack.tempest.tempest.' 'conf_utils.configure_tempest', return_value=ret_ok), \ @@ -268,10 +205,12 @@ class OSTempestTesting(unittest.TestCase): return_value=ret_ok), \ mock.patch.object(self.tempestcommon, 'apply_tempest_blacklist', return_value=ret_ok), \ - mock.patch.object(self.tempestcommon, 'run_verifier_tests'), \ - mock.patch.object(self.tempestcommon, 'parse_verifier_result'): + mock.patch.object(self.tempestcommon, 'run_verifier_tests', + return_value=ret_ok), \ + mock.patch.object(self.tempestcommon, 'parse_verifier_result', + return_value=ret): self.assertEqual(self.tempestcommon.run(), - ret_ok) + ret) self.assertTrue(mock_os_makedirs.called) |