]>
Commit | Line | Data |
---|---|---|
e764d5fa PG |
1 | #!/usr/bin/env python |
2 | ||
3 | # | |
4 | # test_bgp_flowspec_topo.py | |
5 | # Part of NetDEF Topology Tests | |
6 | # | |
7 | # Copyright (c) 2019 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_flowspec_topo.py: Test BGP topology with Flowspec EBGP peering | |
26 | ||
27 | ||
28 | +------+------+ | |
29 | | peer1 | | |
30 | | BGP peer 1 | | |
31 | |192.168.0.161| | |
32 | | | | |
33 | +------+------+ | |
34 | .2 | r1-eth0 | |
35 | | | |
36 | ~~~~~~~~~ | |
37 | +---~~ s1 ~~------+ | |
38 | ~~ ~~ | |
39 | ~~~~~~~~~ | |
40 | | 10.0.1.1 r1-eth0 | |
41 | | 1001::1 r1-eth0 | |
42 | +--------+--------+ | |
43 | | r1 | | |
44 | |BGP 192.168.0.162| | |
45 | | | | |
46 | | | | |
47 | | | | |
48 | +-----------------+ | |
49 | ||
50 | """ | |
51 | ||
52 | import json | |
53 | import functools | |
54 | import os | |
55 | import sys | |
56 | import pytest | |
57 | import getopt | |
58 | ||
59 | # Save the Current Working Directory to find configuration files. | |
60 | CWD = os.path.dirname(os.path.realpath(__file__)) | |
61 | sys.path.append(os.path.join(CWD, "../")) | |
62 | ||
63 | # pylint: disable=C0413 | |
64 | # Import topogen and topotest helpers | |
65 | from lib import topotest | |
66 | from lib.topogen import Topogen, TopoRouter, get_topogen | |
67 | from lib.topolog import logger | |
68 | from lib.lutil import lUtil | |
69 | from lib.lutil import luCommand | |
70 | ||
71 | # Required to instantiate the topology builder class. | |
72 | from mininet.topo import Topo | |
73 | ||
74 | ##################################################### | |
75 | ## | |
76 | ## Network Topology Definition | |
77 | ## | |
78 | ##################################################### | |
79 | ||
80 | ||
81 | class BGPFLOWSPECTopo1(Topo): | |
82 | "BGP EBGP Flowspec Topology 1" | |
83 | ||
84 | def build(self, **_opts): | |
85 | tgen = get_topogen(self) | |
86 | ||
87 | # Setup Routers | |
88 | tgen.add_router("r1") | |
89 | ||
90 | # Setup Control Path Switch 1. r1-eth0 | |
91 | switch = tgen.add_switch("s1") | |
92 | switch.add_link(tgen.gears["r1"]) | |
93 | ||
94 | ## Add eBGP ExaBGP neighbors | |
95 | peer_ip = "10.0.1.101" ## peer | |
96 | peer_route = "via 10.0.1.1" ## router | |
97 | peer = tgen.add_exabgp_peer("peer1", ip=peer_ip, defaultRoute=peer_route) | |
98 | switch.add_link(peer) | |
99 | ||
100 | ||
101 | ##################################################### | |
102 | ## | |
103 | ## Tests starting | |
104 | ## | |
105 | ##################################################### | |
106 | ||
107 | ||
108 | def setup_module(module): | |
109 | tgen = Topogen(BGPFLOWSPECTopo1, module.__name__) | |
110 | ||
111 | tgen.start_topology() | |
112 | # check for zebra capability | |
113 | router = tgen.gears["r1"] | |
114 | ||
115 | # Get r1 reference and run Daemons | |
116 | logger.info("Launching BGP and ZEBRA on r1") | |
117 | router = tgen.gears["r1"] | |
118 | router.load_config( | |
119 | TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format("r1")) | |
120 | ) | |
121 | router.load_config( | |
122 | TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format("r1")) | |
123 | ) | |
124 | router.start() | |
125 | ||
126 | peer_list = tgen.exabgp_peers() | |
127 | for pname, peer in peer_list.iteritems(): | |
128 | peer_dir = os.path.join(CWD, pname) | |
129 | env_file = os.path.join(CWD, "exabgp.env") | |
130 | peer.start(peer_dir, env_file) | |
131 | logger.info(pname) | |
132 | ||
133 | ||
134 | def teardown_module(module): | |
135 | tgen = get_topogen() | |
136 | tgen.stop_topology() | |
137 | ||
138 | ||
139 | def test_bgp_convergence(): | |
140 | "Test for BGP topology convergence" | |
141 | tgen = get_topogen() | |
142 | ||
143 | # Skip if previous fatal error condition is raised | |
144 | ||
145 | if tgen.routers_have_failure(): | |
146 | pytest.skip(tgen.errors) | |
147 | ||
148 | logger.info("waiting for bgp convergence") | |
149 | ||
150 | # Expected result | |
151 | router = tgen.gears["r1"] | |
152 | reffile = os.path.join(CWD, "r1/summary.txt") | |
153 | ||
154 | expected = json.loads(open(reffile).read()) | |
155 | ||
156 | test_func = functools.partial( | |
157 | topotest.router_json_cmp, router, "show bgp summary json", expected | |
158 | ) | |
159 | _, res = topotest.run_and_expect(test_func, None, count=90, wait=0.5) | |
160 | assertmsg = "BGP router network did not converge" | |
161 | assert res is None, assertmsg | |
162 | ||
163 | ||
164 | def test_bgp_flowspec(): | |
165 | tgen = get_topogen() | |
166 | ||
167 | # Skip if previous fatal error condition is raised | |
168 | if tgen.routers_have_failure(): | |
169 | pytest.skip(tgen.errors) | |
170 | ||
171 | router = tgen.gears["r1"] | |
172 | ||
173 | logger.info("Check BGP FS entry for 3.3.3.3 with redirect IP") | |
174 | output = router.vtysh_cmd( | |
175 | "show bgp ipv4 flowspec 3.3.3.3", isjson=False, daemon="bgpd" | |
176 | ) | |
177 | logger.info(output) | |
178 | if ( | |
179 | "NH 50.0.0.2" not in output | |
180 | or "FS:redirect IP" not in output | |
181 | or "Packet Length < 200" not in output | |
182 | ): | |
183 | assertmsg = "traffic to 3.3.3.3 should have been detected as FS entry. NOK" | |
184 | assert 0, assertmsg | |
185 | else: | |
186 | logger.info("Check BGP FS entry for 3.3.3.3 with redirect IP OK") | |
187 | ||
188 | logger.info("Check BGP FS entry for 3::3 with redirect IP") | |
189 | output = router.vtysh_cmd( | |
190 | "show bgp ipv6 flowspec 3::3", isjson=False, daemon="bgpd" | |
191 | ) | |
192 | logger.info(output) | |
193 | if ( | |
194 | "NH 50::2" not in output | |
195 | or "FS:redirect IP" not in output | |
196 | or "Packet Length < 200" not in output | |
197 | ): | |
198 | assertmsg = "traffic to 3::3 should have been detected as FS entry. NOK" | |
199 | assert 0, assertmsg | |
200 | else: | |
201 | logger.info("Check BGP FS entry for 3::3 with redirect IP OK") | |
202 | ||
203 | if __name__ == "__main__": | |
204 | ||
205 | args = ["-s"] + sys.argv[1:] | |
206 | ret = pytest.main(args) | |
207 | ||
208 | sys.exit(ret) |