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