]>
Commit | Line | Data |
---|---|---|
1b484abc DA |
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 | Check if BGP community-list works as OR if multiple community entries specified, | |
24 | like: | |
25 | ||
26 | bgp community-list 1 seq 5 permit 65001:1 65002:2 | |
27 | bgp community-list 1 seq 10 permit 65001:3 | |
28 | ! | |
29 | route-map test deny 10 | |
30 | match community 1 | |
31 | route-map test permit 20 | |
32 | ||
33 | Here, we should deny routes in/out if the path has: | |
34 | (65001:1 AND 65001:2) OR 65001:3. | |
35 | """ | |
36 | ||
37 | import os | |
38 | import sys | |
39 | import json | |
40 | import pytest | |
41 | import functools | |
42 | ||
43 | CWD = os.path.dirname(os.path.realpath(__file__)) | |
44 | sys.path.append(os.path.join(CWD, "../")) | |
45 | ||
46 | # pylint: disable=C0413 | |
47 | from lib import topotest | |
48 | from lib.topogen import Topogen, TopoRouter, get_topogen | |
49 | from lib.common_config import step | |
50 | ||
51 | pytestmark = [pytest.mark.bgpd] | |
52 | ||
53 | ||
54 | def build_topo(tgen): | |
55 | for routern in range(1, 3): | |
56 | tgen.add_router("r{}".format(routern)) | |
57 | ||
58 | switch = tgen.add_switch("s1") | |
59 | switch.add_link(tgen.gears["r1"]) | |
60 | switch.add_link(tgen.gears["r2"]) | |
61 | ||
62 | ||
63 | def setup_module(mod): | |
64 | tgen = Topogen(build_topo, mod.__name__) | |
65 | tgen.start_topology() | |
66 | ||
67 | router_list = tgen.routers() | |
68 | ||
69 | for i, (rname, router) in enumerate(router_list.items(), 1): | |
70 | router.load_config( | |
71 | TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname)) | |
72 | ) | |
73 | router.load_config( | |
74 | TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format(rname)) | |
75 | ) | |
76 | ||
77 | tgen.start_router() | |
78 | ||
79 | ||
80 | def teardown_module(mod): | |
81 | tgen = get_topogen() | |
82 | tgen.stop_topology() | |
83 | ||
84 | ||
85 | def test_bgp_comm_list_match(): | |
86 | tgen = get_topogen() | |
87 | ||
88 | if tgen.routers_have_failure(): | |
89 | pytest.skip(tgen.errors) | |
90 | ||
91 | router = tgen.gears["r2"] | |
92 | ||
93 | def _bgp_converge(): | |
94 | output = json.loads( | |
95 | router.vtysh_cmd( | |
96 | "show bgp ipv4 unicast neighbors 192.168.0.1 filtered-routes json" | |
97 | ) | |
98 | ) | |
99 | expected = { | |
100 | "receivedRoutes": { | |
101 | "172.16.255.1/32": { | |
102 | "path": "65001", | |
103 | }, | |
104 | "172.16.255.3/32": { | |
105 | "path": "65001", | |
106 | }, | |
107 | } | |
108 | } | |
109 | return topotest.json_cmp(output, expected) | |
110 | ||
111 | step("Initial BGP converge") | |
112 | test_func = functools.partial(_bgp_converge) | |
113 | _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5) | |
114 | assert result is None, "Failed to filter BGP UPDATES with community-list on R2" | |
115 | ||
116 | ||
117 | if __name__ == "__main__": | |
118 | args = ["-s"] + sys.argv[1:] | |
119 | sys.exit(pytest.main(args)) |