1import ipaddress
2
3import pytest
4from atf_python.sys.net.tools import ToolsHelper
5from atf_python.sys.net.vnet import VnetTestTemplate
6
7
8class TestIfOps(VnetTestTemplate):
9    TOPOLOGY = {
10        "vnet1": {"ifaces": ["if1", "if2"]},
11        "if1": {"prefixes4": [], "prefixes6": []},
12        "if2": {"prefixes4": [], "prefixes6": []},
13    }
14
15    @pytest.mark.parametrize("family", ["inet", "inet6"])
16    @pytest.mark.require_user("root")
17    def test_change_prefix_route(self, family):
18        """Tests that prefix route changes to the new one upon addr deletion"""
19        vnet = self.vnet_map["vnet1"]
20        first_iface = vnet.iface_alias_map["if1"]
21        second_iface = vnet.iface_alias_map["if2"]
22        if family == "inet":
23            first_addr = ipaddress.ip_interface("192.0.2.1/24")
24            second_addr = ipaddress.ip_interface("192.0.2.2/24")
25        else:
26            first_addr = ipaddress.ip_interface("2001:db8::1/64")
27            second_addr = ipaddress.ip_interface("2001:db8::2/64")
28
29        first_iface.setup_addr(str(first_addr))
30        second_iface.setup_addr(str(second_addr))
31
32        # At this time prefix should be pointing to the first interface
33        routes = ToolsHelper.get_routes(family)
34        px = [r for r in routes if r["destination"] == str(first_addr.network)][0]
35        assert px["interface-name"] == first_iface.name
36
37        # Now delete address from the first interface and verify switchover
38        first_iface.delete_addr(first_addr.ip)
39
40        routes = ToolsHelper.get_routes(family)
41        px = [r for r in routes if r["destination"] == str(first_addr.network)][0]
42        assert px["interface-name"] == second_iface.name
43
44    @pytest.mark.parametrize(
45        "family",
46        [
47            "inet",
48            pytest.param("inet6", marks=pytest.mark.xfail(reason="currently fails")),
49        ],
50    )
51    @pytest.mark.require_user("root")
52    def test_change_prefix_route_same_iface(self, family):
53        """Tests that prefix route changes to the new ifa upon addr deletion"""
54        vnet = self.vnet_map["vnet1"]
55        first_iface = vnet.iface_alias_map["if1"]
56
57        if family == "inet":
58            first_addr = ipaddress.ip_interface("192.0.2.1/24")
59            second_addr = ipaddress.ip_interface("192.0.2.2/24")
60        else:
61            first_addr = ipaddress.ip_interface("2001:db8::1/64")
62            second_addr = ipaddress.ip_interface("2001:db8::2/64")
63
64        first_iface.setup_addr(str(first_addr))
65        first_iface.setup_addr(str(second_addr))
66
67        # At this time prefix should be pointing to the first interface
68        routes = ToolsHelper.get_routes(family)
69        px = [r for r in routes if r["destination"] == str(first_addr.network)][0]
70        assert px["interface-name"] == first_iface.name
71
72        # Now delete address from the first interface and verify switchover
73        first_iface.delete_addr(str(first_addr.ip))
74
75        routes = ToolsHelper.get_routes(family)
76        px = [r for r in routes if r["destination"] == str(first_addr.network)][0]
77        nhop_kidx = px["nhop"]
78        assert px["interface-name"] == first_iface.name
79        nhops = ToolsHelper.get_nhops(family)
80        nh = [nh for nh in nhops if nh["index"] == nhop_kidx][0]
81        assert nh["ifa"] == str(second_addr.ip)
82