]>
Commit | Line | Data |
---|---|---|
66e98bc1 | 1 | #!/usr/bin/env python |
acddc0ed | 2 | # SPDX-License-Identifier: ISC |
66e98bc1 AP |
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 | # | |
66e98bc1 AP |
9 | |
10 | """ | |
11 | <example>.py: Test <example tests>. | |
12 | """ | |
13 | ||
14 | import os | |
15 | import sys | |
16 | import time | |
17 | import json | |
66e98bc1 AP |
18 | import pytest |
19 | ||
20 | # Save the Current Working Directory to find configuration files. | |
21 | CWD = os.path.dirname(os.path.realpath(__file__)) | |
787e7624 | 22 | sys.path.append(os.path.join(CWD, "../")) |
23 | sys.path.append(os.path.join(CWD, "../../")) | |
66e98bc1 AP |
24 | |
25 | # pylint: disable=C0413 | |
26 | # Import topogen and topotest helpers | |
787e7624 | 27 | from lib.topogen import Topogen, get_topogen |
66e98bc1 AP |
28 | |
29 | # Required to instantiate the topology builder class. | |
66e98bc1 AP |
30 | |
31 | # Import topoJson from lib, to create topology and initial configuration | |
32 | from lib.common_config import ( | |
787e7624 | 33 | start_topology, |
34 | write_test_header, | |
35 | write_test_footer, | |
36 | verify_rib, | |
66e98bc1 AP |
37 | ) |
38 | from lib.topolog import logger | |
787e7624 | 39 | from lib.bgp import verify_bgp_convergence |
66e98bc1 AP |
40 | from lib.topojson import build_topo_from_json, build_config_from_json |
41 | ||
6bd548aa | 42 | |
5980ad0a | 43 | # TODO: select markers based on daemons used during test |
6bd548aa DS |
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 | ||
66e98bc1 AP |
55 | # Reading the data from JSON File for topology and configuration creation |
56 | jsonFile = "{}/example_topojson.json".format(CWD) | |
57 | ||
58 | try: | |
787e7624 | 59 | with open(jsonFile, "r") as topoJson: |
66e98bc1 AP |
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 | ||
e82b531d CH |
69 | def build_topo(tgen): |
70 | "Build function" | |
66e98bc1 | 71 | |
e82b531d CH |
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 | |
66e98bc1 | 79 | |
e82b531d CH |
80 | # Building topology from json file |
81 | build_topo_from_json(tgen, topo) | |
66e98bc1 AP |
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)) | |
787e7624 | 93 | logger.info("=" * 40) |
66e98bc1 AP |
94 | |
95 | logger.info("Running setup_module to create topology") | |
96 | ||
97 | # This function initiates the topology build with Topogen... | |
e82b531d | 98 | tgen = Topogen(build_topo, mod.__name__) |
66e98bc1 AP |
99 | # ... and here it calls Mininet initialization functions. |
100 | ||
101 | # Starting topology, create tmp files which are loaded to routers | |
d60a3f0e | 102 | # to start daemons and then start routers |
66e98bc1 AP |
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 | |
6bb29e5e | 132 | tgen.stop_topology() |
66e98bc1 AP |
133 | |
134 | ||
135 | def test_bgp_convergence(request): | |
a53c08bc | 136 | "Test BGP daemon convergence" |
66e98bc1 AP |
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) | |
787e7624 | 150 | assert ( |
151 | bgp_convergence is True | |
152 | ), "test_bgp_convergence failed.. \n" " Error: {}".format(bgp_convergence) | |
66e98bc1 AP |
153 | |
154 | logger.info("BGP is converged successfully \n") | |
155 | write_test_footer(tc_name) | |
156 | ||
157 | ||
158 | def test_static_routes(request): | |
a53c08bc | 159 | "Test to create and verify static routes." |
66e98bc1 AP |
160 | |
161 | tgen = get_topogen() | |
162 | if bgp_convergence is not True: | |
787e7624 | 163 | pytest.skip("skipped because of BGP Convergence failure") |
66e98bc1 AP |
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 | |
787e7624 | 171 | dut = "r3" |
172 | next_hop = ["10.0.0.1", "10.0.0.5"] | |
66e98bc1 AP |
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, | |
787e7624 | 180 | "next_hop": "10.0.0.1", |
66e98bc1 AP |
181 | } |
182 | ] | |
183 | } | |
184 | } | |
185 | # Uncomment below to debug | |
186 | # tgen.mininet_cli() | |
787e7624 | 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) | |
66e98bc1 AP |
189 | |
190 | write_test_footer(tc_name) | |
191 | ||
192 | ||
787e7624 | 193 | if __name__ == "__main__": |
66e98bc1 AP |
194 | args = ["-s"] + sys.argv[1:] |
195 | sys.exit(pytest.main(args)) |