Add possibility to pass prefix before cli command
In some case cli command should be called with prefix, e.g. for set some environment variables like this `env LC_ALL=en_US.UTF-8`. Now CLIClient has optional argument in constructor - prefix, which will applying before each call Change-Id: I1ce5fb322bd75a5499666e184b68ea6e26835a55
This commit is contained in:
parent
4b053d228a
commit
d95375c039
|
@ -29,7 +29,7 @@ LOG = logging.getLogger(__name__)
|
|||
|
||||
|
||||
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 @@ def execute(cmd, action, flags='', params='', fail_ok=False,
|
|||
: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 @@ class CLIClient(object):
|
|||
: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 @@ class CLIClient(object):
|
|||
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 @@ class CLIClient(object):
|
|||
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):
|
||||
|
|
|
@ -11,7 +11,6 @@
|
|||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
|
||||
import mock
|
||||
import subprocess
|
||||
|
||||
|
@ -74,3 +73,20 @@ class TestExecute(base.TestCase):
|
|||
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'})
|
||||
|
|
Loading…
Reference in New Issue