Skip to content

Commit 5b8da56

Browse files
authored
Added Multi-ASIC support for show ip(v6) route (sonic-net#1216)
* Added Multi-ASIC support for show ip(v6) route. Python3 compatible, test coverage > 95%
1 parent a7f39b0 commit 5b8da56

31 files changed

+12337
-26
lines changed

show/bgp_common.py

+413
Large diffs are not rendered by default.

show/main.py

+11-18
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from tabulate import tabulate
1717
from utilities_common.db import Db
1818

19+
from . import bgp_common
1920
from . import chassis_modules
2021
from . import feature
2122
from . import fgnhg
@@ -790,17 +791,13 @@ def get_bgp_peer():
790791

791792
@ip.command()
792793
@click.argument('args', metavar='[IPADDRESS] [vrf <vrf_name>] [...]', nargs=-1, required=False)
794+
@click.option('--display', '-d', 'display', default=None, show_default=False, type=str, help='all|frontend')
795+
@click.option('--namespace', '-n', 'namespace', default=None, type=str, show_default=False, help='Namespace name or all')
793796
@click.option('--verbose', is_flag=True, help="Enable verbose output")
794-
def route(args, verbose):
797+
def route(args, namespace, display, verbose):
795798
"""Show IP (IPv4) routing table"""
796-
cmd = 'sudo vtysh -c "show ip route'
797-
798-
for arg in args:
799-
cmd += " " + str(arg)
800-
801-
cmd += '"'
802-
803-
run_command(cmd, display_cmd=verbose)
799+
# Call common handler to handle the show ip route cmd
800+
bgp_common.show_routes(args, namespace, display, verbose, "ip")
804801

805802
#
806803
# 'prefix-list' subcommand ("show ip prefix-list")
@@ -913,17 +910,13 @@ def interfaces():
913910

914911
@ipv6.command()
915912
@click.argument('args', metavar='[IPADDRESS] [vrf <vrf_name>] [...]', nargs=-1, required=False)
913+
@click.option('--display', '-d', 'display', default=None, show_default=False, type=str, help='all|frontend')
914+
@click.option('--namespace', '-n', 'namespace', default=None, type=str, show_default=False, help='Namespace name or all')
916915
@click.option('--verbose', is_flag=True, help="Enable verbose output")
917-
def route(args, verbose):
916+
def route(args, namespace, display, verbose):
918917
"""Show IPv6 routing table"""
919-
cmd = 'sudo vtysh -c "show ipv6 route'
920-
921-
for arg in args:
922-
cmd += " " + str(arg)
923-
924-
cmd += '"'
925-
926-
run_command(cmd, display_cmd=verbose)
918+
# Call common handler to handle the show ipv6 route cmd
919+
bgp_common.show_routes(args, namespace, display, verbose, "ipv6")
927920

928921

929922
# 'protocol' command

tests/conftest.py

+69-7
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,80 @@ def setup_single_bgp_instance(request):
8080
import utilities_common.bgp_util as bgp_util
8181

8282
if request.param == 'v4':
83-
bgp_summary_json = os.path.join(
83+
bgp_mocked_json = os.path.join(
8484
test_path, 'mock_tables', 'ipv4_bgp_summary.json')
8585
elif request.param == 'v6':
86-
bgp_summary_json = os.path.join(
86+
bgp_mocked_json = os.path.join(
8787
test_path, 'mock_tables', 'ipv6_bgp_summary.json')
88+
elif request.param == 'ip_route':
89+
bgp_mocked_json = os.path.join(
90+
test_path, 'mock_tables', 'ip_route.json')
91+
elif request.param == 'ip_specific_route':
92+
bgp_mocked_json = os.path.join(
93+
test_path, 'mock_tables', 'ip_specific_route.json')
94+
elif request.param == 'ip_special_route':
95+
bgp_mocked_json = os.path.join(
96+
test_path, 'mock_tables', 'ip_special_route.json')
97+
elif request.param == 'ipv6_route':
98+
bgp_mocked_json = os.path.join(
99+
test_path, 'mock_tables', 'ipv6_route.json')
100+
elif request.param == 'ipv6_specific_route':
101+
bgp_mocked_json = os.path.join(
102+
test_path, 'mock_tables', 'ipv6_specific_route.json')
88103
else:
89-
bgp_summary_json = os.path.join(
104+
bgp_mocked_json = os.path.join(
90105
test_path, 'mock_tables', 'dummy.json')
91106

92107
def mock_run_bgp_command(vtysh_cmd, bgp_namespace):
93-
if os.path.isfile(bgp_summary_json):
94-
with open(bgp_summary_json) as json_data:
108+
if os.path.isfile(bgp_mocked_json):
109+
with open(bgp_mocked_json) as json_data:
95110
mock_frr_data = json_data.read()
96111
return mock_frr_data
97112
return ""
98113

99-
bgp_util.run_bgp_command = mock.MagicMock(
100-
return_value=mock_run_bgp_command("", ""))
114+
def mock_run_bgp_ipv6_err_command(vtysh_cmd, bgp_namespace):
115+
return "% Unknown command: show ipv6 route garbage"
101116

117+
if request.param == 'ipv6_route_err':
118+
bgp_util.run_bgp_command = mock.MagicMock(
119+
return_value=mock_run_bgp_ipv6_err_command("", ""))
120+
else:
121+
bgp_util.run_bgp_command = mock.MagicMock(
122+
return_value=mock_run_bgp_command("", ""))
123+
124+
125+
@pytest.fixture
126+
def setup_multi_asic_bgp_instance(request):
127+
import utilities_common.bgp_util as bgp_util
128+
129+
if request.param == 'ip_route':
130+
m_asic_json_file = 'ip_route.json'
131+
elif request.param == 'ip_specific_route':
132+
m_asic_json_file = 'ip_specific_route.json'
133+
elif request.param == 'ipv6_specific_route':
134+
m_asic_json_file = 'ipv6_specific_route.json'
135+
elif request.param == 'ipv6_route':
136+
m_asic_json_file = 'ipv6_route.json'
137+
else:
138+
bgp_mocked_json = os.path.join(
139+
test_path, 'mock_tables', 'dummy.json')
140+
141+
def mock_run_bgp_command(vtysh_cmd, bgp_namespace):
142+
bgp_mocked_json = os.path.join(
143+
test_path, 'mock_tables', bgp_namespace, m_asic_json_file)
144+
if os.path.isfile(bgp_mocked_json):
145+
with open(bgp_mocked_json) as json_data:
146+
mock_frr_data = json_data.read()
147+
return mock_frr_data
148+
else:
149+
return ""
150+
151+
_old_run_bgp_command = bgp_util.run_bgp_command
152+
bgp_util.run_bgp_command = mock_run_bgp_command
153+
154+
yield
155+
156+
bgp_util.run_bgp_command = _old_run_bgp_command
102157

103158
@pytest.fixture
104159
def setup_bgp_commands():
@@ -109,3 +164,10 @@ def setup_bgp_commands():
109164
show.ip.add_command(bgpv4)
110165
show.ipv6.add_command(bgpv6)
111166
return show
167+
168+
169+
@pytest.fixture
170+
def setup_ip_route_commands():
171+
import show.main as show
172+
173+
return show

0 commit comments

Comments
 (0)