Commit 6415368f authored by Martín Ferrari's avatar Martín Ferrari

Node now brings loopback up automatically. Also, new get_interface method. Fix...

Node now brings loopback up automatically. Also, new get_interface method. Fix problem with foreign interfaces (keep reference)
parent 989ded90
......@@ -27,6 +27,7 @@ class Node(object):
self._pid = self._slave = None
self._processes = weakref.WeakValueDictionary()
self._interfaces = weakref.WeakValueDictionary()
self._auto_interfaces = [] # just to keep them alive!
fd, pid = _start_child(debug, nonetns)
self._pid = pid
......@@ -36,6 +37,10 @@ class Node(object):
Node._nodes[Node._nextnode] = self
Node._nextnode += 1
# Bring loopback up
if not nonetns:
self.get_interface("lo").up = True
def __del__(self):
if self._debug: # pragma: no cover
sys.stderr.write("*** Node(%s) __del__\n" % self.pid)
......@@ -99,16 +104,18 @@ class Node(object):
del self._interfaces[iface.index]
iface.destroy()
def get_interface(self, name):
return [i for i in self.get_interfaces() if i.name == name][0]
def get_interfaces(self):
if not self._slave:
return []
ifaces = self._slave.get_if_data()
ret = []
for i in ifaces:
if i not in self._interfaces:
ret.append(netns.interface.ForeignNodeInterface(self, i))
else:
ret.append(self._interfaces[i])
iface = netns.interface.ForeignNodeInterface( self, i)
self._auto_interfaces.append(iface) # keep it referenced!
self._interfaces[i] = iface
# by the way, clean up _interfaces
for i in list(self._interfaces): # copy before deleting!
if i not in ifaces:
......@@ -117,7 +124,7 @@ class Node(object):
self._interfaces[i].destroy()
del self._interfaces[i]
return sorted(ret, key = lambda x: x.index)
return sorted(self._interfaces.values(), key = lambda x: x.index)
def route(self, tipe = 'unicast', prefix = None, prefix_len = 0,
nexthop = None, interface = None, metric = 0):
......
......@@ -17,6 +17,8 @@ class TestNode(unittest.TestCase):
nodes = netns.get_nodes()
self.assertEquals(nodes, [node])
self.assertTrue(node.get_interface("lo").up)
@test_util.skip("Not implemented")
def test_detect_fork(self):
# Test that netns recognises a fork
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment