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