Skip to content

Commit

Permalink
[CLI][Show][BGP] Show BGP Change for no neighbor scenario (#2885)
Browse files Browse the repository at this point in the history
  • Loading branch information
developfast authored Jul 4, 2023
1 parent 73d8d63 commit 6b8ee47
Show file tree
Hide file tree
Showing 9 changed files with 329 additions and 73 deletions.
135 changes: 127 additions & 8 deletions tests/bgp_commands_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

import pytest
import importlib

from click.testing import CliRunner

Expand Down Expand Up @@ -97,12 +98,72 @@
Error: bgp summary from bgp container not in json format
"""

show_error_no_v6_neighbor = """\
No IPv6 neighbor is configured
show_error_no_v6_neighbor_single_asic = """\
IPv6 Unicast Summary:
BGP router identifier 10.1.0.32, local AS number 65100 vrf-id 0
BGP table version 8972
RIB entries 0, using 0 bytes of memory
Peers 0, using 0 KiB of memory
Peer groups 0, using 0 bytes of memory
Neighbhor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName
----------- --- ---- --------- --------- -------- ----- ------ --------- -------------- --------------
Total number of neighbors 0
"""

show_error_no_v4_neighbor_single_asic = """\
IPv4 Unicast Summary:
BGP router identifier 10.1.0.32, local AS number 65100 vrf-id 0
BGP table version 8972
RIB entries 0, using 0 bytes of memory
Peers 0, using 0 KiB of memory
Peer groups 0, using 0 bytes of memory
Neighbhor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName
----------- --- ---- --------- --------- -------- ----- ------ --------- -------------- --------------
Total number of neighbors 0
"""

show_error_no_v4_neighbor = """\
No IPv4 neighbor is configured
show_error_no_v6_neighbor_multi_asic = """\
IPv6 Unicast Summary:
asic0: BGP router identifier 10.1.0.32, local AS number 65100 vrf-id 0
BGP table version 8972
asic1: BGP router identifier 10.1.0.32, local AS number 65100 vrf-id 0
BGP table version 8972
RIB entries 0, using 0 bytes of memory
Peers 0, using 0 KiB of memory
Peer groups 0, using 0 bytes of memory
Neighbhor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName
----------- --- ---- --------- --------- -------- ----- ------ --------- -------------- --------------
Total number of neighbors 0
"""

show_error_no_v4_neighbor_multi_asic = """\
IPv4 Unicast Summary:
asic0: BGP router identifier 10.1.0.32, local AS number 65100 vrf-id 0
BGP table version 8972
asic1: BGP router identifier 10.1.0.32, local AS number 65100 vrf-id 0
BGP table version 8972
RIB entries 0, using 0 bytes of memory
Peers 0, using 0 KiB of memory
Peer groups 0, using 0 bytes of memory
Neighbhor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName
----------- --- ---- --------- --------- -------- ----- ------ --------- -------------- --------------
Total number of neighbors 0
"""

show_bgp_summary_v4_chassis = """\
Expand Down Expand Up @@ -217,11 +278,14 @@
"""


class TestBgpCommands(object):
class TestBgpCommandsSingleAsic(object):
@classmethod
def setup_class(cls):
print("SETUP")
from .mock_tables import mock_single_asic
importlib.reload(mock_single_asic)
from .mock_tables import dbconnector
dbconnector.load_namespace_config()

@pytest.mark.parametrize('setup_single_bgp_instance',
['v4'], indirect=['setup_single_bgp_instance'])
Expand Down Expand Up @@ -337,10 +401,10 @@ def test_bgp_summary_no_v4_neigh(
show = setup_bgp_commands
runner = CliRunner()
result = runner.invoke(
show.cli.commands["ipv6"].commands["bgp"].commands["summary"], [])
show.cli.commands["ip"].commands["bgp"].commands["summary"], [])
print("{}".format(result.output))
assert result.exit_code == 0
assert result.output == show_error_no_v6_neighbor
assert result.output == show_error_no_v4_neighbor_single_asic

@pytest.mark.parametrize('setup_single_bgp_instance',
['show_bgp_summary_no_neigh'], indirect=['setup_single_bgp_instance'])
Expand All @@ -350,8 +414,63 @@ def test_bgp_summary_no_v6_neigh(
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_single_asic

@classmethod
def teardown_class(cls):
print("TEARDOWN")
os.environ['UTILITIES_UNIT_TESTING'] = "0"
from .mock_tables import mock_single_asic
importlib.reload(mock_single_asic)
from .mock_tables import dbconnector
dbconnector.load_database_config()


class TestBgpCommandsMultiAsic(object):
@classmethod
def setup_class(cls):
print("SETUP")
from .mock_tables import mock_multi_asic
importlib.reload(mock_multi_asic)
from .mock_tables import dbconnector
dbconnector.load_namespace_config()

@pytest.mark.parametrize('setup_multi_asic_bgp_instance',
['show_bgp_summary_no_neigh'], indirect=['setup_multi_asic_bgp_instance'])
def test_bgp_summary_multi_asic_no_v4_neigh(
self,
setup_bgp_commands,
setup_multi_asic_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
assert result.output == show_error_no_v4_neighbor_multi_asic

@pytest.mark.parametrize('setup_multi_asic_bgp_instance',
['show_bgp_summary_no_neigh'], indirect=['setup_multi_asic_bgp_instance'])
def test_bgp_summary_multi_asic_no_v6_neigh(
self,
setup_bgp_commands,
setup_multi_asic_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_multi_asic

@classmethod
def teardown_class(cls):
print("TEARDOWN")
from .mock_tables import mock_single_asic
importlib.reload(mock_single_asic)
from .mock_tables import dbconnector
dbconnector.load_database_config
52 changes: 36 additions & 16 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ def setup_single_bgp_instance(request):
elif request.param == 'v6':
bgp_mocked_json = os.path.join(
test_path, 'mock_tables', 'ipv6_bgp_summary.json')
elif request.param == 'show_bgp_summary_no_neigh':
bgp_neigh_mocked_json = os.path.join(
test_path, 'mock_tables', 'no_bgp_neigh.json')
bgp_mocked_json = os.path.join(
test_path, 'mock_tables', 'device_bgp_info.json')
elif request.param == 'show_run_bgp':
bgp_mocked_json = os.path.join(
test_path, 'mock_tables', 'show_run_bgp.txt')
Expand All @@ -187,12 +192,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:
def mock_run_bgp_command(mock_bgp_file):
if os.path.isfile(mock_bgp_file):
with open(mock_bgp_file) as json_data:
mock_frr_data = json_data.read()
return mock_frr_data
return ""
Expand All @@ -218,7 +220,7 @@ def mock_run_show_ip_route_commands(request):
else:
return ""

def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVTYSH_COMMAND):
def mock_run_bgp_route_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVTYSH_COMMAND):
bgp_mocked_json_file = os.path.join(
test_path, 'mock_tables', bgp_mocked_json)
if os.path.isfile(bgp_mocked_json_file):
Expand All @@ -229,11 +231,11 @@ def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVT
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']):
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']):
bgp_util.run_bgp_command = mock.MagicMock(
return_value=mock_run_bgp_command("",""))
return_value=mock_run_bgp_route_command("", ""))
elif request.param.startswith('ipv6_route_err'):
bgp_util.run_bgp_command = mock.MagicMock(
return_value=mock_run_show_ip_route_commands(request))
Expand All @@ -242,20 +244,21 @@ 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_neighbor_single_asic(request))
elif request.param.startswith('bgp_v4_network') or \
request.param.startswith('bgp_v6_network'):
request.param.startswith('bgp_v6_network'):
bgp_util.run_bgp_command = mock.MagicMock(
return_value=mock_show_bgp_network_single_asic(request))
elif request.param == 'ip_route_for_int_ip':
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("", ""))
elif request.param.startswith('show_run_bgp'):
bgp_util.run_bgp_command = mock.MagicMock(
return_value=mock_show_run_bgp(request))
elif request.param == 'show_bgp_summary_no_neigh':
functions_to_call = [mock_run_bgp_command(bgp_neigh_mocked_json), mock_run_bgp_command(bgp_mocked_json)]
bgp_util.run_bgp_command = mock.MagicMock(
side_effect=functions_to_call)
else:
bgp_util.run_bgp_command = mock.MagicMock(
return_value=mock_show_bgp_summary("", ""))
return_value=mock_run_bgp_command(bgp_mocked_json))

yield

Expand Down Expand Up @@ -325,9 +328,26 @@ def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVT
else:
return ""

def mock_run_show_sum_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.VTYSH_COMMAND):
if vtysh_cmd == "show ip bgp summary json":
m_asic_json_file = 'no_bgp_neigh.json'
else:
m_asic_json_file = 'device_bgp_info.json'

bgp_mocked_json = os.path.join(
test_path, 'mock_tables', bgp_namespace, m_asic_json_file)
if os.path.isfile(bgp_mocked_json):
with open(bgp_mocked_json) as json_data:
mock_frr_data = json_data.read()
return mock_frr_data
else:
return ""

_old_run_bgp_command = bgp_util.run_bgp_command
if request.param == 'ip_route_for_int_ip':
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_run_show_sum_bgp_command
else:
bgp_util.run_bgp_command = mock_run_bgp_command

Expand Down
30 changes: 30 additions & 0 deletions tests/mock_tables/asic0/device_bgp_info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"vrfId": 0,
"vrfName": "default",
"tableVersion": 8972,
"routerId": "10.1.0.32",
"defaultLocPrf": 100,
"localAS": 65100,
"routes": { "10.1.0.32/32": [
{
"valid":true,
"bestpath":true,
"pathFrom":"external",
"prefix":"10.1.0.32",
"prefixLen":32,
"network":"10.1.0.32\/32",
"metric":0,
"weight":32768,
"peerId":"(unspec)",
"path":"",
"origin":"IGP",
"nexthops":[
{
"ip":"0.0.0.0",
"hostname":"STR-8102-C19-U24",
"afi":"ipv4",
"used":true
}
]
}
] } }
1 change: 1 addition & 0 deletions tests/mock_tables/asic0/no_bgp_neigh.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
30 changes: 30 additions & 0 deletions tests/mock_tables/asic1/device_bgp_info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"vrfId": 0,
"vrfName": "default",
"tableVersion": 8972,
"routerId": "10.1.0.32",
"defaultLocPrf": 100,
"localAS": 65100,
"routes": { "10.1.0.32/32": [
{
"valid":true,
"bestpath":true,
"pathFrom":"external",
"prefix":"10.1.0.32",
"prefixLen":32,
"network":"10.1.0.32\/32",
"metric":0,
"weight":32768,
"peerId":"(unspec)",
"path":"",
"origin":"IGP",
"nexthops":[
{
"ip":"0.0.0.0",
"hostname":"STR-8102-C19-U24",
"afi":"ipv4",
"used":true
}
]
}
] } }
1 change: 1 addition & 0 deletions tests/mock_tables/asic1/no_bgp_neigh.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
30 changes: 30 additions & 0 deletions tests/mock_tables/device_bgp_info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"vrfId": 0,
"vrfName": "default",
"tableVersion": 8972,
"routerId": "10.1.0.32",
"defaultLocPrf": 100,
"localAS": 65100,
"routes": { "10.1.0.32/32": [
{
"valid":true,
"bestpath":true,
"pathFrom":"external",
"prefix":"10.1.0.32",
"prefixLen":32,
"network":"10.1.0.32\/32",
"metric":0,
"weight":32768,
"peerId":"(unspec)",
"path":"",
"origin":"IGP",
"nexthops":[
{
"ip":"0.0.0.0",
"hostname":"STR-8102-C19-U24",
"afi":"ipv4",
"used":true
}
]
}
] } }
1 change: 1 addition & 0 deletions tests/mock_tables/no_bgp_neigh.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Loading

0 comments on commit 6b8ee47

Please sign in to comment.