]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_path_attribute_discard/test_bgp_path_attribute_discard.py
*: auto-convert to SPDX License IDs
[mirror_frr.git] / tests / topotests / bgp_path_attribute_discard / test_bgp_path_attribute_discard.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 Test if `neighbor path-attribute discard` command works correctly,
11 can discard unwanted attributes from UPDATE messages, and ignore them
12 by continuing to process UPDATE messages.
13 """
14
15 import os
16 import sys
17 import json
18 import pytest
19 import functools
20
21 CWD = os.path.dirname(os.path.realpath(__file__))
22 sys.path.append(os.path.join(CWD, "../"))
23
24 # pylint: disable=C0413
25 from lib import topotest
26 from lib.topogen import Topogen, TopoRouter, get_topogen
27 from lib.common_config import step
28
29 pytestmark = [pytest.mark.bgpd]
30
31
32 def build_topo(tgen):
33 r1 = tgen.add_router("r1")
34 peer1 = tgen.add_exabgp_peer("peer1", ip="10.0.0.2", defaultRoute="via 10.0.0.1")
35
36 switch = tgen.add_switch("s1")
37 switch.add_link(r1)
38 switch.add_link(peer1)
39
40
41 def setup_module(mod):
42 tgen = Topogen(build_topo, mod.__name__)
43 tgen.start_topology()
44
45 router = tgen.gears["r1"]
46 router.load_config(TopoRouter.RD_ZEBRA, os.path.join(CWD, "r1/zebra.conf"))
47 router.load_config(TopoRouter.RD_BGP, os.path.join(CWD, "r1/bgpd.conf"))
48 router.start()
49
50 peer = tgen.gears["peer1"]
51 peer.start(os.path.join(CWD, "peer1"), os.path.join(CWD, "exabgp.env"))
52
53
54 def teardown_module(mod):
55 tgen = get_topogen()
56 tgen.stop_topology()
57
58
59 def test_bgp_path_attribute_discard():
60 tgen = get_topogen()
61
62 if tgen.routers_have_failure():
63 pytest.skip(tgen.errors)
64
65 r1 = tgen.gears["r1"]
66
67 def _bgp_converge():
68 output = json.loads(r1.vtysh_cmd("show bgp ipv4 unicast json detail"))
69 expected = {
70 "routes": {
71 "192.168.100.101/32": [
72 {
73 "valid": True,
74 "atomicAggregate": True,
75 "community": {
76 "string": "65001:101",
77 },
78 }
79 ],
80 "192.168.100.102/32": [
81 {
82 "valid": True,
83 "originatorId": "10.0.0.2",
84 "community": {
85 "string": "65001:102",
86 },
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=0.5)
95 assert result is None, "Failed bgp convergence"
96
97 step("Discard atomic-aggregate, community, and originator-id attributes from peer1")
98 r1.vtysh_cmd(
99 """
100 configure terminal
101 router bgp
102 neighbor 10.0.0.2 path-attribute discard 6 8 9
103 """
104 )
105
106 def _bgp_check_if_attributes_discarded():
107 output = json.loads(r1.vtysh_cmd("show bgp ipv4 unicast json detail"))
108 expected = {
109 "routes": {
110 "192.168.100.101/32": [
111 {
112 "valid": True,
113 "atomicAggregate": None,
114 "community": None,
115 }
116 ],
117 "192.168.100.102/32": [
118 {
119 "valid": True,
120 "originatorId": None,
121 "community": None,
122 }
123 ],
124 }
125 }
126 return topotest.json_cmp(output, expected)
127
128 test_func = functools.partial(_bgp_check_if_attributes_discarded)
129 _, result = topotest.run_and_expect(test_func, None, count=30, wait=0.5)
130 assert (
131 result is None
132 ), "Failed to discard path attributes (atomic-aggregate, community, and originator-id)"
133
134
135 def test_memory_leak():
136 "Run the memory leak test and report results."
137 tgen = get_topogen()
138 if not tgen.is_memleak_enabled():
139 pytest.skip("Memory leak test/report is disabled")
140
141 tgen.report_memory_leaks()
142
143
144 if __name__ == "__main__":
145 args = ["-s"] + sys.argv[1:]
146 sys.exit(pytest.main(args))