4 # Copyright (c) 2020 by VMware, Inc. ("VMware")
5 # Used Copyright (c) 2018 by Network Device Education Foundation, Inc.
6 # ("NetDEF") in this file.
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
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
24 """OSPF Basic Functionality Automation."""
29 from copy
import deepcopy
32 # Save the Current Working Directory to find configuration files.
33 CWD
= os
.path
.dirname(os
.path
.realpath(__file__
))
34 sys
.path
.append(os
.path
.join(CWD
, "../"))
35 sys
.path
.append(os
.path
.join(CWD
, "../lib/"))
37 # pylint: disable=C0413
38 # Import topogen and topotest helpers
39 from lib
.micronet_compat
import Topo
40 from lib
.topogen
import Topogen
, get_topogen
42 # Import topoJson from lib, to create topology and initial configuration
43 from lib
.common_config
import (
47 reset_config_on_routers
,
49 shutdown_bringup_interface
,
59 from lib
.ospf
import verify_ospf_neighbor
, verify_ospf_rib
, create_router_ospf
61 from lib
.topolog
import logger
62 from lib
.topojson
import build_topo_from_json
, build_config_from_json
63 from ipaddress
import IPv4Address
65 pytestmark
= [pytest
.mark
.ospfd
, pytest
.mark
.staticd
]
81 Please view in a fixed-width font such as Courier.
83 +R1 +------------+R2 |
92 +R0 +-------------+R3 |
96 1. Verify ospf functionality after restart ospfd.
97 2. Verify ospf functionality after restart FRR service.
98 3. Verify ospf functionality when staticd is restarted.
101 # Reading the data from JSON File for topology creation
102 jsonFile
= "{}/ospf_chaos.json".format(CWD
)
104 with
open(jsonFile
, "r") as topoJson
:
105 topo
= json
.load(topoJson
)
107 assert False, "Could not read file {}".format(jsonFile
)
110 class CreateTopo(Topo
):
112 Test topology builder.
114 * `Topo`: Topology object
117 def build(self
, *_args
, **_opts
):
118 """Build function."""
119 tgen
= get_topogen(self
)
121 # Building topology from json file
122 build_topo_from_json(tgen
, topo
)
125 def setup_module(mod
):
127 Sets up the pytest environment
132 testsuite_run_time
= time
.asctime(time
.localtime(time
.time()))
133 logger
.info("Testsuite start time: {}".format(testsuite_run_time
))
134 logger
.info("=" * 40)
136 logger
.info("Running setup_module to create topology")
138 # This function initiates the topology build with Topogen...
139 tgen
= Topogen(CreateTopo
, mod
.__name
__)
140 # ... and here it calls Mininet initialization functions.
142 # get list of daemons needs to be started for this suite.
143 daemons
= topo_daemons(tgen
, topo
)
145 # Starting topology, create tmp files which are loaded to routers
146 # to start deamons and then start routers
147 start_topology(tgen
, daemons
)
149 # Creating configuration from JSON
150 build_config_from_json(tgen
, topo
)
152 # Don't run this test if we have any failure.
153 if tgen
.routers_have_failure():
154 pytest
.skip(tgen
.errors
)
156 ospf_covergence
= verify_ospf_neighbor(tgen
, topo
)
157 assert ospf_covergence
is True, "setup_module :Failed \n Error:" " {}".format(
161 logger
.info("Running setup_module() done")
164 def teardown_module(mod
):
166 Teardown the pytest environment.
171 logger
.info("Running teardown_module to delete topology")
175 # Stop toplogy and Remove tmp files
179 "Testsuite end time: {}".format(time
.asctime(time
.localtime(time
.time())))
181 logger
.info("=" * 40)
184 # ##################################
185 # Test cases start here.
186 # ##################################
187 def test_ospf_chaos_tc31_p1(request
):
188 """Verify ospf functionality after restart ospfd."""
189 tc_name
= request
.node
.name
190 write_test_header(tc_name
)
193 step("Bring up the base config as per the topology")
194 reset_config_on_routers(tgen
)
197 "Create static routes(10.0.20.1/32) in R1 and redistribute "
198 "to OSPF using route map."
201 # Create Static routes
206 "network": NETWORK
["ipv4"][0],
213 result
= create_static_routes(tgen
, input_dict
)
214 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
216 ospf_red_r0
= {"r0": {"ospf": {"redistribute": [{"redist_type": "static"}]}}}
217 result
= create_router_ospf(tgen
, topo
, ospf_red_r0
)
218 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
220 step("Verify OSPF neighbors after base config is done.")
221 # Api call verify whether OSPF is converged
222 ospf_covergence
= verify_ospf_neighbor(tgen
, topo
)
223 assert ospf_covergence
is True, "setup_module :Failed \n Error:" " {}".format(
227 step("Verify that route is advertised to R1.")
230 nh
= topo
["routers"]["r0"]["links"]["r1"]["ipv4"].split("/")[0]
231 result
= verify_ospf_rib(tgen
, dut
, input_dict
, next_hop
=nh
)
232 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
234 result
= verify_rib(tgen
, "ipv4", dut
, input_dict
, protocol
=protocol
, next_hop
=nh
)
235 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
237 step("Kill OSPFd daemon on R0.")
238 kill_router_daemons(tgen
, "r0", ["ospfd"])
240 step("Verify OSPF neighbors are down after killing ospfd in R0")
242 # Api call verify whether OSPF is converged
243 ospf_covergence
= verify_ospf_neighbor(tgen
, topo
, dut
=dut
, expected
=False)
244 assert ospf_covergence
is not True, "setup_module :Failed \n Error:" " {}".format(
248 step("Verify that route advertised to R1 are deleted from RIB and FIB.")
251 result
= verify_ospf_rib(tgen
, dut
, input_dict
, expected
=False)
254 ), "Testcase {} : Failed \n " "r1: OSPF routes are present \n Error: {}".format(
259 tgen
, "ipv4", dut
, input_dict
, protocol
=protocol
, expected
=False
263 ), "Testcase {} : Failed \n " "r1: routes are still present \n Error: {}".format(
267 step("Bring up OSPFd daemon on R0.")
268 start_router_daemons(tgen
, "r0", ["ospfd"])
270 step("Verify OSPF neighbors are up after bringing back ospfd in R0")
271 # Api call verify whether OSPF is converged
272 ospf_covergence
= verify_ospf_neighbor(tgen
, topo
)
273 assert ospf_covergence
is True, "setup_module :Failed \n Error:" " {}".format(
278 "All the neighbours are up and routes are installed before the"
279 " restart. Verify OSPF route table and ip route table."
283 result
= verify_ospf_rib(tgen
, dut
, input_dict
, next_hop
=nh
)
284 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
286 result
= verify_rib(tgen
, "ipv4", dut
, input_dict
, protocol
=protocol
, next_hop
=nh
)
287 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
289 step("Kill OSPFd daemon on R1.")
290 kill_router_daemons(tgen
, "r1", ["ospfd"])
292 step("Verify OSPF neighbors are down after killing ospfd in R1")
294 # Api call verify whether OSPF is converged
295 ospf_covergence
= verify_ospf_neighbor(tgen
, topo
, dut
=dut
, expected
=False)
296 assert ospf_covergence
is not True, "setup_module :Failed \n Error:" " {}".format(
300 step("Bring up OSPFd daemon on R1.")
301 start_router_daemons(tgen
, "r1", ["ospfd"])
303 step("Verify OSPF neighbors are up after bringing back ospfd in R1")
304 # Api call verify whether OSPF is converged
305 ospf_covergence
= verify_ospf_neighbor(tgen
, topo
)
306 assert ospf_covergence
is True, "setup_module :Failed \n Error:" " {}".format(
311 "All the neighbours are up and routes are installed before the"
312 " restart. Verify OSPF route table and ip route table."
317 result
= verify_ospf_rib(tgen
, dut
, input_dict
, next_hop
=nh
)
318 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
320 result
= verify_rib(tgen
, "ipv4", dut
, input_dict
, protocol
=protocol
, next_hop
=nh
)
321 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
323 write_test_footer(tc_name
)
326 def test_ospf_chaos_tc32_p1(request
):
327 """Verify ospf functionality after restart FRR service. """
328 tc_name
= request
.node
.name
329 write_test_header(tc_name
)
332 step("Bring up the base config as per the topology")
333 reset_config_on_routers(tgen
)
336 "Create static routes(10.0.20.1/32) in R1 and redistribute "
337 "to OSPF using route map."
340 # Create Static routes
345 "network": NETWORK
["ipv4"][0],
352 result
= create_static_routes(tgen
, input_dict
)
353 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
355 ospf_red_r0
= {"r0": {"ospf": {"redistribute": [{"redist_type": "static"}]}}}
356 result
= create_router_ospf(tgen
, topo
, ospf_red_r0
)
357 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
359 step("Verify OSPF neighbors after base config is done.")
360 # Api call verify whether OSPF is converged
361 ospf_covergence
= verify_ospf_neighbor(tgen
, topo
)
362 assert ospf_covergence
is True, "setup_module :Failed \n Error:" " {}".format(
366 step("Verify that route is advertised to R1.")
370 nh
= topo
["routers"]["r0"]["links"]["r1"]["ipv4"].split("/")[0]
371 result
= verify_ospf_rib(tgen
, dut
, input_dict
, next_hop
=nh
)
372 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
374 result
= verify_rib(tgen
, "ipv4", dut
, input_dict
, protocol
=protocol
, next_hop
=nh
)
375 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
377 step("Restart frr on R0")
378 stop_router(tgen
, "r0")
379 start_router(tgen
, "r0")
381 step("Verify OSPF neighbors are up after restarting R0")
382 # Api call verify whether OSPF is converged
383 ospf_covergence
= verify_ospf_neighbor(tgen
, topo
)
384 assert ospf_covergence
is True, "setup_module :Failed \n Error:" " {}".format(
389 "All the neighbours are up and routes are installed before the"
390 " restart. Verify OSPF route table and ip route table."
394 result
= verify_ospf_rib(tgen
, dut
, input_dict
, next_hop
=nh
)
395 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
397 result
= verify_rib(tgen
, "ipv4", dut
, input_dict
, protocol
=protocol
, next_hop
=nh
)
398 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
400 step("Restart frr on R1")
401 stop_router(tgen
, "r1")
402 start_router(tgen
, "r1")
404 step("Verify OSPF neighbors are up after restarting R1")
405 # Api call verify whether OSPF is converged
406 ospf_covergence
= verify_ospf_neighbor(tgen
, topo
)
407 assert ospf_covergence
is True, "setup_module :Failed \n Error:" " {}".format(
412 "All the neighbours are up and routes are installed before the"
413 " restart. Verify OSPF route table and ip route table."
417 result
= verify_ospf_rib(tgen
, dut
, input_dict
, next_hop
=nh
)
418 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
420 result
= verify_rib(tgen
, "ipv4", dut
, input_dict
, protocol
=protocol
, next_hop
=nh
)
421 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
423 write_test_footer(tc_name
)
426 def test_ospf_chaos_tc34_p1(request
):
428 verify ospf functionality when staticd is restarted.
430 Verify ospf functionalitywhen staticroutes are
431 redistributed & Staticd is restarted.
433 tc_name
= request
.node
.name
434 write_test_header(tc_name
)
437 step("Bring up the base config as per the topology")
438 reset_config_on_routers(tgen
)
441 "Create static routes(10.0.20.1/32) in R1 and redistribute "
442 "to OSPF using route map."
445 # Create Static routes
450 "network": NETWORK
["ipv4"][0],
457 result
= create_static_routes(tgen
, input_dict
)
458 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
460 ospf_red_r0
= {"r0": {"ospf": {"redistribute": [{"redist_type": "static"}]}}}
461 result
= create_router_ospf(tgen
, topo
, ospf_red_r0
)
462 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
464 step("Verify OSPF neighbors after base config is done.")
465 # Api call verify whether OSPF is converged
466 ospf_covergence
= verify_ospf_neighbor(tgen
, topo
)
467 assert ospf_covergence
is True, "setup_module :Failed \n Error:" " {}".format(
471 step("Verify that route is advertised to R1.")
474 nh
= topo
["routers"]["r0"]["links"]["r1"]["ipv4"].split("/")[0]
475 result
= verify_ospf_rib(tgen
, dut
, input_dict
, next_hop
=nh
)
476 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
478 result
= verify_rib(tgen
, "ipv4", dut
, input_dict
, protocol
=protocol
, next_hop
=nh
)
479 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
481 step("Kill staticd daemon on R0.")
482 kill_router_daemons(tgen
, "r0", ["staticd"])
484 step("Verify that route advertised to R1 are deleted from RIB and FIB.")
487 result
= verify_ospf_rib(tgen
, dut
, input_dict
, expected
=False)
490 ), "Testcase {} : Failed \n " "r1: OSPF routes are present \n Error: {}".format(
495 tgen
, "ipv4", dut
, input_dict
, protocol
=protocol
, expected
=False
499 ), "Testcase {} : Failed \n " "r1: routes are still present \n Error: {}".format(
503 step("Bring up staticd daemon on R0.")
504 start_router_daemons(tgen
, "r0", ["staticd"])
506 step("Verify OSPF neighbors are up after bringing back ospfd in R0")
507 # Api call verify whether OSPF is converged
508 ospf_covergence
= verify_ospf_neighbor(tgen
, topo
)
509 assert ospf_covergence
is True, "setup_module :Failed \n Error:" " {}".format(
514 "All the neighbours are up and routes are installed before the"
515 " restart. Verify OSPF route table and ip route table."
519 result
= verify_ospf_rib(tgen
, dut
, input_dict
, next_hop
=nh
)
520 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
522 result
= verify_rib(tgen
, "ipv4", dut
, input_dict
, protocol
=protocol
, next_hop
=nh
)
523 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
525 step("Kill staticd daemon on R1.")
526 kill_router_daemons(tgen
, "r1", ["staticd"])
528 step("Bring up staticd daemon on R1.")
529 start_router_daemons(tgen
, "r1", ["staticd"])
531 step("Verify OSPF neighbors are up after bringing back ospfd in R1")
532 # Api call verify whether OSPF is converged
533 ospf_covergence
= verify_ospf_neighbor(tgen
, topo
)
534 assert ospf_covergence
is True, "setup_module :Failed \n Error:" " {}".format(
539 "All the neighbours are up and routes are installed before the"
540 " restart. Verify OSPF route table and ip route table."
545 result
= verify_ospf_rib(tgen
, dut
, input_dict
, next_hop
=nh
)
546 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
548 result
= verify_rib(tgen
, "ipv4", dut
, input_dict
, protocol
=protocol
, next_hop
=nh
)
549 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
551 write_test_footer(tc_name
)
554 if __name__
== "__main__":
555 args
= ["-s"] + sys
.argv
[1:]
556 sys
.exit(pytest
.main(args
))