Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[show]Fix show route return code on error #2542

Merged
merged 4 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,13 @@ def mock_show_bgp_summary(
bgp_mocked_json = os.path.join(
test_path, 'mock_tables', 'ipv6_bgp_summary_chassis.json')

_old_run_bgp_command = bgp_util.run_bgp_command
bgp_util.run_bgp_command = mock.MagicMock(
return_value=mock_show_bgp_summary("", ""))

yield
bgp_util.run_bgp_command = _old_run_bgp_command


@pytest.fixture
def setup_t1_topo():
Expand Down Expand Up @@ -213,6 +217,7 @@ def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVT
else:
return ""

_old_run_bgp_command = bgp_util.run_bgp_command
if any ([request.param == 'ip_route',\
request.param == 'ip_specific_route', request.param == 'ip_special_route',\
request.param == 'ipv6_route', request.param == 'ipv6_specific_route']):
Expand All @@ -230,7 +235,6 @@ def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVT
bgp_util.run_bgp_command = mock.MagicMock(
return_value=mock_show_bgp_network_single_asic(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(
Expand All @@ -241,8 +245,7 @@ def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVT

yield

if request.param == 'ip_route_for_int_ip':
bgp_util.run_bgp_command = _old_run_bgp_command
bgp_util.run_bgp_command = _old_run_bgp_command


@pytest.fixture
Expand Down
9 changes: 9 additions & 0 deletions tests/ip_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import config.main as config
import show.main as show
from utilities_common.db import Db
import utilities_common.bgp_util as bgp_util

test_path = os.path.dirname(os.path.abspath(__file__))
ip_config_input_path = os.path.join(test_path, "ip_config_input")
Expand All @@ -30,13 +31,20 @@
"""

class TestConfigIP(object):
_old_run_bgp_command = None
@classmethod
def setup_class(cls):
os.environ['UTILITIES_UNIT_TESTING'] = "1"
cls._old_run_bgp_command = bgp_util.run_bgp_command
bgp_util.run_bgp_command = mock.MagicMock(
return_value=cls.mock_run_bgp_command())
print("SETUP")

''' Tests for IPv4 '''

def mock_run_bgp_command():
return ""

def test_add_del_interface_valid_ipv4(self):
db = Db()
runner = CliRunner()
Expand Down Expand Up @@ -272,4 +280,5 @@ def test_intf_unknown_vrf_bind(self):
@classmethod
def teardown_class(cls):
os.environ['UTILITIES_UNIT_TESTING'] = "0"
bgp_util.run_bgp_command = cls._old_run_bgp_command
print("TEARDOWN")
25 changes: 25 additions & 0 deletions tests/ip_show_routes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@

from . import show_ip_route_common
from click.testing import CliRunner
import mock
import sys

test_path = os.path.dirname(os.path.abspath(__file__))
modules_path = os.path.dirname(test_path)
scripts_path = os.path.join(modules_path, "scripts")

sys.path.insert(0, test_path)


class TestShowIpRouteCommands(object):
@classmethod
Expand All @@ -18,6 +22,26 @@ def setup_class(cls):
os.environ["UTILITIES_UNIT_TESTING"] = "0"
os.environ["UTILITIES_UNIT_TESTING_TOPOLOGY"] = ""
import mock_tables.dbconnector

def test_show_ip_route_err(
self,
setup_ip_route_commands):
show = setup_ip_route_commands

def mock_run_bgp_command(*args, **kwargs):
command = args[0]
print("% Unknown command: show ip route unknown")
return 1

with mock.patch('utilities_common.cli.run_command', mock.MagicMock(side_effect=mock_run_bgp_command)) as mock_run_command:
runner = CliRunner()
result = runner.invoke(
show.cli.commands["ip"].commands["route"], ["unknown"])
print("{}".format(result.output))
print(result.exit_code)
assert result.exit_code == 1
assert result.output == "% Unknown command: show ip route unknown" + "\n"

@pytest.mark.parametrize('setup_single_bgp_instance',
['ip_route'], indirect=['setup_single_bgp_instance'])
def test_show_ip_route(
Expand Down Expand Up @@ -117,3 +141,4 @@ def test_show_ipv6_route_err(
print("{}".format(result.output))
assert result.exit_code == 0
assert result.output == show_ip_route_common.show_ipv6_route_err_expected_output + "\n"

9 changes: 9 additions & 0 deletions tests/vlan_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import show.main as show
from utilities_common.db import Db
from importlib import reload
import utilities_common.bgp_util as bgp_util

show_vlan_brief_output="""\
+-----------+-----------------+-----------------+----------------+-------------+
Expand Down Expand Up @@ -143,16 +144,23 @@
+-----------+-----------------+-----------------+----------------+-------------+
"""
class TestVlan(object):
_old_run_bgp_command = None
@classmethod
def setup_class(cls):
os.environ['UTILITIES_UNIT_TESTING'] = "1"
# ensure that we are working with single asic config
cls._old_run_bgp_command = bgp_util.run_bgp_command
bgp_util.run_bgp_command = mock.MagicMock(
return_value=cls.mock_run_bgp_command())
from .mock_tables import dbconnector
from .mock_tables import mock_single_asic
reload(mock_single_asic)
dbconnector.load_namespace_config()
print("SETUP")

def mock_run_bgp_command():
return ""

def test_show_vlan(self):
runner = CliRunner()
result = runner.invoke(show.cli.commands["vlan"], [])
Expand Down Expand Up @@ -579,4 +587,5 @@ def test_config_vlan_add_member_of_portchannel(self):
@classmethod
def teardown_class(cls):
os.environ['UTILITIES_UNIT_TESTING'] = "0"
bgp_util.run_bgp_command = cls._old_run_bgp_command
print("TEARDOWN")
2 changes: 1 addition & 1 deletion utilities_common/bgp_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def run_bgp_command(vtysh_cmd, bgp_namespace=multi_asic.DEFAULT_NAMESPACE, vtysh
cmd = 'sudo {} {} -c "{}"'.format(
vtysh_shell_cmd, bgp_instance_id, vtysh_cmd)
try:
output = clicommon.run_command(cmd, return_cmd=True)
output = clicommon.run_command(cmd)
except Exception:
ctx = click.get_current_context()
ctx.fail("Unable to get summary from bgp {}".format(bgp_instance_id))
Expand Down