Merge "ganesha utils: allow remote execution as root"

This commit is contained in:
Jenkins 2015-02-22 02:23:35 +00:00 committed by Gerrit Code Review
commit 50a267a3af
2 changed files with 38 additions and 4 deletions

View File

@ -62,7 +62,14 @@ class SSHExecutor(object):
self.pool = utils.SSHPool(*args, **kwargs)
def __call__(self, *args, **kwargs):
# argument with identifier 'run_as_root=' is not accepted by
# processutils's ssh_execute() method unlike processutils's execute()
# method. So implement workaround to enable or disable 'run as root'
# behavior.
run_as_root = kwargs.pop('run_as_root', False)
cmd = ' '.join(pipes.quote(a) for a in args)
if run_as_root:
cmd = ' '.join(['sudo', cmd])
ssh = self.pool.get()
try:
ret = processutils.ssh_execute(ssh, cmd, **kwargs)

View File

@ -15,7 +15,10 @@
import os
from manila.share.drivers.ganesha import utils
import ddt
import mock
from manila.share.drivers.ganesha import utils as ganesha_utils
from manila import test
@ -37,15 +40,39 @@ class GaneshaUtilsTests(test.TestCase):
"""Tests Ganesha utility functions."""
def test_patch(self):
ret = utils.patch(patch_test_dict1, patch_test_dict2, patch_test_dict3)
ret = ganesha_utils.patch(patch_test_dict1, patch_test_dict2,
patch_test_dict3)
self.assertEqual(patch_test_dict_result, ret)
def test_walk(self):
ret = [elem for elem in utils.walk(walk_test_dict)]
ret = [elem for elem in ganesha_utils.walk(walk_test_dict)]
self.assertEqual(walk_test_list, ret)
def test_path_from(self):
self.mock_object(os.path, 'abspath',
lambda path: os.path.join('/foo/bar', path))
ret = utils.path_from('baz.py', '../quux', 'tic/tac/toe')
ret = ganesha_utils.path_from('baz.py', '../quux', 'tic/tac/toe')
self.assertEqual('/foo/quux/tic/tac/toe', os.path.normpath(ret))
@ddt.ddt
class SSHExecutorTestCase(test.TestCase):
"""Tests SSHExecutor."""
@ddt.data({'run_as_root': True, 'expected_prefix': 'sudo '},
{'run_as_root': False, 'expected_prefix': ''})
@ddt.unpack
def test_call_ssh_exec_object_with_run_as_root(
self, run_as_root, expected_prefix):
with mock.patch.object(ganesha_utils.utils, 'SSHPool'):
self.execute = ganesha_utils.SSHExecutor()
fake_ssh_object = mock.Mock()
self.mock_object(self.execute.pool, 'get',
mock.Mock(return_value=fake_ssh_object))
self.mock_object(ganesha_utils.processutils, 'ssh_execute',
mock.Mock(return_value=('', '')))
ret = self.execute('ls', run_as_root=run_as_root)
self.assertEqual(('', ''), ret)
self.execute.pool.get.assert_called_once_with()
ganesha_utils.processutils.ssh_execute.assert_called_once_with(
fake_ssh_object, expected_prefix + 'ls')