]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_ebgp_common_subnet_nexthop_unchanged/test_bgp-ebgp-common-subnet-nexthop-unchanged.py
doc: Add `show ipv6 rpf X:X::X:X` command to docs
[mirror_frr.git] / tests / topotests / bgp_ebgp_common_subnet_nexthop_unchanged / test_bgp-ebgp-common-subnet-nexthop-unchanged.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2021 by
4 # Donatas Abraitis <donatas.abraitis@gmail.com>
5 #
6 # Permission to use, copy, modify, and/or distribute this software
7 # for any purpose with or without fee is hereby granted, provided
8 # that the above copyright notice and this permission notice appear
9 # in all copies.
10 #
11 # THE SOFTWARE IS PROVIDED "AS IS" AND NETDEF DISCLAIMS ALL WARRANTIES
12 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETDEF BE LIABLE FOR
14 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
15 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
16 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
17 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
18 # OF THIS SOFTWARE.
19 #
20
21 """
22 https://tools.ietf.org/html/rfc4271
23
24 Check if NEXT_HOP attribute is not changed if peer X shares a
25 common subnet with this address.
26
27 - Otherwise, if the route being announced was learned from an
28 external peer, the speaker can use an IP address of any
29 adjacent router (known from the received NEXT_HOP attribute)
30 that the speaker itself uses for local route calculation in
31 the NEXT_HOP attribute, provided that peer X shares a common
32 subnet with this address. This is a second form of "third
33 party" NEXT_HOP attribute.
34 """
35
36 import os
37 import sys
38 import json
39 import pytest
40 import functools
41
42 pytestmark = [pytest.mark.bgpd]
43
44 CWD = os.path.dirname(os.path.realpath(__file__))
45 sys.path.append(os.path.join(CWD, "../"))
46
47 # pylint: disable=C0413
48 from lib import topotest
49 from lib.topogen import Topogen, TopoRouter, get_topogen
50
51 pytestmark = [pytest.mark.bgpd]
52
53
54 def build_topo(tgen):
55 for routern in range(1, 4):
56 tgen.add_router("r{}".format(routern))
57
58 switch = tgen.add_switch("s1")
59 switch.add_link(tgen.gears["r1"])
60 switch.add_link(tgen.gears["r2"])
61 switch.add_link(tgen.gears["r3"])
62
63
64 def setup_module(mod):
65 tgen = Topogen(build_topo, mod.__name__)
66 tgen.start_topology()
67
68 router_list = tgen.routers()
69
70 for i, (rname, router) in enumerate(router_list.items(), 1):
71 router.load_config(
72 TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname))
73 )
74 router.load_config(
75 TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format(rname))
76 )
77
78 tgen.start_router()
79
80
81 def teardown_module(mod):
82 tgen = get_topogen()
83 tgen.stop_topology()
84
85
86 def test_bgp_ebgp_common_subnet_nh_unchanged():
87 tgen = get_topogen()
88
89 if tgen.routers_have_failure():
90 pytest.skip(tgen.errors)
91
92 r2 = tgen.gears["r2"]
93 r3 = tgen.gears["r3"]
94
95 def _bgp_converge(router):
96 output = json.loads(router.vtysh_cmd("show ip bgp summary json"))
97 expected = {
98 "ipv4Unicast": {
99 "peers": {
100 "192.168.1.1": {"state": "Established"},
101 "192.168.1.103": {"state": "Established"},
102 }
103 }
104 }
105 return topotest.json_cmp(output, expected)
106
107 test_func = functools.partial(_bgp_converge, r3)
108 success, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
109
110 assert result is None, 'Failed bgp convergence in "{}"'.format(r3)
111
112 def _bgp_nh_unchanged(router):
113 output = json.loads(router.vtysh_cmd("show ip bgp 172.16.1.1/32 json"))
114 expected = {"paths": [{"nexthops": [{"ip": "192.168.1.1"}]}]}
115 return topotest.json_cmp(output, expected)
116
117 test_func = functools.partial(_bgp_nh_unchanged, r2)
118 success, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
119
120 assert result is None, 'Wrong next-hop in "{}"'.format(r2)
121
122
123 if __name__ == "__main__":
124 args = ["-s"] + sys.argv[1:]
125 sys.exit(pytest.main(args))