]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/example_topojson_test/test_topo_json_multiple_links/test_example_topojson_multiple_links.py
*: auto-convert to SPDX License IDs
[mirror_frr.git] / tests / topotests / example_topojson_test / test_topo_json_multiple_links / test_example_topojson_multiple_links.py
1 #!/usr/bin/env python
2 # SPDX-License-Identifier: ISC
3
4 #
5 # Copyright (c) 2019 by VMware, Inc. ("VMware")
6 # Used Copyright (c) 2018 by Network Device Education Foundation, Inc. ("NetDEF")
7 # in this file.
8 #
9
10 """
11 <example>.py: Test <example tests>.
12 """
13
14 import os
15 import sys
16 import json
17 import time
18 import pytest
19
20 # Save the Current Working Directory to find configuration files.
21 CWD = os.path.dirname(os.path.realpath(__file__))
22 sys.path.append(os.path.join(CWD, "../"))
23 sys.path.append(os.path.join(CWD, "../../"))
24
25 # pylint: disable=C0413
26 from lib.topogen import Topogen, get_topogen
27
28 # Required to instantiate the topology builder class.
29
30 # Import topoJson from lib, to create topology and initial configuration
31 from lib.common_config import (
32 start_topology,
33 write_test_header,
34 write_test_footer,
35 verify_rib,
36 )
37 from lib.topolog import logger
38 from lib.bgp import verify_bgp_convergence
39 from lib.topojson import build_topo_from_json, build_config_from_json
40
41
42 # TODO: select markers based on daemons used during test
43 # pytest module level markers
44 """
45 pytestmark = pytest.mark.bfdd # single marker
46 pytestmark = [
47 pytest.mark.bgpd,
48 pytest.mark.ospfd,
49 pytest.mark.ospf6d
50 ] # multiple markers
51 """
52
53
54 # Reading the data from JSON File for topology and configuration creation
55 jsonFile = "{}/example_topojson_multiple_links.json".format(CWD)
56 try:
57 with open(jsonFile, "r") as topoJson:
58 topo = json.load(topoJson)
59 except IOError:
60 assert False, "Could not read file {}".format(jsonFile)
61
62 # Global variables
63 bgp_convergence = False
64 input_dict = {}
65
66
67 def build_topo(tgen):
68 "Build function"
69
70 # This function only purpose is to create topology
71 # as defined in input json file.
72 #
73 # Example
74 #
75 # Creating 2 routers having 2 links in between,
76 # one is used to establised BGP neighborship
77
78 # Building topology from json file
79 build_topo_from_json(tgen, topo)
80
81
82 def setup_module(mod):
83 """
84 Sets up the pytest environment
85
86 * `mod`: module name
87 """
88
89 testsuite_run_time = time.asctime(time.localtime(time.time()))
90 logger.info("Testsuite start time: {}".format(testsuite_run_time))
91 logger.info("=" * 40)
92
93 logger.info("Running setup_module to create topology")
94
95 # This function initiates the topology build with Topogen...
96 tgen = Topogen(build_topo, mod.__name__)
97 # ... and here it calls Mininet initialization functions.
98
99 # Starting topology, create tmp files which are loaded to routers
100 # to start daemons and then start routers
101 start_topology(tgen)
102
103 # This function only purpose is to create configuration
104 # as defined in input json file.
105 #
106 # Example
107 #
108 # Creating configuration defined in input JSON
109 # file, example, BGP config, interface config, static routes
110 # config, prefix list config
111
112 # Creating configuration from JSON
113 build_config_from_json(tgen, topo)
114
115 logger.info("Running setup_module() done")
116
117
118 def teardown_module(mod):
119 """
120 Teardown the pytest environment
121
122 * `mod`: module name
123 """
124
125 logger.info("Running teardown_module to delete topology")
126
127 tgen = get_topogen()
128
129 # Stop toplogy and Remove tmp files
130 tgen.stop_topology()
131
132
133 def test_bgp_convergence(request):
134 "Test BGP daemon convergence"
135
136 tgen = get_topogen()
137 global bgp_convergence
138 # test case name
139 tc_name = request.node.name
140 write_test_header(tc_name)
141
142 # Don't run this test if we have any failure.
143 if tgen.routers_have_failure():
144 pytest.skip(tgen.errors)
145
146 # Api call verify whether BGP is converged
147 bgp_convergence = verify_bgp_convergence(tgen, topo)
148 assert (
149 bgp_convergence is True
150 ), "test_bgp_convergence failed.. \n" " Error: {}".format(bgp_convergence)
151
152 logger.info("BGP is converged successfully \n")
153 write_test_footer(tc_name)
154
155
156 def test_static_routes(request):
157 "Test to create and verify static routes."
158
159 tgen = get_topogen()
160 if bgp_convergence is not True:
161 pytest.skip("skipped because of BGP Convergence failure")
162
163 # test case name
164 tc_name = request.node.name
165 write_test_header(tc_name)
166
167 # Static routes are created as part of initial configuration,
168 # verifying RIB
169 dut = "r3"
170 protocol = "bgp"
171 next_hop = "10.0.0.1"
172 input_dict = {"r1": topo["routers"]["r1"]}
173
174 # Uncomment below to debug
175 # tgen.mininet_cli()
176 result = verify_rib(tgen, "ipv4", dut, input_dict, next_hop=next_hop)
177 assert result is True, "Testcase {} :Failed \n Error: {}".format(tc_name, result)
178
179 write_test_footer(tc_name)
180
181
182 if __name__ == "__main__":
183 args = ["-s"] + sys.argv[1:]
184 sys.exit(pytest.main(args))