3 # Copyright (c) 2021 by
4 # Donatas Abraitis <donatas.abraitis@gmail.com>
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
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
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
34 CWD
= os
.path
.dirname(os
.path
.realpath(__file__
))
35 sys
.path
.append(os
.path
.join(CWD
, "../"))
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
44 pytestmark
= [pytest
.mark
.bgpd
]
47 class TemplateTopo(Topo
):
48 def build(self
, *_args
, **_opts
):
49 tgen
= get_topogen(self
)
51 for routern
in range(1, 3):
52 tgen
.add_router("r{}".format(routern
))
54 switch
= tgen
.add_switch("s1")
55 switch
.add_link(tgen
.gears
["r1"])
56 switch
.add_link(tgen
.gears
["r2"])
59 def setup_module(mod
):
60 tgen
= Topogen(TemplateTopo
, mod
.__name
__)
63 router_list
= tgen
.routers()
65 for i
, (rname
, router
) in enumerate(router_list
.items(), 1):
67 TopoRouter
.RD_ZEBRA
, os
.path
.join(CWD
, "{}/zebra.conf".format(rname
))
70 TopoRouter
.RD_BGP
, os
.path
.join(CWD
, "{}/bgpd.conf".format(rname
))
76 def teardown_module(mod
):
81 def test_bgp_default_originate_route_map():
84 if tgen
.routers_have_failure():
85 pytest
.skip(tgen
.errors
)
87 router
= tgen
.gears
["r2"]
89 def _bgp_converge(router
):
90 output
= json
.loads(router
.vtysh_cmd("show ip bgp neighbor 192.168.255.1 json"))
93 "bgpState": "Established",
94 "addressFamilyInfo": {"ipv4Unicast": {"acceptedPrefixCounter": 1}},
97 return topotest
.json_cmp(output
, expected
)
99 def _bgp_default_route_is_valid(router
):
100 output
= json
.loads(router
.vtysh_cmd("show ip bgp 0.0.0.0/0 json"))
101 expected
= {"paths": [{"valid": True}]}
102 return topotest
.json_cmp(output
, expected
)
104 step("Converge network")
105 test_func
= functools
.partial(_bgp_converge
, router
)
106 success
, result
= topotest
.run_and_expect(test_func
, None, count
=30, wait
=0.5)
107 assert result
is None, "Failed to see bgp convergence at r2"
109 step("Withdraw 10.0.0.0/22 from R2")
111 "conf t\nrouter bgp\naddress-family ipv4\nno redistribute connected"
114 step("Check if we don't have 0.0.0.0/0 at R2")
115 test_func
= functools
.partial(_bgp_default_route_is_valid
, router
)
116 success
, result
= topotest
.run_and_expect(test_func
, not None, count
=30, wait
=0.5)
117 assert result
is not None, "0.0.0.0/0 exists at r2"
119 step("Announce 10.0.0.0/22 from R2")
120 router
.vtysh_cmd("conf t\nrouter bgp\naddress-family ipv4\nredistribute connected")
122 step("Check if we have 0.0.0.0/0 at R2")
123 test_func
= functools
.partial(_bgp_default_route_is_valid
, router
)
124 success
, result
= topotest
.run_and_expect(test_func
, None, count
=30, wait
=0.5)
125 assert result
is None, "0.0.0.0/0 does not exist at r2"
127 step("Withdraw 10.0.0.0/22 from R2 again")
129 "conf t\nrouter bgp\naddress-family ipv4\nno redistribute connected"
132 step("Check if we don't have 0.0.0.0/0 at R2 again")
133 test_func
= functools
.partial(_bgp_default_route_is_valid
, router
)
134 success
, result
= topotest
.run_and_expect(test_func
, not None, count
=30, wait
=0.5)
135 assert result
is not None, "0.0.0.0/0 exists at r2"
138 if __name__
== "__main__":
139 args
= ["-s"] + sys
.argv
[1:]
140 sys
.exit(pytest
.main(args
))