Merge "Fix namespace exist() method"

This commit is contained in:
Jenkins
2014-03-29 20:05:52 +00:00
committed by Gerrit Code Review
2 changed files with 17 additions and 9 deletions

View File

@@ -466,7 +466,7 @@ class IpNetnsCommand(IpCommandBase):
check_exit_code=check_exit_code) check_exit_code=check_exit_code)
def exists(self, name): def exists(self, name):
output = self._run('list', options='o') output = self._parent._execute('o', 'netns', ['list'])
for line in output.split('\n'): for line in output.split('\n'):
if name == line.strip(): if name == line.strip():

View File

@@ -713,17 +713,25 @@ class TestIpNetnsCommand(TestIPCmdBase):
def test_namespace_exists(self): def test_namespace_exists(self):
retval = '\n'.join(NETNS_SAMPLE) retval = '\n'.join(NETNS_SAMPLE)
self.parent._run.return_value = retval # need another instance to avoid mocking
self.assertTrue( netns_cmd = ip_lib.IpNetnsCommand(ip_lib.SubProcessBase())
self.netns_cmd.exists('bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb')) with mock.patch('neutron.agent.linux.utils.execute') as execute:
self._assert_call('o', ('list',)) execute.return_value = retval
self.assertTrue(
netns_cmd.exists('bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb'))
execute.assert_called_once_with(['ip', '-o', 'netns', 'list'],
root_helper=None)
def test_namespace_doest_not_exist(self): def test_namespace_doest_not_exist(self):
retval = '\n'.join(NETNS_SAMPLE) retval = '\n'.join(NETNS_SAMPLE)
self.parent._run.return_value = retval # need another instance to avoid mocking
self.assertFalse( netns_cmd = ip_lib.IpNetnsCommand(ip_lib.SubProcessBase())
self.netns_cmd.exists('bbbbbbbb-1111-2222-3333-bbbbbbbbbbbb')) with mock.patch('neutron.agent.linux.utils.execute') as execute:
self._assert_call('o', ('list',)) execute.return_value = retval
self.assertFalse(
netns_cmd.exists('bbbbbbbb-1111-2222-3333-bbbbbbbbbbbb'))
execute.assert_called_once_with(['ip', '-o', 'netns', 'list'],
root_helper=None)
def test_execute(self): def test_execute(self):
self.parent.namespace = 'ns' self.parent.namespace = 'ns'