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