Merge "Revert "Improve performance of ensure_namespace""

This commit is contained in:
Jenkins 2016-04-26 01:24:58 +00:00 committed by Gerrit Code Review
commit b3a41f4a31
3 changed files with 31 additions and 32 deletions

View File

@ -40,7 +40,6 @@ OPTS = [
LOOPBACK_DEVNAME = 'lo'
IP_NETNS_PATH = '/var/run/netns'
SYS_NET_PATH = '/sys/class/net'
DEFAULT_GW_PATTERN = re.compile(r"via (\S+)")
METRIC_PATTERN = re.compile(r"metric (\S+)")
@ -246,10 +245,7 @@ class IPWrapper(SubProcessBase):
@classmethod
def get_namespaces(cls):
if not cfg.CONF.AGENT.use_helper_for_ns_read:
return os.listdir(IP_NETNS_PATH)
output = cls._execute([], 'netns', ['list'], run_as_root=True)
output = cls._execute([], 'netns', ('list',))
return [l.split()[0] for l in output.splitlines()]
@ -879,11 +875,9 @@ class IpNetnsCommand(IpCommandBase):
log_fail_as_error=log_fail_as_error, **kwargs)
def exists(self, name):
if not cfg.CONF.AGENT.use_helper_for_ns_read:
return name in os.listdir(IP_NETNS_PATH)
output = self._parent._execute(
['o'], 'netns', ['list'], run_as_root=True)
['o'], 'netns', ['list'],
run_as_root=cfg.CONF.AGENT.use_helper_for_ns_read)
for line in [l.split()[0] for l in output.splitlines()]:
if name == line:
return True

View File

@ -86,12 +86,6 @@ class IpLibTestFramework(functional_base.BaseSudoTestCase):
class IpLibTestCase(IpLibTestFramework):
def test_namespace_exists(self):
namespace = self.useFixture(net_helpers.NamespaceFixture())
self.assertTrue(namespace.ip_wrapper.netns.exists(namespace.name))
namespace.destroy()
self.assertFalse(namespace.ip_wrapper.netns.exists(namespace.name))
def test_device_exists(self):
attr = self.generate_device_details()

View File

@ -15,7 +15,6 @@
import mock
import netaddr
from oslo_config import cfg
import testtools
from neutron.agent.common import utils # noqa
@ -284,37 +283,23 @@ class TestIpWrapper(base.BaseTestCase):
def test_get_namespaces(self):
self.execute.return_value = '\n'.join(NETNS_SAMPLE)
cfg.CONF.AGENT.use_helper_for_ns_read = True
retval = ip_lib.IPWrapper.get_namespaces()
self.assertEqual(retval,
['12345678-1234-5678-abcd-1234567890ab',
'bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb',
'cccccccc-cccc-cccc-cccc-cccccccccccc'])
self.execute.assert_called_once_with([], 'netns', ['list'],
run_as_root=True)
self.execute.assert_called_once_with([], 'netns', ('list',))
def test_get_namespaces_iproute2_4(self):
self.execute.return_value = '\n'.join(NETNS_SAMPLE_IPROUTE2_4)
cfg.CONF.AGENT.use_helper_for_ns_read = True
retval = ip_lib.IPWrapper.get_namespaces()
self.assertEqual(retval,
['12345678-1234-5678-abcd-1234567890ab',
'bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb',
'cccccccc-cccc-cccc-cccc-cccccccccccc'])
self.execute.assert_called_once_with([], 'netns', ['list'],
run_as_root=True)
@mock.patch('os.listdir', return_value=NETNS_SAMPLE)
def test_get_namespaces_listdir(self, mocked_listdir):
cfg.CONF.AGENT.use_helper_for_ns_read = False
retval = ip_lib.IPWrapper.get_namespaces()
self.assertEqual(retval,
['12345678-1234-5678-abcd-1234567890ab',
'bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb',
'cccccccc-cccc-cccc-cccc-cccccccccccc'])
mocked_listdir.assert_called_once_with(ip_lib.IP_NETNS_PATH)
self.execute.assert_called_once_with([], 'netns', ('list',))
def test_add_tuntap(self):
ip_lib.IPWrapper().add_tuntap('tap0')
@ -1184,6 +1169,32 @@ class TestIpNetnsCommand(TestIPCmdBase):
self.netns_cmd.delete('ns')
self._assert_sudo([], ('delete', 'ns'), use_root_namespace=True)
def test_namespace_exists_use_helper(self):
self.config(group='AGENT', use_helper_for_ns_read=True)
retval = '\n'.join(NETNS_SAMPLE)
# need another instance to avoid mocking
netns_cmd = ip_lib.IpNetnsCommand(ip_lib.SubProcessBase())
with mock.patch('neutron.agent.common.utils.execute') as execute:
execute.return_value = retval
self.assertTrue(
netns_cmd.exists('bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb'))
execute.assert_called_once_with(['ip', '-o', 'netns', 'list'],
run_as_root=True,
log_fail_as_error=True)
def test_namespace_doest_not_exist_no_helper(self):
self.config(group='AGENT', use_helper_for_ns_read=False)
retval = '\n'.join(NETNS_SAMPLE)
# need another instance to avoid mocking
netns_cmd = ip_lib.IpNetnsCommand(ip_lib.SubProcessBase())
with mock.patch('neutron.agent.common.utils.execute') as execute:
execute.return_value = retval
self.assertFalse(
netns_cmd.exists('bbbbbbbb-1111-2222-3333-bbbbbbbbbbbb'))
execute.assert_called_once_with(['ip', '-o', 'netns', 'list'],
run_as_root=False,
log_fail_as_error=True)
def test_execute(self):
self.parent.namespace = 'ns'
with mock.patch('neutron.agent.common.utils.execute') as execute: