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