]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_labeled_unicast_addpath/test_bgp_labeled_unicast_addpath.py
Merge pull request #12780 from opensourcerouting/spdx-license-id
[mirror_frr.git] / tests / topotests / bgp_labeled_unicast_addpath / test_bgp_labeled_unicast_addpath.py
1 #!/usr/bin/env python
2 # SPDX-License-Identifier: ISC
3
4 #
5 # Copyright (c) 2022 by
6 # Donatas Abraitis <donatas@opensourcerouting.org>
7 #
8
9 """
10 Check if labeled-unicast works correctly with addpath capability.
11 Initially R3 MUST announce 10.0.0.1/32 multipath(2) from R1 + R2.
12 Later, we enable R5 and 10.0.0.1/32 multipath(3) MUST be announced,
13 R1 + R2 + R5.
14 """
15
16 import os
17 import sys
18 import json
19 import pytest
20 import functools
21
22 CWD = os.path.dirname(os.path.realpath(__file__))
23 sys.path.append(os.path.join(CWD, "../"))
24
25 # pylint: disable=C0413
26 from lib import topotest
27 from lib.topogen import Topogen, TopoRouter, get_topogen
28 from lib.common_config import step
29
30 pytestmark = [pytest.mark.bgpd]
31
32
33 def build_topo(tgen):
34 for routern in range(1, 6):
35 tgen.add_router("r{}".format(routern))
36
37 switch = tgen.add_switch("s1")
38 switch.add_link(tgen.gears["r1"])
39 switch.add_link(tgen.gears["r3"])
40
41 switch = tgen.add_switch("s2")
42 switch.add_link(tgen.gears["r2"])
43 switch.add_link(tgen.gears["r3"])
44
45 switch = tgen.add_switch("s3")
46 switch.add_link(tgen.gears["r3"])
47 switch.add_link(tgen.gears["r4"])
48
49 switch = tgen.add_switch("s4")
50 switch.add_link(tgen.gears["r3"])
51 switch.add_link(tgen.gears["r5"])
52
53
54 def setup_module(mod):
55 tgen = Topogen(build_topo, mod.__name__)
56 tgen.start_topology()
57
58 router_list = tgen.routers()
59
60 for i, (rname, router) in enumerate(router_list.items(), 1):
61 router.load_config(
62 TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname))
63 )
64 router.load_config(
65 TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format(rname))
66 )
67
68 tgen.start_router()
69
70
71 def teardown_module(mod):
72 tgen = get_topogen()
73 tgen.stop_topology()
74
75
76 def test_bgp_addpath_labeled_unicast():
77 tgen = get_topogen()
78
79 if tgen.routers_have_failure():
80 pytest.skip(tgen.errors)
81
82 r3 = tgen.gears["r3"]
83 r4 = tgen.gears["r4"]
84
85 def _bgp_check_advertised_routes(prefix_num):
86 output = json.loads(
87 r3.vtysh_cmd(
88 "show bgp ipv4 labeled-unicast neighbors 192.168.34.4 advertised-routes json"
89 )
90 )
91 expected = {
92 "advertisedRoutes": {
93 "10.0.0.1/32": {
94 "appliedStatusSymbols": {
95 "*": True,
96 ">": True,
97 "=": True,
98 }
99 }
100 },
101 "totalPrefixCounter": prefix_num,
102 }
103 return topotest.json_cmp(output, expected)
104
105 test_func = functools.partial(_bgp_check_advertised_routes, 2)
106 _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
107 assert (
108 result is None
109 ), "Failed to advertise labeled-unicast with addpath (multipath)"
110
111 def _bgp_check_received_routes():
112 output = json.loads(r4.vtysh_cmd("show bgp ipv4 labeled-unicast json"))
113 expected = {
114 "routes": {
115 "10.0.0.1/32": [
116 {
117 "valid": True,
118 "path": "65003 65001",
119 },
120 {
121 "valid": True,
122 "path": "65003 65002",
123 },
124 ]
125 }
126 }
127 return topotest.json_cmp(output, expected)
128
129 test_func = functools.partial(_bgp_check_received_routes)
130 _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
131 assert result is None, "Failed to receive labeled-unicast with addpath (multipath)"
132
133 step("Enable BGP session for R5")
134 r3.vtysh_cmd(
135 """
136 configure terminal
137 router bgp 65003
138 no neighbor 192.168.35.5 shutdown
139 """
140 )
141
142 test_func = functools.partial(_bgp_check_advertised_routes, 3)
143 _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
144 assert (
145 result is None
146 ), "Failed to advertise labeled-unicast with addpath (multipath)"
147
148 step("Disable BGP session for R5")
149 r3.vtysh_cmd(
150 """
151 configure terminal
152 router bgp 65003
153 neighbor 192.168.35.5 shutdown
154 """
155 )
156
157 test_func = functools.partial(_bgp_check_advertised_routes, 2)
158 _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
159 assert (
160 result is None
161 ), "Failed to advertise labeled-unicast with addpath (multipath)"
162
163
164 if __name__ == "__main__":
165 args = ["-s"] + sys.argv[1:]
166 sys.exit(pytest.main(args))