]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_comm_list_match/test_bgp_comm_list_match.py
*: auto-convert to SPDX License IDs
[mirror_frr.git] / tests / topotests / bgp_comm_list_match / test_bgp_comm_list_match.py
1 #!/usr/bin/env python
2 # SPDX-License-Identifier: ISC
3
4 #
5 # Copyright (c) 2022 by
6 # Donatas Abraitis <donatas@opensourcerouting.org>
7 #
8
9 """
10 Check if BGP community-list works as OR if multiple community entries specified,
11 like:
12
13 bgp community-list 1 seq 5 permit 65001:1 65002:2
14 bgp community-list 1 seq 10 permit 65001:3
15 !
16 route-map test deny 10
17 match community 1
18 route-map test permit 20
19
20 Here, we should deny routes in/out if the path has:
21 (65001:1 AND 65001:2) OR 65001:3.
22 """
23
24 import os
25 import sys
26 import json
27 import pytest
28 import functools
29
30 CWD = os.path.dirname(os.path.realpath(__file__))
31 sys.path.append(os.path.join(CWD, "../"))
32
33 # pylint: disable=C0413
34 from lib import topotest
35 from lib.topogen import Topogen, TopoRouter, get_topogen
36 from lib.common_config import step
37
38 pytestmark = [pytest.mark.bgpd]
39
40
41 def build_topo(tgen):
42 for routern in range(1, 3):
43 tgen.add_router("r{}".format(routern))
44
45 switch = tgen.add_switch("s1")
46 switch.add_link(tgen.gears["r1"])
47 switch.add_link(tgen.gears["r2"])
48
49
50 def setup_module(mod):
51 tgen = Topogen(build_topo, mod.__name__)
52 tgen.start_topology()
53
54 router_list = tgen.routers()
55
56 for i, (rname, router) in enumerate(router_list.items(), 1):
57 router.load_config(
58 TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname))
59 )
60 router.load_config(
61 TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format(rname))
62 )
63
64 tgen.start_router()
65
66
67 def teardown_module(mod):
68 tgen = get_topogen()
69 tgen.stop_topology()
70
71
72 def test_bgp_comm_list_match():
73 tgen = get_topogen()
74
75 if tgen.routers_have_failure():
76 pytest.skip(tgen.errors)
77
78 router = tgen.gears["r2"]
79
80 def _bgp_converge():
81 output = json.loads(
82 router.vtysh_cmd(
83 "show bgp ipv4 unicast neighbors 192.168.0.1 filtered-routes json"
84 )
85 )
86 expected = {
87 "receivedRoutes": {
88 "172.16.255.1/32": {
89 "path": "65001",
90 },
91 "172.16.255.3/32": {
92 "path": "65001",
93 },
94 }
95 }
96 return topotest.json_cmp(output, expected)
97
98 step("Initial BGP converge")
99 test_func = functools.partial(_bgp_converge)
100 _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
101 assert result is None, "Failed to filter BGP UPDATES with community-list on R2"
102
103
104 if __name__ == "__main__":
105 args = ["-s"] + sys.argv[1:]
106 sys.exit(pytest.main(args))