[odl-sfc] Add function to retrieve a resource from HEAT
authorJuan Vidal <juan.vidal.allende@ericsson.com>
Tue, 21 Feb 2017 10:24:38 +0000 (10:24 +0000)
committerJuan Vidal <juan.vidal.allende@ericsson.com>
Tue, 28 Feb 2017 12:56:28 +0000 (12:56 +0000)
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 <juan.vidal.allende@ericsson.com>
functest/tests/unit/utils/test_openstack_utils.py
functest/utils/openstack_utils.py
requirements.txt
test-requirements.txt

index 447271f..ef3764c 100644 (file)
@@ -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)
index 3093cb5..270b90d 100755 (executable)
@@ -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
index 68b889b..ee629ea 100644 (file)
@@ -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
index 16466b8..471e9c3 100644 (file)
@@ -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