def setUp(self): rem_value = 1 self.null_rem = NullRem(rem_value) self.dead_rem = DeadRem(rem_value) self.rtt = Rtt(rem_value) self.bw = Bw(rem_value, 1, 1) self.avg = Avg([Rtt(1), Rtt(5), Rtt(10)])
def testCompareRtt(self): ''' Compare 2 Rtt ''' # self.rtt is better than Rtt(5) self.failUnless(self.rtt > Rtt(5)) # Rtt(5) is worse than self.rtt self.failUnless(Rtt(5) < self.rtt) # self.rtt and Rtt(1) are the same self.failUnless(Rtt(1) == self.rtt)
def testChangeRouteRem(self): ''' Change route rem ''' self.route_node.route_add(lvl=0, dst=123, gw=5, rem=Rtt(50)) res = self.route_node.route_rem(1, Rtt(10)) # Route don't exist self.failUnlessEqual(res, (0, None)) res = self.route_node.route_rem(5, Rtt(10)) self.failUnlessEqual(res, (1, Rtt(50)))
def testChangeRouteRem(self): ''' MapRoute: change route rem ''' # Changing rem for a non existent route res = self.map.route_rem(lvl=0, dst=200, gw=5, newrem=Rtt(10)) self.failUnlessEqual(res, 0) self.map.route_add(lvl=0, dst=200, gw=5, rem=Rtt(10)) res = self.map.route_rem(lvl=0, dst=200, gw=5, newrem=Rtt(5)) self.failUnlessEqual(res, 1)
def testReset(self): ''' Delete all routes ''' self.route_node.route_add(lvl=0, dst=123, gw=5, rem=Rtt(50)) self.failUnlessEqual(self.route_node.is_empty(), False) self.route_node.route_reset() self.failUnlessEqual(self.route_node.is_empty(), True)
def __init__(self, bestdev, devs, idn=None, ip=None, netid=None, ntkd=None): """ ip: neighbour's ip; nip: neighbour's nip; ntkd: neighbour's ntk remote instance idn: neighbour's id; use Neighbour.ip_to_id to create it devs: a dict which maps a device to the average rtt bestdev: a pair (d, avg_rtt), where devs[d] is the best element of devs. netid: network id of the node """ self.devs = devs self.bestdev = bestdev self.ip = ip self.nip = None self.id = idn if self.bestdev: # TODO(low): support the other metrics self.rem = Rtt(self.bestdev[1]) else: self.rem = DeadRem() # The neighbour is dead self.ntkd = ntkd self.netid = netid
def testRouteAdd(self): ''' Add a route ''' self.failUnlessEqual(self.route_node.routes, []) res = self.route_node.route_add(lvl=0, dst=1234, gw=1, rem=Rtt(20)) self.failUnlessEqual(res, (1, None)) self.failUnless(isinstance(self.route_node.routes[0], RouteGw)) self.failUnlessEqual(self.route_node.routes[0].gw, 1) # substitute an old route res = self.route_node.route_add(lvl=0, dst=1234, gw=1, rem=Rtt(10)) self.failUnlessEqual(res, (2, Rtt(20))) # Add new route res = self.route_node.route_add(lvl=0, dst=1234, gw=2, rem=Rtt(5)) self.failUnlessEqual(res, (1, None)) self.failUnlessEqual(self.route_node.nroutes(), 2)
def store(self, ip_table): """Substitute the old ip_table with the new and notify about the changes ip_table: the new ip_table; """ # the rows deleted during truncation died_ip_list = [] ip_table, died_ip_list = self._truncate(ip_table) # first of all we cycle through the old ip_table # looking for nodes that aren't in the new one for key in self.ip_table: # if we find a row that isn't in the new ip_table and whose # deletion hasn't already been notified (raising an event) # during truncation if not key in ip_table and not key in died_ip_list: self.delete(key, remove_from_iptable=False) # now we cycle through the new ip_table # looking for nodes who weren't in the old one # or whose rtt has sensibly changed for key in ip_table: # if a node has been added if not key in self.ip_table: # generate an id and add the entry in translation_table self.ip_to_id(key) # create a TCP connection to the neighbour self.ntk_client[key] = rpc.TCPClient(ip_to_str(key)) # send a message notifying we added a node self.events.send('NEIGH_NEW', (Neigh(bestdev=ip_table[key].bestdev, devs=ip_table[key].devs, idn=self.translation_table[key], ip=key, netid=self.netid_table[key], ntkd=self.ntk_client[key]), )) else: # otherwise (if the node already was in old ip_table) check if # its rtt has changed more than rtt_variation new_rtt = ip_table[key].bestdev[1] old_rtt = self.ip_table[key].bestdev[1] rtt_variation = abs(new_rtt - old_rtt) / float(old_rtt) if rtt_variation > self.rtt_variation_threshold: # send a message notifying the node's rtt changed self.events.send( 'NEIGH_REM_CHGED', (Neigh(bestdev=self.ip_table[key].bestdev, devs=self.ip_table[key].devs, idn=self.translation_table[key], ip=key, netid=self.netid_table[key], ntkd=self.ntk_client[key]), Rtt(new_rtt))) # finally, update the ip_table self.ip_table = ip_table
def testRouteAdd(self): ''' MapRoute: add a route ''' # MapRoute is empty so we have 256 free node! self.failUnlessEqual(self.map.free_nodes_nb(lvl=0), 256) # Add a node res = self.map.route_add(lvl=0, dst=200, gw=5, rem=Rtt(1)) self.failUnlessEqual(res, 1) self.failUnlessEqual(self.map.free_nodes_nb(lvl=0), 255)
def testCompareAvg(self): ''' Compare 2 Avg ''' avg2 = Avg([Rtt(30), Rtt(25), Rtt(15)]) # avg2.value == 59976 self.failUnless(avg2 < self.avg) self.failUnless(self.avg > avg2) self.failUnless(self.avg == Avg([Rtt(1), Rtt(5), Rtt(10)]))
def testNeighGet(self): '''Getting the neighbour reach the hash node''' self.failUnlessEqual(self.p2p.neigh_get([2, 19, 82, 84]), None) n = [2, 19, 82, 84] self.p2p.participant_add(n) IP = 84215045 NETID = 123456 NEIGH = Neigh(bestdev=('eth0', 1), devs={'eth0': 1}, ip=IP, netid=NETID, idn=5) self.p2p.neigh.netid_table[IP] = NETID self.p2p.neigh.translation_table[IP] = 5 self.p2p.neigh.store({IP: NEIGH}) for lvl, id, in enumerate(n): self.p2p.maproute.route_add(lvl, id, 5, Rtt(1)) self.failUnlessEqual(self.p2p.neigh_get(n).values(), NEIGH.values())
def testDeleteRoute(self): '''Delete a route''' res = self.route_node.route_del(5) self.failUnlessEqual(res, 0) self.route_node.route_add(lvl=0, dst=123, gw=3, rem=Rtt(50)) self.route_node.route_add(lvl=0, dst=123, gw=4, rem=Rtt(50)) self.route_node.route_add(lvl=0, dst=123, gw=5, rem=Rtt(50)) res = self.route_node.route_del(5) self.failUnlessEqual(res, 1) self.failUnlessEqual( self.route_node.routes, [RouteGw(3, Rtt(50)), RouteGw(4, Rtt(50))]) res = self.route_node.route_del(3) self.failUnlessEqual(self.route_node.routes, [RouteGw(4, Rtt(50))]) res = self.route_node.route_del(4) self.failUnless(self.route_node.is_empty())
def testRouteGetbyGw(self): ''' Getting a route having as gateway "gw" ''' self.failUnlessEqual(self.route_node.route_getby_gw(5), None) self.route_node.route_add(lvl=0, dst=123, gw=5, rem=Rtt(1)) gw5 = self.route_node.route_getby_gw(5) self.failUnlessEqual(gw5.gw, 5)
def testAvgInitError(self): ''' Check Avg initialization error ''' self.assertRaises(RemError, Avg, [Rtt(1), Rtt(5), 'Not a Rem instance'])
def setUp(self): self.route_gw = RouteGw(object(), Rtt(1))
def testDeleteRoute(self): ''' MapRoute: delete a route ''' self.map.route_add(lvl=0, dst=200, gw=5, rem=Rtt(1)) self.map.route_del(lvl=0, dst=200, gw=5) self.failUnlessEqual(self.map.free_nodes_nb(lvl=0), 256)
def testCompareRouteGw(self): ''' Compare RouteGw ''' self.failUnless(self.route_gw > RouteGw(object(), Rtt(10))) self.assertRaises(RouteGwError, lambda: self.route_gw == None)
def testRemModify(self): ''' Modify a rem''' self.failUnless(self.route_gw.rem_modify(Rtt(1)) == Rtt(1)) self.failUnless(self.route_gw.rem_modify(Rtt(10)) == Rtt(1)) self.failUnless(self.route_gw.rem == Rtt(10))
def testIsEmpty(self): '''Check if a RouteNode instance is empty ''' self.failUnlessEqual(self.route_node.is_empty(), True) self.route_node.route_add(lvl=0, dst=123, gw=5, rem=Rtt(50)) self.failUnlessEqual(self.route_node.is_empty(), False)