Skip to content

Commit

Permalink
topotests: add bgp_redistribute_table test
Browse files Browse the repository at this point in the history
There is no test that ensures the test of the 'redistribute
table-direct' facility. Add a test that checks that routes
created before and after BGP is started, is correctly imported.

Signed-off-by: Philippe Guibert <[email protected]>
  • Loading branch information
pguibert6WIND committed Sep 13, 2023
1 parent 233781c commit af53b73
Show file tree
Hide file tree
Showing 8 changed files with 318 additions and 0 deletions.
Empty file.
8 changes: 8 additions & 0 deletions tests/topotests/bgp_redistribute_table/r1/bgpd.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
router bgp 65500
bgp router-id 192.0.2.1
no bgp ebgp-requires-policy
neighbor 192.168.0.2 remote-as 65501
address-family ipv4 unicast
neighbor 192.168.0.2 activate
exit-address-family
!
52 changes: 52 additions & 0 deletions tests/topotests/bgp_redistribute_table/r1/ipv4_routes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"172.31.0.2/32": [
{
"prefix": "172.31.0.2/32",
"prefixLen": 32,
"protocol": "bgp",
"vrfId": 0,
"vrfName": "default",
"selected": true,
"destSelected": true,
"distance": 20,
"metric": 0,
"installed": true,
"nexthops": [
{
"fib": true,
"ip": "192.168.0.2",
"afi": "ipv4",
"interfaceName": "r1-eth0",
"active": true,
"weight": 1
}
]
}
],
"172.31.0.10/32": [
{
"prefix": "172.31.0.10/32",
"prefixLen": 32,
"protocol": "bgp",
"vrfId": 0,
"vrfName": "default",
"selected": true,
"destSelected": true,
"distance": 20,
"metric": 0,
"installed": true,
"table": 254,
"nexthops": [
{
"flags": 3,
"fib": true,
"ip": "192.168.0.2",
"afi": "ipv4",
"interfaceName": "r1-eth0",
"active": true,
"weight": 1
}
]
}
]
}
78 changes: 78 additions & 0 deletions tests/topotests/bgp_redistribute_table/r1/ipv4_routes_post.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"172.31.0.2/32": [
{
"prefix": "172.31.0.2/32",
"prefixLen": 32,
"protocol": "bgp",
"vrfId": 0,
"vrfName": "default",
"selected": true,
"destSelected": true,
"distance": 20,
"metric": 0,
"installed": true,
"nexthops": [
{
"fib": true,
"ip": "192.168.0.2",
"afi": "ipv4",
"interfaceName": "r1-eth0",
"active": true,
"weight": 1
}
]
}
],
"172.31.0.10/32": [
{
"prefix": "172.31.0.10/32",
"prefixLen": 32,
"protocol": "bgp",
"vrfId": 0,
"vrfName": "default",
"selected": true,
"destSelected": true,
"distance": 20,
"metric": 0,
"installed": true,
"table": 254,
"nexthops": [
{
"flags": 3,
"fib": true,
"ip": "192.168.0.2",
"afi": "ipv4",
"interfaceName": "r1-eth0",
"active": true,
"weight": 1
}
]
}
],
"172.31.0.15/32": [
{
"prefix": "172.31.0.15/32",
"prefixLen": 32,
"protocol": "bgp",
"vrfId": 0,
"vrfName": "default",
"selected": true,
"destSelected": true,
"distance": 20,
"metric": 0,
"installed": true,
"table": 254,
"nexthops": [
{
"flags": 3,
"fib": true,
"ip": "192.168.0.2",
"afi": "ipv4",
"interfaceName": "r1-eth0",
"active": true,
"weight": 1
}
]
}
]
}
7 changes: 7 additions & 0 deletions tests/topotests/bgp_redistribute_table/r1/zebra.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
log stdout
interface r1-eth1
ip address 172.31.0.1/32
!
interface r1-eth0
ip address 192.168.0.1/24
!
10 changes: 10 additions & 0 deletions tests/topotests/bgp_redistribute_table/r2/bgpd.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
router bgp 65501
bgp router-id 192.0.2.2
no bgp ebgp-requires-policy
neighbor 192.168.0.1 remote-as 65500
address-family ipv4 unicast
network 172.31.0.2/32
neighbor 192.168.0.1 activate
redistribute table-direct 220
exit-address-family
!
9 changes: 9 additions & 0 deletions tests/topotests/bgp_redistribute_table/r2/zebra.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
log stdout
ip route 172.31.0.10/32 172.31.1.10 table 220
interface r2-eth0
ip address 192.168.0.2/24
!
interface r2-eth1
ip address 172.31.0.2/32
ip address 172.31.1.2/24
!
154 changes: 154 additions & 0 deletions tests/topotests/bgp_redistribute_table/test_bgp_redistribute_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#!/usr/bin/env python
# SPDX-License-Identifier: ISC

