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