summaryrefslogtreecommitdiff
path: root/tests/topotests/bgp_nexthop_ipv6/test_bgp_nexthop_ipv6_topo1.py
blob: 58daee32c3eb00769163852f1f57fd1e41aa740c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env python
# SPDX-License-Identifier: ISC

#
# test_bgp_nexthop_ipv6_topo1.py
#
# Copyright (c) 2024 by
# Cumulus Networks, Inc.
# 6WIND S.A.
#

"""
Ensure that BGP ipv6 nexthops are correct
"""

import os
import sys
import pytest
from functools import partial
import json

# Save the Current Working Directory to find configuration files.
CWD = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(CWD, "../"))

# pylint: disable=C0413
# Import topogen and topotest helpers
from lib import topotest
from lib.topogen import Topogen, TopoRouter, get_topogen
from lib.topolog import logger

# Required to instantiate the topology builder class.


pytestmark = [pytest.mark.bgpd]


def build_topo(tgen):
    r"""
    All peers are FRR BGP peers except r3 that is a exabgp peer.
    rr is a route-reflector for AS 65000 iBGP peers.
    Exabgp does not send any IPv6 Link-Local nexthop

                   iBGP peers        |  eBGP peers
                                     |
                    AS 65000         |
                                     |
                     +---+           |
                     | r6|           |
                     +---+           |
                       |             |
             fd00:0:3::0/64          |
                       |             |  AS 65002
                     +---+           |         +---+
                     |rr |----fd00:0:4::0/64---| r5|
                     +---+           |_________+---+
                       |             |         +---+
                 fd00:0:2::0/64----------------| r4|
                  /    |     \       |         +---+
              +---+  +---+  +---+    |  AS 65001
              | r1|  | r2|  |r3 |    |
              +---+  +---+  +---+
    """

    def connect_routers(tgen, left, right):
        for rname in [left, right]:
            if rname not in tgen.routers().keys():
                tgen.add_router(rname)

        switch = tgen.add_switch("s-{}-{}".format(left, right))
        switch.add_link(tgen.gears[left], nodeif="eth-{}".format(right))
        switch.add_link(tgen.gears[right], nodeif="eth-{}".format(left))

    def connect_switchs(tgen, rname, switch):
        if rname not in tgen.routers().keys():
            tgen.add_router(rname)

        switch.add_link(tgen.gears[rname], nodeif="eth-{}".format(switch.name))

    def connect_dummy(tgen, rname, switch):
        if rname not in tgen.routers().keys():
            tgen.add_router(rname)

        switch.add_link(tgen.gears[rname], nodeif="eth-dummy")

    # sw_du switch is for a dummy interface (for local network)
    for i in range(1, 7):
        if i == 3:
            # r3 is an exabgp peer
            continue
        sw_du = tgen.add_switch("sw%s" % i)
        connect_dummy(tgen, "r%s" % i, sw_du)

    # sw switch is for interconnecting peers on the same subnet
    sw = tgen.add_switch("sw")
    connect_switchs(tgen, "rr", sw)
    connect_switchs(tgen, "r1", sw)
    connect_switchs(tgen, "r2", sw)
    connect_switchs(tgen, "r4", sw)

    # directly connected without switch routers
    connect_routers(tgen, "rr", "r5")
    connect_routers(tgen, "rr", "r6")

    ## Add iBGP ExaBGP neighbor
    peer_ip = "fd00:0:2::3"  ## peer
    peer_route = "via fd00:0:2::9"  ## router
    r3 = tgen.add_exabgp_peer("r3", ip=peer_ip, defaultRoute=peer_route)
    sw.add_link(r3)


#####################################################
##
##   Tests starting
##
#####################################################


def setup_module(module):
    "Setup topology"
    tgen = Topogen(build_topo, module.__name__)
    tgen.start_topology()

    # This is a sample of configuration loading.
    router_list = tgen.routers()
    for rname, router in router_list.items():
        router.load_config(
            TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname))
        )
        router.load_config(
            TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format(rname))
        )

    tgen.start_router()

    # Start r3 exabgp peer
    r3 = tgen.gears["r3"]
    r3.start(os.path.join(CWD, "r3"), os.path.join(CWD, "exabgp.env"))


def get_link_local(rname, ifname, cache):
    ip = cache.get(rname, {}).get(ifname)
    if ip:
        return ip

    tgen = get_topogen()
    out = tgen.gears[rname].vtysh_cmd("show interface %s json" % ifname, isjson=True)
    for address in out[ifname]["ipAddresses"]:
        if not address["address"].startswith("fe80::"):
            continue
        ip = address["address"].split("/")[0]
        cache.setdefault(rname, {})[ifname] = ip
        return ip


def replace_link_local(expected, cache):
    for prefix, prefix_info in expected.get("routes", {}).items():
        for nexthop in prefix_info[0].get("nexthops", []):
            ip = nexthop.get("ip", "")
            if not ip.startswith("link-local:"):
                continue
            rname = ip.split(":")[1]
            ifname = ip.split(":")[2]
            ip = get_link_local(rname, ifname, cache)
            nexthop["ip"] = ip


def check_rr_sub_group(expected):
    tgen = get_topogen()

    rr = tgen.gears["rr"]

    output = json.loads(rr.vtysh_cmd("show bgp update-groups json"))
    actual = [
        subgroup["peers"]
        for entry in output.get("default", {}).values()
        for subgroup in entry["subGroup"]
    ]

    return topotest.json_cmp(actual, expected)


