Skip to content

Commit

Permalink
avoid printing error if no neighbors are present (sonic-net#2502)
Browse files Browse the repository at this point in the history
* avoid printing error if no neighbors are present

Signed-off-by: Arvindsrinivasan Lakshmi Narasimhan <arlakshm@microsoft.com>
  • Loading branch information
arlakshm authored and preetham-singh committed Nov 18, 2022
1 parent 2000b1f commit 6a4b494
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
36 changes: 36 additions & 0 deletions tests/bgp_commands_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@
Error: bgp summary from bgp container not in json format
"""

show_error_no_v6_neighbor = """\
No IPv6 neighbor is configured
"""

show_error_no_v4_neighbor = """\
No IPv4 neighbor is configured
"""

show_bgp_summary_v4_chassis = """\
IPv4 Unicast Summary:
Expand Down Expand Up @@ -319,3 +327,31 @@ def test_bgp_summary_v4_all_chassis(
print("{}".format(result.output))
assert result.exit_code == 0
assert result.output == show_bgp_summary_v4_all_chassis

@pytest.mark.parametrize('setup_single_bgp_instance',
['show_bgp_summary_no_neigh'], indirect=['setup_single_bgp_instance'])
def test_bgp_summary_no_v4_neigh(
self,
setup_bgp_commands,
setup_single_bgp_instance):
show = setup_bgp_commands
runner = CliRunner()
result = runner.invoke(
show.cli.commands["ipv6"].commands["bgp"].commands["summary"], [])
print("{}".format(result.output))
assert result.exit_code == 0
assert result.output == show_error_no_v6_neighbor

@pytest.mark.parametrize('setup_single_bgp_instance',
['show_bgp_summary_no_neigh'], indirect=['setup_single_bgp_instance'])
def test_bgp_summary_no_v6_neigh(
self,
setup_bgp_commands,
setup_single_bgp_instance):
show = setup_bgp_commands
runner = CliRunner()
result = runner.invoke(
show.cli.commands["ip"].commands["bgp"].commands["summary"], [])
print("{}".format(result.output))
assert result.exit_code == 0
assert result.output == show_error_no_v4_neighbor
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ def setup_single_bgp_instance(request):
bgp_mocked_json = os.path.join(
test_path, 'mock_tables', 'dummy.json')

def mock_show_bgp_summary_no_neigh(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVTYSH_COMMAND):
return "{}"

def mock_show_bgp_summary(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVTYSH_COMMAND):
if os.path.isfile(bgp_mocked_json):
with open(bgp_mocked_json) as json_data:
Expand Down Expand Up @@ -217,6 +220,9 @@ def mock_run_show_ip_route_commands(request):
elif request.param == 'ip_route_for_int_ip':
_old_run_bgp_command = bgp_util.run_bgp_command
bgp_util.run_bgp_command = mock_run_bgp_command_for_static
elif request.param == "show_bgp_summary_no_neigh":
bgp_util.run_bgp_command = mock.MagicMock(
return_value=mock_show_bgp_summary_no_neigh("", ""))
else:
bgp_util.run_bgp_command = mock.MagicMock(
return_value=mock_show_bgp_summary("", ""))
Expand Down
4 changes: 3 additions & 1 deletion utilities_common/bgp_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,10 @@ def get_bgp_summary_from_all_bgp_instances(af, namespace, display):
except ValueError:
ctx.fail("bgp summary from bgp container not in json format")

# exit cli command without printing the error message
if key not in cmd_output_json:
ctx.fail("bgp summary from bgp container in invalid format")
click.echo("No IP{} neighbor is configured".format(af))
exit()

device.current_namespace = ns

Expand Down

0 comments on commit 6a4b494

Please sign in to comment.