]>
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 | |
45 | from mininet.topo import Topo | |
46 | ||
787e7624 | 47 | |
03252d60 DA |
48 | class TemplateTopo(Topo): |
49 | def build(self, *_args, **_opts): | |
50 | tgen = get_topogen(self) | |
51 | ||
52 | for routern in range(1, 4): | |
787e7624 | 53 | tgen.add_router("r{}".format(routern)) |
54 | ||
55 | switch = tgen.add_switch("s1") | |
56 | switch.add_link(tgen.gears["r1"]) | |
57 | switch.add_link(tgen.gears["r2"]) | |
58 | switch.add_link(tgen.gears["r3"]) | |
03252d60 | 59 | |
03252d60 DA |
60 | |
61 | def setup_module(mod): | |
62 | tgen = Topogen(TemplateTopo, mod.__name__) | |
63 | tgen.start_topology() | |
64 | ||
65 | router_list = tgen.routers() | |
66 | ||
e5f0ed14 | 67 | for i, (rname, router) in enumerate(router_list.items(), 1): |
03252d60 | 68 | router.load_config( |
787e7624 | 69 | TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname)) |
03252d60 DA |
70 | ) |
71 | router.load_config( | |
787e7624 | 72 | TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format(rname)) |
03252d60 DA |
73 | ) |
74 | ||
75 | tgen.start_router() | |
76 | ||
787e7624 | 77 | |
03252d60 DA |
78 | def teardown_module(mod): |
79 | tgen = get_topogen() | |
80 | tgen.stop_topology() | |
81 | ||
787e7624 | 82 | |
03252d60 DA |
83 | def test_bgp_set_local_preference(): |
84 | tgen = get_topogen() | |
85 | ||
86 | if tgen.routers_have_failure(): | |
87 | pytest.skip(tgen.errors) | |
88 | ||
787e7624 | 89 | router = tgen.gears["r1"] |
03252d60 DA |
90 | |
91 | def _bgp_converge(router): | |
92 | output = json.loads(router.vtysh_cmd("show ip bgp neighbor json")) | |
93 | expected = { | |
787e7624 | 94 | "192.168.255.2": { |
95 | "bgpState": "Established", | |
96 | "addressFamilyInfo": {"ipv4Unicast": {"acceptedPrefixCounter": 2}}, | |
97 | }, | |
98 | "192.168.255.3": { | |
99 | "bgpState": "Established", | |
100 | "addressFamilyInfo": {"ipv4Unicast": {"acceptedPrefixCounter": 2}}, | |
03252d60 | 101 | }, |
03252d60 DA |
102 | } |
103 | return topotest.json_cmp(output, expected) | |
104 | ||
105 | def _bgp_check_local_preference(router): | |
106 | output = json.loads(router.vtysh_cmd("show ip bgp 172.16.255.254/32 json")) | |
107 | expected = { | |
787e7624 | 108 | "paths": [ |
109 | {"locPrf": 50, "nexthops": [{"ip": "192.168.255.3"}]}, | |
110 | {"locPrf": 150, "nexthops": [{"ip": "192.168.255.2"}]}, | |
03252d60 DA |
111 | ] |
112 | } | |
113 | return topotest.json_cmp(output, expected) | |
114 | ||
115 | test_func = functools.partial(_bgp_converge, router) | |
116 | success, result = topotest.run_and_expect(test_func, None, count=15, wait=0.5) | |
117 | ||
118 | assert result is None, 'Failed to see BGP convergence in "{}"'.format(router) | |
119 | ||
120 | test_func = functools.partial(_bgp_check_local_preference, router) | |
121 | success, result = topotest.run_and_expect(test_func, None, count=15, wait=0.5) | |
122 | ||
787e7624 | 123 | assert result is None, 'Failed to see applied BGP local-preference in "{}"'.format( |
124 | router | |
125 | ) | |
126 | ||
03252d60 | 127 | |
787e7624 | 128 | if __name__ == "__main__": |
03252d60 DA |
129 | args = ["-s"] + sys.argv[1:] |
130 | sys.exit(pytest.main(args)) |