]>
Commit | Line | Data |
---|---|---|
f3bc47b7 | 1 | #!/usr/bin/env python |
acddc0ed | 2 | # SPDX-License-Identifier: ISC |
f3bc47b7 DA |
3 | |
4 | # | |
5 | # Copyright (c) 2022 by | |
6 | # Donatas Abraitis <donatas@opensourcerouting.org> | |
7 | # | |
f3bc47b7 DA |
8 | |
9 | """ | |
10 | ||
11 | """ | |
12 | ||
13 | import os | |
14 | import sys | |
15 | import json | |
16 | import pytest | |
17 | import functools | |
18 | ||
19 | CWD = os.path.dirname(os.path.realpath(__file__)) | |
20 | sys.path.append(os.path.join(CWD, "../")) | |
21 | ||
22 | # pylint: disable=C0413 | |
23 | from lib import topotest | |
24 | from lib.topogen import Topogen, TopoRouter, get_topogen | |
25 | from lib.common_config import step | |
26 | ||
27 | pytestmark = [pytest.mark.bgpd] | |
28 | ||
29 | ||
30 | def build_topo(tgen): | |
31 | for routern in range(1, 4): | |
32 | tgen.add_router("r{}".format(routern)) | |
33 | ||
34 | switch = tgen.add_switch("s1") | |
35 | switch.add_link(tgen.gears["r1"]) | |
36 | switch.add_link(tgen.gears["r2"]) | |
37 | ||
38 | switch = tgen.add_switch("s2") | |
39 | switch.add_link(tgen.gears["r1"]) | |
40 | switch.add_link(tgen.gears["r3"]) | |
41 | ||
42 | ||
43 | def setup_module(mod): | |
44 | tgen = Topogen(build_topo, mod.__name__) | |
45 | tgen.start_topology() | |
46 | ||
47 | router_list = tgen.routers() | |
48 | ||
49 | for i, (rname, router) in enumerate(router_list.items(), 1): | |
50 | router.load_config( | |
51 | TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname)) | |
52 | ) | |
53 | router.load_config( | |
54 | TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format(rname)) | |
55 | ) | |
56 | ||
57 | tgen.start_router() | |
58 | ||
59 | ||
60 | def teardown_module(mod): | |
61 | tgen = get_topogen() | |
62 | tgen.stop_topology() | |
63 | ||
64 | ||
65 | def test_bgp_local_as_same_remote_as(): | |
66 | tgen = get_topogen() | |
67 | ||
68 | if tgen.routers_have_failure(): | |
69 | pytest.skip(tgen.errors) | |
70 | ||
71 | def _bgp_check_local_as_same_remote_as(): | |
72 | output = json.loads( | |
73 | tgen.gears["r2"].vtysh_cmd("show bgp ipv4 unicast 172.16.255.1/32 json") | |
74 | ) | |
75 | expected = { | |
76 | "paths": [ | |
77 | { | |
78 | "valid": True, | |
79 | "aspath": {"string": "Local"}, | |
80 | "nexthops": [{"ip": "192.168.1.1", "hostname": "r1"}], | |
81 | } | |
82 | ] | |
83 | } | |
84 | return topotest.json_cmp(output, expected) | |
85 | ||
86 | step("Check if iBGP works when local-as == remote-as") | |
87 | test_func = functools.partial(_bgp_check_local_as_same_remote_as) | |
88 | _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5) | |
89 | assert result is None, "Failed to see BGP prefixes on R2" | |
90 | ||
91 | ||
92 | def test_bgp_peer_group_local_as_same_remote_as(): | |
93 | tgen = get_topogen() | |
94 | ||
95 | if tgen.routers_have_failure(): | |
96 | pytest.skip(tgen.errors) | |
97 | ||
98 | def _bgp_check_local_as_same_remote_as(): | |
99 | output = json.loads( | |
100 | tgen.gears["r3"].vtysh_cmd("show bgp ipv4 unicast 172.16.255.1/32 json") | |
101 | ) | |
102 | expected = { | |
103 | "paths": [ | |
104 | { | |
105 | "valid": True, | |
106 | "aspath": {"string": "Local"}, | |
107 | "nexthops": [{"ip": "192.168.2.1", "hostname": "r1"}], | |
108 | } | |
109 | ] | |
110 | } | |
111 | return topotest.json_cmp(output, expected) | |
112 | ||
113 | step("Initial BGP converge") | |
114 | test_func = functools.partial(_bgp_check_local_as_same_remote_as) | |
115 | _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5) | |
116 | assert result is None, "Failed to see BGP prefixes on R3" | |
117 | ||
118 | ||
119 | if __name__ == "__main__": | |
120 | args = ["-s"] + sys.argv[1:] | |
121 | sys.exit(pytest.main(args)) |