]>
Commit | Line | Data |
---|---|---|
03252d60 DA |
1 | #!/usr/bin/env python |
2 | ||
3 | # | |
4 | # bgp_set_local-preference_add_subtract.py | |
5 | # Part of NetDEF Topology Tests | |
6 | # | |
7 | # Copyright (c) 2020 by | |
8 | # Donatas Abraitis <donatas.abraitis@gmail.com> | |
9 | # | |
10 | # Permission to use, copy, modify, and/or distribute this software | |
11 | # for any purpose with or without fee is hereby granted, provided | |
12 | # that the above copyright notice and this permission notice appear | |
13 | # in all copies. | |
14 | # | |
15 | # THE SOFTWARE IS PROVIDED "AS IS" AND NETDEF DISCLAIMS ALL WARRANTIES | |
16 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
17 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETDEF BE LIABLE FOR | |
18 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY | |
19 | # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, | |
20 | # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | |
21 | # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE | |
22 | # OF THIS SOFTWARE. | |
23 | # | |
24 | ||
25 | """ | |
26 | bgp_set_local-preference_add_subtract.py: | |
27 | Test if we can add/subtract the value to/from an existing | |
28 | LOCAL_PREF in route-maps. | |
29 | """ | |
30 | ||
31 | import os | |
32 | import sys | |
33 | import json | |
34 | import time | |
35 | import pytest | |
36 | import functools | |
37 | ||
38 | CWD = os.path.dirname(os.path.realpath(__file__)) | |
787e7624 | 39 | sys.path.append(os.path.join(CWD, "../")) |
03252d60 DA |
40 | |
41 | # pylint: disable=C0413 | |
42 | from lib import topotest | |
43 | from lib.topogen import Topogen, TopoRouter, get_topogen | |
44 | from lib.topolog import logger | |
8db751b8 | 45 | from lib.micronet_compat import Topo |
03252d60 | 46 | |
bf3a0a9a DS |
47 | pytestmark = [pytest.mark.bgpd] |
48 | ||
787e7624 | 49 | |
03252d60 DA |
50 | class TemplateTopo(Topo): |
51 | def build(self, *_args, **_opts): | |
52 | tgen = get_topogen(self) | |
53 | ||
54 | for routern in range(1, 4): | |
787e7624 | 55 | tgen.add_router("r{}".format(routern)) |
56 | ||
57 | switch = tgen.add_switch("s1") | |
58 | switch.add_link(tgen.gears["r1"]) | |
59 | switch.add_link(tgen.gears["r2"]) | |
60 | switch.add_link(tgen.gears["r3"]) | |
03252d60 | 61 | |
03252d60 DA |
62 | |
63 | def setup_module(mod): | |
64 | tgen = Topogen(TemplateTopo, mod.__name__) | |
65 | tgen.start_topology() | |
66 | ||
67 | router_list = tgen.routers() | |
68 | ||
e5f0ed14 | 69 | for i, (rname, router) in enumerate(router_list.items(), 1): |
03252d60 | 70 | router.load_config( |
787e7624 | 71 | TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname)) |
03252d60 DA |
72 | ) |
73 | router.load_config( | |
787e7624 | 74 | TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format(rname)) |
03252d60 DA |
75 | ) |
76 | ||
77 | tgen.start_router() | |
78 | ||
787e7624 | 79 | |
03252d60 DA |
80 | def teardown_module(mod): |
81 | tgen = get_topogen() | |
82 | tgen.stop_topology() | |
83 | ||
787e7624 | 84 | |
03252d60 DA |
85 | def test_bgp_set_local_preference(): |
86 | tgen = get_topogen() | |
87 | ||
88 | if tgen.routers_have_failure(): | |
89 | pytest.skip(tgen.errors) | |
90 | ||
787e7624 | 91 | router = tgen.gears["r1"] |
03252d60 DA |
92 | |
93 | def _bgp_converge(router): | |
94 | output = json.loads(router.vtysh_cmd("show ip bgp neighbor json")) | |
95 | expected = { | |
787e7624 | 96 | "192.168.255.2": { |
97 | "bgpState": "Established", | |
98 | "addressFamilyInfo": {"ipv4Unicast": {"acceptedPrefixCounter": 2}}, | |
99 | }, | |
100 | "192.168.255.3": { | |
101 | "bgpState": "Established", | |
102 | "addressFamilyInfo": {"ipv4Unicast": {"acceptedPrefixCounter": 2}}, | |
03252d60 | 103 | }, |
03252d60 DA |
104 | } |
105 | return topotest.json_cmp(output, expected) | |
106 | ||
107 | def _bgp_check_local_preference(router): | |
108 | output = json.loads(router.vtysh_cmd("show ip bgp 172.16.255.254/32 json")) | |
109 | expected = { | |
787e7624 | 110 | "paths": [ |
111 | {"locPrf": 50, "nexthops": [{"ip": "192.168.255.3"}]}, | |
112 | {"locPrf": 150, "nexthops": [{"ip": "192.168.255.2"}]}, | |
03252d60 DA |
113 | ] |
114 | } | |
115 | return topotest.json_cmp(output, expected) | |
116 | ||
117 | test_func = functools.partial(_bgp_converge, router) | |
118 | success, result = topotest.run_and_expect(test_func, None, count=15, wait=0.5) | |
119 | ||
120 | assert result is None, 'Failed to see BGP convergence in "{}"'.format(router) | |
121 | ||
122 | test_func = functools.partial(_bgp_check_local_preference, router) | |
123 | success, result = topotest.run_and_expect(test_func, None, count=15, wait=0.5) | |
124 | ||
787e7624 | 125 | assert result is None, 'Failed to see applied BGP local-preference in "{}"'.format( |
126 | router | |
127 | ) | |
128 | ||
03252d60 | 129 | |
787e7624 | 130 | if __name__ == "__main__": |
03252d60 DA |
131 | args = ["-s"] + sys.argv[1:] |
132 | sys.exit(pytest.main(args)) |