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