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