2 # SPDX-License-Identifier: ISC
5 # Copyright (c) 2022 by
6 # Louis Scalbert <louis.scalbert@6wind.com>
19 CWD
= os
.path
.dirname(os
.path
.realpath(__file__
))
20 sys
.path
.append(os
.path
.join(CWD
, "../"))
22 # pylint: disable=C0413
23 from lib
import topotest
24 from lib
.topogen
import Topogen
, TopoRouter
, get_topogen
25 from lib
.common_config
import step
27 pytestmark
= [pytest
.mark
.bgpd
]
31 for routern
in range(1, 4):
32 tgen
.add_router("r{}".format(routern
))
34 switch
= tgen
.add_switch("s1")
35 switch
.add_link(tgen
.gears
["r1"])
36 switch
.add_link(tgen
.gears
["r2"])
38 switch
= tgen
.add_switch("s2")
39 switch
.add_link(tgen
.gears
["r1"])
40 switch
.add_link(tgen
.gears
["r3"])
43 def setup_module(mod
):
44 tgen
= Topogen(build_topo
, mod
.__name
__)
47 router_list
= tgen
.routers()
49 for routern
in range(1, 4):
50 tgen
.gears
["r{}".format(routern
)].cmd("ip link add vrf1 type vrf table 10")
51 tgen
.gears
["r{}".format(routern
)].cmd("ip link set vrf1 up")
52 tgen
.gears
["r{}".format(routern
)].cmd("ip address add dev vrf1 {}.{}.{}.{}/32".format(routern
, routern
, routern
,routern
))
53 tgen
.gears
["r2"].cmd("ip address add dev vrf1 192.0.2.8/32")
54 tgen
.gears
["r3"].cmd("ip address add dev vrf1 192.0.2.8/32")
56 for i
, (rname
, router
) in enumerate(router_list
.items(), 1):
58 TopoRouter
.RD_ZEBRA
, os
.path
.join(CWD
, "{}/zebra.conf".format(rname
))
61 TopoRouter
.RD_BGP
, os
.path
.join(CWD
, "{}/bgpd.conf".format(rname
))
64 TopoRouter
.RD_LDP
, os
.path
.join(CWD
, "{}/ldpd.conf".format(rname
))
69 tgen
.gears
["r1"].cmd("ip route add 192.0.2.2 via 192.168.1.2 metric 20")
70 tgen
.gears
["r1"].cmd("ip route add 192.0.2.3 via 192.168.2.2 metric 20")
73 def teardown_module(mod
):
77 def test_bgp_path_selection_ecmp():
80 if tgen
.routers_have_failure():
81 pytest
.skip(tgen
.errors
)
83 def _bgp_check_path_selection_ecmp():
85 tgen
.gears
["r1"].vtysh_cmd("show bgp ipv4 unicast 192.0.2.8/32 json")
91 "aspath": {"string": "65002"},
93 "nexthops": [{"ip": "192.0.2.2", "metric": 20}],
97 "aspath": {"string": "65002"},
99 "nexthops": [{"ip": "192.0.2.3", "metric": 20}],
104 return topotest
.json_cmp(output
, expected
)
106 step("Check if two ECMP paths are present")
107 test_func
= functools
.partial(_bgp_check_path_selection_ecmp
)
108 _
, result
= topotest
.run_and_expect(test_func
, None, count
=60, wait
=0.5)
109 assert result
is None, "Failed to see BGP prefixes on R1"
112 def test_bgp_path_selection_vpn_ecmp():
115 if tgen
.routers_have_failure():
116 pytest
.skip(tgen
.errors
)
118 def _bgp_check_path_selection_vpn_ecmp():
120 tgen
.gears
["r1"].vtysh_cmd("show bgp vrf vrf1 ipv4 unicast 192.0.2.8/32 json")
126 "aspath": {"string": "65002"},
128 "nexthops": [{"ip": "192.0.2.2", "metric": 20}],
132 "aspath": {"string": "65002"},
134 "nexthops": [{"ip": "192.0.2.3", "metric": 20}],
139 return topotest
.json_cmp(output
, expected
)
141 step("Check if two ECMP paths are present")
142 test_func
= functools
.partial(_bgp_check_path_selection_vpn_ecmp
)
143 _
, result
= topotest
.run_and_expect(test_func
, None, count
=60, wait
=0.5)
144 assert result
is None, "Failed to see BGP prefixes on R1"
147 def test_bgp_path_selection_metric():
150 if tgen
.routers_have_failure():
151 pytest
.skip(tgen
.errors
)
153 def _bgp_check_path_selection_metric():
155 tgen
.gears
["r1"].vtysh_cmd("show bgp ipv4 unicast 192.0.2.8/32 json")
161 "aspath": {"string": "65002"},
162 "nexthops": [{"ip": "192.0.2.2", "metric": 10}],
163 "bestpath":{ "selectionReason":"IGP Metric"},
167 "aspath": {"string": "65002"},
168 "nexthops": [{"ip": "192.0.2.3", "metric": 20}],
173 return topotest
.json_cmp(output
, expected
)
175 tgen
.gears
["r1"].cmd("ip route add 192.0.2.2 via 192.168.1.2 metric 10")
176 tgen
.gears
["r1"].cmd("ip route del 192.0.2.2 via 192.168.1.2 metric 20")
178 step("Check if IGP metric best path is selected")
179 test_func
= functools
.partial(_bgp_check_path_selection_metric
)
180 _
, result
= topotest
.run_and_expect(test_func
, None, count
=60, wait
=0.5)
181 assert result
is None, "Failed to see BGP prefixes on R1"
184 def test_bgp_path_selection_vpn_metric():
187 if tgen
.routers_have_failure():
188 pytest
.skip(tgen
.errors
)
190 def _bgp_check_path_selection_vpn_metric():
192 tgen
.gears
["r1"].vtysh_cmd("show bgp vrf vrf1 ipv4 unicast 192.0.2.8/32 json")
198 "aspath": {"string": "65002"},
199 "nexthops": [{"ip": "192.0.2.2", "metric": 10}],
200 "bestpath":{ "selectionReason":"IGP Metric"},
204 "aspath": {"string": "65002"},
205 "nexthops": [{"ip": "192.0.2.3", "metric": 20}],
210 return topotest
.json_cmp(output
, expected
)
212 step("Check if IGP metric best path is selected")
213 test_func
= functools
.partial(_bgp_check_path_selection_vpn_metric
)
214 _
, result
= topotest
.run_and_expect(test_func
, None, count
=60, wait
=0.5)
215 assert result
is None, "Failed to see BGP prefixes on R1"
218 if __name__
== "__main__":
219 args
= ["-s"] + sys
.argv
[1:]
220 sys
.exit(pytest
.main(args
))