]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_node_target_extcommunities/test_bgp_node_target_extcommunities.py
Merge pull request #13455 from sri-mohan1/srib-ldpd
[mirror_frr.git] / tests / topotests / bgp_node_target_extcommunities / test_bgp_node_target_extcommunities.py
1 #!/usr/bin/env python
2 # SPDX-License-Identifier: ISC
3
4 # Copyright (c) 2023 by
5 # Donatas Abraitis <donatas@opensourcerouting.org>
6 #
7
8 """
9 Test if Node Target Extended Communities works.
10
11 At r1 we set NT to 192.168.1.3 and 192.168.1.4 (this is the R3/R4 router-id),
12 and that means 10.10.10.10/32 MUST be installed on R3 and R4, but not on R2,
13 because this route does not have NT:192.168.1.2.
14 """
15
16 import os
17 import sys
18 import json
19 import pytest
20 import functools
21
22 CWD = os.path.dirname(os.path.realpath(__file__))
23 sys.path.append(os.path.join(CWD, "../"))
24
25 # pylint: disable=C0413
26 from lib import topotest
27 from lib.topogen import Topogen, TopoRouter, get_topogen
28
29 pytestmark = [pytest.mark.bgpd]
30
31
32 def build_topo(tgen):
33 for routern in range(1, 5):
34 tgen.add_router("r{}".format(routern))
35
36 switch = tgen.add_switch("s1")
37 switch.add_link(tgen.gears["r1"])
38 switch.add_link(tgen.gears["r2"])
39 switch.add_link(tgen.gears["r3"])
40 switch.add_link(tgen.gears["r4"])
41
42
43 def setup_module(mod):
44 tgen = Topogen(build_topo, mod.__name__)
45 tgen.start_topology()
46
47 router_list = tgen.routers()
48
49 for i, (rname, router) in enumerate(router_list.items(), 1):
50 router.load_frr_config(os.path.join(CWD, "{}/frr.conf".format(rname)))
51
52 tgen.start_router()
53
54
55 def teardown_module(mod):
56 tgen = get_topogen()
57 tgen.stop_topology()
58
59
60 def test_bgp_node_target_extended_communities():
61 tgen = get_topogen()
62
63 if tgen.routers_have_failure():
64 pytest.skip(tgen.errors)
65
66 r1 = tgen.gears["r1"]
67 r2 = tgen.gears["r2"]
68 r3 = tgen.gears["r3"]
69 r4 = tgen.gears["r4"]
70
71 def _bgp_converge():
72 output = json.loads(r1.vtysh_cmd("show bgp summary json"))
73 expected = {
74 "ipv4Unicast": {
75 "peers": {
76 "192.168.1.2": {
77 "pfxSnt": 1,
78 "state": "Established",
79 },
80 "192.168.1.3": {
81 "pfxSnt": 1,
82 "state": "Established",
83 },
84 "192.168.1.4": {
85 "pfxSnt": 1,
86 "state": "Established",
87 },
88 }
89 }
90 }
91 return topotest.json_cmp(output, expected)
92
93 test_func = functools.partial(_bgp_converge)
94 _, result = topotest.run_and_expect(test_func, None, count=30, wait=1)
95 assert result is None, "Failed announcing 10.10.10.10/32 to r2, r3, and r4"
96
97 def _bgp_check_route(router, exists):
98 output = json.loads(router.vtysh_cmd("show bgp ipv4 unicast json"))
99 if exists:
100 expected = {
101 "routes": {
102 "10.10.10.10/32": [
103 {
104 "valid": True,
105 }
106 ]
107 }
108 }
109 else:
110 expected = {
111 "routes": {
112 "10.10.10.10/32": None,
113 }
114 }
115 return topotest.json_cmp(output, expected)
116
117 test_func = functools.partial(_bgp_check_route, r3, True)
118 _, result = topotest.run_and_expect(test_func, None, count=30, wait=1)
119 assert result is None, "10.10.10.10/32 is not installed, but SHOULD be"
120
121 test_func = functools.partial(_bgp_check_route, r4, True)
122 _, result = topotest.run_and_expect(test_func, None, count=30, wait=1)
123 assert result is None, "10.10.10.10/32 is not installed, but SHOULD be"
124
125 test_func = functools.partial(_bgp_check_route, r2, False)
126 _, result = topotest.run_and_expect(test_func, None, count=30, wait=1)
127 assert result is None, "10.10.10.10/32 is installed, but SHOULD NOT be"
128
129
130 if __name__ == "__main__":
131 args = ["-s"] + sys.argv[1:]
132 sys.exit(pytest.main(args))