]>
Commit | Line | Data |
---|---|---|
5fba2248 DA |
1 | #!/usr/bin/env python |
2 | ||
3 | # | |
4 | # bgp_aggregate-address_route-map.py | |
5 | # Part of NetDEF Topology Tests | |
6 | # | |
7 | # Copyright (c) 2019 by | |
8 | # Network Device Education Foundation, Inc. ("NetDEF") | |
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_aggregate-address_route-map.py: | |
27 | ||
28 | Test if works the following commands: | |
29 | router bgp 65031 | |
30 | address-family ipv4 unicast | |
31 | aggregate-address 192.168.255.0/24 route-map aggr-rmap | |
32 | ||
33 | route-map aggr-rmap permit 10 | |
34 | set metric 123 | |
35 | """ | |
36 | ||
37 | import os | |
38 | import sys | |
39 | import json | |
40 | import time | |
41 | import pytest | |
65f40ecd | 42 | import functools |
5fba2248 DA |
43 | |
44 | CWD = os.path.dirname(os.path.realpath(__file__)) | |
45 | sys.path.append(os.path.join(CWD, '../')) | |
46 | ||
47 | # pylint: disable=C0413 | |
48 | from lib import topotest | |
49 | from lib.topogen import Topogen, TopoRouter, get_topogen | |
50 | from lib.topolog import logger | |
51 | from mininet.topo import Topo | |
52 | ||
53 | class TemplateTopo(Topo): | |
54 | def build(self, *_args, **_opts): | |
55 | tgen = get_topogen(self) | |
56 | ||
57 | for routern in range(1, 3): | |
58 | tgen.add_router('r{}'.format(routern)) | |
59 | ||
60 | switch = tgen.add_switch('s1') | |
61 | switch.add_link(tgen.gears['r1']) | |
62 | switch.add_link(tgen.gears['r2']) | |
63 | ||
64 | def setup_module(mod): | |
65 | tgen = Topogen(TemplateTopo, mod.__name__) | |
66 | tgen.start_topology() | |
67 | ||
68 | router_list = tgen.routers() | |
69 | ||
70 | for i, (rname, router) in enumerate(router_list.iteritems(), 1): | |
71 | router.load_config( | |
72 | TopoRouter.RD_ZEBRA, | |
73 | os.path.join(CWD, '{}/zebra.conf'.format(rname)) | |
74 | ) | |
75 | router.load_config( | |
76 | TopoRouter.RD_BGP, | |
77 | os.path.join(CWD, '{}/bgpd.conf'.format(rname)) | |
78 | ) | |
79 | ||
80 | tgen.start_router() | |
81 | ||
82 | def teardown_module(mod): | |
83 | tgen = get_topogen() | |
84 | tgen.stop_topology() | |
85 | ||
86 | def test_bgp_maximum_prefix_invalid(): | |
87 | tgen = get_topogen() | |
88 | ||
89 | if tgen.routers_have_failure(): | |
90 | pytest.skip(tgen.errors) | |
91 | ||
65f40ecd DA |
92 | router = tgen.gears['r2'] |
93 | ||
5fba2248 | 94 | def _bgp_converge(router): |
65f40ecd DA |
95 | output = json.loads(router.vtysh_cmd("show ip bgp neighbor 192.168.255.1 json")) |
96 | expected = { | |
97 | '192.168.255.1': { | |
98 | 'bgpState': 'Established', | |
99 | 'addressFamilyInfo': { | |
100 | 'ipv4Unicast': { | |
101 | 'acceptedPrefixCounter': 3 | |
102 | } | |
103 | } | |
104 | } | |
105 | } | |
106 | return topotest.json_cmp(output, expected) | |
5fba2248 DA |
107 | |
108 | def _bgp_aggregate_address_has_metric(router): | |
65f40ecd DA |
109 | output = json.loads(router.vtysh_cmd("show ip bgp 172.16.255.0/24 json")) |
110 | expected = { | |
111 | 'paths': [ | |
112 | { | |
113 | 'med': 123 | |
114 | } | |
115 | ] | |
116 | } | |
117 | return topotest.json_cmp(output, expected) | |
118 | ||
119 | test_func = functools.partial(_bgp_converge, router) | |
120 | success, result = topotest.run_and_expect(test_func, None, count=30, wait=0.5) | |
121 | ||
122 | assert result is None, 'Failed to see bgp convergence in "{}"'.format(router) | |
123 | ||
124 | test_func = functools.partial(_bgp_aggregate_address_has_metric, router) | |
125 | success, result = topotest.run_and_expect(test_func, None, count=30, wait=0.5) | |
5fba2248 | 126 | |
65f40ecd | 127 | assert result is None, 'Failed to see applied metric for aggregated prefix in "{}"'.format(router) |
5fba2248 DA |
128 | |
129 | if __name__ == '__main__': | |
130 | args = ["-s"] + sys.argv[1:] | |
131 | sys.exit(pytest.main(args)) |