Path: blob/main/tests/sys/net/routing/test_routing_l3.py
39604 views
import ipaddress1import socket23import pytest4from atf_python.sys.net.rtsock import RtConst5from atf_python.sys.net.rtsock import Rtsock6from atf_python.sys.net.rtsock import RtsockRtMessage7from atf_python.sys.net.rtsock import SaHelper8from atf_python.sys.net.tools import ToolsHelper9from atf_python.sys.net.vnet import SingleVnetTestTemplate10from atf_python.sys.net.vnet import VnetTestTemplate111213class TestIfOps(VnetTestTemplate):14TOPOLOGY = {15"vnet1": {"ifaces": ["if1", "if2"]},16"if1": {"prefixes4": [], "prefixes6": []},17"if2": {"prefixes4": [], "prefixes6": []},18}1920@pytest.mark.parametrize("family", ["inet", "inet6"])21@pytest.mark.require_user("root")22def test_change_prefix_route(self, family):23"""Tests that prefix route changes to the new one upon addr deletion"""24vnet = self.vnet_map["vnet1"]25first_iface = vnet.iface_alias_map["if1"]26second_iface = vnet.iface_alias_map["if2"]27if family == "inet":28first_addr = ipaddress.ip_interface("192.0.2.1/24")29second_addr = ipaddress.ip_interface("192.0.2.2/24")30else:31first_addr = ipaddress.ip_interface("2001:db8::1/64")32second_addr = ipaddress.ip_interface("2001:db8::2/64")3334first_iface.setup_addr(str(first_addr))35second_iface.setup_addr(str(second_addr))3637# At this time prefix should be pointing to the first interface38routes = ToolsHelper.get_routes(family)39px = [r for r in routes if r["destination"] == str(first_addr.network)][0]40assert px["interface-name"] == first_iface.name4142# Now delete address from the first interface and verify switchover43first_iface.delete_addr(first_addr.ip)4445routes = ToolsHelper.get_routes(family)46px = [r for r in routes if r["destination"] == str(first_addr.network)][0]47assert px["interface-name"] == second_iface.name4849@pytest.mark.parametrize(50"family",51[52"inet",53pytest.param("inet6", marks=pytest.mark.xfail(reason="currently fails")),54],55)56@pytest.mark.require_user("root")57def test_change_prefix_route_same_iface(self, family):58"""Tests that prefix route changes to the new ifa upon addr deletion"""59vnet = self.vnet_map["vnet1"]60first_iface = vnet.iface_alias_map["if1"]6162if family == "inet":63first_addr = ipaddress.ip_interface("192.0.2.1/24")64second_addr = ipaddress.ip_interface("192.0.2.2/24")65else:66first_addr = ipaddress.ip_interface("2001:db8::1/64")67second_addr = ipaddress.ip_interface("2001:db8::2/64")6869first_iface.setup_addr(str(first_addr))70first_iface.setup_addr(str(second_addr))7172# At this time prefix should be pointing to the first interface73routes = ToolsHelper.get_routes(family)74px = [r for r in routes if r["destination"] == str(first_addr.network)][0]75assert px["interface-name"] == first_iface.name7677# Now delete address from the first interface and verify switchover78first_iface.delete_addr(str(first_addr.ip))7980routes = ToolsHelper.get_routes(family)81px = [r for r in routes if r["destination"] == str(first_addr.network)][0]82nhop_kidx = px["nhop"]83assert px["interface-name"] == first_iface.name84nhops = ToolsHelper.get_nhops(family)85nh = [nh for nh in nhops if nh["index"] == nhop_kidx][0]86assert nh["ifa"] == str(second_addr.ip)878889class TestRouteCornerCase1(SingleVnetTestTemplate):90@pytest.mark.parametrize("family", ["inet", "inet6"])91@pytest.mark.require_user("root")92def test_add_direct_route_p2p_wo_ifa(self, family):9394tun_ifname = ToolsHelper.get_output("/sbin/ifconfig tun create").rstrip()95tun_ifindex = socket.if_nametoindex(tun_ifname)96assert tun_ifindex > 097rtsock = Rtsock()9899if family == "inet":100prefix = "172.16.0.0/12"101else:102prefix = "2a02:6b8::/64"103IFT_ETHER = 0x06104gw_link = SaHelper.link_sa(ifindex=tun_ifindex, iftype=IFT_ETHER)105106msg = rtsock.new_rtm_add(prefix, gw_link)107msg.add_link_attr(RtConst.RTA_IFP, tun_ifindex)108rtsock.write_message(msg)109110data = rtsock.read_data(msg.rtm_seq)111msg_in = RtsockRtMessage.from_bytes(data)112msg_in.print_in_message()113114desired_sa = {115RtConst.RTA_DST: msg.get_sa(RtConst.RTA_DST),116RtConst.RTA_NETMASK: msg.get_sa(RtConst.RTA_NETMASK),117}118119msg_in.verify(msg.rtm_type, desired_sa)120121122