Skip to content

Commit

Permalink
ipdb: support net_ns_pid
Browse files Browse the repository at this point in the history
Bug-Url: #306
  • Loading branch information
Peter V. Saveliev committed Oct 27, 2016
1 parent c0dfd84 commit 5d4d5b9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
6 changes: 4 additions & 2 deletions pyroute2/ipdb/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ class Interface(Transactional):
'vlans',
'ipaddr',
'ports',
'net_ns_fd']
'net_ns_fd',
'net_ns_pid']
_fields = [ifinfmsg.nla2name(i[0]) for i in ifinfmsg.nla_map]
for name in ('bridge_data', ):
data = getattr(ifinfmsg.ifinfo, name)
Expand Down Expand Up @@ -705,6 +706,7 @@ def error(x):
request = IPLinkRequest()
for key in added:
if (key == 'net_ns_fd') or \
(key == 'net_ns_pid') or \
(key not in self._virtual_fields) and \
(key != 'kind'):
request[key] = added[key]
Expand All @@ -725,7 +727,7 @@ def error(x):
run(nl.link, 'set', **request)
# hardcoded pause -- if the interface was moved
# across network namespaces
if 'net_ns_fd' in request:
if ('net_ns_fd' in request) or ('net_ns_pid' in request):
while True:
# wait until the interface will disappear
# from the main network namespace
Expand Down
32 changes: 31 additions & 1 deletion tests/general/test_netns.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,37 @@ def test_create_peer_attrs(self):
netnsmod.remove(foo)
netnsmod.remove(bar)

def test_move_interface(self):
def test_move_ns_pid(self):
foo = str(uuid4())
bar = str(uuid4())
ifA = uifname()
netnsmod.create(foo)
netnsmod.create(bar)

ns_foo = IPDB(nl=NetNS(foo))
ns_bar = IPDB(nl=NetNS(bar))

try:
ns_foo.create(ifname=ifA, kind='dummy').commit()
with ns_foo.interfaces[ifA] as iface:
iface.net_ns_pid = ns_bar.nl.server.pid

assert ifA in ns_bar.interfaces.keys()
assert ifA not in ns_foo.interfaces.keys()

with ns_bar.interfaces[ifA] as iface:
iface.net_ns_pid = ns_foo.nl.server.pid

assert ifA not in ns_bar.interfaces.keys()
assert ifA in ns_foo.interfaces.keys()

finally:
ns_foo.release()
ns_bar.release()
netnsmod.remove(foo)
netnsmod.remove(bar)

def test_move_ns_fd(self):
foo = str(uuid4())
bar = str(uuid4())
ifA = uifname()
Expand Down

0 comments on commit 5d4d5b9

Please sign in to comment.