]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_llgr/test_bgp_llgr.py
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / tests / topotests / bgp_llgr / test_bgp_llgr.py
1 #!/usr/bin/env python
2 # SPDX-License-Identifier: ISC
3
4 # Copyright (c) 2021 by
5 # Donatas Abraitis <donatas.abraitis@gmail.com>
6 #
7
8 """
9 Test if BGP Long-lived Graceful Restart capability works:
10 Check if we can see 172.16.1.1/32 after initial converge in R3.
11 Check if we can see 172.16.1.1/32 as best selected due to higher weigth in R2.
12 Kill bgpd in R1.
13 Check if we can see 172.16.1.1/32 as stale in R2.
14 Check if we can see 172.16.1.1/32 depreferenced due to LLGR_STALE in R2.
15 Check if we can see 172.16.1.1/32 after R1 was killed in R3.
16 """
17
18 import os
19 import sys
20 import json
21 import pytest
22 import functools
23
24 pytestmark = [pytest.mark.bgpd]
25
26 CWD = os.path.dirname(os.path.realpath(__file__))
27 sys.path.append(os.path.join(CWD, "../"))
28
29 # pylint: disable=C0413
30 from lib import topotest
31 from lib.topogen import Topogen, TopoRouter, get_topogen
32
33 from lib.common_config import (
34 kill_router_daemons,
35 start_router_daemons,
36 step,
37 )
38
39 pytestmark = [pytest.mark.bgpd]
40
41
42 def build_topo(tgen):
43 for routern in range(0, 6):
44 tgen.add_router("r{}".format(routern))
45
46 switch = tgen.add_switch("s0")
47 switch.add_link(tgen.gears["r0"])
48 switch.add_link(tgen.gears["r2"])
49
50 switch = tgen.add_switch("s1")
51 switch.add_link(tgen.gears["r1"])
52 switch.add_link(tgen.gears["r2"])
53
54 switch = tgen.add_switch("s2")
55 switch.add_link(tgen.gears["r2"])
56 switch.add_link(tgen.gears["r3"])
57
58 # Dynamic neighbor
59 switch = tgen.add_switch("s3")
60 switch.add_link(tgen.gears["r2"])
61 switch.add_link(tgen.gears["r4"])
62
63
64 def setup_module(mod):
65 tgen = Topogen(build_topo, mod.__name__)
66 tgen.start_topology()
67
68 router_list = tgen.routers()
69
70 for i, (rname, router) in enumerate(router_list.items(), 1):
71 router.load_config(
72 TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname))
73 )
74 router.load_config(
75 TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format(rname))
76 )
77
78 tgen.start_router()
79
80
81 def teardown_module(mod):
82 tgen = get_topogen()
83 tgen.stop_topology()
84
85
86 def test_bgp_llgr():
87 tgen = get_topogen()
88
89 if tgen.routers_have_failure():
90 pytest.skip(tgen.errors)
91
92 r2 = tgen.gears["r2"]
93 r3 = tgen.gears["r3"]
94
95 def _bgp_converge(router):
96 output = json.loads(router.vtysh_cmd("show ip bgp json"))
97 expected = {
98 "routes": {
99 "172.16.1.1/32": [{"nexthops": [{"ip": "192.168.2.2", "used": True}]}],
100 "172.16.1.2/32": [{"nexthops": [{"ip": "192.168.2.2", "used": True}]}],
101 }
102 }
103 return topotest.json_cmp(output, expected)
104
105 step("Check if we can see 172.16.1.1/32 after initial converge in R3")
106 test_func = functools.partial(_bgp_converge, r3)
107 _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
108 assert result is None, "Cannot see 172.16.1.1/32 in r3"
109
110 def _bgp_weight_prefered_route(router):
111 output = json.loads(router.vtysh_cmd("show ip bgp 172.16.1.1/32 json"))
112 expected = {
113 "paths": [
114 {
115 "bestpath": {"selectionReason": "Weight"},
116 "nexthops": [
117 {
118 "ip": "192.168.1.1",
119 }
120 ],
121 }
122 ]
123 }
124 return topotest.json_cmp(output, expected)
125
126 step(
127 "Check if we can see 172.16.1.1/32 as best selected due to higher weigth in R2"
128 )
129 test_func = functools.partial(_bgp_weight_prefered_route, r2)
130 _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
131 assert (
132 result is None
133 ), "Prefix 172.16.1.1/32 is not selected as bests path due to weight"
134
135 step("Kill bgpd in R1")
136 kill_router_daemons(tgen, "r1", ["bgpd"])
137
138 def _bgp_stale_route(router, prefix):
139 output = json.loads(router.vtysh_cmd("show ip bgp {} json".format(prefix)))
140 expected = {"paths": [{"community": {"string": "llgr-stale"}, "stale": True}]}
141 return topotest.json_cmp(output, expected)
142
143 step("Check if we can see 172.16.1.1/32 as stale in R2")
144 test_func = functools.partial(_bgp_stale_route, r2, "172.16.1.1/32")
145 _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
146 assert result is None, "Prefix 172.16.1.1/32 is not stale"
147
148 def _bgp_llgr_depreference_route(router):
149 output = json.loads(router.vtysh_cmd("show ip bgp 172.16.1.1/32 json"))
150 expected = {
151 "paths": [
152 {
153 "bestpath": {"selectionReason": "First path received"},
154 "nexthops": [
155 {
156 "ip": "192.168.0.1",
157 }
158 ],
159 }
160 ]
161 }
162 return topotest.json_cmp(output, expected)
163
164 step("Check if we can see 172.16.1.1/32 depreferenced due to LLGR_STALE in R2")
165 test_func = functools.partial(_bgp_llgr_depreference_route, r2)
166 _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
167 assert result is None, "Prefix 172.16.1.1/32 is not depreferenced due to LLGR_STALE"
168
169 step("Check if we can see 172.16.1.1/32 after R1 was killed in R3")
170 test_func = functools.partial(_bgp_converge, r3)
171 _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
172 assert result is None, "Cannot see 172.16.1.1/32 in r3"
173
174 step("Kill bgpd in R4 (dynamic peer)")
175 kill_router_daemons(tgen, "r4", ["bgpd"])
176
177 step("Check if we can see 172.16.1.2/32 after R4 (dynamic peer) was killed")
178 test_func = functools.partial(_bgp_stale_route, r2, "172.16.1.2/32")
179 _, result = topotest.run_and_expect(test_func, None, count=120, wait=0.5)
180 assert result is None, "Cannot see 172.16.1.2/32 in r2"
181
182
183 if __name__ == "__main__":
184 args = ["-s"] + sys.argv[1:]
185 sys.exit(pytest.main(args))