Add the function of getting endpoint 33/34933/2
authorLinda Wang <wangwulin@huawei.com>
Thu, 18 May 2017 01:53:18 +0000 (01:53 +0000)
committerLinda Wang <wangwulin@huawei.com>
Fri, 19 May 2017 07:16:25 +0000 (07:16 +0000)
Functest is trying to leverage SNAPS-OO library to replace some
utility functions, in which def get_endpoint is called by testcases
onos, odl, multisite and cloudify_ims.

JIRA: SNAPS-78

Change-Id: Icb2778e0337a5d4246762ac3037773b39d5d554d
Signed-off-by: Linda Wang <wangwulin@huawei.com>
snaps/openstack/utils/keystone_utils.py
snaps/openstack/utils/tests/keystone_utils_tests.py

index 8175b9a..622481c 100644 (file)
@@ -24,6 +24,27 @@ logger = logging.getLogger('keystone_utils')
 V2_VERSION = 'v2.0'
 
 
+def get_session_auth(os_creds):
+    """
+    Return the session auth for keystone session
+    :param os_creds: the OpenStack credentials (OSCreds) object
+    :return: the auth
+    """
+    if os_creds.identity_api_version == 3:
+        auth = v3.Password(auth_url=os_creds.auth_url,
+                           username=os_creds.username,
+                           password=os_creds.password,
+                           project_name=os_creds.project_name,
+                           user_domain_id=os_creds.user_domain_id,
+                           project_domain_id=os_creds.project_domain_id)
+    else:
+        auth = v2.Password(auth_url=os_creds.auth_url,
+                           username=os_creds.username,
+                           password=os_creds.password,
+                           tenant_name=os_creds.project_name)
+    return auth
+
+
 def keystone_session(os_creds):
     """
     Creates a keystone session used for authenticating OpenStack clients
@@ -32,13 +53,7 @@ def keystone_session(os_creds):
     """
     logger.debug('Retrieving Keystone Session')
 
-    if os_creds.identity_api_version == 3:
-        auth = v3.Password(auth_url=os_creds.auth_url, username=os_creds.username, password=os_creds.password,
-                           project_name=os_creds.project_name, user_domain_id=os_creds.user_domain_id,
-                           project_domain_id=os_creds.project_domain_id)
-    else:
-        auth = v2.Password(auth_url=os_creds.auth_url, username=os_creds.username, password=os_creds.password,
-                           tenant_name=os_creds.project_name)
+    auth = get_session_auth(os_creds)
 
     req_session = None
     if os_creds.proxy_settings:
@@ -56,6 +71,20 @@ def keystone_client(os_creds):
     return Client(version=os_creds.identity_api_version, session=keystone_session(os_creds))
 
 
+def get_endpoint(os_creds, service_type, endpoint_type='publicURL'):
+    """
+    Returns the endpoint of specific service
+    :param os_creds: the OpenStack credentials (OSCreds) object
+    :param service_type: the type of specific service
+    :param endpoint_type: the type of endpoint
+    :return: the endpoint url
+    """
+    auth = get_session_auth(os_creds)
+    return keystone_session(os_creds).get_endpoint(auth=auth,
+                                                   service_type=service_type,
+                                                   endpoint_type=endpoint_type)
+
+
 def get_project(keystone=None, os_creds=None, project_name=None):
     """
     Returns the first project object or None if not found
index c072fd3..7bd7f5a 100644 (file)
@@ -46,6 +46,32 @@ class KeystoneSmokeTests(OSComponentTestCase):
             keystone = keystone_utils.keystone_client(OSCreds('user', 'pass', 'url', 'project'))
             keystone.users.list()
 
+    def test_get_endpoint_success(self):
+        """
+        Tests to ensure that proper credentials and proper service type can succeed.
+        """
+        endpoint = keystone_utils.get_endpoint(self.os_creds,
+                                               service_type="identity")
+        self.assertIsNotNone(endpoint)
+
+    def test_get_endpoint_fail_without_proper_service(self):
+        """
+        Tests to ensure that proper credentials and improper service type cannot succeed.
+        """
+        with self.assertRaises(Exception):
+            keystone_utils.get_endpoint(self.os_creds, service_type="glance")
+
+    def test_get_endpoint_fail_without_proper_credentials(self):
+        """
+        Tests to ensure that improper credentials and proper service type cannot succeed.
+        """
+        from snaps.openstack.os_credentials import OSCreds
+
+        with self.assertRaises(Exception):
+            keystone_utils.get_endpoint(
+                OSCreds('user', 'pass', 'url', 'project'),
+                service_type="image")
+
 
 class KeystoneUtilsTests(OSComponentTestCase):
     """