condensing python code in utils check_credentials
authorDan Radez <dradez@redhat.com>
Mon, 7 Dec 2015 19:04:55 +0000 (14:04 -0500)
committerDan Radez <dradez@redhat.com>
Mon, 7 Dec 2015 19:06:03 +0000 (14:06 -0500)
comment said:
TODO: there must be a short way to do this
doing if os.environ["something"] == "" throws an error

This does the same thing removing the try catches using conditionals and list functions

Change-Id: I614e0aa49ab62aeeb738bdd36ef55452d585d8fb
Signed-off-by: Dan Radez <dradez@redhat.com>
testcases/functest_utils.py

index 59af217..d09ae83 100644 (file)
@@ -23,25 +23,8 @@ def check_credentials():
     """
     Check if the OpenStack credentials (openrc) are sourced
     """
-    # TODO: there must be a short way to do this
-    # doing if os.environ["something"] == "" throws an error
-    try:
-        os.environ['OS_AUTH_URL']
-    except KeyError:
-        return False
-    try:
-        os.environ['OS_USERNAME']
-    except KeyError:
-        return False
-    try:
-        os.environ['OS_PASSWORD']
-    except KeyError:
-        return False
-    try:
-        os.environ['OS_TENANT_NAME']
-    except KeyError:
-        return False
-    return True
+    env_vars = ['OS_AUTH_URL','OS_USERNAME','OS_PASSWORD','OS_TENANT_NAME']
+    return all(map(lambda v: v in os.environ and os.environ[v], env_vars))
 
 
 def get_credentials(service):