diff --git a/tests/bgp_commands_test.py b/tests/bgp_commands_test.py index 24b4fdeafb..4954311440 100644 --- a/tests/bgp_commands_test.py +++ b/tests/bgp_commands_test.py @@ -91,6 +91,13 @@ 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 +""" class TestBgpCommands(object): @classmethod @@ -139,3 +146,31 @@ def test_bgp_summary_error( print("{}".format(result.output)) assert result.exit_code == 2 assert result.output == show_error_invalid_json + + @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 diff --git a/tests/conftest.py b/tests/conftest.py index fd70b2dbec..890f8035d0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -119,6 +119,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_run_bgp_command(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: @@ -148,6 +151,9 @@ def mock_run_show_ip_route_commands(request): request.param == 'ipv6_route', request.param == 'ipv6_specific_route']): bgp_util.run_bgp_command = mock.MagicMock( return_value=mock_run_show_ip_route_commands(request)) + 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_run_bgp_command("", "")) diff --git a/utilities_common/bgp_util.py b/utilities_common/bgp_util.py index e9ca3b0ee1..c3125a7a1d 100644 --- a/utilities_common/bgp_util.py +++ b/utilities_common/bgp_util.py @@ -186,8 +186,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