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