Skip to content

Commit

Permalink
Added Multi-ASIC support for show ip(v6) route (sonic-net#1216)
Browse files Browse the repository at this point in the history
* Added Multi-ASIC support for show ip(v6) route. Python3 compatible, test coverage > 95%
  • Loading branch information
gechiang authored Nov 24, 2020
1 parent a7f39b0 commit 5b8da56
Show file tree
Hide file tree
Showing 31 changed files with 12,337 additions and 26 deletions.
413 changes: 413 additions & 0 deletions show/bgp_common.py

Large diffs are not rendered by default.

29 changes: 11 additions & 18 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from tabulate import tabulate
from utilities_common.db import Db

from . import bgp_common
from . import chassis_modules
from . import feature
from . import fgnhg
Expand Down Expand Up @@ -790,17 +791,13 @@ def get_bgp_peer():

@ip.command()
@click.argument('args', metavar='[IPADDRESS] [vrf <vrf_name>] [...]', nargs=-1, required=False)
@click.option('--display', '-d', 'display', default=None, show_default=False, type=str, help='all|frontend')
@click.option('--namespace', '-n', 'namespace', default=None, type=str, show_default=False, help='Namespace name or all')
@click.option('--verbose', is_flag=True, help="Enable verbose output")
def route(args, verbose):
def route(args, namespace, display, verbose):
"""Show IP (IPv4) routing table"""
cmd = 'sudo vtysh -c "show ip route'

for arg in args:
cmd += " " + str(arg)

cmd += '"'

run_command(cmd, display_cmd=verbose)
# Call common handler to handle the show ip route cmd
bgp_common.show_routes(args, namespace, display, verbose, "ip")

#
# 'prefix-list' subcommand ("show ip prefix-list")
Expand Down Expand Up @@ -913,17 +910,13 @@ def interfaces():

@ipv6.command()
@click.argument('args', metavar='[IPADDRESS] [vrf <vrf_name>] [...]', nargs=-1, required=False)
@click.option('--display', '-d', 'display', default=None, show_default=False, type=str, help='all|frontend')
@click.option('--namespace', '-n', 'namespace', default=None, type=str, show_default=False, help='Namespace name or all')
@click.option('--verbose', is_flag=True, help="Enable verbose output")
def route(args, verbose):
def route(args, namespace, display, verbose):
"""Show IPv6 routing table"""
cmd = 'sudo vtysh -c "show ipv6 route'

for arg in args:
cmd += " " + str(arg)

cmd += '"'

run_command(cmd, display_cmd=verbose)
# Call common handler to handle the show ipv6 route cmd
bgp_common.show_routes(args, namespace, display, verbose, "ipv6")


# 'protocol' command
Expand Down
76 changes: 69 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,80 @@ def setup_single_bgp_instance(request):
import utilities_common.bgp_util as bgp_util

if request.param == 'v4':
bgp_summary_json = os.path.join(
bgp_mocked_json = os.path.join(
test_path, 'mock_tables', 'ipv4_bgp_summary.json')
elif request.param == 'v6':
bgp_summary_json = os.path.join(
bgp_mocked_json = os.path.join(
test_path, 'mock_tables', 'ipv6_bgp_summary.json')
elif request.param == 'ip_route':
bgp_mocked_json = os.path.join(
test_path, 'mock_tables', 'ip_route.json')
elif request.param == 'ip_specific_route':
bgp_mocked_json = os.path.join(
test_path, 'mock_tables', 'ip_specific_route.json')
elif request.param == 'ip_special_route':
bgp_mocked_json = os.path.join(
test_path, 'mock_tables', 'ip_special_route.json')
elif request.param == 'ipv6_route':
bgp_mocked_json = os.path.join(
test_path, 'mock_tables', 'ipv6_route.json')
elif request.param == 'ipv6_specific_route':
bgp_mocked_json = os.path.join(
test_path, 'mock_tables', 'ipv6_specific_route.json')
else:
bgp_summary_json = os.path.join(
bgp_mocked_json = os.path.join(
test_path, 'mock_tables', 'dummy.json')

def mock_run_bgp_command(vtysh_cmd, bgp_namespace):
if os.path.isfile(bgp_summary_json):
with open(bgp_summary_json) as json_data:
if os.path.isfile(bgp_mocked_json):
with open(bgp_mocked_json) as json_data:
mock_frr_data = json_data.read()
return mock_frr_data
return ""

bgp_util.run_bgp_command = mock.MagicMock(
return_value=mock_run_bgp_command("", ""))
def mock_run_bgp_ipv6_err_command(vtysh_cmd, bgp_namespace):
return "% Unknown command: show ipv6 route garbage"

if request.param == 'ipv6_route_err':
bgp_util.run_bgp_command = mock.MagicMock(
return_value=mock_run_bgp_ipv6_err_command("", ""))
else:
bgp_util.run_bgp_command = mock.MagicMock(
return_value=mock_run_bgp_command("", ""))


@pytest.fixture
def setup_multi_asic_bgp_instance(request):
import utilities_common.bgp_util as bgp_util

if request.param == 'ip_route':
m_asic_json_file = 'ip_route.json'
elif request.param == 'ip_specific_route':
m_asic_json_file = 'ip_specific_route.json'
elif request.param == 'ipv6_specific_route':
m_asic_json_file = 'ipv6_specific_route.json'
elif request.param == 'ipv6_route':
m_asic_json_file = 'ipv6_route.json'
else:
bgp_mocked_json = os.path.join(
test_path, 'mock_tables', 'dummy.json')

def mock_run_bgp_command(vtysh_cmd, bgp_namespace):
bgp_mocked_json = os.path.join(
test_path, 'mock_tables', bgp_namespace, m_asic_json_file)
if os.path.isfile(bgp_mocked_json):
with open(bgp_mocked_json) as json_data:
mock_frr_data = json_data.read()
return mock_frr_data
else:
return ""

_old_run_bgp_command = bgp_util.run_bgp_command
bgp_util.run_bgp_command = mock_run_bgp_command

yield

bgp_util.run_bgp_command = _old_run_bgp_command

@pytest.fixture
def setup_bgp_commands():
Expand All @@ -109,3 +164,10 @@ def setup_bgp_commands():
show.ip.add_command(bgpv4)
show.ipv6.add_command(bgpv6)
return show


@pytest.fixture
def setup_ip_route_commands():
import show.main as show

return show
Loading

0 comments on commit 5b8da56

Please sign in to comment.