def teardown_module(_mod):
    "Teardown the pytest environment"
    tgen = get_topogen()

    # This function tears down the whole topology.
    tgen.stop_topology()


def test_converge_protocols():
    "Wait for protocol convergence"

    tgen = get_topogen()
    # Don't run this test if we have any failure.
    if tgen.routers_have_failure():
        pytest.skip(tgen.errors)

    rr = tgen.gears["rr"]
    ref_file = "{}/{}/show_bgp_ipv6_summary.json".format(CWD, rr.name)
    expected = json.loads(open(ref_file).read())

    test_func = partial(
        topotest.router_json_cmp,
        rr,
        "show bgp ipv6 summary json",
        expected,
    )
    _, res = topotest.run_and_expect(test_func, None, count=30, wait=1)
    assertmsg = "{}: BGP convergence".format(rr.name)
    assert res is None, assertmsg


def test_bgp_ipv6_table_step1():
    tgen = get_topogen()

    # Don't run this test if we have any failure.
    if tgen.routers_have_failure():
        pytest.skip(tgen.errors)

    global link_local_cache
    link_local_cache = {}
    router_list = tgen.routers().values()
    for router in router_list:
        ref_file = "{}/{}/show_bgp_ipv6_step1.json".format(CWD, router.name)
        expected = json.loads(open(ref_file).read())
        replace_link_local(expected, link_local_cache)

        test_func = partial(
            topotest.router_json_cmp,
            router,
            "show bgp ipv6 unicast json",
            expected,
        )
        _, res = topotest.run_and_expect(test_func, None, count=30, wait=1)
        assertmsg = "{}: BGP IPv6 Nexthop failure".format(router.name)
        assert res is None, assertmsg

    # check rr sub-groups
    expected = [
        ["fd00:0:2::1", "fd00:0:2::2"],
        ["fd00:0:2::3"],
        ["fd00:0:2::4"],
        ["fd00:0:3::5"],
        ["fd00:0:4::6"],
    ]

    test_func = partial(check_rr_sub_group, expected)
    _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
    assert result is None, "Peer group split failed"


def test_bgp_ipv6_table_step2():
    tgen = get_topogen()

    # Don't run this test if we have any failure.
    if tgen.routers_have_failure():
        pytest.skip(tgen.errors)

    rr = tgen.gears["rr"]
    rr.vtysh_cmd(
        """
configure terminal
router bgp 65000
 address-family ipv6 unicast
  no neighbor fd00:0:2::1 nexthop-local unchanged
  no neighbor fd00:0:2::2 nexthop-local unchanged
  no neighbor fd00:0:2::3 nexthop-local unchanged
  no neighbor fd00:0:2::4 nexthop-local unchanged
  no neighbor fd00:0:3::5 nexthop-local unchanged
  no neighbor fd00:0:4::6 nexthop-local unchanged
"""
    )

    router_list = tgen.routers().values()
    for router in router_list:
        ref_file = "{}/{}/show_bgp_ipv6_step2.json".format(CWD, router.name)
        expected = json.loads(open(ref_file).read())
        replace_link_local(expected, link_local_cache)

        test_func = partial(
            topotest.router_json_cmp,
            router,
            "show bgp ipv6 unicast json",
            expected,
        )
        _, res = topotest.run_and_expect(test_func, None, count=30, wait=1)
        assertmsg = "{}: BGP IPv6 Nexthop failure".format(router.name)
        assert res is None, assertmsg

    # check rr sub-groups
    expected = [
        ["fd00:0:2::1", "fd00:0:2::2"],
        ["fd00:0:2::3"],
        ["fd00:0:3::5", "fd00:0:2::4"],
        ["fd00:0:4::6"],
    ]

    test_func = partial(check_rr_sub_group, expected)
    _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
    assert result is None, "Peer group split failed"


def test_bgp_ipv6_table_step3():
    tgen = get_topogen()

    # Don't run this test if we have any failure.
    if tgen.routers_have_failure():
        pytest.skip(tgen.errors)

    rr = tgen.gears["rr"]
    rr.vtysh_cmd(
        """
configure terminal
router bgp 65000
 address-family ipv6 unicast
  neighbor fd00:0:2::1 nexthop-local unchanged
  neighbor fd00:0:2::2 nexthop-local unchanged
  neighbor fd00:0:2::3 nexthop-local unchanged
  neighbor fd00:0:2::4 nexthop-local unchanged
  neighbor fd00:0:3::5 nexthop-local unchanged
  neighbor fd00:0:4::6 nexthop-local unchanged
"""
    )

    router_list = tgen.routers().values()
    for router in router_list:
        ref_file = "{}/{}/show_bgp_ipv6_step1.json".format(CWD, router.name)
        expected = json.loads(open(ref_file).read())
        replace_link_local(expected, link_local_cache)

        test_func = partial(
            topotest.router_json_cmp,
            router,
            "show bgp ipv6 unicast json",
            expected,
        )
        _, res = topotest.run_and_expect(test_func, None, count=30, wait=1)
        assertmsg = "{}: BGP IPv6 Nexthop failure".format(router.name)
        assert res is None, assertmsg

    # check rr sub-groups
    expected = [
        ["fd00:0:2::1", "fd00:0:2::2"],
        ["fd00:0:2::3"],
        ["fd00:0:2::4"],
        ["fd00:0:3::5"],
        ["fd00:0:4::6"],
    ]

    test_func = partial(check_rr_sub_group, expected)
    _, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5)
    assert result is None, "Peer group split failed"


if __name__ == "__main__":
    args = ["-s"] + sys.argv[1:]
    sys.exit(pytest.main(args))