]>
Commit | Line | Data |
---|---|---|
4256a209 | 1 | #!/usr/bin/python |
2 | ||
3 | # | |
4 | # Copyright (c) 2020 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 | """OSPF Basic Functionality Automation.""" | |
25 | import ipaddress | |
26 | from lib.ospf import ( | |
27 | verify_ospf_neighbor, | |
28 | config_ospf_interface, | |
29 | clear_ospf, | |
30 | verify_ospf_rib, | |
31 | create_router_ospf, | |
32 | verify_ospf_interface, | |
88b7d3e7 | 33 | redistribute_ospf, |
4256a209 | 34 | ) |
35 | from lib.topojson import build_topo_from_json, build_config_from_json | |
36 | from lib.topolog import logger | |
37 | from lib.common_config import ( | |
38 | start_topology, | |
39 | write_test_header, | |
40 | write_test_footer, | |
41 | reset_config_on_routers, | |
42 | verify_rib, | |
43 | create_static_routes, | |
44 | step, | |
45 | create_route_maps, | |
46 | shutdown_bringup_interface, | |
47 | create_interfaces_cfg, | |
701a0192 | 48 | topo_daemons, |
4256a209 | 49 | ) |
50 | from ipaddress import IPv4Address | |
51 | from lib.topogen import Topogen, get_topogen | |
52 | from mininet.topo import Topo | |
53 | import os | |
54 | import sys | |
55 | import time | |
56 | import pytest | |
57 | import json | |
4256a209 | 58 | |
59 | # Save the Current Working Directory to find configuration files. | |
60 | CWD = os.path.dirname(os.path.realpath(__file__)) | |
61 | sys.path.append(os.path.join(CWD, "../")) | |
62 | sys.path.append(os.path.join(CWD, "../lib/")) | |
63 | # pylint: disable=C0413 | |
64 | # Import topogen and topotest helpers | |
65 | ||
66 | # Global variables | |
67 | topo = None | |
68 | # Reading the data from JSON File for topology creation | |
69 | jsonFile = "{}/ospf_nssa.json".format(CWD) | |
70 | try: | |
71 | with open(jsonFile, "r") as topoJson: | |
72 | topo = json.load(topoJson) | |
73 | except IOError: | |
74 | assert False, "Could not read file {}".format(jsonFile) | |
75 | NETWORK = { | |
76 | "ipv4": [ | |
77 | "11.0.20.1/32", | |
78 | "11.0.20.2/32", | |
79 | "11.0.20.3/32", | |
80 | "11.0.20.4/32", | |
81 | "11.0.20.5/32", | |
82 | ] | |
83 | } | |
84 | """ | |
85 | TOPOOLOGY = | |
86 | Please view in a fixed-width font such as Courier. | |
87 | +---+ A1 +---+ | |
88 | +R1 +------------+R2 | | |
89 | +-+-+- +--++ | |
90 | | -- -- | | |
91 | | -- A0 -- | | |
92 | A0| ---- | | |
93 | | ---- | A2 | |
94 | | -- -- | | |
95 | | -- -- | | |
96 | +-+-+- +-+-+ | |
97 | +R0 +-------------+R3 | | |
98 | +---+ A3 +---+ | |
99 | ||
100 | ||
101 | ||
102 | TESTCASES = | |
103 | 1. OSPF Learning - Verify OSPF can learn different types of LSA and | |
104 | processes them.[Edge learning different types of LSAs] | |
105 | 2. Verify that ospf non back bone area can be configured as NSSA area | |
106 | 3. Verify that ospf NSSA area DUT is capable receiving & processing | |
107 | Type7 N2 route. | |
108 | """ | |
109 | ||
110 | ||
111 | class CreateTopo(Topo): | |
112 | """ | |
113 | Test topology builder. | |
114 | ||
115 | * `Topo`: Topology object | |
116 | """ | |
117 | ||
118 | def build(self, *_args, **_opts): | |
119 | """Build function.""" | |
120 | tgen = get_topogen(self) | |
121 | ||
122 | # Building topology from json file | |
123 | build_topo_from_json(tgen, topo) | |
124 | ||
125 | ||
126 | def setup_module(mod): | |
127 | """ | |
128 | Sets up the pytest environment | |
129 | ||
130 | * `mod`: module name | |
131 | """ | |
132 | global topo | |
133 | testsuite_run_time = time.asctime(time.localtime(time.time())) | |
134 | logger.info("Testsuite start time: {}".format(testsuite_run_time)) | |
135 | logger.info("=" * 40) | |
136 | ||
137 | logger.info("Running setup_module to create topology") | |
138 | ||
139 | # This function initiates the topology build with Topogen... | |
140 | tgen = Topogen(CreateTopo, mod.__name__) | |
141 | # ... and here it calls Mininet initialization functions. | |
142 | ||
035267a3 | 143 | # get list of daemons needs to be started for this suite. |
144 | daemons = topo_daemons(tgen, topo) | |
145 | ||
4256a209 | 146 | # Starting topology, create tmp files which are loaded to routers |
147 | # to start deamons and then start routers | |
035267a3 | 148 | start_topology(tgen, daemons) |
149 | ||
4256a209 | 150 | # Creating configuration from JSON |
151 | build_config_from_json(tgen, topo) | |
152 | ||
153 | # Don't run this test if we have any failure. | |
154 | if tgen.routers_have_failure(): | |
155 | pytest.skip(tgen.errors) | |
156 | # Api call verify whether OSPF is converged | |
157 | ospf_covergence = verify_ospf_neighbor(tgen, topo) | |
158 | assert ospf_covergence is True, "setup_module :Failed \n Error:" " {}".format( | |
159 | ospf_covergence | |
160 | ) | |
161 | ||
162 | logger.info("Running setup_module() done") | |
163 | ||
164 | ||
165 | def teardown_module(mod): | |
166 | """ | |
167 | Teardown the pytest environment. | |
168 | ||
169 | * `mod`: module name | |
170 | """ | |
171 | ||
172 | logger.info("Running teardown_module to delete topology") | |
173 | ||
174 | tgen = get_topogen() | |
175 | ||
176 | # Stop toplogy and Remove tmp files | |
177 | tgen.stop_topology() | |
178 | ||
179 | logger.info( | |
180 | "Testsuite end time: {}".format(time.asctime(time.localtime(time.time()))) | |
181 | ) | |
182 | logger.info("=" * 40) | |
183 | ||
184 | ||
4256a209 | 185 | # ################################## |
186 | # Test cases start here. | |
187 | # ################################## | |
188 | ||
189 | ||
190 | def test_ospf_learning_tc15_p0(request): | |
191 | """Verify OSPF can learn different types of LSA and processes them. | |
192 | ||
193 | OSPF Learning : Edge learning different types of LSAs. | |
194 | """ | |
195 | tc_name = request.node.name | |
196 | write_test_header(tc_name) | |
197 | tgen = get_topogen() | |
198 | ||
199 | # Don't run this test if we have any failure. | |
200 | if tgen.routers_have_failure(): | |
201 | pytest.skip(tgen.errors) | |
202 | ||
203 | global topo | |
204 | step("Bring up the base config as per the topology") | |
205 | step("Configure area 1 as NSSA Area") | |
206 | ||
207 | reset_config_on_routers(tgen) | |
208 | ||
209 | step("Verify that Type 3 summary LSA is originated for the same Area 0") | |
210 | ip = topo["routers"]["r1"]["links"]["r3-link0"]["ipv4"] | |
211 | ip_net = str(ipaddress.ip_interface(u"{}".format(ip)).network) | |
212 | ||
213 | dut = "r0" | |
214 | input_dict = { | |
215 | "r1": { | |
216 | "static_routes": [{"network": ip_net, "no_of_ip": 1, "routeType": "N IA"}] | |
217 | } | |
218 | } | |
219 | ||
220 | dut = "r0" | |
221 | result = verify_ospf_rib(tgen, dut, input_dict) | |
222 | assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result) | |
223 | ||
224 | protocol = "ospf" | |
225 | result = verify_rib(tgen, "ipv4", dut, input_dict, protocol=protocol) | |
226 | assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result) | |
227 | ||
228 | input_dict = { | |
229 | "r2": { | |
230 | "static_routes": [ | |
231 | {"network": NETWORK["ipv4"][0], "no_of_ip": 5, "next_hop": "Null0"} | |
232 | ] | |
233 | } | |
234 | } | |
235 | result = create_static_routes(tgen, input_dict) | |
236 | assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result) | |
237 | ||
238 | step("Redistribute static route in R2 ospf.") | |
239 | dut = "r2" | |
88b7d3e7 | 240 | redistribute_ospf(tgen, topo, dut, "static") |
4256a209 | 241 | |
242 | step("Verify that Type 5 LSA is originated by R2.") | |
243 | dut = "r0" | |
244 | protocol = "ospf" | |
245 | result = verify_rib(tgen, "ipv4", dut, input_dict, protocol=protocol) | |
246 | assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result) | |
247 | ||
248 | step("Verify that R0 receives Type 4 summary LSA.") | |
249 | dut = "r0" | |
250 | input_dict = { | |
251 | "r1": { | |
252 | "static_routes": [ | |
253 | {"network": NETWORK["ipv4"][0], "no_of_ip": 1, "routeType": "N E2"} | |
254 | ] | |
255 | } | |
256 | } | |
257 | ||
258 | dut = "r1" | |
259 | result = verify_ospf_rib(tgen, dut, input_dict) | |
260 | assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result) | |
261 | ||
262 | result = verify_rib(tgen, "ipv4", dut, input_dict, protocol=protocol) | |
263 | assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result) | |
264 | ||
265 | ospf_covergence = verify_ospf_neighbor(tgen, topo) | |
266 | assert ospf_covergence is True, "setup_module :Failed \n Error:" " {}".format( | |
267 | ospf_covergence | |
268 | ) | |
269 | ||
270 | step("Change area 1 as non nssa area (on the fly changing area" " type on DUT).") | |
271 | ||
272 | for rtr in ["r1", "r2", "r3"]: | |
273 | input_dict = { | |
274 | rtr: {"ospf": {"area": [{"id": "0.0.0.2", "type": "nssa", "delete": True}]}} | |
275 | } | |
276 | result = create_router_ospf(tgen, topo, input_dict) | |
277 | assert result is True, "Testcase {} : Failed \n Error: {}".format( | |
278 | tc_name, result | |
279 | ) | |
280 | ||
281 | step("Verify that OSPF neighbours are reset after changing area type.") | |
282 | step("Verify that ABR R2 originates type 5 LSA in area 1.") | |
283 | step("Verify that route is calculated and installed in R1.") | |
284 | ||
285 | input_dict = { | |
286 | "r1": { | |
287 | "static_routes": [ | |
288 | {"network": NETWORK["ipv4"][0], "no_of_ip": 1, "routeType": "N E2"} | |
289 | ] | |
290 | } | |
291 | } | |
292 | ||
293 | dut = "r1" | |
294 | result = verify_ospf_rib(tgen, dut, input_dict) | |
295 | assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result) | |
296 | ||
297 | result = verify_rib(tgen, "ipv4", dut, input_dict, protocol=protocol) | |
298 | assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result) | |
299 | ||
300 | write_test_footer(tc_name) | |
301 | ||
302 | ||
303 | if __name__ == "__main__": | |
304 | args = ["-s"] + sys.argv[1:] | |
305 | sys.exit(pytest.main(args)) |