]>
Commit | Line | Data |
---|---|---|
57ea1920 PG |
1 | #!/usr/bin/env python |
2 | ||
3 | # | |
4 | # Copyright (c) 2019 by VMware, Inc. ("VMware") | |
5 | # Used Copyright (c) 2018 by Network Device Education Foundation, Inc. | |
6 | # ("NetDEF") in this file. | |
7 | # | |
8 | # Permission to use, copy, modify, and/or distribute this software | |
9 | # for any purpose with or without fee is hereby granted, provided | |
10 | # that the above copyright notice and this permission notice appear | |
11 | # in all copies. | |
12 | # | |
13 | # THE SOFTWARE IS PROVIDED "AS IS" AND VMWARE DISCLAIMS ALL WARRANTIES | |
14 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
15 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL VMWARE BE LIABLE FOR | |
16 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY | |
17 | # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, | |
18 | # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | |
19 | # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE | |
20 | # OF THIS SOFTWARE. | |
21 | # | |
22 | ||
23 | ||
24 | """ | |
25 | Following tests are covered to test ecmp functionality on iBGP. | |
26 | 1. Verify bgp fast-convergence functionality | |
27 | """ | |
28 | import os | |
29 | import sys | |
30 | import time | |
31 | import json | |
32 | import pytest | |
33 | from time import sleep | |
34 | ||
35 | # Save the Current Working Directory to find configuration files. | |
36 | CWD = os.path.dirname(os.path.realpath(__file__)) | |
37 | sys.path.append(os.path.join(CWD, "../")) | |
38 | sys.path.append(os.path.join(CWD, "../../")) | |
39 | ||
40 | # pylint: disable=C0413 | |
41 | # Import topogen and topotest helpers | |
42 | from lib.topogen import Topogen, get_topogen | |
e82b531d | 43 | from lib import topojson |
57ea1920 PG |
44 | |
45 | from lib.common_config import ( | |
46 | start_topology, | |
47 | write_test_header, | |
48 | write_test_footer, | |
49 | verify_rib, | |
50 | create_static_routes, | |
51 | check_address_types, | |
52 | interface_status, | |
53 | reset_config_on_routers, | |
54 | required_linux_kernel_version, | |
55 | shutdown_bringup_interface, | |
56 | apply_raw_config, | |
57 | ) | |
58 | from lib.topolog import logger | |
59 | from lib.bgp import verify_bgp_convergence, create_router_bgp, clear_bgp | |
60 | from lib.topojson import build_topo_from_json, build_config_from_json | |
61 | ||
62 | ||
63 | pytestmark = [pytest.mark.bgpd, pytest.mark.staticd] | |
64 | ||
65 | ||
57ea1920 PG |
66 | # Global variables |
67 | NEXT_HOPS = {"ipv4": [], "ipv6": []} | |
68 | NETWORK = {"ipv4": "192.168.1.10/32", "ipv6": "fd00:0:0:1::10/128"} | |
69 | NEXT_HOP_IP = {"ipv4": "10.0.0.1", "ipv6": "fd00::1"} | |
70 | BGP_CONVERGENCE = False | |
71 | ||
72 | ||
57ea1920 PG |
73 | def setup_module(mod): |
74 | """ | |
75 | Sets up the pytest environment. | |
76 | ||
77 | * `mod`: module name | |
78 | """ | |
57ea1920 PG |
79 | global ADDR_TYPES |
80 | ||
81 | testsuite_run_time = time.asctime(time.localtime(time.time())) | |
82 | logger.info("Testsuite start time: {}".format(testsuite_run_time)) | |
83 | logger.info("=" * 40) | |
84 | ||
e82b531d CH |
85 | tgen = topojson.setup_module_from_json(mod.__file__) |
86 | topo = tgen.json_topo | |
57ea1920 PG |
87 | |
88 | # Don't run this test if we have any failure. | |
89 | if tgen.routers_have_failure(): | |
90 | pytest.skip(tgen.errors) | |
91 | ||
92 | # Api call verify whether BGP is converged | |
93 | ADDR_TYPES = check_address_types() | |
94 | ||
95 | BGP_CONVERGENCE = verify_bgp_convergence(tgen, topo) | |
96 | assert BGP_CONVERGENCE is True, "setup_module :Failed \n Error:" " {}".format( | |
97 | BGP_CONVERGENCE | |
98 | ) | |
99 | ||
100 | # STATIC_ROUTE = True | |
101 | logger.info("Running setup_module() done") | |
102 | ||
103 | ||
104 | def teardown_module(): | |
e82b531d | 105 | get_topogen().stop_topology() |
57ea1920 PG |
106 | |
107 | ||
108 | def static_or_nw(tgen, topo, tc_name, test_type, dut): | |
109 | ||
110 | if test_type == "redist_static": | |
111 | input_dict_static = { | |
112 | dut: { | |
113 | "static_routes": [ | |
114 | {"network": NETWORK["ipv4"], "next_hop": NEXT_HOP_IP["ipv4"]}, | |
115 | {"network": NETWORK["ipv6"], "next_hop": NEXT_HOP_IP["ipv6"]}, | |
116 | ] | |
117 | } | |
118 | } | |
119 | logger.info("Configuring static route on router %s", dut) | |
120 | result = create_static_routes(tgen, input_dict_static) | |
121 | assert result is True, "Testcase {} : Failed \n Error: {}".format( | |
122 | tc_name, result | |
123 | ) | |
124 | ||
125 | input_dict_2 = { | |
126 | dut: { | |
127 | "bgp": { | |
128 | "address_family": { | |
129 | "ipv4": { | |
130 | "unicast": {"redistribute": [{"redist_type": "static"}]} | |
131 | }, | |
132 | "ipv6": { | |
133 | "unicast": {"redistribute": [{"redist_type": "static"}]} | |
134 | }, | |
135 | } | |
136 | } | |
137 | } | |
138 | } | |
139 | ||
140 | logger.info("Configuring redistribute static route on router %s", dut) | |
141 | result = create_router_bgp(tgen, topo, input_dict_2) | |
142 | assert result is True, "Testcase {} : Failed \n Error: {}".format( | |
143 | tc_name, result | |
144 | ) | |
145 | ||
146 | elif test_type == "advertise_nw": | |
147 | input_dict_nw = { | |
148 | dut: { | |
149 | "bgp": { | |
150 | "address_family": { | |
151 | "ipv4": { | |
152 | "unicast": { | |
153 | "advertise_networks": [{"network": NETWORK["ipv4"]}] | |
154 | } | |
155 | }, | |
156 | "ipv6": { | |
157 | "unicast": { | |
158 | "advertise_networks": [{"network": NETWORK["ipv6"]}] | |
159 | } | |
160 | }, | |
161 | } | |
162 | } | |
163 | } | |
164 | } | |
165 | ||
166 | logger.info( | |
167 | "Advertising networks %s %s from router %s", | |
168 | NETWORK["ipv4"], | |
169 | NETWORK["ipv6"], | |
170 | dut, | |
171 | ) | |
172 | result = create_router_bgp(tgen, topo, input_dict_nw) | |
173 | assert result is True, "Testcase {} : Failed \n Error: {}".format( | |
174 | tc_name, result | |
175 | ) | |
176 | ||
177 | ||
178 | @pytest.mark.parametrize("test_type", ["redist_static"]) | |
e82b531d | 179 | def test_ecmp_fast_convergence(request, test_type, tgen, topo): |
57ea1920 PG |
180 | """This test is to verify bgp fast-convergence cli functionality""" |
181 | ||
182 | tc_name = request.node.name | |
183 | write_test_header(tc_name) | |
57ea1920 PG |
184 | |
185 | # Verifying RIB routes | |
186 | dut = "r3" | |
187 | protocol = "bgp" | |
188 | ||
189 | reset_config_on_routers(tgen) | |
190 | static_or_nw(tgen, topo, tc_name, test_type, "r2") | |
191 | ||
192 | for addr_type in ADDR_TYPES: | |
193 | input_dict = {"r3": {"static_routes": [{"network": NETWORK[addr_type]}]}} | |
194 | ||
195 | logger.info("Verifying %s routes on r3", addr_type) | |
196 | result = verify_rib( | |
197 | tgen, | |
198 | addr_type, | |
199 | dut, | |
200 | input_dict, | |
201 | protocol=protocol, | |
202 | ) | |
203 | assert result is True, "Testcase {} : Failed \n Error: {}".format( | |
204 | tc_name, result | |
205 | ) | |
206 | ||
207 | intf1 = topo["routers"]["r2"]["links"]["r3-link1"]["interface"] | |
208 | intf2 = topo["routers"]["r2"]["links"]["r3-link2"]["interface"] | |
209 | ||
210 | logger.info("Shutdown one of the link b/w r2 and r3") | |
211 | shutdown_bringup_interface(tgen, "r2", intf1, False) | |
212 | ||
213 | logger.info("Verify bgp neighbors are still up") | |
214 | result = verify_bgp_convergence(tgen, topo) | |
215 | assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result) | |
216 | ||
217 | logger.info("Shutdown another link b/w r2 and r3") | |
218 | shutdown_bringup_interface(tgen, "r2", intf2, False) | |
219 | ||
220 | logger.info("Wait for 10 sec and make sure bgp neighbors are still up") | |
221 | sleep(10) | |
222 | result = verify_bgp_convergence(tgen, topo) | |
223 | assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result) | |
224 | ||
225 | logger.info("No shut links b/w r2 and r3") | |
226 | shutdown_bringup_interface(tgen, "r2", intf1, True) | |
227 | shutdown_bringup_interface(tgen, "r2", intf2, True) | |
228 | ||
229 | logger.info("Enable bgp fast-convergence cli") | |
230 | raw_config = { | |
231 | "r2": { | |
232 | "raw_config": [ | |
233 | "router bgp {}".format(topo["routers"]["r2"]["bgp"]["local_as"]), | |
234 | "bgp fast-convergence", | |
235 | ] | |
236 | } | |
237 | } | |
238 | result = apply_raw_config(tgen, raw_config) | |
239 | assert result is True, "Testcase {} : Failed Error: {}".format(tc_name, result) | |
240 | ||
241 | logger.info("Shutdown one link b/w r2 and r3") | |
242 | shutdown_bringup_interface(tgen, "r2", intf1, False) | |
243 | ||
244 | logger.info("Verify bgp neighbors goes down immediately") | |
245 | result = verify_bgp_convergence(tgen, topo, dut="r2", expected=False) | |
246 | assert result is not True, "Testcase {} : Failed \n Error: {}".format( | |
247 | tc_name, result | |
248 | ) | |
249 | ||
250 | logger.info("Shutdown second link b/w r2 and r3") | |
251 | shutdown_bringup_interface(tgen, "r2", intf2, False) | |
252 | ||
253 | logger.info("Verify bgp neighbors goes down immediately") | |
254 | result = verify_bgp_convergence(tgen, topo, dut="r2", expected=False) | |
255 | assert result is not True, "Testcase {} : Failed \n Error: {}".format( | |
256 | tc_name, result | |
257 | ) | |
258 | ||
259 | write_test_footer(tc_name) | |
260 | ||
261 | ||
262 | if __name__ == "__main__": | |
263 | args = ["-s"] + sys.argv[1:] | |
264 | sys.exit(pytest.main(args)) |