def testActionASPathPrepend(self): """Testing ASPath Prepending""" path = SimBGP.CPath() self.routemap1 = SimBGP.CRouteMap("RM1", "permit", 10) self.routemap1.action.append("as-path prepend 10 10 10".split()) self.routemap1.performAction(path) self.assertEqual(len(path.aspath), 3)
def testAddPathsBasic(self): """Checking propagation of all paths in iBGP and one paths only in eBGP""" SimBGP.runConfigFile("../../config/add_paths.cfg") if SimBGP._router_list["1.1"].loc_rib.has_key("2.0/8"): self.assertEqual(len(SimBGP._router_list["1.1"].loc_rib["2.0/8"]), 3) if SimBGP._router_list["1.2"].loc_rib.has_key("2.0/8"): self.assertEqual(len(SimBGP._router_list["1.2"].loc_rib["2.0/8"]), 3) if SimBGP._router_list["1.3"].loc_rib.has_key("2.0/8"): self.assertEqual(len(SimBGP._router_list["1.3"].loc_rib["2.0/8"]), 4) if SimBGP._router_list["1.4"].loc_rib.has_key("2.0/8"): self.assertEqual(len(SimBGP._router_list["1.4"].loc_rib["2.0/8"]), 4) #These routers have one local path and receives zero or one path from AS1 #but does not consider the latter because the local one has priority if SimBGP._router_list["2.1"].loc_rib.has_key("2.0/8"): self.assertEqual(len(SimBGP._router_list["2.1"].loc_rib["2.0/8"]), 1) if SimBGP._router_list["2.1"].peers["1.1"].rib_in.has_key("2.0/8"): self.assertEqual( len(SimBGP._router_list["2.1"].peers["1.1"].rib_in["2.0/8"]), 1) if SimBGP._router_list["3.2"].loc_rib.has_key("2.0/8"): self.assertEqual(len(SimBGP._router_list["3.2"].loc_rib["2.0/8"]), 1) if SimBGP._router_list["3.2"].peers["1.2"].rib_in.has_key("2.0/8"): self.assertEqual( len(SimBGP._router_list["3.2"].peers["1.2"].rib_in["2.0/8"]), 0)
def testActionLocPref(self): """Testing locpref tagging""" path = SimBGP.CPath() self.routemap1 = SimBGP.CRouteMap("RM1", "permit", 10) self.routemap1.action.append("local-preference 80".split()) self.routemap1.performAction(path) self.assertEqual(path.local_pref, 80)
def testActionMetric(self): """Testing MED tagging""" path = SimBGP.CPath() self.routemap1 = SimBGP.CRouteMap("RM1", "permit", 10) self.routemap1.action.append("metric 45".split()) self.routemap1.performAction(path) self.assertEqual(path.med, 45)
def testLinkDelay(self): link = SimBGP.CIGPLink("1", "2", 300000, SimBGP.interpretBandwidth("100M")) self.assertEqual(link.link_delay(100000000), 2000000) #Test default value link = SimBGP.CIGPLink("1", "2") self.assertEqual(link.link_delay(100000000), 1000000 * (5.0 / 3000.0 + 1.0))
def testMatchMetric(self): """Testing filtering based on MED match""" path = SimBGP.CPath() path.med = 10 self.routemap1 = SimBGP.CRouteMap("RM1", "permit", 10) self.routemap1.match.append("metric 10".split()) self.assertTrue(self.routemap1.isMatch("2.0/8", path)) self.routemap1 = SimBGP.CRouteMap("RM1", "permit", 10) self.routemap1.match.append("metric 5".split()) self.assertFalse(self.routemap1.isMatch("2.0/8", path))
def testMatchPrefix(self): """Testing filtering based on prefix match""" #Testing prefix match self.routemap1 = SimBGP.CRouteMap("RM1", "permit", 10) self.routemap1.match.append("ip address 2.0/8".split()) self.assertTrue(self.routemap1.isMatch("2.0/8", None)) #Testing prefix not matching self.routemap1 = SimBGP.CRouteMap("RM1", "permit", 10) self.routemap1.match.append("ip address 3.0/8".split()) self.assertFalse(self.routemap1.isMatch("2.0/8", None))
def testMatchASPath(self): """Testing filtering based on ASPath""" #Testing for regular expressions matching self.routemap1 = SimBGP.CRouteMap("RM1", "permit", 10) self.routemap1.match.append("as-path ^0".split()) path = SimBGP.CPath() path.aspath = [0, 1] self.assertTrue(self.routemap1.isMatch(2.0 / 8, path)) #Testing for regular expressions not matching self.routemap1 = SimBGP.CRouteMap("RM1", "permit", 10) self.routemap1.match.append("as-path ^1".split()) path = SimBGP.CPath() path.aspath = [0, 1] self.assertFalse(self.routemap1.isMatch(2.0 / 8, path))
def testActionCommunity(self): """Testing community tagging""" path = SimBGP.CPath() path.community = ['1', '2', '3'] self.routemap1 = SimBGP.CRouteMap("RM1", "permit", 10) self.routemap1.action.append("community none".split()) self.routemap1.performAction(path) self.assertEqual(path.community, []) path.community = ['1', '2', '3'] self.routemap1 = SimBGP.CRouteMap("RM1", "permit", 10) self.routemap1.action.append("community 1:4:2".split()) self.routemap1.performAction(path) self.assertEqual(path.community, ['1', '2', '4']) self.routemap1 = SimBGP.CRouteMap("RM1", "permit", 10) self.routemap1.action.append("community 5:2 additive".split()) self.routemap1.performAction(path) self.assertEqual(path.community, ['1', '2', '2', '4', '5'])
def setUp(self): SimBGP.init() SimBGP._router_list["A"]=SimBGP.CRouter(1,"A") SimBGP._router_list["B"]=SimBGP.CRouter(1,"B") SimBGP._router_list["C"]=SimBGP.CRouter(2,"C") self.link1=SimBGP.CLink("A","B") self.link2=SimBGP.CLink("B","C")
def testMatchCommunities(self): """Testing filtering based on communities""" self.routemap1 = SimBGP.CRouteMap("RM1", "permit", 10) self.routemap1.match.append("community-list 1:2:3 exact".split()) self.routemap2 = SimBGP.CRouteMap("RM2", "permit", 10) self.routemap2.match.append("community-list 1 exact".split()) self.routemap3 = SimBGP.CRouteMap("RM3", "permit", 10) self.routemap3.match.append("community-list 1".split()) self.routemap4 = SimBGP.CRouteMap("RM4", "permit", 10) self.routemap4.match.append("community-list 4".split()) path = SimBGP.CPath() path.community.append("1") path.community.append("3") path.community.append("2") path.community.sort() #Exact community list - Match self.assertTrue(self.routemap1.isMatch(2.0 / 8, path)) #Exact community list - Don't match self.assertFalse(self.routemap2.isMatch(2.0 / 8, path)) #Contains community - Match self.assertTrue(self.routemap3.isMatch(2.0 / 8, path)) #Contains community - Doesn't match self.assertFalse(self.routemap4.isMatch(2.0 / 8, path))
def testAddPathsWithdraw(self): """Checking path withdrawal""" #SimBGP.SHOW_RECEIVE_EVENTS=True #SimBGP.SHOW_FINAL_RIBS=True SimBGP.runConfigFile("../../config/add_paths.cfg") #Activate msg logging SimBGP.SHOW_RECEIVE_EVENTS = True #Save stdout old_stdout = sys.stdout #redirect stdout to tmp file tmpfile = tempfile.TemporaryFile() sys.stdout = tmpfile # -- Remove one of the two pahts (best locpref) SimBGP._router_list["2.1"].withdraw_prefix("2.0/8") SimBGP.run() sys.stdout.flush() #Restore stdout sys.stdout = old_stdout tmpfile.seek(0) #Check that all routers have only one path for id, router in SimBGP._router_list.iteritems(): for p in router.loc_rib["2.0/8"]: self.assertNotEqual(p.nexthop, "2.1") #Check msg log : Five W must be seen # One from 2.1 to 1.1, two from 1.1 to RRs, and two from RRs to # 1.5 (because they are using its path now) # Other messages must be BGP update num_withdraws = 0 for line in tmpfile: #print line.strip() if 'W' in line: num_withdraws += 1 tmpfile.close() self.assertEqual(num_withdraws, 5) # -- Remove last path, check that adjribins are empty #Save stdout old_stdout = sys.stdout #redirect stdout to tmp file tmpfile = tempfile.TemporaryFile() sys.stdout = tmpfile SimBGP._router_list["3.2"].withdraw_prefix("2.0/8") SimBGP.run() #Restore stdout sys.stdout = old_stdout tmpfile.seek(0) for id, router in SimBGP._router_list.iteritems(): self.assertEqual(len(router.loc_rib["2.0/8"]), 0) num_withdraws = 0 for line in tmpfile: #print line.strip() if 'W' in line: num_withdraws += 1 tmpfile.close() self.assertEqual(num_withdraws, 11)
def setUp(self): SimBGP.init()
def testAddNPathsLoop(self): SimBGP.init() SimBGP.readConfigFile("../../config/add_n_paths_loop.cfg") SimBGP.computeIGP() #Test with two paths SimBGP._event_Scheduler.add( SimBGP.CEvent(SimBGP.toSystemTime(10.0), ["2.1", "1/8"], SimBGP.EVENT_ANNOUNCE_PREFIX)) SimBGP._event_Scheduler.add( SimBGP.CEvent(SimBGP.toSystemTime(10.0), ["3.1", "1/8"], SimBGP.EVENT_ANNOUNCE_PREFIX)) SimBGP.run() #Loop? self.assertFalse(SimBGP._systime < SimBGP.toSystemTime(2000))
def testSelectionModes(self): SimBGP.init() SimBGP.readConfigFile("../../config/add-paths-mode-test.cfg") SimBGP.computeIGP() SimBGP.run() router = SimBGP._router_list["1.1"] path1 = SimBGP.CPath() path2 = SimBGP.CPath() path3 = SimBGP.CPath() path4 = SimBGP.CPath() path5 = SimBGP.CPath() path1.local_pref = 100 path2.local_pref = 100 path3.local_pref = 100 path4.local_pref = 80 path5.local_pref = 80 path4.src_pid = "1.2" path1.aspath = ["1", "2"] path2.aspath = ["1", "3"] path3.aspath = ["2", "1", "3"] path4.aspath = ["1"] path5.aspath = ["1"] path1.igp_cost = 10 path2.igp_cost = 20 path3.igp_cost = 30 path4.igp_cost = 40 path4.igp_cost = 50 path5.igp_cost = 60 router.loc_rib["1.0/8"] = [path1, path2, path3, path4, path5] #BGP AddNBests router.bgp_mode = SimBGP.BGP_MODE_ADD_N_BESTS router.bgp_num_paths = 2 result = router.compute_paths_for_peer("1.2", "1.0/8") self.assertEqual(len(result), 2) router.bgp_num_paths = 4 result = router.compute_paths_for_peer("1.2", "1.0/8") self.assertEqual(len(result), 3) #BGP AddNPaths router.bgp_mode = SimBGP.BGP_MODE_ADD_N_PATHS router.bgp_num_paths = 2 result = router.compute_paths_for_peer("1.2", "1.0/8") self.assertEqual(len(result), 2) router.bgp_num_paths = 4 result = router.compute_paths_for_peer("1.2", "1.0/8") self.assertEqual(len(result), 4) #BGP AddAllPaths router.bgp_mode = SimBGP.BGP_MODE_ADD_ALL_PATHS path4.src_pid = "1.3" result = router.compute_paths_for_peer("1.2", "1.0/8") self.assertEqual(len(result), 5) #BGP Best Bin router.bgp_mode = SimBGP.BGP_MODE_BEST_BIN result = router.compute_paths_for_peer("1.2", "1.0/8") self.assertEqual(len(result), 3) for path in result: self.assertEqual(path.local_pref, 100) #Add better path => second bin needed path1.local_pref = 120 result = router.compute_paths_for_peer("1.2", "1.0/8") self.assertEqual(len(result), 3) for path in result: self.assertTrue(path.local_pref >= 100) path1.local_pref = 100 #BGP AddAllBest router.bgp_mode = SimBGP.BGP_MODE_ADD_BEST_PATHS result = router.compute_paths_for_peer("1.2", "1.0/8") self.assertEqual(len(result), 2) #BGP AddGroupBest router.bgp_mode = SimBGP.BGP_MODE_GROUP_BEST result = router.compute_paths_for_peer("1.2", "1.0/8") self.assertEqual(len(result), 2) ases = [] for path in result: self.assertTrue(path.aspath[0] not in ases) ases.append(path.aspath[0]) #Decisive step router.bgp_mode = SimBGP.BGP_MODE_DECISIVE_STEP result = router.compute_paths_for_peer("1.2", "1.0/8") self.assertEqual(len(result), 2) #second test : med is decisive step, and 3 first paths have ASPath len of 2 path3.aspath = ["1", "2"] path1.med = 10 path2.med = 20 path3.med = 20 result = router.compute_paths_for_peer("1.2", "1.0/8") self.assertEqual(len(result), 3)
def testAddBestPaths(self): #Discard AddPaths config SimBGP.init() SimBGP.readConfigFile("../../config/add_paths2.cfg") SimBGP.computeIGP() #Test with two paths : One with LP100, one with LP80 SimBGP._event_Scheduler.add( SimBGP.CEvent(SimBGP.toSystemTime(10.0), ["2.1", "1/8"], SimBGP.EVENT_ANNOUNCE_PREFIX)) SimBGP._event_Scheduler.add( SimBGP.CEvent(SimBGP.toSystemTime(10.0), ["3.1", "1/8"], SimBGP.EVENT_ANNOUNCE_PREFIX)) SimBGP.run() #Two paths available : one best, plus one from second bin #Check that all routers have two paths with different nexthop for id, router in SimBGP._router_list.iteritems(): if id[0:2] == "1.": self.assertNotEqual(router.loc_rib["1/8"][0].nexthop, router.loc_rib["1/8"][1].nexthop) #Add one more path with LP 100 : All routers have both LP100, plus possibly LP80 for the announcers SimBGP._event_Scheduler.add( SimBGP.CEvent(SimBGP.toSystemTime(11.0), ["4.1", "1/8"], SimBGP.EVENT_ANNOUNCE_PREFIX)) SimBGP.run() for id, router in SimBGP._router_list.iteritems(): if id[0:2] == "1.": self.assertNotEqual(router.loc_rib["1/8"][0].nexthop, router.loc_rib["1/8"][1].nexthop) #print router, router.loc_rib["1/8"][0].nexthop, router.loc_rib["1/8"][1].nexthop if id == "1.1": self.assertEqual(len(router.loc_rib["1/8"]), 3) else: self.assertEqual(len(router.loc_rib["1/8"]), 2) #Add one more path again : 2 LP100 and 2 LP80. Only 1.1 and 1.5 knows about LP80 SimBGP._event_Scheduler.add( SimBGP.CEvent(SimBGP.toSystemTime(12.0), ["5.1", "1/8"], SimBGP.EVENT_ANNOUNCE_PREFIX)) SimBGP.run() for id, router in SimBGP._router_list.iteritems(): if id[0:2] == "1.": self.assertNotEqual(router.loc_rib["1/8"][0].nexthop, router.loc_rib["1/8"][1].nexthop) if id == "1.1" or id == "1.5": self.assertEqual(len(router.loc_rib["1/8"]), 3) else: self.assertEqual(len(router.loc_rib["1/8"]), 2) #Same, but use ASPath prepending instead of local pref SimBGP.init() SimBGP.readConfigFile("../../config/add_paths3.cfg") SimBGP.computeIGP() #Test with two paths : One with ASPL1, one with ASPL4 SimBGP._event_Scheduler.add( SimBGP.CEvent(SimBGP.toSystemTime(10.0), ["2.1", "1/8"], SimBGP.EVENT_ANNOUNCE_PREFIX)) SimBGP._event_Scheduler.add( SimBGP.CEvent(SimBGP.toSystemTime(10.0), ["3.1", "1/8"], SimBGP.EVENT_ANNOUNCE_PREFIX)) SimBGP.run() #Two paths available : one best, plus one from second bin #Check that all routers have only best, except for 1.1 for id, router in SimBGP._router_list.iteritems(): if id[0:2] == "1.": if id == "1.1": self.assertEqual(len(router.loc_rib["1/8"]), 2) else: self.assertEqual(len(router.loc_rib["1/8"]), 1) #Add one more path with ASPathLen 1 : All routers have both ASPL1, plus possibly ASPL4 for the announcers SimBGP._event_Scheduler.add( SimBGP.CEvent(SimBGP.toSystemTime(11.0), ["4.1", "1/8"], SimBGP.EVENT_ANNOUNCE_PREFIX)) SimBGP.run() for id, router in SimBGP._router_list.iteritems(): if id[0:2] == "1.": self.assertNotEqual(router.loc_rib["1/8"][0].nexthop, router.loc_rib["1/8"][1].nexthop) #print router, router.loc_rib["1/8"][0].nexthop, router.loc_rib["1/8"][1].nexthop if id == "1.1": self.assertEqual(len(router.loc_rib["1/8"]), 3) else: self.assertEqual(len(router.loc_rib["1/8"]), 2) #Add one more path again : 2 ASPL 1 and 2 ASPL 4. Only 1.1 and 1.5 knows about ASPL4 SimBGP._event_Scheduler.add( SimBGP.CEvent(SimBGP.toSystemTime(12.0), ["5.1", "1/8"], SimBGP.EVENT_ANNOUNCE_PREFIX)) SimBGP.run() for id, router in SimBGP._router_list.iteritems(): if id[0:2] == "1.": self.assertNotEqual(router.loc_rib["1/8"][0].nexthop, router.loc_rib["1/8"][1].nexthop) if id == "1.1" or id == "1.5": self.assertEqual(len(router.loc_rib["1/8"]), 3) else: self.assertEqual(len(router.loc_rib["1/8"]), 2)
def tearDown(self): SimBGP.SHOW_RECEIVE_EVENTS = False SimBGP.SHOW_FINAL_RIBS = False SimBGP.init()
def testAddPathsBestBin(self): #Discard AddPaths config SimBGP.init() SimBGP.readConfigFile("../../config/add_paths2.cfg") SimBGP.computeIGP() #Test with two paths : One with LP100, one with LP80 SimBGP._event_Scheduler.add( SimBGP.CEvent(SimBGP.toSystemTime(10.0), ["2.1", "1/8"], SimBGP.EVENT_ANNOUNCE_PREFIX)) SimBGP._event_Scheduler.add( SimBGP.CEvent(SimBGP.toSystemTime(10.0), ["3.1", "1/8"], SimBGP.EVENT_ANNOUNCE_PREFIX)) SimBGP.run() #Two paths available : one best, plus one from second bin #Check that all routers have two paths with different nexthop for id, router in SimBGP._router_list.iteritems(): if id[0:2] == "1.": self.assertNotEqual(router.loc_rib["1/8"][0].nexthop, router.loc_rib["1/8"][1].nexthop) #Add one more path with LP 100 : All routers have both LP100, plus possibly LP80 for the announcers SimBGP._event_Scheduler.add( SimBGP.CEvent(SimBGP.toSystemTime(11.0), ["4.1", "1/8"], SimBGP.EVENT_ANNOUNCE_PREFIX)) SimBGP.run() for id, router in SimBGP._router_list.iteritems(): if id[0:2] == "1.": self.assertNotEqual(router.loc_rib["1/8"][0].nexthop, router.loc_rib["1/8"][1].nexthop) #print router, router.loc_rib["1/8"][0].nexthop, router.loc_rib["1/8"][1].nexthop if id == "1.1": self.assertEqual(len(router.loc_rib["1/8"]), 3) else: self.assertEqual(len(router.loc_rib["1/8"]), 2) #Add one path with LP80, remove one with LP100 => 2LP80, 1LP100 SimBGP._event_Scheduler.add( SimBGP.CEvent(SimBGP.toSystemTime(12.0), ["5.1", "1/8"], SimBGP.EVENT_ANNOUNCE_PREFIX)) SimBGP._event_Scheduler.add( SimBGP.CEvent(SimBGP.toSystemTime(12.0), ["3.1", "1/8"], SimBGP.EVENT_WITHDRAW_PREFIX)) SimBGP.run() for id, router in SimBGP._router_list.iteritems(): if id[0:2] == "1.": self.assertNotEqual(router.loc_rib["1/8"][0].nexthop, router.loc_rib["1/8"][1].nexthop) try: self.assertEqual(len(router.loc_rib["1/8"]), 3) except AssertionError: print >> sys.stderr, "Router ", id, "is missing a path" raise