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): ...@@ -27,6 +27,7 @@ class Node(object):
self._pid = self._slave = None self._pid = self._slave = None
self._processes = weakref.WeakValueDictionary() self._processes = weakref.WeakValueDictionary()
self._interfaces = weakref.WeakValueDictionary() self._interfaces = weakref.WeakValueDictionary()
self._auto_interfaces = [] # just to keep them alive!
fd, pid = _start_child(debug, nonetns) fd, pid = _start_child(debug, nonetns)
self._pid = pid self._pid = pid
...@@ -36,6 +37,10 @@ class Node(object): ...@@ -36,6 +37,10 @@ class Node(object):
Node._nodes[Node._nextnode] = self Node._nodes[Node._nextnode] = self
Node._nextnode += 1 Node._nextnode += 1
# Bring loopback up
if not nonetns:
self.get_interface("lo").up = True
def __del__(self): def __del__(self):
if self._debug: # pragma: no cover if self._debug: # pragma: no cover
sys.stderr.write("*** Node(%s) __del__\n" % self.pid) sys.stderr.write("*** Node(%s) __del__\n" % self.pid)
...@@ -99,16 +104,18 @@ class Node(object): ...@@ -99,16 +104,18 @@ class Node(object):
del self._interfaces[iface.index] del self._interfaces[iface.index]
iface.destroy() iface.destroy()
def get_interface(self, name):
return [i for i in self.get_interfaces() if i.name == name][0]
def get_interfaces(self): def get_interfaces(self):
if not self._slave: if not self._slave:
return [] return []
ifaces = self._slave.get_if_data() ifaces = self._slave.get_if_data()
ret = []
for i in ifaces: for i in ifaces:
if i not in self._interfaces: if i not in self._interfaces:
ret.append(netns.interface.ForeignNodeInterface(self, i)) iface = netns.interface.ForeignNodeInterface( self, i)
else: self._auto_interfaces.append(iface) # keep it referenced!
ret.append(self._interfaces[i]) self._interfaces[i] = iface
# by the way, clean up _interfaces # by the way, clean up _interfaces
for i in list(self._interfaces): # copy before deleting! for i in list(self._interfaces): # copy before deleting!
if i not in ifaces: if i not in ifaces:
...@@ -117,7 +124,7 @@ class Node(object): ...@@ -117,7 +124,7 @@ class Node(object):
self._interfaces[i].destroy() self._interfaces[i].destroy()
del self._interfaces[i] 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, def route(self, tipe = 'unicast', prefix = None, prefix_len = 0,
nexthop = None, interface = None, metric = 0): nexthop = None, interface = None, metric = 0):
......
...@@ -17,6 +17,8 @@ class TestNode(unittest.TestCase): ...@@ -17,6 +17,8 @@ class TestNode(unittest.TestCase):
nodes = netns.get_nodes() nodes = netns.get_nodes()
self.assertEquals(nodes, [node]) self.assertEquals(nodes, [node])
self.assertTrue(node.get_interface("lo").up)
@test_util.skip("Not implemented") @test_util.skip("Not implemented")
def test_detect_fork(self): def test_detect_fork(self):
# Test that netns recognises a fork # 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