def test_update_router(self): self.test_create_router() subnets2 = [l2.Subnet(dhcp_ip="10.2.0.2", name="private-subnet", enable_dhcp=True, topic="fake_tenant1", gateway_ip="10.2.0.1", cidr="10.2.0.0/24", id="test_subnet10_2")] lswitch2 = l2.LogicalSwitch(subnets=subnets2, unique_key=6, name='test_lswitch_2', is_external=False, segmentation_id=42, topic='fake_tenant1', id='test_lswitch_2', version=5) router_ports2 = [l3.LogicalRouterPort(network="10.2.0.1/24", lswitch=lswitch2, topic="fake_tenant1", mac="fa:16:3e:50:96:f6", unique_key=7, id="fake_router_1_port2")] self.controller.update(lswitch2) router = copy.copy(self.router) router.ports = router_ports2 router.version += 1 self.app._add_router_port.reset_mock() self.controller.update(router) self.app._add_router_port.assert_called_once_with(router_ports2[0]) self.app._delete_router_port.assert_called_once_with( self.router_ports[0])
def build_logical_router_port(router_port_info, mac, network, unique_key): return l3.LogicalRouterPort(id=router_port_info['port_id'], topic=router_port_info['tenant_id'], lswitch=router_port_info['network_id'], mac=mac, network=network, unique_key=unique_key)
def build_logical_router_port(router_port_info, mac, network, unique_key): return l3.LogicalRouterPort(id=router_port_info['port_id'], topic=utils.get_obj_topic(router_port_info), lswitch=router_port_info['network_id'], mac=mac, network=network, unique_key=unique_key)
def create_logical_router(self, switch): """ Only works with testbed0/mininet_multi_switches_ipv4.py :param switch: :return: """ if self.nb_api is None: self.nb_api = api_nb.NbApi.get_instance(False) # TODO: lswitch from nb api router_ports = [] dpid = str(switch.dp.id) for port in switch.ports: # network = "192.168.33.1/24", network = None ip = None if dpid == '1': if port.port_no == 1: network = SUBNET1 ip = DP1_PORT1_GATEWAY_IP else: network = SUBNET2 ip = DP1_PORT2_GATEWAY_IP elif dpid == '2': if port.port_no == 1: network = SUBNET2 ip = DP2_PORT1_GATEWAY_IP else: network = SUBNET3 ip = DP2_PORT2_GATEWAY_IP elif dpid == '3': if port.port_no == 1: network = SUBNET3 ip = DP3_PORT1_GATEWAY_IP else: network = SUBNET4 ip = DP3_PORT2_GATEWAY_IP else: print "Datapath {} not supported. Router not created!".format(dpid) return if network and ip: router_port = l3.LogicalRouterPort(lswitch="{}".format(switch.dp.id), topic="fake_tenant1", network=network, gateway_ip=ip, mac="{}".format(port.hw_addr), port_no=str(port.port_no), unique_key=4, id="{}:{}".format(switch.dp.id, port.port_no)) router_ports.append(router_port) router = l3.LogicalRouter(name="router_of_{}".format(switch.dp.id), topic="fake_tenant1", version=10, id="{}".format(switch.dp.id), unique_key=5, ports=router_ports) self.nb_api.create(router)
def _add_another_router_interface(self): router_port1 = l3.LogicalRouterPort(network="20.0.0.1/24", lswitch="fake_switch2", topic="fake_tenant1", mac="fa:16:3e:50:96:fe", unique_key=15, id="fake_router_port2") self.router.add_router_port(router_port1)
def test_reply_ttl_invalid_message_with_rate_limit(self): pkt = packet.Packet() pkt.add_protocol(ethernet.ethernet(dst='aa:bb:cc:dd:ee:ff')) pkt.add_protocol(ipv4.ipv4(proto=in_proto.IPPROTO_UDP)) pkt.add_protocol(udp.udp()) pkt.serialize() lswitch = l2.LogicalSwitch( id='lswitch1', topic='topic1', unique_key=9, version=1, ) self.app.db_store.update(lswitch) lrouter = l3.LogicalRouter( id='lrouter1', topic='topic1', version=1, unique_key=22, ports=[ l3.LogicalRouterPort( id='lrouter1-port1', unique_key=55, topic='topic1', mac='aa:bb:cc:dd:ee:ff', network='10.0.0.1/24', lswitch='lswitch1', ), ], ) self.app.db_store.update(lrouter) event = ofp_event.EventOFPMsgBase( msg=ofproto_parser.OFPPacketIn( datapath=mock.Mock(), reason=self.app.ofproto.OFPR_INVALID_TTL, match=ofproto_parser.OFPMatch( metadata=lswitch.unique_key, reg5=lrouter.unique_key, ), data=pkt.data, ) ) with mock.patch("dragonflow.controller.common." "icmp_error_generator.generate") as icmp_error: for _ in range(self.app.conf.router_ttl_invalid_max_rate * 2): self.app.packet_in_handler(event) self.assertEqual(self.app.conf.router_ttl_invalid_max_rate, icmp_error.call_count) icmp_error.assert_called_with(icmp.ICMP_TIME_EXCEEDED, icmp.ICMP_TTL_EXPIRED_CODE, mock.ANY, "10.0.0.1", mock.ANY)
def test_create_router(self): self.subnets = [ l2.Subnet(dhcp_ip="10.1.0.2", name="private-subnet", enable_dhcp=True, topic="fake_tenant1", gateway_ip="10.1.0.1", cidr="10.1.0.0/24", id="test_subnet10_1") ] self.lswitch = l2.LogicalSwitch(subnets=self.subnets, unique_key=3, name='test_lswitch_1', is_external=False, segmentation_id=41, topic='fake_tenant1', id='test_lswitch_1', version=5) self.router_ports = [ l3.LogicalRouterPort(network="10.1.0.1/24", lswitch=self.lswitch, topic="fake_tenant1", mac="fa:16:3e:50:96:f5", unique_key=4, id="fake_router_1_port1") ] self.router = l3.LogicalRouter(name="fake_router_1", topic="fake_tenant1", version=10, id="fake_router_1", unique_key=5, ports=self.router_ports) self.controller.update(self.lswitch) self.app.mod_flow.reset_mock() self.controller.update(self.router) self.app._add_router_port.assert_called_once_with(self.router_ports[0]) parser = self.app.parser ofproto = self.app.ofproto match = parser.OFPMatch(metadata=5, eth_dst="fa:16:3e:50:96:f5") actions = [parser.OFPActionSetField(reg7=4)] inst = [ parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions), parser.OFPInstructionGotoTable(const.EGRESS_TABLE), ] self.app.mod_flow.assert_called_once_with( inst=inst, table_id=const.L3_LOOKUP_TABLE, priority=const.PRIORITY_VERY_LOW, match=match)
def test_reply_icmp_unreachable_with_rate_limit(self): pkt = packet.Packet() pkt.add_protocol(ethernet.ethernet(dst='aa:bb:cc:dd:ee:ff')) pkt.add_protocol(ipv4.ipv4(dst='10.0.0.1', proto=in_proto.IPPROTO_UDP)) pkt.add_protocol(udp.udp()) pkt.serialize() lrouter = l3.LogicalRouter( id='lrouter1', topic='topic1', version=1, unique_key=22, ports=[ l3.LogicalRouterPort( id='lrouter1-port1', unique_key=55, topic='topic1', mac='aa:bb:cc:dd:ee:ff', network='10.0.0.1/24', ), ], ) self.app.db_store.update(lrouter) event = ofp_event.EventOFPMsgBase( msg=ofproto_parser.OFPPacketIn( datapath=mock.Mock(), reason=self.app.ofproto.OFPR_PACKET_IN, match=ofproto_parser.OFPMatch( reg7=lrouter.ports[0].unique_key, ), data=pkt.data, ) ) with mock.patch("dragonflow.controller.common." "icmp_error_generator.generate") as icmp_error: for _ in range(self.app.conf.router_port_unreach_max_rate * 2): self.app.packet_in_handler(event) self.assertEqual( self.app.conf.router_port_unreach_max_rate, icmp_error.call_count) icmp_error.assert_called_with(icmp.ICMP_DEST_UNREACH, icmp.ICMP_PORT_UNREACH_CODE, pkt.data, pkt=mock.ANY)
minimal layout needed for the tests. For most unit tests of applications, the layout would include the classification and the application under test. """ return datapath_layout.Layout((), ()) def tearDown(self): for model in model_framework.iter_models(False): model.clear_registered_callbacks() super(DFAppTestBase, self).tearDown() fake_logical_router_ports = [ l3.LogicalRouterPort(network="10.0.0.1/24", lswitch="fake_switch1", topic="fake_tenant1", mac="fa:16:3e:50:96:f4", unique_key=14, id="fake_router_port1") ] fake_logic_router1 = l3.LogicalRouter(name="router1", topic="fake_tenant1", version=10, routes=[], id="fake_router_id", unique_key=1, ports=fake_logical_router_ports) fake_logic_switch1 = l2.LogicalSwitch(unique_key=1, name='private', is_external=False,