]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_orf/test_bgp_orf.py
doc: Add `show ipv6 rpf X:X::X:X` command to docs
[mirror_frr.git] / tests / topotests / bgp_orf / test_bgp_orf.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2022 by
4 # Donatas Abraitis <donatas@opensourcerouting.org>
5 #
6 # Permission to use, copy, modify, and/or distribute this software
7 # for any purpose with or without fee is hereby granted, provided
8 # that the above copyright notice and this permission notice appear
9 # in all copies.
10 #
11 # THE SOFTWARE IS PROVIDED "AS IS" AND NETDEF DISCLAIMS ALL WARRANTIES
12 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETDEF BE LIABLE FOR
14 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
15 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
16 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
17 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
18 # OF THIS SOFTWARE.
19 #
20
21 """
22 Test if BGP ORF filtering is working correctly when modifying
23 prefix-list.
24
25 Initially advertise 10.10.10.1/32 from R1 to R2. Add new prefix
26 10.10.10.2/32 to r1 prefix list on R2. Test if we updated ORF
27 prefix-list correctly.
28 """
29
30 import os
31 import sys
32 import json
33 import pytest
34 import functools
35
36 pytestmark = pytest.mark.bgpd
37
38 CWD = os.path.dirname(os.path.realpath(__file__))
39 sys.path.append(os.path.join(CWD, "../"))
40
41 # pylint: disable=C0413
42 from lib import topotest
43 from lib.topogen import Topogen, TopoRouter, get_topogen
44
45 pytestmark = [pytest.mark.bgpd]
46
47
48 def setup_module(mod):
49 topodef = {"s1": ("r1", "r2")}
50 tgen = Topogen(topodef, mod.__name__)
51 tgen.start_topology()
52
53 router_list = tgen.routers()
54
55 for i, (rname, router) in enumerate(router_list.items(), 1):
56 router.load_config(
57 TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname))
58 )
59 router.load_config(
60 TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format(rname))
61 )
62
63 tgen.start_router()
64
65
66 def teardown_module(mod):
67 tgen = get_topogen()
68 tgen.stop_topology()
69
70
71 def test_bgp_orf():
72 tgen = get_topogen()
73
74 if tgen.routers_have_failure():
75 pytest.skip(tgen.errors)
76
77 r1 = tgen.gears["r1"]
78 r2 = tgen.gears["r2"]
79
80 def _bgp_converge_r1():
81 output = json.loads(
82 r1.vtysh_cmd(
83 "show bgp ipv4 unicast neighbor 192.168.1.2 advertised-routes json"
84 )
85 )
86 expected = {"advertisedRoutes": {"10.10.10.1/32": {}, "10.10.10.2/32": None}}
87 return topotest.json_cmp(output, expected)
88
89 test_func = functools.partial(_bgp_converge_r1)
90 _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
91 assert result is None, "Can't apply ORF from R1 to R2"
92
93 def _bgp_converge_r2():
94 output = json.loads(r2.vtysh_cmd("show bgp ipv4 unicast summary json"))
95 expected = {
96 "peers": {
97 "192.168.1.1": {
98 "pfxRcd": 1,
99 "pfxSnt": 1,
100 "state": "Established",
101 "peerState": "OK",
102 }
103 }
104 }
105 return topotest.json_cmp(output, expected)
106
107 test_func = functools.partial(_bgp_converge_r2)
108 _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
109 assert result is None, "ORF filtering is not working from R1 to R2"
110
111 r2.vtysh_cmd(
112 """
113 configure terminal
114 ip prefix-list r1 seq 10 permit 10.10.10.2/32
115 """
116 )
117
118 def _bgp_orf_changed_r1():
119 output = json.loads(
120 r1.vtysh_cmd(
121 "show bgp ipv4 unicast neighbor 192.168.1.2 advertised-routes json"
122 )
123 )
124 expected = {"advertisedRoutes": {"10.10.10.1/32": {}, "10.10.10.2/32": {}}}
125 return topotest.json_cmp(output, expected)
126
127 test_func = functools.partial(_bgp_orf_changed_r1)
128 _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
129 assert result is None, "Can't apply new ORF from R1 to R2"
130
131 def _bgp_orf_changed_r2():
132 output = json.loads(r2.vtysh_cmd("show bgp ipv4 unicast json"))
133 expected = {
134 "routes": {
135 "10.10.10.1/32": [{"valid": True}],
136 "10.10.10.2/32": [{"valid": True}],
137 }
138 }
139 return topotest.json_cmp(output, expected)
140
141 test_func = functools.partial(_bgp_orf_changed_r2)
142 _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
143 assert result is None, "New ORF filtering is not working from R1 to R2"
144
145 r2.vtysh_cmd(
146 """
147 configure terminal
148 no ip prefix-list r1 seq 10 permit 10.10.10.2/32
149 """
150 )
151
152 test_func = functools.partial(_bgp_converge_r1)
153 _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
154 assert result is None, "Can't apply initial ORF from R1 to R2"
155
156
157 if __name__ == "__main__":
158 args = ["-s"] + sys.argv[1:]
159 sys.exit(pytest.main(args))