]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/example_topojson_test/test_topo_json_single_link/test_example_topojson.py
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / tests / topotests / example_topojson_test / test_topo_json_single_link / test_example_topojson.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 time
17 import json
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.json".format(CWD)
56
57 try:
58 with open(jsonFile, "r") as topoJson:
59 topo = json.load(topoJson)
60 except IOError:
61 assert False, "Could not read file {}".format(jsonFile)
62
63 # Global variables
64 bgp_convergence = False
65 input_dict = {}
66
67
68 def build_topo(tgen):
69 "Build function"
70
71 # This function only purpose is to create topology
72 # as defined in input json file.
73 #
74 # Example
75 #
76 # Creating 2 routers having single links in between,
77 # which is used to establised BGP neighborship
78
79 # Building topology from json file
80 build_topo_from_json(tgen, topo)
81
82
83 def setup_module(mod):
84 """
85 Sets up the pytest environment
86
87 * `mod`: module name
88 """
89
90 testsuite_run_time = time.asctime(time.localtime(time.time()))
91 logger.info("Testsuite start time: {}".format(testsuite_run_time))
92 logger.info("=" * 40)
93
94 logger.info("Running setup_module to create topology")
95
96 # This function initiates the topology build with Topogen...
97 tgen = Topogen(build_topo, mod.__name__)
98 # ... and here it calls Mininet initialization functions.
99
100 # Starting topology, create tmp files which are loaded to routers
101 # to start daemons and then start routers
102 start_topology(tgen)
103
104 # This function only purpose is to create configuration
105 # as defined in input json file.
106 #
107 # Example
108 #
109 # Creating configuration defined in input JSON
110 # file, example, BGP config, interface config, static routes
111 # config, prefix list config
112
113 # Creating configuration from JSON
114 build_config_from_json(tgen, topo)
115
116 logger.info("Running setup_module() done")
117
118
119 def teardown_module(mod):
120 """
121 Teardown the pytest environment
122
123 * `mod`: module name
124 """
125
126 logger.info("Running teardown_module to delete topology")
127
128 tgen = get_topogen()
129
130 # Stop toplogy and Remove tmp files
131 tgen.stop_topology()
132
133
134 def test_bgp_convergence(request):
135 "Test BGP daemon convergence"
136
137 tgen = get_topogen()
138 global bgp_convergence
139 # test case name
140 tc_name = request.node.name
141 write_test_header(tc_name)
142
143 # Don't run this test if we have any failure.
144 if tgen.routers_have_failure():
145 pytest.skip(tgen.errors)
146
147 # Api call verify whether BGP is converged
148 bgp_convergence = verify_bgp_convergence(tgen, topo)
149 assert (
150 bgp_convergence is True
151 ), "test_bgp_convergence failed.. \n" " Error: {}".format(bgp_convergence)
152
153 logger.info("BGP is converged successfully \n")
154 write_test_footer(tc_name)
155
156
157 def test_static_routes(request):
158 "Test to create and verify static routes."
159
160 tgen = get_topogen()
161 if bgp_convergence is not True:
162 pytest.skip("skipped because of BGP Convergence failure")
163
164 # test case name
165 tc_name = request.node.name
166 write_test_header(tc_name)
167
168 # Static routes are created as part of initial configuration,
169 # verifying RIB
170 dut = "r3"
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))