def test_fib_class(): obj = Fib() assert obj.at(1) == 1 assert obj.at(2) == 1 assert list(obj.get_all(2)) == [1, 1] assert list(obj.get_all(6)) == [1, 1, 2, 3, 5, 8]
def test_at(self): # Usually an object is created. You can also set it up using setUp() method. obj = Fib() self.assertEqual(8, obj.at(6)) self.assertEqual(1, obj.at(1)) self.assertEqual(1, obj.at(2))
def test_put_route(): fib = Fib() fib.put_route("4.0.0.0/8", ["nh1", "nh2"]) fib.put_route("4.1.0.0/16", ["nh3"]) fib.put_route("4.0.0.0/8", ["nh1", "nh4"]) assert str(fib) == ("4.0.0.0/8 -> nh1, nh4\n" "4.1.0.0/16 -> nh3\n")
def test_get_repr(): fib = Fib() fib.put_route("2.0.0.0/8", ["nh1", "nh2"]) fib.put_route("1.1.1.1/32", ["nh1"]) fib.put_route("2.2.1.0/24", ["nh1", "nh3"]) assert str(fib) == ("1.1.1.1/32 -> nh1\n" "2.0.0.0/8 -> nh1, nh2\n" "2.2.1.0/24 -> nh1, nh3\n")
def test_prop_parent_pos_child_pos(): # Add a parent aggregate rioute and a child more specific route, each with only positive # next-hops. fib = Fib() rib = Rib(fib) # Install two routes into the RIB: rib.put_route("1.2.0.0/16", ["nh1", "nh2"]) # Parent aggregate route, positive nexthops rib.put_route("1.2.3.0/24", ["nh3", "nh4"]) # Child more specific route, positive nexthops # The RIB must contain the following routes: assert str(rib) == ("1.2.0.0/16 -> nh1, nh2\n" "1.2.3.0/24 -> nh3, nh4\n") # The FIB must contain the following routes: assert str(fib) == ("1.2.0.0/16 -> nh1, nh2\n" "1.2.3.0/24 -> nh3, nh4\n") # Delete the parent route from the RIB. rib.del_route("1.2.0.0/16") assert str(rib) == "1.2.3.0/24 -> nh3, nh4\n" # The corresponding route should be deleted from the FIB. assert str(fib) == "1.2.3.0/24 -> nh3, nh4\n" # Delete the child route from the RIB. rib.del_route("1.2.3.0/24") # The RIB must be empty. assert str(rib) == "" # The FIB must be empty. assert str(fib) == ""
def test_del_route(): fib = Fib() rib = Rib(fib) rib.put_route("4.0.0.0/8", ["nh1", "nh2"]) rib.put_route("5.5.0.0/16", ["nh3", "nh4"]) rib.del_route("4.0.0.0/8") rib.del_route("3.0.0.0/8") assert str(rib) == ("5.5.0.0/16 -> nh3, nh4\n")
def test_put_route(): fib = Fib() rib = Rib(fib) rib.put_route("4.0.0.0/8", ["nh1", "nh2"]) rib.put_route("3.3.0.0/16", ["nh1", "nh3"], ["nh4", "nh2"]) rib.put_route("4.1.1.0/24", [], ["nh1"]) rib.put_route("4.0.0.0/8", ["nh1", "nh3"]) assert str(rib) == ("3.3.0.0/16 -> nh1, ~nh2, nh3, ~nh4\n" "4.0.0.0/8 -> nh1, nh3\n" "4.1.1.0/24 -> ~nh1\n")
def test_get_repr(): fib = Fib() rib = Rib(fib) rib.put_route("2.0.0.0/8", ["nh1", "nh2"]) rib.put_route("2.2.2.0/24", ["nh1", "nh3"], ["nh4", "nh2"]) rib.put_route("1.1.1.1/32", [], ["nh1"]) rib.put_route("2.2.1.0/24", ["nh1", "nh3"]) assert str(rib) == ("1.1.1.1/32 -> ~nh1\n" "2.0.0.0/8 -> nh1, nh2\n" "2.2.1.0/24 -> nh1, nh3\n" "2.2.2.0/24 -> nh1, ~nh2, nh3, ~nh4\n")
def process_etcd_kv(self, node_id, key, value, ev_type): self.logger.debug("k/v: ev_type=%s, node_id=%s, key=%s, value=%s", ev_type, node_id, key, value) if node_id == self.node_id: return changed = False if ev_type == "put": changed = self.update_node(node_id, key, value) elif ev_type == "delete": changed = self.remove_node(node_id) if changed: new_fib = Fib(self.wg_dev, self.node, self.node_table, self.wg_prvkey_path, self.vrf, logger = self.logger) new_fib.update_diff(self.fib) self.fib = new_fib
def test_fib_list(): genfibs = list(fibgen(100)) fibber = Fib(100) fibs = [] while True: try: fibs.append(next(fibber)) except: break assert genfibs == fibs
class ComplexType: def __init__(self, shared=None): if shared is not None: self.shared = shared else: self.shared = Value(ComplexData) self.worker = Fib() def calc(self, number): self.shared.fib = self.worker.calc(number) def result(self): return self.shared.fib
def test_prop_delete_nexthop_two_levels(): # Test slides 59 and 60 in Pascal's "negative disaggregation" presentation. # Delete a nexthop from a parent route, and check that the computed complementary nexthops in # the child route and the grandchild route. fib = Fib() rib = Rib(fib) # Install the following three routes into the RIB: rib.put_route("0.0.0.0/0", ["nh1", "nh2", "nh3", "nh4"]) # Parent default route rib.put_route("10.0.0.0/16", [], ["nh1"]) # Child, negative nexthop rib.put_route("10.0.10.0/24", [], ["nh2"]) # Grandchild, negative nexthop # The RIB must contain the following routes: assert str(rib) == ("0.0.0.0/0 -> nh1, nh2, nh3, nh4\n" "10.0.0.0/16 -> ~nh1\n" "10.0.10.0/24 -> ~nh2\n") # The FIB must contain the following routes: assert str(fib) == ("0.0.0.0/0 -> nh1, nh2, nh3, nh4\n" "10.0.0.0/16 -> nh2, nh3, nh4\n" "10.0.10.0/24 -> nh3, nh4\n") # Delete nexthop nh3 from the parent route 0.0.0.0/0 (by replacing the route with a new one # that has the reduced set of nexthops). rib.put_route("0.0.0.0/0", ["nh1", "nh2", "nh4"]) # The RIB must contain the following routes: assert str(rib) == ("0.0.0.0/0 -> nh1, nh2, nh4\n" # nh3 is gone "10.0.0.0/16 -> ~nh1\n" "10.0.10.0/24 -> ~nh2\n") # The FIB must contain the following routes: assert str(fib) == ("0.0.0.0/0 -> nh1, nh2, nh4\n" # nh3 is gone "10.0.0.0/16 -> nh2, nh4\n" # computed nh3 is gone "10.0.10.0/24 -> nh4\n") # computed nh3 is gone # Delete all routes from the RIB. rib.del_route("0.0.0.0/0") rib.del_route("10.0.0.0/16") rib.del_route("10.0.10.0/24") # The RIB must be empty. assert str(rib) == "" # The FIB must be empty. assert str(fib) == ""
def test_prop_mix_positive_negative(): # Child routes have mixture of positive and negative nexthops fib = Fib() rib = Rib(fib) # Install the following three routes into the RIB: rib.put_route("1.0.0.0/8", ["nh1", "nh2", "nh3"]) # Parent aggregate route rib.put_route("1.1.0.0/16", ["nh4"], ["nh1"]) # Child, positive and negative nexthop rib.put_route("1.1.1.0/24", ["nh5"], ["nh2"]) # Grandchild, positive and negative nexthop # The RIB must contain the following routes: assert str(rib) == ("1.0.0.0/8 -> nh1, nh2, nh3\n" "1.1.0.0/16 -> ~nh1, nh4\n" "1.1.1.0/24 -> ~nh2, nh5\n") # The FIB must contain the following routes: assert str(fib) == ("1.0.0.0/8 -> nh1, nh2, nh3\n" "1.1.0.0/16 -> nh2, nh3, nh4\n" "1.1.1.0/24 -> nh3, nh4, nh5\n") # Delete nexthop nh3 from the parent route 1.0.0.0/8 (by replacing the route with a new one # that has the reduced set of nexthops). rib.put_route("1.0.0.0/8", ["nh1", "nh2"]) # The RIB must contain the following routes: assert str(rib) == ("1.0.0.0/8 -> nh1, nh2\n" # nh3 is gone "1.1.0.0/16 -> ~nh1, nh4\n" "1.1.1.0/24 -> ~nh2, nh5\n") # The FIB must contain the following routes: assert str(fib) == ("1.0.0.0/8 -> nh1, nh2\n" # nh3 is gone "1.1.0.0/16 -> nh2, nh4\n" # computed nh3 is gone "1.1.1.0/24 -> nh4, nh5\n") # computed nh3 is gone # Delete all routes from the RIB. rib.del_route("1.0.0.0/8") rib.del_route("1.1.0.0/16") rib.del_route("1.1.1.0/24") # The RIB must be empty. assert str(rib) == "" # The FIB must be empty. assert str(fib) == ""
def test_prop_two_children(): # Test slide 57 in Pascal's "negative disaggregation" presentation: # Add a parent aggregate with positive nexthops and two children with negative nexthops. fib = Fib() rib = Rib(fib) # Install the following routes into the RIB: rib.put_route("0.0.0.0/0", ["nh1", "nh2", "nh3", "nh4"]) # Parent default route rib.put_route("10.0.0.0/16", [], ["nh1"]) # First child route, negative nh rib.put_route("10.1.0.0/16", [], ["nh4"]) # Second child route, negative nh # The RIB must contain the following routes: assert str(rib) == ("0.0.0.0/0 -> nh1, nh2, nh3, nh4\n" "10.0.0.0/16 -> ~nh1\n" "10.1.0.0/16 -> ~nh4\n") # The FIB must contain the following routes: assert str(fib) == ("0.0.0.0/0 -> nh1, nh2, nh3, nh4\n" "10.0.0.0/16 -> nh2, nh3, nh4\n" "10.1.0.0/16 -> nh1, nh2, nh3\n") # Delete the parent route from the RIB. rib.del_route("0.0.0.0/0") # The RIB must contain the following routes: assert str(rib) == ("10.0.0.0/16 -> ~nh1\n" "10.1.0.0/16 -> ~nh4\n") # The FIB must contain the following routes (note: no nexthops, so discard routes): assert str(fib) == ("10.0.0.0/16 -> \n" "10.1.0.0/16 -> \n") # Delete both remaining child routes from the RIB. rib.del_route("10.0.0.0/16") rib.del_route("10.1.0.0/16") # The RIB must be empty. assert str(rib) == "" # The FIB must be empty. assert str(fib) == ""
def test_prop_one_child(): # Test slide 56 in Pascal's "negative disaggregation" presentation: # Add a parent aggregate with positive nexthops and one child with a negative nexthop. fib = Fib() rib = Rib(fib) # Install two routes into the RIB: one parent aggregate with four ECMP positive nexthops, and # one child more specific with one negative nexthop. rib.put_route("0.0.0.0/0", ["nh1", "nh2", "nh3", "nh4"]) # Parent default route rib.put_route("10.0.0.0/16", [], ["nh1"]) # Child route with negative nexthop # The RIB must contain the following routes: assert str(rib) == ("0.0.0.0/0 -> nh1, nh2, nh3, nh4\n" "10.0.0.0/16 -> ~nh1\n") # The RIB must contain the following routes: assert str(fib) == ("0.0.0.0/0 -> nh1, nh2, nh3, nh4\n" # Parent route, same as RIB "10.0.0.0/16 -> nh2, nh3, nh4\n") # Child route, complementary nhs # Delete the parent route from the RIB. rib.del_route("0.0.0.0/0") # The RIB must contain the following routes: assert str(rib) == "10.0.0.0/16 -> ~nh1\n" # The FIB must contain the following routes: assert str(fib) == "10.0.0.0/16 -> \n" # Delete the child route from the RIB. rib.del_route("10.0.0.0/16") # The RIB must be empty. assert str(rib) == "" # The FIB must be empty. assert str(fib) == ""
def test_prop_one_route_pos(): # Add a single route with only positive next-hops fib = Fib() rib = Rib(fib) # Install one route into the RIB: rib.put_route("1.2.3.0/24", ["nh1", "nh2"]) # A single route, positive nexthops # The RIB must contain the following route: assert str(rib) == ("1.2.3.0/24 -> nh1, nh2\n") # The FIB must contain the following route: assert str(fib) == ("1.2.3.0/24 -> nh1, nh2\n") # Delete the route from the RIB. rib.del_route("1.2.3.0/24") # The RIB must be empty. assert str(rib) == ("") # The FIB must be empty. assert str(fib) == ("")
def test_fib_of_index_10(): assert Fib.calculate(10) == 89
def test_fib_of_index_9(): assert Fib.calculate(9) == 55
class TestFib(unittest.TestCase): def setUp(self): self.fib = Fib() def test_recursively(self): self.assertEqual(self.fib.recursively(0), 0) self.assertEqual(self.fib.recursively(1), 1) self.assertEqual(self.fib.recursively(2), 1) self.assertEqual(self.fib.recursively(3), 2) self.assertEqual(self.fib.recursively(4), 3) self.assertEqual(self.fib.recursively(5), 5) self.assertEqual(self.fib.recursively(6), 8) self.assertEqual(self.fib.recursively(7), 13) self.assertEqual(self.fib.recursively(8), 21) self.assertEqual(self.fib.recursively(9), 34) self.assertEqual(self.fib.recursively(10), 55) def test_whileLoop(self): self.assertEqual(self.fib.whileLoop(0), 0) self.assertEqual(self.fib.whileLoop(1), 1) self.assertEqual(self.fib.whileLoop(2), 1) self.assertEqual(self.fib.whileLoop(3), 2) self.assertEqual(self.fib.whileLoop(4), 3) self.assertEqual(self.fib.whileLoop(5), 5) self.assertEqual(self.fib.whileLoop(6), 8) self.assertEqual(self.fib.whileLoop(7), 13) self.assertEqual(self.fib.whileLoop(8), 21) self.assertEqual(self.fib.whileLoop(9), 34) self.assertEqual(self.fib.whileLoop(10), 55) def test_timedRecursively(self): self.assertEqual(self.fib.timedRecursively(40), 102334155) def test_timedWhileLoop(self): self.assertEqual(self.fib.timedWhileLoop(40), 102334155) self.assertEqual(self.fib.timedWhileLoop(50), 12586269025) self.assertEqual(self.fib.timedWhileLoop(60), 1548008755920)
def setUp(self): self.fib = Fib()
def test_get_route(): fib = Fib() fib.put_route("4.0.0.0/8", ["nh1", "nh2"]) assert str(fib.get_route("4.0.0.0/8")) == "4.0.0.0/8 -> nh1, nh2" assert fib.get_route("3.0.0.0/8") is None
def test_constructor(): _fib = Fib()
def test_del_route(): fib = Fib() fib.put_route("4.0.0.0/8", ["nh1", "nh2"]) fib.del_route("4.0.0.0/8") fib.del_route("3.0.0.0/8") assert str(fib) == ("")
# pylint: disable=superfluous-parens """A tiny example binary for the native Python rules of Bazel.""" from pipeline.samples.bazel.python.lib import GetNumber from fib import Fib print("The number is %d" % GetNumber()) print("Fib(5) == %d" % Fib(5))
# -*- coding: utf-8 -*- from fib import Fib b = Fib() b.hello() print(type(Fib)) print(type(b))
def test_constructor(): fib = Fib() _rib = Rib(fib)
def test_fib_of_index_6(): assert Fib.calculate(6) == 13
def test_fib_of_index_5(): assert Fib.calculate(5) == 8
def test_fib_of_index_1(): assert Fib.calculate(0) == 1
def test_prop_deep_nesting(): # Deep nesting of more specific routes: parent, child, grand child, grand-grand child, ... fib = Fib() rib = Rib(fib) # Install the following three routes into the RIB: rib.put_route("0.0.0.0/0", ["nh1", "nh2", "nh3", "nh4", "nh5"]) # Parent rib.put_route("1.0.0.0/8", [], ["nh1"]) # Child rib.put_route("1.128.0.0/9", [], ["nh2"]) # Grand child rib.put_route("1.192.0.0/10", [], ["nh3"]) # Grand-grand child rib.put_route("1.224.0.0/11", [], ["nh4"]) # Grand-grand-grand child rib.put_route("1.240.0.0/12", [], ["nh5"]) # Grand-grand-grand-grand child # The RIB must contain the following routes: assert str(rib) == ("0.0.0.0/0 -> nh1, nh2, nh3, nh4, nh5\n" "1.0.0.0/8 -> ~nh1\n" "1.128.0.0/9 -> ~nh2\n" "1.192.0.0/10 -> ~nh3\n" "1.224.0.0/11 -> ~nh4\n" "1.240.0.0/12 -> ~nh5\n") # The FIB must contain the following routes: assert str(fib) == ("0.0.0.0/0 -> nh1, nh2, nh3, nh4, nh5\n" "1.0.0.0/8 -> nh2, nh3, nh4, nh5\n" "1.128.0.0/9 -> nh3, nh4, nh5\n" "1.192.0.0/10 -> nh4, nh5\n" "1.224.0.0/11 -> nh5\n" "1.240.0.0/12 -> \n") # Delete nexthop nh3 from the parent route 0.0.0.0/0. rib.put_route("0.0.0.0/0", ["nh1", "nh2", "nh4", "nh5"]) # The RIB must contain the following routes: assert str(rib) == ("0.0.0.0/0 -> nh1, nh2, nh4, nh5\n" "1.0.0.0/8 -> ~nh1\n" "1.128.0.0/9 -> ~nh2\n" "1.192.0.0/10 -> ~nh3\n" "1.224.0.0/11 -> ~nh4\n" "1.240.0.0/12 -> ~nh5\n") # The FIB must contain the following routes: assert str(fib) == ("0.0.0.0/0 -> nh1, nh2, nh4, nh5\n" "1.0.0.0/8 -> nh2, nh4, nh5\n" "1.128.0.0/9 -> nh4, nh5\n" "1.192.0.0/10 -> nh4, nh5\n" "1.224.0.0/11 -> nh5\n" "1.240.0.0/12 -> \n") # Delete all routes from the RIB. rib.del_route("0.0.0.0/0") rib.del_route("1.0.0.0/8") rib.del_route("1.128.0.0/9") rib.del_route("1.192.0.0/10") rib.del_route("1.224.0.0/11") rib.del_route("1.240.0.0/12") # The RIB must be empty. assert str(rib) == "" # The FIB must be empty. assert str(fib) == ""
def test_fib(self): self.assertEquals(Fib(5), 8)
def test_prop_nesting_with_siblings(): # Deep nesting of more specific routes using the following tree: # # 1.0.0.0/8 -> nh1, nh2, nh3, nh4, nh5, nh6, nh7 # | # +--- 1.1.0.0/16 -> ~nh1 # | | # | +--- 1.1.1.0/24 -> ~nh2 # | | # | +--- 1.1.2.0/24 -> ~nh3 # | # +--- 1.2.0.0/16 -> ~nh4 # | # +--- 1.2.1.0/24 -> ~nh5 # | # +--- 1.2.2.0/24 -> ~nh6 # # Note: we add the routes in a random order fib = Fib() rib = Rib(fib) # Install the following three routes into the RIB: rib.put_route("1.2.1.0/24", [], ["nh5"]) rib.put_route("1.1.2.0/24", [], ["nh3"]) rib.put_route("1.1.0.0/16", [], ["nh1"]) rib.put_route("1.1.1.0/24", [], ["nh2"]) rib.put_route("1.2.0.0/16", [], ["nh4"]) rib.put_route("1.0.0.0/8", ["nh1", "nh2", "nh3", "nh4", "nh5", "nh6", "nh7"]) rib.put_route("1.2.2.0/24", [], ["nh6"]) # The RIB must contain the following routes: assert str(rib) == ("1.0.0.0/8 -> nh1, nh2, nh3, nh4, nh5, nh6, nh7\n" "1.1.0.0/16 -> ~nh1\n" "1.1.1.0/24 -> ~nh2\n" "1.1.2.0/24 -> ~nh3\n" "1.2.0.0/16 -> ~nh4\n" "1.2.1.0/24 -> ~nh5\n" "1.2.2.0/24 -> ~nh6\n") # The FIB must contain the following routes: assert str(fib) == ("1.0.0.0/8 -> nh1, nh2, nh3, nh4, nh5, nh6, nh7\n" "1.1.0.0/16 -> nh2, nh3, nh4, nh5, nh6, nh7\n" "1.1.1.0/24 -> nh3, nh4, nh5, nh6, nh7\n" "1.1.2.0/24 -> nh2, nh4, nh5, nh6, nh7\n" "1.2.0.0/16 -> nh1, nh2, nh3, nh5, nh6, nh7\n" "1.2.1.0/24 -> nh1, nh2, nh3, nh6, nh7\n" "1.2.2.0/24 -> nh1, nh2, nh3, nh5, nh7\n") # Delete nexthop nh3 from the parent route 0.0.0.0/0. rib.put_route("1.0.0.0/8", ["nh1", "nh2", "nh4", "nh5", "nh6", "nh7"]) # The RIB must contain the following routes: assert str(rib) == ("1.0.0.0/8 -> nh1, nh2, nh4, nh5, nh6, nh7\n" "1.1.0.0/16 -> ~nh1\n" "1.1.1.0/24 -> ~nh2\n" "1.1.2.0/24 -> ~nh3\n" "1.2.0.0/16 -> ~nh4\n" "1.2.1.0/24 -> ~nh5\n" "1.2.2.0/24 -> ~nh6\n") # The FIB must contain the following routes: assert str(fib) == ("1.0.0.0/8 -> nh1, nh2, nh4, nh5, nh6, nh7\n" "1.1.0.0/16 -> nh2, nh4, nh5, nh6, nh7\n" "1.1.1.0/24 -> nh4, nh5, nh6, nh7\n" "1.1.2.0/24 -> nh2, nh4, nh5, nh6, nh7\n" "1.2.0.0/16 -> nh1, nh2, nh5, nh6, nh7\n" "1.2.1.0/24 -> nh1, nh2, nh6, nh7\n" "1.2.2.0/24 -> nh1, nh2, nh5, nh7\n") # Delete all routes from the RIB. rib.del_route("1.0.0.0/8") rib.del_route("1.1.0.0/16") rib.del_route("1.1.1.0/24") rib.del_route("1.1.2.0/24") rib.del_route("1.2.0.0/16") rib.del_route("1.2.1.0/24") rib.del_route("1.2.2.0/24") # The RIB must be empty. assert str(rib) == "" # The FIB must be empty. assert str(fib) == ""