]>
Commit | Line | Data |
---|---|---|
b820f3d0 | 1 | #!/usr/bin/env python |
acddc0ed | 2 | # SPDX-License-Identifier: ISC |
b820f3d0 DA |
3 | |
4 | # Copyright (c) 2021 by | |
5 | # Donatas Abraitis <donatas.abraitis@gmail.com> | |
6 | # | |
b820f3d0 DA |
7 | |
8 | """ | |
9 | Test if BGP community alias is visible in CLI outputs | |
10 | """ | |
11 | ||
12 | import os | |
13 | import sys | |
14 | import json | |
b820f3d0 DA |
15 | import pytest |
16 | import functools | |
17 | ||
18 | pytestmark = pytest.mark.bgpd | |
19 | ||
20 | CWD = os.path.dirname(os.path.realpath(__file__)) | |
21 | sys.path.append(os.path.join(CWD, "../")) | |
22 | ||
23 | # pylint: disable=C0413 | |
24 | from lib import topotest | |
25 | from lib.topogen import Topogen, TopoRouter, get_topogen | |
b820f3d0 | 26 | |
bf3a0a9a DS |
27 | pytestmark = [pytest.mark.bgpd] |
28 | ||
b820f3d0 | 29 | |
e82b531d CH |
30 | def build_topo(tgen): |
31 | for routern in range(1, 3): | |
32 | tgen.add_router("r{}".format(routern)) | |
b820f3d0 | 33 | |
e82b531d CH |
34 | switch = tgen.add_switch("s1") |
35 | switch.add_link(tgen.gears["r1"]) | |
36 | switch.add_link(tgen.gears["r2"]) | |
b820f3d0 DA |
37 | |
38 | ||
39 | def setup_module(mod): | |
e82b531d | 40 | tgen = Topogen(build_topo, mod.__name__) |
b820f3d0 DA |
41 | tgen.start_topology() |
42 | ||
43 | router_list = tgen.routers() | |
44 | ||
45 | for i, (rname, router) in enumerate(router_list.items(), 1): | |
46 | router.load_config( | |
47 | TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname)) | |
48 | ) | |
49 | router.load_config( | |
50 | TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format(rname)) | |
51 | ) | |
52 | ||
53 | tgen.start_router() | |
54 | ||
55 | ||
56 | def teardown_module(mod): | |
57 | tgen = get_topogen() | |
58 | tgen.stop_topology() | |
59 | ||
60 | ||
61 | def test_bgp_community_alias(): | |
62 | tgen = get_topogen() | |
63 | ||
64 | if tgen.routers_have_failure(): | |
65 | pytest.skip(tgen.errors) | |
66 | ||
67 | router = tgen.gears["r1"] | |
68 | ||
69 | def _bgp_converge(router): | |
903b3fff | 70 | output = json.loads(router.vtysh_cmd("show ip route json")) |
b820f3d0 | 71 | expected = { |
903b3fff DA |
72 | "172.16.16.1/32": [ |
73 | { | |
74 | "tag": 10, | |
75 | "communities": "community-r2-1 65001:2", | |
76 | "largeCommunities": "large-community-r2-1 65001:1:2", | |
77 | } | |
78 | ], | |
79 | "172.16.16.2/32": [ | |
80 | { | |
81 | "tag": 20, | |
82 | "communities": "65002:1 community-r2-2", | |
83 | "largeCommunities": "", | |
84 | } | |
85 | ], | |
86 | "172.16.16.3/32": [ | |
b820f3d0 | 87 | { |
903b3fff DA |
88 | "tag": 100, |
89 | "communities": "", | |
90 | "largeCommunities": "", | |
b820f3d0 | 91 | } |
903b3fff | 92 | ], |
b820f3d0 DA |
93 | } |
94 | return topotest.json_cmp(output, expected) | |
95 | ||
96 | test_func = functools.partial(_bgp_converge, router) | |
97 | success, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5) | |
903b3fff | 98 | assert result is None, "Cannot see BGP community aliases at r1" |
b820f3d0 | 99 | |
b15be160 DA |
100 | def _bgp_show_prefixes_by_alias(router): |
101 | output = json.loads( | |
903b3fff DA |
102 | router.vtysh_cmd( |
103 | "show bgp ipv4 unicast alias large-community-r2-1 json detail" | |
104 | ) | |
b15be160 DA |
105 | ) |
106 | expected = { | |
107 | "routes": { | |
04705e48 TA |
108 | "172.16.16.1/32": { |
109 | "paths": [ | |
110 | { | |
111 | "community": {"string": "community-r2-1 65001:2"}, | |
112 | "largeCommunity": { | |
113 | "string": "large-community-r2-1 65001:1:2" | |
114 | }, | |
115 | } | |
116 | ] | |
117 | } | |
b15be160 DA |
118 | } |
119 | } | |
120 | return topotest.json_cmp(output, expected) | |
121 | ||
122 | test_func = functools.partial(_bgp_show_prefixes_by_alias, router) | |
123 | success, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5) | |
903b3fff | 124 | assert result is None, "Cannot see BGP prefixes by community alias at r1" |
b15be160 | 125 | |
947a27f5 DA |
126 | def _bgp_show_prefixes_by_large_community_list(router): |
127 | output = json.loads( | |
128 | router.vtysh_cmd("show bgp ipv4 unicast large-community-list r2 json") | |
129 | ) | |
130 | expected = {"routes": {"172.16.16.1/32": [{"valid": True}]}} | |
131 | return topotest.json_cmp(output, expected) | |
132 | ||
133 | test_func = functools.partial(_bgp_show_prefixes_by_large_community_list, router) | |
134 | success, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5) | |
135 | assert result is None, "Cannot see BGP prefixes by large community list at r1" | |
136 | ||
b820f3d0 DA |
137 | |
138 | if __name__ == "__main__": | |
139 | args = ["-s"] + sys.argv[1:] | |
140 | sys.exit(pytest.main(args)) |