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