else:
return self._execute(options, command, args)
- def _as_root(self, options, command, args):
+ def _as_root(self, options, command, args, use_root_namespace=False):
if not self.root_helper:
raise exceptions.SudoRequired()
+
+ namespace = self.namespace if not use_root_namespace else None
+
return self._execute(options,
command,
args,
self.root_helper,
- self.namespace)
+ namespace)
@classmethod
def _execute(cls, options, command, args, root_helper=None,
def _as_root(self, *args, **kwargs):
return self._parent._as_root(kwargs.get('options', []),
self.COMMAND,
- args)
+ args,
+ kwargs.get('use_root_namespace', False))
class IpDeviceCommandBase(IpCommandBase):
COMMAND = 'netns'
def add(self, name):
- self._as_root('add', name)
+ self._as_root('add', name, use_root_namespace=True)
return IPWrapper(self._parent.root_helper, name)
def delete(self, name):
- if not self._parent.root_helper:
- raise exceptions.SudoRequired()
- else:
- return utils.execute(
- ['ip', 'netns', 'delete', name],
- root_helper=self._parent.root_helper)
+ self._as_root('delete', name, use_root_namespace=True)
def execute(self, cmds, addl_env={}):
if not self._parent.root_helper:
root_helper=self._parent.root_helper)
def exists(self, name):
- output = self._as_root('list', options='o')
+ output = self._as_root('list', options='o', use_root_namespace=True)
for line in output.split('\n'):
if name == line.strip():
def test_as_root(self):
self.ip_cmd._as_root('link')
- self.ip.assert_has_calls([mock.call._as_root([], 'foo', ('link', ))])
+ self.ip.assert_has_calls(
+ [mock.call._as_root([], 'foo', ('link', ), False)])
def test_as_root_with_options(self):
self.ip_cmd._as_root('link', options='o')
- self.ip.assert_has_calls([mock.call._as_root('o', 'foo', ('link', ))])
+ self.ip.assert_has_calls(
+ [mock.call._as_root('o', 'foo', ('link', ), False)])
class TestIPDeviceCommandBase(unittest.TestCase):
self.parent.assert_has_calls([
mock.call._run(options, self.command, args)])
- def _assert_sudo(self, options, args):
- self.parent.assert_has_calls([
- mock.call._as_root(options, self.command, args)])
+ def _assert_sudo(self, options, args, force_root_namespace=False):
+ self.parent.assert_has_calls(
+ [mock.call._as_root(options, self.command, args,
+ force_root_namespace)])
class TestIpLinkCommand(TestIPCmdBase):
def test_add_namespace(self):
ns = self.netns_cmd.add('ns')
- self._assert_sudo([], ('add', 'ns'))
+ self._assert_sudo([], ('add', 'ns'), force_root_namespace=True)
self.assertEqual(ns.namespace, 'ns')
def test_delete_namespace(self):
with mock.patch('quantum.agent.linux.utils.execute') as execute:
self.netns_cmd.delete('ns')
- execute.assert_called_once_with(['ip', 'netns', 'delete', 'ns'],
- root_helper='sudo')
+ self._assert_sudo([], ('delete', 'ns'), force_root_namespace=True)
def test_namespace_exists(self):
retval = '\n'.join(NETNS_SAMPLE)
self.parent._as_root.return_value = retval
self.assertTrue(
self.netns_cmd.exists('bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb'))
- self._assert_sudo('o', ('list',))
+ self._assert_sudo('o', ('list',), force_root_namespace=True)
def test_namespace_doest_not_exist(self):
retval = '\n'.join(NETNS_SAMPLE)
self.parent._as_root.return_value = retval
self.assertFalse(
self.netns_cmd.exists('bbbbbbbb-1111-2222-3333-bbbbbbbbbbbb'))
- self._assert_sudo('o', ('list',))
+ self._assert_sudo('o', ('list',), force_root_namespace=True)
def test_execute(self):
self.parent.namespace = 'ns'