]>
Commit | Line | Data |
---|---|---|
760aee9a | 1 | #!/usr/bin/env python |
acddc0ed | 2 | # SPDX-License-Identifier: ISC |
760aee9a DA |
3 | |
4 | # | |
5 | # Copyright (c) 2022 by | |
6 | # Donatas Abraitis <donatas@opensourcerouting.org> | |
7 | # | |
760aee9a DA |
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": { | |
04705e48 TA |
71 | "192.168.100.101/32": { |
72 | "paths": [ | |
73 | { | |
74 | "valid": True, | |
75 | "atomicAggregate": True, | |
76 | "community": { | |
77 | "string": "65001:101", | |
78 | }, | |
79 | } | |
80 | ], | |
81 | }, | |
82 | "192.168.100.102/32": { | |
83 | "paths": [ | |
84 | { | |
85 | "valid": True, | |
86 | "originatorId": "10.0.0.2", | |
87 | "community": { | |
88 | "string": "65001:102", | |
89 | }, | |
90 | } | |
91 | ], | |
92 | }, | |
760aee9a DA |
93 | } |
94 | } | |
95 | return topotest.json_cmp(output, expected) | |
96 | ||
97 | test_func = functools.partial(_bgp_converge) | |
98 | _, result = topotest.run_and_expect(test_func, None, count=30, wait=0.5) | |
99 | assert result is None, "Failed bgp convergence" | |
100 | ||
101 | step("Discard atomic-aggregate, community, and originator-id attributes from peer1") | |
102 | r1.vtysh_cmd( | |
103 | """ | |
104 | configure terminal | |
105 | router bgp | |
106 | neighbor 10.0.0.2 path-attribute discard 6 8 9 | |
107 | """ | |
108 | ) | |
109 | ||
110 | def _bgp_check_if_attributes_discarded(): | |
111 | output = json.loads(r1.vtysh_cmd("show bgp ipv4 unicast json detail")) | |
112 | expected = { | |
113 | "routes": { | |
04705e48 TA |
114 | "192.168.100.101/32": { |
115 | "paths": [ | |
116 | { | |
117 | "valid": True, | |
118 | "atomicAggregate": None, | |
119 | "community": None, | |
120 | } | |
121 | ], | |
122 | }, | |
123 | "192.168.100.102/32": { | |
124 | "paths": [ | |
125 | { | |
126 | "valid": True, | |
127 | "originatorId": None, | |
128 | "community": None, | |
129 | } | |
130 | ], | |
131 | }, | |
760aee9a DA |
132 | } |
133 | } | |
134 | return topotest.json_cmp(output, expected) | |
135 | ||
136 | test_func = functools.partial(_bgp_check_if_attributes_discarded) | |
137 | _, result = topotest.run_and_expect(test_func, None, count=30, wait=0.5) | |
138 | assert ( | |
139 | result is None | |
140 | ), "Failed to discard path attributes (atomic-aggregate, community, and originator-id)" | |
141 | ||
142 | ||
143 | def test_memory_leak(): | |
144 | "Run the memory leak test and report results." | |
145 | tgen = get_topogen() | |
146 | if not tgen.is_memleak_enabled(): | |
147 | pytest.skip("Memory leak test/report is disabled") | |
148 | ||
149 | tgen.report_memory_leaks() | |
150 | ||
151 | ||
152 | if __name__ == "__main__": | |
153 | args = ["-s"] + sys.argv[1:] | |
154 | sys.exit(pytest.main(args)) |