]>
Commit | Line | Data |
---|---|---|
2f8e3650 DA |
1 | #!/usr/bin/env python |
2 | ||
3 | # | |
4 | # Copyright (c) 2021 by | |
5 | # Donatas Abraitis <donatas.abraitis@gmail.com> | |
6 | # | |
7 | # Permission to use, copy, modify, and/or distribute this software | |
8 | # for any purpose with or without fee is hereby granted, provided | |
9 | # that the above copyright notice and this permission notice appear | |
10 | # in all copies. | |
11 | # | |
12 | # THE SOFTWARE IS PROVIDED "AS IS" AND NETDEF DISCLAIMS ALL WARRANTIES | |
13 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
14 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETDEF BE LIABLE FOR | |
15 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY | |
16 | # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, | |
17 | # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | |
18 | # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE | |
19 | # OF THIS SOFTWARE. | |
20 | # | |
21 | ||
22 | """ | |
23 | Test if peer-group works for numbered and unnumbered configurations. | |
24 | """ | |
25 | ||
26 | import os | |
27 | import sys | |
28 | import json | |
29 | import pytest | |
30 | import functools | |
31 | ||
32 | CWD = os.path.dirname(os.path.realpath(__file__)) | |
33 | sys.path.append(os.path.join(CWD, "../")) | |
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 | |
8db751b8 | 39 | from lib.micronet_compat import Topo |
2f8e3650 DA |
40 | |
41 | ||
98ca91e1 DS |
42 | pytestmark = [pytest.mark.bgpd] |
43 | ||
44 | ||
e82b531d CH |
45 | def build_topo(tgen): |
46 | for routern in range(1, 4): | |
47 | tgen.add_router("r{}".format(routern)) | |
2f8e3650 | 48 | |
e82b531d CH |
49 | switch = tgen.add_switch("s1") |
50 | switch.add_link(tgen.gears["r1"]) | |
51 | switch.add_link(tgen.gears["r2"]) | |
52 | switch.add_link(tgen.gears["r3"]) | |
2f8e3650 DA |
53 | |
54 | ||
55 | def setup_module(mod): | |
e82b531d | 56 | tgen = Topogen(build_topo, mod.__name__) |
2f8e3650 DA |
57 | tgen.start_topology() |
58 | ||
59 | router_list = tgen.routers() | |
60 | ||
61 | for i, (rname, router) in enumerate(router_list.items(), 1): | |
62 | router.load_config( | |
63 | TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname)) | |
64 | ) | |
65 | router.load_config( | |
66 | TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format(rname)) | |
67 | ) | |
68 | ||
69 | tgen.start_router() | |
70 | ||
71 | ||
72 | def teardown_module(mod): | |
73 | tgen = get_topogen() | |
74 | tgen.stop_topology() | |
75 | ||
76 | ||
77 | def test_bgp_peer_group(): | |
78 | tgen = get_topogen() | |
79 | ||
80 | if tgen.routers_have_failure(): | |
81 | pytest.skip(tgen.errors) | |
82 | ||
83 | def _bgp_peer_group_configured(): | |
84 | output = json.loads(tgen.gears["r1"].vtysh_cmd("show ip bgp neighbor json")) | |
85 | expected = { | |
86 | "r1-eth0": {"peerGroup": "PG", "bgpState": "Established"}, | |
87 | "192.168.255.3": {"peerGroup": "PG", "bgpState": "Established"}, | |
88 | } | |
89 | return topotest.json_cmp(output, expected) | |
90 | ||
91 | test_func = functools.partial(_bgp_peer_group_configured) | |
92 | success, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5) | |
93 | ||
94 | assert result is None, 'Failed bgp convergence in "{}"'.format(tgen.gears["r1"]) | |
95 | ||
96 | ||
97 | if __name__ == "__main__": | |
98 | args = ["-s"] + sys.argv[1:] | |
99 | sys.exit(pytest.main(args)) |