]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_lu_topo1/test_bgp_lu.py
Merge pull request #9550 from donaldsharp/better_fails
[mirror_frr.git] / tests / topotests / bgp_lu_topo1 / test_bgp_lu.py
1 #!/usr/bin/env python
2
3 #
4 # test_bgp_lu.py
5 # Part of NetDEF Topology Tests
6 #
7 # Copyright (c) 2020 by Volta Networks
8 #
9 # Permission to use, copy, modify, and/or distribute this software
10 # for any purpose with or without fee is hereby granted, provided
11 # that the above copyright notice and this permission notice appear
12 # in all copies.
13 #
14 # THE SOFTWARE IS PROVIDED "AS IS" AND NETDEF DISCLAIMS ALL WARRANTIES
15 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETDEF BE LIABLE FOR
17 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
18 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
19 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
20 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 # OF THIS SOFTWARE.
22 #
23
24 """
25 test_bgp_lu.py: Test BGP LU label allocation
26 """
27
28 import os
29 import sys
30 import json
31 from functools import partial
32 import pytest
33
34 # Save the Current Working Directory to find configuration files.
35 CWD = os.path.dirname(os.path.realpath(__file__))
36 sys.path.append(os.path.join(CWD, "../"))
37
38 # pylint: disable=C0413
39 # Import topogen and topotest helpers
40 from lib import topotest
41 from lib.topogen import Topogen, TopoRouter, get_topogen
42
43 # Required to instantiate the topology builder class.
44
45
46 pytestmark = [pytest.mark.bgpd]
47
48
49 # Basic scenario for BGP-LU. Nodes are directly connected.
50 # Node 3 is advertising many routes to 2, which advertises them
51 # as BGP-LU to 1; this way we get routes with actual labels, as
52 # opposed to implicit-null routes in the 2-node case.
53 #
54 # AS1 BGP-LU AS2 iBGP AS2
55 # +-----+ +-----+ +-----+
56 # | |.1 .2| |.2 .3| |
57 # | 1 +----------------+ 2 +-----------------+ 3 |
58 # | | 10.0.0.0/24 | | 10.0.1.0/24 | |
59 # +-----+ +-----+ +-----+
60
61
62 def build_topo(tgen):
63 "Build function"
64
65 # This function only purpose is to define allocation and relationship
66 # between routers, switches and hosts.
67 #
68 #
69 # Create routers
70 tgen.add_router("R1")
71 tgen.add_router("R2")
72 tgen.add_router("R3")
73
74 # R1-R2
75 switch = tgen.add_switch("s1")
76 switch.add_link(tgen.gears["R1"])
77 switch.add_link(tgen.gears["R2"])
78
79 # R2-R3
80 switch = tgen.add_switch("s2")
81 switch.add_link(tgen.gears["R2"])
82 switch.add_link(tgen.gears["R3"])
83
84
85 def setup_module(mod):
86 "Sets up the pytest environment"
87 # This function initiates the topology build with Topogen...
88 tgen = Topogen(build_topo, mod.__name__)
89 # ... and here it calls Mininet initialization functions.
90 tgen.start_topology()
91
92 # This is a sample of configuration loading.
93 router_list = tgen.routers()
94
95 # For all registred routers, load the zebra configuration file
96 for rname, router in router_list.items():
97 router.load_config(
98 TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname))
99 )
100 router.load_config(
101 TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format(rname))
102 )
103
104 # After loading the configurations, this function loads configured daemons.
105 tgen.start_router()
106
107
108 def teardown_module(mod):
109 "Teardown the pytest environment"
110 tgen = get_topogen()
111
112 # This function tears down the whole topology.
113 tgen.stop_topology()
114
115
116 def check_labelpool(router):
117 json_file = "{}/{}/labelpool.summ.json".format(CWD, router.name)
118 expected = json.loads(open(json_file).read())
119
120 test_func = partial(
121 topotest.router_json_cmp, router, "show bgp labelpool summary json", expected
122 )
123 _, result = topotest.run_and_expect(test_func, None, count=20, wait=1)
124 assertmsg = '"{}" JSON output mismatches - Did not converge'.format(router.name)
125 assert result is None, assertmsg
126
127
128 def test_converge_bgplu():
129 "Wait for protocol convergence"
130
131 tgen = get_topogen()
132 # Don't run this test if we have any failure.
133 if tgen.routers_have_failure():
134 pytest.skip(tgen.errors)
135
136 # tgen.mininet_cli();
137 r1 = tgen.gears["R1"]
138 r2 = tgen.gears["R2"]
139
140 check_labelpool(r1)
141 check_labelpool(r2)
142
143
144 def test_clear_bgplu():
145 "Wait for protocol convergence"
146
147 tgen = get_topogen()
148 # Don't run this test if we have any failure.
149 if tgen.routers_have_failure():
150 pytest.skip(tgen.errors)
151
152 # tgen.mininet_cli();
153 r1 = tgen.gears["R1"]
154 r2 = tgen.gears["R2"]
155
156 r1.vtysh_cmd("clear bgp 10.0.0.2")
157 check_labelpool(r1)
158 check_labelpool(r2)
159
160 r2.vtysh_cmd("clear bgp 10.0.1.3")
161 check_labelpool(r1)
162 check_labelpool(r2)
163
164 r1.vtysh_cmd("clear bgp 10.0.0.2")
165 r2.vtysh_cmd("clear bgp 10.0.1.3")
166 check_labelpool(r1)
167 check_labelpool(r2)
168
169
170 def test_memory_leak():
171 "Run the memory leak test and report results."
172 tgen = get_topogen()
173 if not tgen.is_memleak_enabled():
174 pytest.skip("Memory leak test/report is disabled")
175
176 tgen.report_memory_leaks()
177
178
179 if __name__ == "__main__":
180 args = ["-s"] + sys.argv[1:]
181 sys.exit(pytest.main(args))