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