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