#
# test_bgp_redistribute_table.py
# Part of NetDEF Topology Tests
#
# Copyright (c) 2023 by 6WIND
#

"""
test_bgp_redistribute_table.py: Test the FRR BGP daemon with 'redistribute table-direct'
"""

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

# 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):
"Build function"

# Create 2 routers.
tgen.add_router("r1")
tgen.add_router("r2")

switch = tgen.add_switch("s1")
switch.add_link(tgen.gears["r1"])
switch.add_link(tgen.gears["r2"])

switch = tgen.add_switch("s2")
switch.add_link(tgen.gears["r1"])

switch = tgen.add_switch("s3")
switch.add_link(tgen.gears["r2"])


def setup_module(mod):
"Sets up the pytest environment"
tgen = Topogen(build_topo, mod.__name__)
tgen.start_topology()

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))
)

# Initialize all routers.
tgen.start_router()


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

tgen.stop_topology()


def test_protocols_convergence():
"""
Assert that all protocols have converged
statuses as they depend on it.
"""
tgen = get_topogen()
if tgen.routers_have_failure():
pytest.skip(tgen.errors)

router = tgen.gears["r1"]

# Check IPv4 routing tables on r1
logger.info("Checking IPv4 routes for convergence on r1")
router = tgen.gears["r1"]
json_file = "{}/{}/ipv4_routes.json".format(CWD, router.name)
expected = json.loads(open(json_file).read())
test_func = partial(
topotest.router_json_cmp,
router,
"show ip route bgp json",
expected,
)
_, result = topotest.run_and_expect(test_func, None, count=10, wait=0.5)
assertmsg = '"{}" JSON output mismatches'.format(router.name)
assert result is None, assertmsg


def test_stop_bgp_and_add_kernel_route_and_restart_bgp():
"Sets up the pytest environment"
tgen = get_topogen()
if tgen.routers_have_failure():
pytest.skip(tgen.errors)

router_list = tgen.routers()

router = tgen.gears["r2"]
logger.info("Removing r2 BGP configuration")
router.vtysh_cmd("configure terminal\nno router bgp 65501\n")

cmd = "ip route add 172.31.0.15/32 via 172.31.1.100 table 220"
tgen.net["r2"].cmd(cmd)

router = tgen.gears["r2"]
logger.info("Restoring r2 BGP configuration")
tgen.net["r2"].cmd("vtysh -f {}".format(os.path.join(CWD, "r2/bgpd.conf")))

# Check IPv4 routing tables on r1
logger.info("Checking IPv4 routes for convergence on r1 with kernel route")
router = tgen.gears["r1"]
json_file = "{}/{}/ipv4_routes_post.json".format(CWD, router.name)
expected = json.loads(open(json_file).read())
test_func = partial(
topotest.router_json_cmp,
router,
"show ip route bgp json",
expected,
)
_, result = topotest.run_and_expect(test_func, None, count=10, wait=0.5)
assertmsg = '"{}" JSON output mismatches'.format(router.name)
assert result is None, assertmsg


def test_memory_leak():
"Run the memory leak test and report results."
tgen = get_topogen()
if not tgen.is_memleak_enabled():
pytest.skip("Memory leak test/report is disabled")

tgen.report_memory_leaks()


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

0 comments on commit af53b73

Please sign in to comment.