]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_conditional_advertisement_track_peer/test_bgp_conditional_advertisement_track_peer.py
Merge pull request #12191 from manojvn/463777
[mirror_frr.git] / tests / topotests / bgp_conditional_advertisement_track_peer / test_bgp_conditional_advertisement_track_peer.py
1 #!/usr/bin/env python
2
3 #
4 # Copyright (c) 2022 by
5 # Donatas Abraitis <donatas@opensourcerouting.org>
6 #
7 # Permission to use, copy, modify, and/or distribute this software
8 # for any purpose with or without fee is hereby granted, provided
9 # that the above copyright notice and this permission notice appear
10 # in all copies.
11 #
12 # THE SOFTWARE IS PROVIDED "AS IS" AND NETDEF DISCLAIMS ALL WARRANTIES
13 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETDEF BE LIABLE FOR
15 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
16 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
17 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
18 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
19 # OF THIS SOFTWARE.
20 #
21
22 """
23 Conditionally advertise 172.16.255.2/32 to r1, only if 172.16.255.3/32
24 is received from r3.
25
26 Also, withdraw if 172.16.255.3/32 disappears.
27 """
28
29 import os
30 import sys
31 import json
32 import pytest
33 import functools
34
35 CWD = os.path.dirname(os.path.realpath(__file__))
36 sys.path.append(os.path.join(CWD, "../"))
37
38 # pylint: disable=C0413
39 from lib import topotest
40 from lib.topogen import Topogen, TopoRouter, get_topogen
41 from lib.common_config import (
42 step,
43 )
44
45 pytestmark = [pytest.mark.bgpd]
46
47
48 def build_topo(tgen):
49 for routern in range(1, 5):
50 tgen.add_router("r{}".format(routern))
51
52 switch = tgen.add_switch("s1")
53 switch.add_link(tgen.gears["r1"])
54 switch.add_link(tgen.gears["r2"])
55
56 switch = tgen.add_switch("s2")
57 switch.add_link(tgen.gears["r2"])
58 switch.add_link(tgen.gears["r3"])
59
60
61 def setup_module(mod):
62 tgen = Topogen(build_topo, mod.__name__)
63 tgen.start_topology()
64
65 router_list = tgen.routers()
66
67 for i, (rname, router) in enumerate(router_list.items(), 1):
68 router.load_config(
69 TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname))
70 )
71 router.load_config(
72 TopoRouter.RD_STATIC, os.path.join(CWD, "{}/staticd.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_conditional_advertisement_track_peer():
87 tgen = get_topogen()
88
89 r1 = tgen.gears["r1"]
90 r2 = tgen.gears["r2"]
91 r3 = tgen.gears["r3"]
92
93 if tgen.routers_have_failure():
94 pytest.skip(tgen.errors)
95
96 def _bgp_converge():
97 output = json.loads(
98 r2.vtysh_cmd(
99 "show bgp ipv4 unicast neighbors 192.168.1.1 advertised-routes json"
100 )
101 )
102 expected = {
103 "advertisedRoutes": {"172.16.255.2/32": None},
104 "totalPrefixCounter": 0,
105 "filteredPrefixCounter": 0,
106 }
107 return topotest.json_cmp(output, expected)
108
109 # Verify if R2 does not send any routes to R1
110 test_func = functools.partial(_bgp_converge)
111 _, result = topotest.run_and_expect(test_func, None, count=30, wait=0.5)
112 assert result is None, "R2 SHOULD not send any routes to R1"
113
114 step("Enable session between R2 and R3")
115 r3.vtysh_cmd(
116 """
117 configure terminal
118 router bgp
119 no neighbor 192.168.2.2 shutdown
120 """
121 )
122
123 def _bgp_check_conditional_static_routes_from_r2():
124 output = json.loads(r1.vtysh_cmd("show bgp ipv4 unicast json"))
125 expected = {
126 "routes": {
127 "172.16.255.2/32": [{"valid": True, "nexthops": [{"hostname": "r2"}]}]
128 }
129 }
130 return topotest.json_cmp(output, expected)
131
132 # Verify if R1 received 172.16.255.2/32 from R2
133 test_func = functools.partial(_bgp_check_conditional_static_routes_from_r2)
134 _, result = topotest.run_and_expect(test_func, None, count=30, wait=0.5)
135 assert result is None, "R1 SHOULD receive 172.16.255.2/32 from R2"
136
137 step("Disable session between R2 and R3 again")
138 r3.vtysh_cmd(
139 """
140 configure terminal
141 router bgp
142 neighbor 192.168.2.2 shutdown
143 """
144 )
145
146 # Verify if R2 is not sending any routes to R1 again
147 test_func = functools.partial(_bgp_converge)
148 _, result = topotest.run_and_expect(test_func, None, count=30, wait=0.5)
149 assert result is None, "R2 SHOULD not send any routes to R1"
150
151
152 if __name__ == "__main__":
153 args = ["-s"] + sys.argv[1:]
154 sys.exit(pytest.main(args))