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