]>
Commit | Line | Data |
---|---|---|
12919c42 PG |
1 | #!/usr/bin/env python |
2 | ||
3 | # | |
4 | # test_bgp_vrf_netns_topo1.py | |
5 | # Part of NetDEF Topology Tests | |
6 | # | |
7 | # Copyright (c) 2018 by 6WIND | |
8 | # | |
9 | # Permission to use, copy, modify, and/or distribute this software | |
10 | # for any purpose with or without fee is hereby granted, provided | |
11 | # that the above copyright notice and this permission notice appear | |
12 | # in all copies. | |
13 | # | |
14 | # THE SOFTWARE IS PROVIDED "AS IS" AND NETDEF DISCLAIMS ALL WARRANTIES | |
15 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
16 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETDEF BE LIABLE FOR | |
17 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY | |
18 | # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, | |
19 | # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | |
20 | # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE | |
21 | # OF THIS SOFTWARE. | |
22 | # | |
23 | ||
24 | """ | |
25 | test_bgp_vrf_netns_topo1.py: Test BGP topology with EBGP on NETNS VRF | |
26 | """ | |
27 | ||
28 | import json | |
29 | import os | |
30 | import sys | |
e3060696 | 31 | import functools |
12919c42 | 32 | import pytest |
12919c42 PG |
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, "../")) |
12919c42 PG |
37 | |
38 | # pylint: disable=C0413 | |
39 | # Import topogen and topotest helpers | |
40 | from lib import topotest | |
41 | from lib.topogen import Topogen, TopoRouter, get_topogen | |
42 | from lib.topolog import logger | |
43 | ||
44 | # Required to instantiate the topology builder class. | |
12919c42 | 45 | |
bf3a0a9a DS |
46 | pytestmark = [pytest.mark.bgpd] |
47 | ||
48 | ||
12919c42 PG |
49 | total_ebgp_peers = 1 |
50 | CustomizeVrfWithNetns = True | |
51 | ||
52 | ##################################################### | |
53 | ## | |
54 | ## Network Topology Definition | |
55 | ## | |
56 | ##################################################### | |
57 | ||
787e7624 | 58 | |
e82b531d CH |
59 | def build_topo(tgen): |
60 | tgen.add_router("r1") | |
12919c42 | 61 | |
e82b531d CH |
62 | # Setup Switches |
63 | switch = tgen.add_switch("s1") | |
64 | switch.add_link(tgen.gears["r1"]) | |
12919c42 | 65 | |
e82b531d CH |
66 | # Add eBGP ExaBGP neighbors |
67 | peer_ip = "10.0.1.101" | |
68 | peer_route = "via 10.0.1.1" | |
69 | peer = tgen.add_exabgp_peer("peer1", ip=peer_ip, defaultRoute=peer_route) | |
70 | switch = tgen.gears["s1"] | |
71 | switch.add_link(peer) | |
12919c42 PG |
72 | |
73 | ||
74 | ##################################################### | |
75 | ## | |
76 | ## Tests starting | |
77 | ## | |
78 | ##################################################### | |
79 | ||
787e7624 | 80 | |
12919c42 | 81 | def setup_module(module): |
e82b531d | 82 | tgen = Topogen(build_topo, module.__name__) |
12919c42 PG |
83 | tgen.start_topology() |
84 | ||
85 | # Get r1 reference | |
787e7624 | 86 | router = tgen.gears["r1"] |
12919c42 PG |
87 | |
88 | # check for zebra capability | |
89 | if CustomizeVrfWithNetns == True: | |
787e7624 | 90 | if router.check_capability(TopoRouter.RD_ZEBRA, "--vrfwnetns") == False: |
91 | return pytest.skip( | |
92 | "Skipping BGP VRF NETNS Test. VRF NETNS backend not available on FRR" | |
93 | ) | |
94 | if os.system("ip netns list") != 0: | |
95 | return pytest.skip( | |
96 | "Skipping BGP VRF NETNS Test. NETNS not available on System" | |
97 | ) | |
12919c42 PG |
98 | # retrieve VRF backend kind |
99 | if CustomizeVrfWithNetns == True: | |
787e7624 | 100 | logger.info("Testing with VRF Namespace support") |
12919c42 | 101 | |
6a95bfc8 CH |
102 | # create VRF r1-bgp-cust1 |
103 | # move r1-eth0 to VRF r1-bgp-cust1 | |
8db751b8 CH |
104 | |
105 | ns = "{}-bgp-cust1".format("r1") | |
106 | router.net.add_netns(ns) | |
107 | router.net.set_intf_netns("r1-eth0", ns, up=True) | |
108 | ||
787e7624 | 109 | # run daemons |
12919c42 PG |
110 | router.load_config( |
111 | TopoRouter.RD_ZEBRA, | |
787e7624 | 112 | os.path.join(CWD, "{}/zebra.conf".format("r1")), |
113 | "--vrfwnetns", | |
12919c42 PG |
114 | ) |
115 | router.load_config( | |
787e7624 | 116 | TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format("r1")) |
12919c42 PG |
117 | ) |
118 | ||
787e7624 | 119 | logger.info("Launching BGP and ZEBRA") |
12919c42 PG |
120 | # BGP and ZEBRA start without underlying VRF |
121 | router.start() | |
122 | # Starting Hosts and init ExaBGP on each of them | |
787e7624 | 123 | logger.info("starting exaBGP on peer1") |
12919c42 | 124 | peer_list = tgen.exabgp_peers() |
e5f0ed14 | 125 | for pname, peer in peer_list.items(): |
12919c42 | 126 | peer_dir = os.path.join(CWD, pname) |
787e7624 | 127 | env_file = os.path.join(CWD, "exabgp.env") |
128 | logger.info("Running ExaBGP peer") | |
12919c42 PG |
129 | peer.start(peer_dir, env_file) |
130 | logger.info(pname) | |
131 | ||
787e7624 | 132 | |
12919c42 PG |
133 | def teardown_module(module): |
134 | tgen = get_topogen() | |
8db751b8 CH |
135 | |
136 | # Move interfaces out of vrf namespace and delete the namespace | |
137 | tgen.net["r1"].reset_intf_netns("r1-eth0") | |
138 | tgen.net["r1"].delete_netns("r1-bgp-cust1") | |
12919c42 PG |
139 | |
140 | tgen.stop_topology() | |
141 | ||
787e7624 | 142 | |
12919c42 PG |
143 | def test_bgp_vrf_learn(): |
144 | "Test daemon learnt VRF context" | |
145 | tgen = get_topogen() | |
146 | ||
147 | # Skip if previous fatal error condition is raised | |
148 | if tgen.routers_have_failure(): | |
149 | pytest.skip(tgen.errors) | |
150 | ||
151 | # Expected result | |
787e7624 | 152 | output = tgen.gears["r1"].vtysh_cmd("show vrf", isjson=False) |
153 | logger.info("output is: {}".format(output)) | |
12919c42 | 154 | |
787e7624 | 155 | output = tgen.gears["r1"].vtysh_cmd("show bgp vrfs", isjson=False) |
156 | logger.info("output is: {}".format(output)) | |
12919c42 | 157 | |
e3060696 | 158 | |
12919c42 PG |
159 | def test_bgp_convergence(): |
160 | "Test for BGP topology convergence" | |
161 | tgen = get_topogen() | |
162 | ||
163 | # uncomment if you want to troubleshoot | |
164 | # tgen.mininet_cli() | |
165 | # Skip if previous fatal error condition is raised | |
166 | if tgen.routers_have_failure(): | |
167 | pytest.skip(tgen.errors) | |
168 | ||
787e7624 | 169 | logger.info("waiting for bgp convergence") |
12919c42 PG |
170 | |
171 | # Expected result | |
787e7624 | 172 | router = tgen.gears["r1"] |
173 | if router.has_version("<", "3.0"): | |
174 | reffile = os.path.join(CWD, "r1/summary20.txt") | |
12919c42 | 175 | else: |
787e7624 | 176 | reffile = os.path.join(CWD, "r1/summary.txt") |
12919c42 PG |
177 | |
178 | expected = json.loads(open(reffile).read()) | |
179 | ||
787e7624 | 180 | test_func = functools.partial( |
6a95bfc8 | 181 | topotest.router_json_cmp, router, "show bgp vrf r1-bgp-cust1 summary json", expected |
787e7624 | 182 | ) |
e3060696 | 183 | _, res = topotest.run_and_expect(test_func, None, count=90, wait=0.5) |
787e7624 | 184 | assertmsg = "BGP router network did not converge" |
12919c42 PG |
185 | assert res is None, assertmsg |
186 | ||
787e7624 | 187 | |
12919c42 PG |
188 | def test_bgp_vrf_netns(): |
189 | tgen = get_topogen() | |
190 | ||
191 | # Skip if previous fatal error condition is raised | |
192 | if tgen.routers_have_failure(): | |
193 | pytest.skip(tgen.errors) | |
194 | ||
195 | expect = { | |
787e7624 | 196 | "routerId": "10.0.1.1", |
197 | "routes": {}, | |
12919c42 PG |
198 | } |
199 | ||
200 | for subnet in range(0, 10): | |
787e7624 | 201 | netkey = "10.201.{}.0/24".format(subnet) |
202 | expect["routes"][netkey] = [] | |
203 | peer = {"valid": True} | |
204 | expect["routes"][netkey].append(peer) | |
205 | ||
206 | test_func = functools.partial( | |
207 | topotest.router_json_cmp, | |
208 | tgen.gears["r1"], | |
6a95bfc8 | 209 | "show ip bgp vrf r1-bgp-cust1 ipv4 json", |
787e7624 | 210 | expect, |
211 | ) | |
e3060696 | 212 | _, res = topotest.run_and_expect(test_func, None, count=12, wait=0.5) |
6a95bfc8 | 213 | assertmsg = 'expected routes in "show ip bgp vrf r1-bgp-cust1 ipv4" output' |
12919c42 PG |
214 | assert res is None, assertmsg |
215 | ||
787e7624 | 216 | |
217 | if __name__ == "__main__": | |
12919c42 PG |
218 | |
219 | args = ["-s"] + sys.argv[1:] | |
220 | ret = pytest.main(args) | |
221 | ||
222 | sys.exit(ret) |