Skip to content

Commit

Permalink
Merge pull request #91 from BYGX-wcr/202205
Browse files Browse the repository at this point in the history
Revert "Revert "Change the default behavior of show ip bgp network (#…
  • Loading branch information
BYGX-wcr authored Aug 21, 2024
2 parents a4efecc + 4cea5fe commit f020763
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
30 changes: 17 additions & 13 deletions show/bgp_frr_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
def bgp():
"""Show IPv4 BGP (Border Gateway Protocol) information"""
if device_info.is_supervisor():
# if the device is a chassis, the command need to be executed by rexec
click.echo("Since the current device is a chassis supervisor, " +
"this command will be executed remotely on all linecards")
proc = subprocess.run(["rexec", "all"] + ["-c", " ".join(sys.argv)])
sys.exit(proc.returncode)
pass
subcommand = sys.argv[3]
if subcommand not in "network":
# the command will be executed directly by rexec if it is not "show ip bgp network"
click.echo("Since the current device is a chassis supervisor, " +
"this command will be executed remotely on all linecards")
proc = subprocess.run(["rexec", "all"] + ["-c", " ".join(sys.argv)])
sys.exit(proc.returncode)


# 'summary' subcommand ("show ip bgp summary")
Expand Down Expand Up @@ -92,7 +93,7 @@ def neighbors(ipaddress, info_type, namespace):
@bgp.command()
@click.argument('ipaddress',
metavar='[<ipv4-address>|<ipv4-prefix>]',
required=False)
required=True if device_info.is_supervisor() else False)
@click.argument('info_type',
metavar='[bestpath|json|longer-prefixes|multipath]',
type=click.Choice(
Expand All @@ -103,19 +104,22 @@ def neighbors(ipaddress, info_type, namespace):
'namespace',
type=str,
show_default=True,
required=True if multi_asic.is_multi_asic is True else False,
required=False,
help='Namespace name or all',
default=multi_asic.DEFAULT_NAMESPACE,
default="all",
callback=multi_asic_util.multi_asic_namespace_validation_callback)
def network(ipaddress, info_type, namespace):
"""Show IP (IPv4) BGP network"""

if device_info.is_supervisor():
# the command will be executed by rexec
click.echo("Since the current device is a chassis supervisor, " +
"this command will be executed remotely on all linecards")
proc = subprocess.run(["rexec", "all"] + ["-c", " ".join(sys.argv)])
sys.exit(proc.returncode)

namespace = namespace.strip()
if multi_asic.is_multi_asic():
if namespace == multi_asic.DEFAULT_NAMESPACE:
ctx = click.get_current_context()
ctx.fail('-n/--namespace option required. provide namespace from list {}'
.format(multi_asic.get_namespace_list()))
if namespace != "all" and namespace not in multi_asic.get_namespace_list():
ctx = click.get_current_context()
ctx.fail('invalid namespace {}. provide namespace from list {}'
Expand Down
6 changes: 3 additions & 3 deletions tests/bgp_commands_input/bgp_network_test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,10 +595,10 @@ def mock_show_bgp_network_multi_asic(param):
'rc': 0,
'rc_output': bgp_v6_network_longer_prefixes
},
'bgp_v4_network_multi_asic': {
'bgp_v4_network_default_multi_asic': {
'args': [],
'rc': 2,
'rc_err_msg': multi_asic_bgp_network_err
'rc': 0,
'rc_output': bgp_v4_network_all_asic
},
'bgp_v4_network_asic0': {
'args': ['-nasic0'],
Expand Down
16 changes: 16 additions & 0 deletions tests/remote_show_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def setup_class(cls):
pass

@mock.patch("sonic_py_common.device_info.is_supervisor", mock.MagicMock(return_value=True))
@mock.patch("sys.argv", ["show", "ip", "bgp", "summary"])
def test_show_ip_bgp_rexec(self, setup_bgp_commands):
show = setup_bgp_commands
runner = CliRunner()
Expand All @@ -44,6 +45,7 @@ def test_show_ip_bgp_rexec(self, setup_bgp_commands):
assert MULTI_LC_REXEC_OUTPUT == result.output

@mock.patch("sonic_py_common.device_info.is_supervisor", mock.MagicMock(return_value=True))
@mock.patch("sys.argv", ["show", "ip", "bgp", "summary"])
def test_show_ip_bgp_error_rexec(self, setup_bgp_commands):
show = setup_bgp_commands
runner = CliRunner()
Expand All @@ -55,3 +57,17 @@ def test_show_ip_bgp_error_rexec(self, setup_bgp_commands):
subprocess.run = _old_subprocess_run
assert result.exit_code == 1
assert MULTI_LC_ERR_OUTPUT == result.output

@mock.patch("sonic_py_common.device_info.is_supervisor", mock.MagicMock(return_value=True))
@mock.patch("sys.argv", ["show", "ip", "bgp", "network", "10.0.0.0/24"])
def test_show_ip_bgp_network_rexec(self, setup_bgp_commands):
show = setup_bgp_commands
runner = CliRunner()

_old_subprocess_run = subprocess.run
subprocess.run = mock_rexec_command
result = runner.invoke(show.cli.commands["ip"].commands["bgp"], args=["network", "10.0.0.0/24"])
print(result.output)
subprocess.run = _old_subprocess_run
assert result.exit_code == 0
assert MULTI_LC_REXEC_OUTPUT == result.output
2 changes: 1 addition & 1 deletion tests/show_bgp_network_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def setup_class(cls):

@pytest.mark.parametrize(
'setup_multi_asic_bgp_instance, test_vector',
[('bgp_v4_network', 'bgp_v4_network_multi_asic'),
[('bgp_v4_network_all_asic', 'bgp_v4_network_default_multi_asic'),
('bgp_v6_network', 'bgp_v6_network_multi_asic'),
('bgp_v4_network_asic0', 'bgp_v4_network_asic0'),
('bgp_v4_network_ip_address_asic0', 'bgp_v4_network_ip_address_asic0'),
Expand Down

0 comments on commit f020763

Please sign in to comment.