Limit the substitution of ' in env vars
authorCédric Ollivier <cedric.ollivier@orange.com>
Tue, 14 Feb 2017 10:16:22 +0000 (11:16 +0100)
committerCédric Ollivier <cedric.ollivier@orange.com>
Tue, 14 Feb 2017 10:24:07 +0000 (11:24 +0100)
Only ' located and the beginning or at the end of the string are
removed. It completes the first proposal [1].

[1] https://gerrit.opnfv.org/gerrit/#/c/28563/

Change-Id: Ic21cea4c6e98d92983f385c875c3e214411b5f2d
Signed-off-by: Cédric Ollivier <cedric.ollivier@orange.com>
functest/tests/unit/utils/test_openstack_utils.py
functest/utils/openstack_utils.py

index 0971b4e..447271f 100644 (file)
@@ -379,6 +379,9 @@ class OSUtilsTesting(unittest.TestCase):
         self._test_source_credentials('export OS_TENANT_NAME = "admin"')
         self._test_source_credentials('OS_TENANT_NAME', value='')
         self._test_source_credentials('export OS_TENANT_NAME', value='')
+        # This test will fail as soon as rc_file is fixed
+        self._test_source_credentials(
+            'export "\'OS_TENANT_NAME\'" = "\'admin\'"')
 
     @mock.patch('functest.utils.openstack_utils.os.getenv',
                 return_value=None)
index c21ed81..3093cb5 100755 (executable)
@@ -114,10 +114,12 @@ def get_credentials(other_creds={}):
 def source_credentials(rc_file):
     with open(rc_file, "r") as f:
         for line in f:
-            var = line.rstrip('"\n').replace(
-                'export ', '').replace("'", "").split("=")
-            key = re.sub(r'^ *| *$', '', var[0])
-            value = re.sub(r'^[" ]*|[ "]*$', '', "".join(var[1:]))
+            var = line.rstrip('"\n').replace('export ', '').split("=")
+            # The two next lines should be modified as soon as rc_file
+            # conforms with common rules. Be aware that it could induce
+            # issues if value starts with '
+            key = re.sub(r'^["\' ]*|[ \'"]*$', '', var[0])
+            value = re.sub(r'^["\' ]*|[ \'"]*$', '', "".join(var[1:]))
             os.environ[key] = value