]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_default_route_route_map_match2/test_bgp_default-originate_route-map_match2.py
doc: Add `show ipv6 rpf X:X::X:X` command to docs
[mirror_frr.git] / tests / topotests / bgp_default_route_route_map_match2 / test_bgp_default-originate_route-map_match2.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 Test if default-originate works with conditional match.
23 If 10.0.0.0/22 is recived from r2, then we announce 0.0.0.0/0
24 to r2.
25 """
26
27 import os
28 import sys
29 import json
30 import pytest
31 import functools
32
33 CWD = os.path.dirname(os.path.realpath(__file__))
34 sys.path.append(os.path.join(CWD, "../"))
35
36 # pylint: disable=C0413
37 from lib import topotest
38 from lib.topogen import Topogen, TopoRouter, get_topogen
39 from lib.common_config import step
40
41 pytestmark = [pytest.mark.bgpd]
42
43
44 def build_topo(tgen):
45 for routern in range(1, 3):
46 tgen.add_router("r{}".format(routern))
47
48 switch = tgen.add_switch("s1")
49 switch.add_link(tgen.gears["r1"])
50 switch.add_link(tgen.gears["r2"])
51
52
53 def setup_module(mod):
54 tgen = Topogen(build_topo, mod.__name__)
55 tgen.start_topology()
56
57 router_list = tgen.routers()
58
59 for i, (rname, router) in enumerate(router_list.items(), 1):
60 router.load_config(
61 TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname))
62 )
63 router.load_config(
64 TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format(rname))
65 )
66
67 tgen.start_router()
68
69
70 def teardown_module(mod):
71 tgen = get_topogen()
72 tgen.stop_topology()
73
74
75 def test_bgp_default_originate_route_map():
76 tgen = get_topogen()
77
78 if tgen.routers_have_failure():
79 pytest.skip(tgen.errors)
80
81 router = tgen.gears["r2"]
82
83 def _bgp_converge(router):
84 output = json.loads(router.vtysh_cmd("show ip bgp neighbor 192.168.255.1 json"))
85 expected = {
86 "192.168.255.1": {
87 "bgpState": "Established",
88 "addressFamilyInfo": {"ipv4Unicast": {"acceptedPrefixCounter": 1}},
89 }
90 }
91 return topotest.json_cmp(output, expected)
92
93 def _bgp_default_route_is_valid(router):
94 output = json.loads(router.vtysh_cmd("show ip bgp 0.0.0.0/0 json"))
95 expected = {"paths": [{"valid": True}]}
96 return topotest.json_cmp(output, expected)
97
98 step("Converge network")
99 test_func = functools.partial(_bgp_converge, router)
100 success, result = topotest.run_and_expect(test_func, None, count=30, wait=0.5)
101 assert result is None, "Failed to see bgp convergence at r2"
102
103 step("Withdraw 10.0.0.0/22 from R2")
104 router.vtysh_cmd(
105 "conf t\nrouter bgp\naddress-family ipv4\nno redistribute connected"
106 )
107
108 step("Check if we don't have 0.0.0.0/0 at R2")
109 test_func = functools.partial(_bgp_default_route_is_valid, router)
110 success, result = topotest.run_and_expect(test_func, not None, count=30, wait=0.5)
111 assert result is not None, "0.0.0.0/0 exists at r2"
112
113 step("Announce 10.0.0.0/22 from R2")
114 router.vtysh_cmd("conf t\nrouter bgp\naddress-family ipv4\nredistribute connected")
115
116 step("Check if we have 0.0.0.0/0 at R2")
117 test_func = functools.partial(_bgp_default_route_is_valid, router)
118 success, result = topotest.run_and_expect(test_func, None, count=30, wait=0.5)
119 assert result is None, "0.0.0.0/0 does not exist at r2"
120
121 step("Withdraw 10.0.0.0/22 from R2 again")
122 router.vtysh_cmd(
123 "conf t\nrouter bgp\naddress-family ipv4\nno redistribute connected"
124 )
125
126 step("Check if we don't have 0.0.0.0/0 at R2 again")
127 test_func = functools.partial(_bgp_default_route_is_valid, router)
128 success, result = topotest.run_and_expect(test_func, not None, count=30, wait=0.5)
129 assert result is not None, "0.0.0.0/0 exists at r2"
130
131
132 if __name__ == "__main__":
133 args = ["-s"] + sys.argv[1:]
134 sys.exit(pytest.main(args))