]>
Commit | Line | Data |
---|---|---|
bdf51d2a | 1 | #!/usr/bin/env python |
acddc0ed | 2 | # SPDX-License-Identifier: ISC |
bdf51d2a DA |
3 | |
4 | # Copyright (c) 2022 by | |
5 | # Donatas Abraitis <donatas@opensourcerouting.org> | |
6 | # | |
bdf51d2a DA |
7 | |
8 | """ | |
9 | Test if BGP ORF filtering is working correctly when modifying | |
10 | prefix-list. | |
11 | ||
12 | Initially advertise 10.10.10.1/32 from R1 to R2. Add new prefix | |
13 | 10.10.10.2/32 to r1 prefix list on R2. Test if we updated ORF | |
14 | prefix-list correctly. | |
15 | """ | |
16 | ||
17 | import os | |
18 | import sys | |
19 | import json | |
20 | import pytest | |
21 | import functools | |
22 | ||
23 | pytestmark = pytest.mark.bgpd | |
24 | ||
25 | CWD = os.path.dirname(os.path.realpath(__file__)) | |
26 | sys.path.append(os.path.join(CWD, "../")) | |
27 | ||
28 | # pylint: disable=C0413 | |
29 | from lib import topotest | |
30 | from lib.topogen import Topogen, TopoRouter, get_topogen | |
31 | ||
32 | pytestmark = [pytest.mark.bgpd] | |
33 | ||
34 | ||
35 | def setup_module(mod): | |
36 | topodef = {"s1": ("r1", "r2")} | |
37 | tgen = Topogen(topodef, mod.__name__) | |
38 | tgen.start_topology() | |
39 | ||
40 | router_list = tgen.routers() | |
41 | ||
42 | for i, (rname, router) in enumerate(router_list.items(), 1): | |
43 | router.load_config( | |
44 | TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname)) | |
45 | ) | |
46 | router.load_config( | |
47 | TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format(rname)) | |
48 | ) | |
49 | ||
50 | tgen.start_router() | |
51 | ||
52 | ||
53 | def teardown_module(mod): | |
54 | tgen = get_topogen() | |
55 | tgen.stop_topology() | |
56 | ||
57 | ||
58 | def test_bgp_orf(): | |
59 | tgen = get_topogen() | |
60 | ||
61 | if tgen.routers_have_failure(): | |
62 | pytest.skip(tgen.errors) | |
63 | ||
64 | r1 = tgen.gears["r1"] | |
65 | r2 = tgen.gears["r2"] | |
66 | ||
67 | def _bgp_converge_r1(): | |
68 | output = json.loads( | |
69 | r1.vtysh_cmd( | |
70 | "show bgp ipv4 unicast neighbor 192.168.1.2 advertised-routes json" | |
71 | ) | |
72 | ) | |
73 | expected = {"advertisedRoutes": {"10.10.10.1/32": {}, "10.10.10.2/32": None}} | |
74 | return topotest.json_cmp(output, expected) | |
75 | ||
76 | test_func = functools.partial(_bgp_converge_r1) | |
77 | _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5) | |
78 | assert result is None, "Can't apply ORF from R1 to R2" | |
79 | ||
80 | def _bgp_converge_r2(): | |
81 | output = json.loads(r2.vtysh_cmd("show bgp ipv4 unicast summary json")) | |
82 | expected = { | |
83 | "peers": { | |
84 | "192.168.1.1": { | |
85 | "pfxRcd": 1, | |
86 | "pfxSnt": 1, | |
87 | "state": "Established", | |
88 | "peerState": "OK", | |
89 | } | |
90 | } | |
91 | } | |
92 | return topotest.json_cmp(output, expected) | |
93 | ||
94 | test_func = functools.partial(_bgp_converge_r2) | |
95 | _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5) | |
96 | assert result is None, "ORF filtering is not working from R1 to R2" | |
97 | ||
98 | r2.vtysh_cmd( | |
99 | """ | |
100 | configure terminal | |
101 | ip prefix-list r1 seq 10 permit 10.10.10.2/32 | |
102 | """ | |
103 | ) | |
104 | ||
105 | def _bgp_orf_changed_r1(): | |
106 | output = json.loads( | |
107 | r1.vtysh_cmd( | |
108 | "show bgp ipv4 unicast neighbor 192.168.1.2 advertised-routes json" | |
109 | ) | |
110 | ) | |
111 | expected = {"advertisedRoutes": {"10.10.10.1/32": {}, "10.10.10.2/32": {}}} | |
112 | return topotest.json_cmp(output, expected) | |
113 | ||
114 | test_func = functools.partial(_bgp_orf_changed_r1) | |
115 | _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5) | |
116 | assert result is None, "Can't apply new ORF from R1 to R2" | |
117 | ||
118 | def _bgp_orf_changed_r2(): | |
119 | output = json.loads(r2.vtysh_cmd("show bgp ipv4 unicast json")) | |
120 | expected = { | |
121 | "routes": { | |
122 | "10.10.10.1/32": [{"valid": True}], | |
123 | "10.10.10.2/32": [{"valid": True}], | |
124 | } | |
125 | } | |
126 | return topotest.json_cmp(output, expected) | |
127 | ||
128 | test_func = functools.partial(_bgp_orf_changed_r2) | |
129 | _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5) | |
130 | assert result is None, "New ORF filtering is not working from R1 to R2" | |
131 | ||
132 | r2.vtysh_cmd( | |
133 | """ | |
134 | configure terminal | |
135 | no ip prefix-list r1 seq 10 permit 10.10.10.2/32 | |
136 | """ | |
137 | ) | |
138 | ||
139 | test_func = functools.partial(_bgp_converge_r1) | |
140 | _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5) | |
141 | assert result is None, "Can't apply initial ORF from R1 to R2" | |
142 | ||
143 | ||
144 | if __name__ == "__main__": | |
145 | args = ["-s"] + sys.argv[1:] | |
146 | sys.exit(pytest.main(args)) |