]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_sender_as_path_loop_detection/test_bgp_sender-as-path-loop-detection.py
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / tests / topotests / bgp_sender_as_path_loop_detection / test_bgp_sender-as-path-loop-detection.py
1 #!/usr/bin/env python
2 # SPDX-License-Identifier: ISC
3
4 #
5 # test_bgp_sender-as-path-loop-detection.py
6 # Part of NetDEF Topology Tests
7 #
8 # Copyright (c) 2019 by
9 # Donatas Abraitis <donatas.abraitis@gmail.com>
10 #
11
12 """
13 Test if neighbor <neighbor> sender-as-path-loop-detection
14 command works as expeced.
15 """
16
17 import os
18 import sys
19 import json
20 import pytest
21 import functools
22
23 CWD = os.path.dirname(os.path.realpath(__file__))
24 sys.path.append(os.path.join(CWD, "../"))
25
26 # pylint: disable=C0413
27 from lib import topotest
28 from lib.topogen import Topogen, TopoRouter, get_topogen
29
30
31 pytestmark = [pytest.mark.bgpd]
32
33
34 def build_topo(tgen):
35 for routern in range(1, 4):
36 tgen.add_router("r{}".format(routern))
37
38 switch = tgen.add_switch("s1")
39 switch.add_link(tgen.gears["r1"])
40 switch.add_link(tgen.gears["r2"])
41
42 switch = tgen.add_switch("s2")
43 switch.add_link(tgen.gears["r2"])
44 switch.add_link(tgen.gears["r3"])
45
46
47 def setup_module(mod):
48 tgen = Topogen(build_topo, mod.__name__)
49 tgen.start_topology()
50
51 router_list = tgen.routers()
52
53 for i, (rname, router) in enumerate(router_list.items(), 1):
54 router.load_config(
55 TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname))
56 )
57 router.load_config(
58 TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format(rname))
59 )
60
61 tgen.start_router()
62
63
64 def teardown_module(mod):
65 tgen = get_topogen()
66 tgen.stop_topology()
67
68
69 def test_bgp_sender_as_path_loop_detection():
70 tgen = get_topogen()
71
72 if tgen.routers_have_failure():
73 pytest.skip(tgen.errors)
74
75 r2 = tgen.gears["r2"]
76
77 def _bgp_converge():
78 output = json.loads(r2.vtysh_cmd("show ip bgp neighbor 192.168.255.2 json"))
79 expected = {
80 "192.168.255.2": {
81 "bgpState": "Established",
82 "addressFamilyInfo": {"ipv4Unicast": {"acceptedPrefixCounter": 3}},
83 }
84 }
85 return topotest.json_cmp(output, expected)
86
87 def _bgp_has_route_from_r1():
88 output = json.loads(r2.vtysh_cmd("show ip bgp 172.16.255.253/32 json"))
89 expected = {
90 "paths": [
91 {
92 "aspath": {
93 "segments": [{"type": "as-sequence", "list": [65001, 65003]}],
94 "length": 2,
95 }
96 }
97 ]
98 }
99 return topotest.json_cmp(output, expected)
100
101 def _bgp_suppress_route_to_r1():
102 output = json.loads(
103 r2.vtysh_cmd("show ip bgp neighbor 192.168.255.2 advertised-routes json")
104 )
105 expected = {"totalPrefixCounter": 0}
106 return topotest.json_cmp(output, expected)
107
108 def _bgp_suppress_route_to_r3():
109 output = json.loads(
110 r2.vtysh_cmd("show ip bgp neighbor 192.168.254.2 advertised-routes json")
111 )
112 expected = {"totalPrefixCounter": 2}
113 return topotest.json_cmp(output, expected)
114
115 test_func = functools.partial(_bgp_converge)
116 _, result = topotest.run_and_expect(test_func, None, count=30, wait=0.5)
117 assert result is None, "Failed bgp to convergence"
118
119 test_func = functools.partial(_bgp_has_route_from_r1)
120 _, result = topotest.run_and_expect(test_func, None, count=30, wait=0.5)
121 assert result is None, "Failed to see a route from r1"
122
123 test_func = functools.partial(_bgp_suppress_route_to_r3)
124 _, result = topotest.run_and_expect(test_func, None, count=30, wait=0.5)
125 assert result is None, "Route 172.16.255.253/32 should not be sent to r3 from r2"
126
127 test_func = functools.partial(_bgp_suppress_route_to_r1)
128 _, result = topotest.run_and_expect(test_func, None, count=30, wait=0.5)
129 assert result is None, "Routes should not be sent to r1 from r2"
130
131
132 if __name__ == "__main__":
133 args = ["-s"] + sys.argv[1:]
134 sys.exit(pytest.main(args))