Follow-up to fix some namespace tests

- FD in pyroute2 doesn't need to close any sessions [1] when use in
  neutron-fwaas.

- Indicate arguments for setns() method.

[1] https://github.com/svinota/pyroute2/blob/master/pyroute2/netns/__init__.py#L247

Change-Id: Iec1cfc0902a516f63af552c7f53c7099a33f253e
This commit is contained in:
Nguyen Van Trung 2018-07-09 08:22:59 +07:00 committed by Yushiro FURUKAWA
parent f6e0a06f69
commit 81d41e27b6
2 changed files with 12 additions and 20 deletions

View File

@ -44,18 +44,15 @@ def in_namespace(namespace):
return
org_netns_fd = os.open(PROCESS_NETNS, os.O_RDONLY)
pynetns.setns(namespace)
try:
pynetns.setns(namespace)
try:
yield
finally:
try:
# NOTE(cby): this code is not executed only if we fail to
# move in target namespace
pynetns.setns(org_netns_fd)
except Exception as e:
msg = _('Failed to move back in original netns: %s') % e
LOG.critical(msg)
raise BackInNamespaceExit(msg)
yield
finally:
os.close(org_netns_fd)
try:
# NOTE(cby): this code is not executed only if we fail to
# move in target namespace
pynetns.setns(org_netns_fd)
except Exception as e:
msg = _('Failed to move back in original netns: %s') % e
LOG.critical(msg)
raise BackInNamespaceExit(msg)

View File

@ -45,10 +45,8 @@ class InNamespaceTest(base.BaseTestCase):
with utils.in_namespace(self.NEW_NETNS):
self.setns_mock.assert_called_once_with(self.NEW_NETNS)
setns_calls = [mock.call(self.ORG_NETNS_FD)]
close_calls = [mock.call(self.ORG_NETNS_FD)]
setns_calls = [mock.call(self.NEW_NETNS), mock.call(self.ORG_NETNS_FD)]
self.setns_mock.assert_has_calls(setns_calls)
self.close_mock.assert_has_calls(close_calls)
def test_in_no_namespace(self):
for namespace in ('', None):
@ -63,10 +61,8 @@ class InNamespaceTest(base.BaseTestCase):
self.setns_mock.assert_called_once_with(self.NEW_NETNS)
raise ValueError
setns_calls = [mock.call(self.ORG_NETNS_FD)]
close_calls = [mock.call(self.ORG_NETNS_FD)]
setns_calls = [mock.call(self.NEW_NETNS), mock.call(self.ORG_NETNS_FD)]
self.setns_mock.assert_has_calls(setns_calls)
self.close_mock.assert_has_calls(close_calls)
def test_in_namespace_enter_failed(self):
self.setns_mock.side_effect = ValueError
@ -75,7 +71,6 @@ class InNamespaceTest(base.BaseTestCase):
self.fail('It should fail before we reach this code')
self.setns_mock.assert_called_once_with(self.NEW_NETNS)
self.close_mock.assert_called_once_with(self.ORG_NETNS_FD)
def test_in_namespace_exit_failed(self):
self.setns_mock.side_effect = [self.NEW_NETNS_FD, ValueError]