]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_path_attribute_treat_as_withdraw/test_bgp_path_attribute_treat_as_withdraw.py
tests: update tests using 'show bgp json detail'
[mirror_frr.git] / tests / topotests / bgp_path_attribute_treat_as_withdraw / test_bgp_path_attribute_treat_as_withdraw.py
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 Test if `neighbor path-attribute treat-as-withdraw` command works correctly,
24 can withdraw unwanted prefixes from BGP table.
25 """
26
27 import os
28 import sys
29 import json
30 import pytest
31 import functools
32
33 CWD = os.path.dirname(os.path.realpath(__file__))
34 sys.path.append(os.path.join(CWD, "../"))
35
36 # pylint: disable=C0413
37 from lib import topotest
38 from lib.topogen import Topogen, TopoRouter, get_topogen
39 from lib.common_config import step
40
41 pytestmark = [pytest.mark.bgpd]
42
43
44 def build_topo(tgen):
45 r1 = tgen.add_router("r1")
46 r2 = tgen.add_router("r2")
47
48 switch = tgen.add_switch("s1")
49 switch.add_link(r1)
50 switch.add_link(r2)
51
52
53 def setup_module(mod):
54 tgen = Topogen(build_topo, mod.__name__)
55 tgen.start_topology()
56
57 r1 = tgen.gears["r1"]
58 r1.load_config(TopoRouter.RD_ZEBRA, os.path.join(CWD, "r1/zebra.conf"))
59 r1.load_config(TopoRouter.RD_BGP, os.path.join(CWD, "r1/bgpd.conf"))
60 r1.start()
61
62 r2 = tgen.gears["r2"]
63 r2.load_config(TopoRouter.RD_ZEBRA, os.path.join(CWD, "r2/zebra.conf"))
64 r2.load_config(TopoRouter.RD_BGP, os.path.join(CWD, "r2/bgpd.conf"))
65 r2.start()
66
67
68 def teardown_module(mod):
69 tgen = get_topogen()
70 tgen.stop_topology()
71
72
73 def test_bgp_path_attribute_treat_as_withdraw():
74 tgen = get_topogen()
75
76 if tgen.routers_have_failure():
77 pytest.skip(tgen.errors)
78
79 r2 = tgen.gears["r2"]
80
81 def _bgp_converge():
82 output = json.loads(r2.vtysh_cmd("show bgp ipv4 unicast json detail"))
83 expected = {
84 "routes": {
85 "10.10.10.10/32": {
86 "paths": [
87 {
88 "valid": True,
89 "atomicAggregate": True,
90 }
91 ],
92 },
93 "10.10.10.20/32": {
94 "paths": [
95 {
96 "valid": True,
97 }
98 ],
99 },
100 }
101 }
102 return topotest.json_cmp(output, expected)
103
104 test_func = functools.partial(_bgp_converge)
105 _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
106 assert result is None, "Failed bgp convergence"
107
108 step("Withdraw prefixes with atomic-aggregate from r1")
109 r2.vtysh_cmd(
110 """
111 configure terminal
112 router bgp
113 neighbor 10.0.0.1 path-attribute treat-as-withdraw 6
114 """
115 )
116
117 def _bgp_check_if_route_withdrawn():
118 output = json.loads(r2.vtysh_cmd("show bgp ipv4 unicast json detail"))
119 expected = {
120 "routes": {
121 "10.10.10.10/32": None,
122 "10.10.10.20/32": {
123 "paths": [
124 {
125 "valid": True,
126 }
127 ],
128 },
129 }
130 }
131 return topotest.json_cmp(output, expected)
132
133 test_func = functools.partial(_bgp_check_if_route_withdrawn)
134 _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
135 assert result is None, "Failed to withdraw prefixes with atomic-aggregate attribute"
136
137
138 def test_memory_leak():
139 "Run the memory leak test and report results."
140 tgen = get_topogen()
141 if not tgen.is_memleak_enabled():
142 pytest.skip("Memory leak test/report is disabled")
143
144 tgen.report_memory_leaks()
145
146
147 if __name__ == "__main__":
148 args = ["-s"] + sys.argv[1:]
149 sys.exit(pytest.main(args))