2 # SPDX-License-Identifier: ISC
4 # Copyright (c) 2021 by
5 # Donatas Abraitis <donatas.abraitis@gmail.com>
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
20 CWD
= os
.path
.dirname(os
.path
.realpath(__file__
))
21 sys
.path
.append(os
.path
.join(CWD
, "../"))
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
28 pytestmark
= [pytest
.mark
.bgpd
]
32 for routern
in range(1, 3):
33 tgen
.add_router("r{}".format(routern
))
35 switch
= tgen
.add_switch("s1")
36 switch
.add_link(tgen
.gears
["r1"])
37 switch
.add_link(tgen
.gears
["r2"])
40 def setup_module(mod
):
41 tgen
= Topogen(build_topo
, mod
.__name
__)
44 router_list
= tgen
.routers()
46 for i
, (rname
, router
) in enumerate(router_list
.items(), 1):
48 TopoRouter
.RD_ZEBRA
, os
.path
.join(CWD
, "{}/zebra.conf".format(rname
))
51 TopoRouter
.RD_BGP
, os
.path
.join(CWD
, "{}/bgpd.conf".format(rname
))
57 def teardown_module(mod
):
62 def test_bgp_default_originate_route_map():
65 if tgen
.routers_have_failure():
66 pytest
.skip(tgen
.errors
)
68 router
= tgen
.gears
["r2"]
70 def _bgp_converge(router
):
71 output
= json
.loads(router
.vtysh_cmd("show ip bgp neighbor 192.168.255.1 json"))
74 "bgpState": "Established",
75 "addressFamilyInfo": {"ipv4Unicast": {"acceptedPrefixCounter": 1}},
78 return topotest
.json_cmp(output
, expected
)
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
)
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"
90 step("Withdraw 10.0.0.0/22 from R2")
92 "conf t\nrouter bgp\naddress-family ipv4\nno redistribute connected"
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"
100 step("Announce 10.0.0.0/22 from R2")
101 router
.vtysh_cmd("conf t\nrouter bgp\naddress-family ipv4\nredistribute connected")
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"
108 step("Withdraw 10.0.0.0/22 from R2 again")
110 "conf t\nrouter bgp\naddress-family ipv4\nno redistribute connected"
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"
119 if __name__
== "__main__":
120 args
= ["-s"] + sys
.argv
[1:]
121 sys
.exit(pytest
.main(args
))