]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_max_med_on_startup/test_bgp_max_med_on_startup.py
a83d43310bf9737b5808aa0301488e3f2cf2d1c8
[mirror_frr.git] / tests / topotests / bgp_max_med_on_startup / test_bgp_max_med_on_startup.py
1 #!/usr/bin/env python
2
3 #
4 # test_bgp_max_med_on_startup.py
5 #
6 # Copyright (c) 2022 Rubicon Communications, LLC.
7 #
8 # Permission to use, copy, modify, and/or distribute this software
9 # for any purpose with or without fee is hereby granted, provided
10 # that the above copyright notice and this permission notice appear
11 # in all copies.
12 #
13 # THE SOFTWARE IS PROVIDED "AS IS" AND NETDEF DISCLAIMS ALL WARRANTIES
14 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETDEF BE LIABLE FOR
16 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
17 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
18 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 # OF THIS SOFTWARE.
21 #
22
23 """
24 Test whether `bgp max-med on-startup (5-86400) [(0-4294967295)]` is working
25 correctly.
26 """
27
28 import os
29 import sys
30 import json
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
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_max_med_on_startup():
76 tgen = get_topogen()
77
78 if tgen.routers_have_failure():
79 pytest.skip(tgen.errors)
80
81 router1 = tgen.gears["r1"]
82 router2 = tgen.gears["r2"]
83
84 def _bgp_converge(router):
85 output = json.loads(router.vtysh_cmd("show ip bgp neighbor 192.168.255.1 json"))
86 expected = {"192.168.255.1": {"bgpState": "Established"}}
87 return topotest.json_cmp(output, expected)
88
89 def _bgp_has_routes(router, metric):
90 output = json.loads(
91 router.vtysh_cmd("show ip bgp neighbor 192.168.255.1 routes json")
92 )
93 expected = {"routes": {"172.16.255.254/32": [{"metric": metric}]}}
94 return topotest.json_cmp(output, expected)
95
96 # Check session is established
97 test_func = functools.partial(_bgp_converge, router2)
98 success, result = topotest.run_and_expect(test_func, None, count=30, wait=0.5)
99 assert result is None, "Failed bgp convergence on r2"
100
101 # Check metric has value of max-med
102 test_func = functools.partial(_bgp_has_routes, router2, 777)
103 success, result = topotest.run_and_expect(test_func, None, count=30, wait=0.5)
104 assert result is None, "r2 does not receive routes with metric 777"
105
106 # Check that when the max-med timer expires, metric is updated
107 test_func = functools.partial(_bgp_has_routes, router2, 0)
108 success, result = topotest.run_and_expect(test_func, None, count=16, wait=0.5)
109 assert result is None, "r2 does not receive routes with metric 0"
110
111
112 if __name__ == "__main__":
113 args = ["-s"] + sys.argv[1:]
114 sys.exit(pytest.main(args))