Merge "Add possibility to pass prefix before cli command"
diff --git a/tempest/lib/cli/base.py b/tempest/lib/cli/base.py
index a43d002..72a15b5 100644
--- a/tempest/lib/cli/base.py
+++ b/tempest/lib/cli/base.py
@@ -29,7 +29,7 @@
 
 
 def execute(cmd, action, flags='', params='', fail_ok=False,
-            merge_stderr=False, cli_dir='/usr/bin'):
+            merge_stderr=False, cli_dir='/usr/bin', prefix=''):
     """Executes specified command for the given action.
 
     :param cmd: command to be executed
@@ -48,9 +48,12 @@
     :type merge_stderr: boolean
     :param cli_dir: The path where the cmd can be executed
     :type cli_dir: string
+    :param prefix: prefix to insert before command
+    :type prefix: string
     """
-    cmd = ' '.join([os.path.join(cli_dir, cmd),
+    cmd = ' '.join([prefix, os.path.join(cli_dir, cmd),
                     flags, action, params])
+    cmd = cmd.strip()
     LOG.info("running: '%s'" % cmd)
     if six.PY2:
         cmd = cmd.encode('utf-8')
@@ -88,10 +91,12 @@
     :type cli_dir: string
     :param insecure: if True, --insecure is passed to python client binaries.
     :type insecure: boolean
+    :param prefix: prefix to insert before commands
+    :type prefix: string
     """
 
     def __init__(self, username='', password='', tenant_name='', uri='',
-                 cli_dir='', insecure=False, *args, **kwargs):
+                 cli_dir='', insecure=False, prefix='', *args, **kwargs):
         """Initialize a new CLIClient object."""
         super(CLIClient, self).__init__()
         self.cli_dir = cli_dir if cli_dir else '/usr/bin'
@@ -100,6 +105,7 @@
         self.password = password
         self.uri = uri
         self.insecure = insecure
+        self.prefix = prefix
 
     def nova(self, action, flags='', params='', fail_ok=False,
              endpoint_type='publicURL', merge_stderr=False):
@@ -365,7 +371,7 @@
         else:
             flags = creds + ' ' + flags
         return execute(cmd, action, flags, params, fail_ok, merge_stderr,
-                       self.cli_dir)
+                       self.cli_dir, prefix=self.prefix)
 
 
 class ClientTestBase(base.BaseTestCase):
diff --git a/tempest/tests/lib/cli/test_execute.py b/tempest/tests/lib/cli/test_execute.py
index cc9c94c..aaeb6f4 100644
--- a/tempest/tests/lib/cli/test_execute.py
+++ b/tempest/tests/lib/cli/test_execute.py
@@ -11,7 +11,6 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
-
 import mock
 import subprocess
 
@@ -74,3 +73,20 @@
         self.assertRaises(exceptions.CommandFailed, cli_base.execute,
                           "/bin/ls", action="tempest", flags="--foobar",
                           merge_stderr=True)
+
+    def test_execute_with_prefix(self):
+        result = cli_base.execute("env", action="",
+                                  prefix="env NEW_VAR=1")
+        self.assertIsInstance(result, str)
+        self.assertIn("NEW_VAR=1", result)
+
+
+class TestCLIClient(base.TestCase):
+
+    @mock.patch.object(cli_base, 'execute')
+    def test_execute_with_prefix(self, mock_execute):
+        cli = cli_base.CLIClient(prefix='env LAC_ALL=C')
+        cli.glance('action')
+        self.assertEqual(mock_execute.call_count, 1)
+        self.assertEqual(mock_execute.call_args[1],
+                         {'prefix': 'env LAC_ALL=C'})