From f8c0ae3d732cb423902c1de5caec262a98b044b6 Mon Sep 17 00:00:00 2001 From: Juan Vidal Date: Tue, 21 Feb 2017 10:24:38 +0000 Subject: [odl-sfc] Add function to retrieve a resource from HEAT Introduces function to retrieve a HEAT client. For the moment, the only wrapper function is the one to retrieve resources. Added unit tests to cover the new functions. python-heatclient is added to requirements.txt and test_requirements.txt. Change-Id: I547138141c6aad611f2353599fb70a013c83058a Signed-off-by: Juan Vidal --- functest/tests/unit/utils/test_openstack_utils.py | 58 ++++++++++++++++++++++- functest/utils/openstack_utils.py | 28 +++++++++++ requirements.txt | 1 + test-requirements.txt | 1 + 4 files changed, 87 insertions(+), 1 deletion(-) diff --git a/functest/tests/unit/utils/test_openstack_utils.py b/functest/tests/unit/utils/test_openstack_utils.py index 447271fc6..ef3764cc5 100644 --- a/functest/tests/unit/utils/test_openstack_utils.py +++ b/functest/tests/unit/utils/test_openstack_utils.py @@ -104,7 +104,6 @@ class OSUtilsTesting(unittest.TestCase): 'servers.create.return_value': self.instance, 'flavors.list.return_value': [self.flavor], 'flavors.find.return_value': self.flavor, - 'flavors.list.return_value': [self.flavor], 'servers.add_floating_ip.return_value': mock.Mock(), 'servers.force_delete.return_value': mock.Mock(), 'aggregates.list.return_value': [self.aggregate], @@ -162,6 +161,15 @@ class OSUtilsTesting(unittest.TestCase): } 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'} @@ -543,6 +551,36 @@ class OSUtilsTesting(unittest.TestCase): 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]) @@ -1700,6 +1738,24 @@ class OSUtilsTesting(unittest.TestCase): '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) + if __name__ == "__main__": unittest.main(verbosity=2) diff --git a/functest/utils/openstack_utils.py b/functest/utils/openstack_utils.py index 3093cb558..270b90dfd 100755 --- a/functest/utils/openstack_utils.py +++ b/functest/utils/openstack_utils.py @@ -18,6 +18,7 @@ from keystoneauth1 import loading from keystoneauth1 import session from cinderclient import client as cinderclient from glanceclient import client as glanceclient +from heatclient import client as heatclient from novaclient import client as novaclient from keystoneclient import client as keystoneclient from neutronclient.neutron import client as neutronclient @@ -28,6 +29,7 @@ import functest.utils.functest_utils as ft_utils logger = ft_logger.Logger("openstack_utils").getLogger() DEFAULT_API_VERSION = '2' +DEFAULT_HEAT_API_VERSION = '1' # ********************************************* @@ -241,6 +243,20 @@ def get_glance_client(other_creds={}): return glanceclient.Client(get_glance_client_version(), session=sess) +def get_heat_client_version(): + api_version = os.getenv('OS_ORCHESTRATION_API_VERSION') + if api_version is not None: + logger.info("OS_ORCHESTRATION_API_VERSION is set in env as '%s'", + api_version) + return api_version + return DEFAULT_HEAT_API_VERSION + + +def get_heat_client(other_creds={}): + sess = get_session(other_creds) + return heatclient.Client(get_heat_client_version(), session=sess) + + # ********************************************* # NOVA # ********************************************* @@ -1383,3 +1399,15 @@ def delete_user(keystone_client, user_id): logger.error("Error [delete_user(keystone_client, '%s')]: %s" % (user_id, e)) return False + + +# ********************************************* +# HEAT +# ********************************************* +def get_resource(heat_client, stack_id, resource): + try: + resources = heat_client.resources.get(stack_id, resource) + return resources + except Exception, e: + logger.error("Error [get_resource]: %s" % e) + return None diff --git a/requirements.txt b/requirements.txt index 68b889b35..ee629eab8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,6 +9,7 @@ pyyaml==3.10 gitpython==1.0.1 python-openstackclient==2.3.0 python-ceilometerclient==2.6.2 +python-heatclient==1.7.0 python-keystoneclient==3.5.0 python-neutronclient==6.0.0 python-novaclient==6.0.0 diff --git a/test-requirements.txt b/test-requirements.txt index 16466b8a3..471e9c30a 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -14,6 +14,7 @@ mock==1.3.0 nose==1.3.7 python-ceilometerclient==2.6.2 python-congressclient==1.5.0 +python-heatclient==1.7.0 python-keystoneclient==3.5.0 python-neutronclient==6.0.0 python-openstackclient==2.3.0 -- cgit 1.2.3-korg