Skip to content

Commit 6b8ee47

Browse files
authored
[CLI][Show][BGP] Show BGP Change for no neighbor scenario (#2885)
1 parent 73d8d63 commit 6b8ee47

9 files changed

+329
-73
lines changed

tests/bgp_commands_test.py

+127-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22

33
import pytest
4+
import importlib
45

56
from click.testing import CliRunner
67

@@ -97,12 +98,72 @@
9798
Error: bgp summary from bgp container not in json format
9899
"""
99100

100-
show_error_no_v6_neighbor = """\
101-
No IPv6 neighbor is configured
101+
show_error_no_v6_neighbor_single_asic = """\
102+
103+
IPv6 Unicast Summary:
104+
BGP router identifier 10.1.0.32, local AS number 65100 vrf-id 0
105+
BGP table version 8972
106+
RIB entries 0, using 0 bytes of memory
107+
Peers 0, using 0 KiB of memory
108+
Peer groups 0, using 0 bytes of memory
109+
110+
111+
Neighbhor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName
112+
----------- --- ---- --------- --------- -------- ----- ------ --------- -------------- --------------
113+
114+
Total number of neighbors 0
115+
"""
116+
117+
show_error_no_v4_neighbor_single_asic = """\
118+
119+
IPv4 Unicast Summary:
120+
BGP router identifier 10.1.0.32, local AS number 65100 vrf-id 0
121+
BGP table version 8972
122+
RIB entries 0, using 0 bytes of memory
123+
Peers 0, using 0 KiB of memory
124+
Peer groups 0, using 0 bytes of memory
125+
126+
127+
Neighbhor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName
128+
----------- --- ---- --------- --------- -------- ----- ------ --------- -------------- --------------
129+
130+
Total number of neighbors 0
102131
"""
103132

104-
show_error_no_v4_neighbor = """\
105-
No IPv4 neighbor is configured
133+
show_error_no_v6_neighbor_multi_asic = """\
134+
135+
IPv6 Unicast Summary:
136+
asic0: BGP router identifier 10.1.0.32, local AS number 65100 vrf-id 0
137+
BGP table version 8972
138+
asic1: BGP router identifier 10.1.0.32, local AS number 65100 vrf-id 0
139+
BGP table version 8972
140+
RIB entries 0, using 0 bytes of memory
141+
Peers 0, using 0 KiB of memory
142+
Peer groups 0, using 0 bytes of memory
143+
144+
145+
Neighbhor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName
146+
----------- --- ---- --------- --------- -------- ----- ------ --------- -------------- --------------
147+
148+
Total number of neighbors 0
149+
"""
150+
151+
show_error_no_v4_neighbor_multi_asic = """\
152+
153+
IPv4 Unicast Summary:
154+
asic0: BGP router identifier 10.1.0.32, local AS number 65100 vrf-id 0
155+
BGP table version 8972
156+
asic1: BGP router identifier 10.1.0.32, local AS number 65100 vrf-id 0
157+
BGP table version 8972
158+
RIB entries 0, using 0 bytes of memory
159+
Peers 0, using 0 KiB of memory
160+
Peer groups 0, using 0 bytes of memory
161+
162+
163+
Neighbhor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName
164+
----------- --- ---- --------- --------- -------- ----- ------ --------- -------------- --------------
165+
166+
Total number of neighbors 0
106167
"""
107168

108169
show_bgp_summary_v4_chassis = """\
@@ -217,11 +278,14 @@
217278
"""
218279

219280

220-
class TestBgpCommands(object):
281+
class TestBgpCommandsSingleAsic(object):
221282
@classmethod
222283
def setup_class(cls):
223284
print("SETUP")
285+
from .mock_tables import mock_single_asic
286+
importlib.reload(mock_single_asic)
224287
from .mock_tables import dbconnector
288+
dbconnector.load_namespace_config()
225289

226290
@pytest.mark.parametrize('setup_single_bgp_instance',
227291
['v4'], indirect=['setup_single_bgp_instance'])
@@ -337,10 +401,10 @@ def test_bgp_summary_no_v4_neigh(
337401
show = setup_bgp_commands
338402
runner = CliRunner()
339403
result = runner.invoke(
340-
show.cli.commands["ipv6"].commands["bgp"].commands["summary"], [])
404+
show.cli.commands["ip"].commands["bgp"].commands["summary"], [])
341405
print("{}".format(result.output))
342406
assert result.exit_code == 0
343-
assert result.output == show_error_no_v6_neighbor
407+
assert result.output == show_error_no_v4_neighbor_single_asic
344408

345409
@pytest.mark.parametrize('setup_single_bgp_instance',
346410
['show_bgp_summary_no_neigh'], indirect=['setup_single_bgp_instance'])
@@ -350,8 +414,63 @@ def test_bgp_summary_no_v6_neigh(
350414
setup_single_bgp_instance):
351415
show = setup_bgp_commands
352416
runner = CliRunner()
417+
result = runner.invoke(
418+
show.cli.commands["ipv6"].commands["bgp"].commands["summary"], [])
419+
print("{}".format(result.output))
420+
assert result.exit_code == 0
421+
assert result.output == show_error_no_v6_neighbor_single_asic
422+
423+
@classmethod
424+
def teardown_class(cls):
425+
print("TEARDOWN")
426+
os.environ['UTILITIES_UNIT_TESTING'] = "0"
427+
from .mock_tables import mock_single_asic
428+
importlib.reload(mock_single_asic)
429+
from .mock_tables import dbconnector
430+
dbconnector.load_database_config()
431+
432+
433+
class TestBgpCommandsMultiAsic(object):
434+
@classmethod
435+
def setup_class(cls):
436+
print("SETUP")
437+
from .mock_tables import mock_multi_asic
438+
importlib.reload(mock_multi_asic)
439+
from .mock_tables import dbconnector
440+
dbconnector.load_namespace_config()
441+
442+
@pytest.mark.parametrize('setup_multi_asic_bgp_instance',
443+
['show_bgp_summary_no_neigh'], indirect=['setup_multi_asic_bgp_instance'])
444+
def test_bgp_summary_multi_asic_no_v4_neigh(
445+
self,
446+
setup_bgp_commands,
447+
setup_multi_asic_bgp_instance):
448+
show = setup_bgp_commands
449+
runner = CliRunner()
353450
result = runner.invoke(
354451
show.cli.commands["ip"].commands["bgp"].commands["summary"], [])
355452
print("{}".format(result.output))
356453
assert result.exit_code == 0
357-
assert result.output == show_error_no_v4_neighbor
454+
assert result.output == show_error_no_v4_neighbor_multi_asic
455+
456+
@pytest.mark.parametrize('setup_multi_asic_bgp_instance',
457+
['show_bgp_summary_no_neigh'], indirect=['setup_multi_asic_bgp_instance'])
458+
def test_bgp_summary_multi_asic_no_v6_neigh(
459+
self,
460+
setup_bgp_commands,
461+
setup_multi_asic_bgp_instance):
462+
show = setup_bgp_commands
463+
runner = CliRunner()
464+
result = runner.invoke(
465+
show.cli.commands["ipv6"].commands["bgp"].commands["summary"], [])
466+
print("{}".format(result.output))
467+
assert result.exit_code == 0
468+
assert result.output == show_error_no_v6_neighbor_multi_asic
469+
470+
@classmethod
471+
def teardown_class(cls):
472+
print("TEARDOWN")
473+
from .mock_tables import mock_single_asic
474+
importlib.reload(mock_single_asic)
475+
from .mock_tables import dbconnector
476+
dbconnector.load_database_config

tests/conftest.py

+36-16
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ def setup_single_bgp_instance(request):
170170
elif request.param == 'v6':
171171
bgp_mocked_json = os.path.join(
172172
test_path, 'mock_tables', 'ipv6_bgp_summary.json')
173+
elif request.param == 'show_bgp_summary_no_neigh':
174+
bgp_neigh_mocked_json = os.path.join(
175+
test_path, 'mock_tables', 'no_bgp_neigh.json')
176+
bgp_mocked_json = os.path.join(
177+
test_path, 'mock_tables', 'device_bgp_info.json')
173178
elif request.param == 'show_run_bgp':
174179
bgp_mocked_json = os.path.join(
175180
test_path, 'mock_tables', 'show_run_bgp.txt')
@@ -187,12 +192,9 @@ def setup_single_bgp_instance(request):
187192
bgp_mocked_json = os.path.join(
188193
test_path, 'mock_tables', 'dummy.json')
189194

190-
def mock_show_bgp_summary_no_neigh(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVTYSH_COMMAND):
191-
return "{}"
192-
193-
def mock_show_bgp_summary(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVTYSH_COMMAND):
194-
if os.path.isfile(bgp_mocked_json):
195-
with open(bgp_mocked_json) as json_data:
195+
def mock_run_bgp_command(mock_bgp_file):
196+
if os.path.isfile(mock_bgp_file):
197+
with open(mock_bgp_file) as json_data:
196198
mock_frr_data = json_data.read()
197199
return mock_frr_data
198200
return ""
@@ -218,7 +220,7 @@ def mock_run_show_ip_route_commands(request):
218220
else:
219221
return ""
220222

221-
def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVTYSH_COMMAND):
223+
def mock_run_bgp_route_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVTYSH_COMMAND):
222224
bgp_mocked_json_file = os.path.join(
223225
test_path, 'mock_tables', bgp_mocked_json)
224226
if os.path.isfile(bgp_mocked_json_file):
@@ -229,11 +231,11 @@ def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVT
229231
return ""
230232

231233
_old_run_bgp_command = bgp_util.run_bgp_command
232-
if any ([request.param == 'ip_route',\
233-
request.param == 'ip_specific_route', request.param == 'ip_special_route',\
234-
request.param == 'ipv6_route', request.param == 'ipv6_specific_route']):
234+
if any([request.param == 'ip_route',
235+
request.param == 'ip_specific_route', request.param == 'ip_special_route',
236+
request.param == 'ipv6_route', request.param == 'ipv6_specific_route']):
235237
bgp_util.run_bgp_command = mock.MagicMock(
236-
return_value=mock_run_bgp_command("",""))
238+
return_value=mock_run_bgp_route_command("", ""))
237239
elif request.param.startswith('ipv6_route_err'):
238240
bgp_util.run_bgp_command = mock.MagicMock(
239241
return_value=mock_run_show_ip_route_commands(request))
@@ -242,20 +244,21 @@ def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVT
242244
bgp_util.run_bgp_command = mock.MagicMock(
243245
return_value=mock_show_bgp_neighbor_single_asic(request))
244246
elif request.param.startswith('bgp_v4_network') or \
245-
request.param.startswith('bgp_v6_network'):
247+
request.param.startswith('bgp_v6_network'):
246248
bgp_util.run_bgp_command = mock.MagicMock(
247249
return_value=mock_show_bgp_network_single_asic(request))
248250
elif request.param == 'ip_route_for_int_ip':
249251
bgp_util.run_bgp_command = mock_run_bgp_command_for_static
250-
elif request.param == "show_bgp_summary_no_neigh":
251-
bgp_util.run_bgp_command = mock.MagicMock(
252-
return_value=mock_show_bgp_summary_no_neigh("", ""))
253252
elif request.param.startswith('show_run_bgp'):
254253
bgp_util.run_bgp_command = mock.MagicMock(
255254
return_value=mock_show_run_bgp(request))
255+
elif request.param == 'show_bgp_summary_no_neigh':
256+
functions_to_call = [mock_run_bgp_command(bgp_neigh_mocked_json), mock_run_bgp_command(bgp_mocked_json)]
257+
bgp_util.run_bgp_command = mock.MagicMock(
258+
side_effect=functions_to_call)
256259
else:
257260
bgp_util.run_bgp_command = mock.MagicMock(
258-
return_value=mock_show_bgp_summary("", ""))
261+
return_value=mock_run_bgp_command(bgp_mocked_json))
259262

260263
yield
261264

@@ -325,9 +328,26 @@ def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVT
325328
else:
326329
return ""
327330

331+
def mock_run_show_sum_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.VTYSH_COMMAND):
332+
if vtysh_cmd == "show ip bgp summary json":
333+
m_asic_json_file = 'no_bgp_neigh.json'
334+
else:
335+
m_asic_json_file = 'device_bgp_info.json'
336+
337+
bgp_mocked_json = os.path.join(
338+
test_path, 'mock_tables', bgp_namespace, m_asic_json_file)
339+
if os.path.isfile(bgp_mocked_json):
340+
with open(bgp_mocked_json) as json_data:
341+
mock_frr_data = json_data.read()
342+
return mock_frr_data
343+
else:
344+
return ""
345+
328346
_old_run_bgp_command = bgp_util.run_bgp_command
329347
if request.param == 'ip_route_for_int_ip':
330348
bgp_util.run_bgp_command = mock_run_bgp_command_for_static
349+
elif request.param == 'show_bgp_summary_no_neigh':
350+
bgp_util.run_bgp_command = mock_run_show_sum_bgp_command
331351
else:
332352
bgp_util.run_bgp_command = mock_run_bgp_command
333353

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"vrfId": 0,
3+
"vrfName": "default",
4+
"tableVersion": 8972,
5+
"routerId": "10.1.0.32",
6+
"defaultLocPrf": 100,
7+
"localAS": 65100,
8+
"routes": { "10.1.0.32/32": [
9+
{
10+
"valid":true,
11+
"bestpath":true,
12+
"pathFrom":"external",
13+
"prefix":"10.1.0.32",
14+
"prefixLen":32,
15+
"network":"10.1.0.32\/32",
16+
"metric":0,
17+
"weight":32768,
18+
"peerId":"(unspec)",
19+
"path":"",
20+
"origin":"IGP",
21+
"nexthops":[
22+
{
23+
"ip":"0.0.0.0",
24+
"hostname":"STR-8102-C19-U24",
25+
"afi":"ipv4",
26+
"used":true
27+
}
28+
]
29+
}
30+
] } }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"vrfId": 0,
3+
"vrfName": "default",
4+
"tableVersion": 8972,
5+
"routerId": "10.1.0.32",
6+
"defaultLocPrf": 100,
7+
"localAS": 65100,
8+
"routes": { "10.1.0.32/32": [
9+
{
10+
"valid":true,
11+
"bestpath":true,
12+
"pathFrom":"external",
13+
"prefix":"10.1.0.32",
14+
"prefixLen":32,
15+
"network":"10.1.0.32\/32",
16+
"metric":0,
17+
"weight":32768,
18+
"peerId":"(unspec)",
19+
"path":"",
20+
"origin":"IGP",
21+
"nexthops":[
22+
{
23+
"ip":"0.0.0.0",
24+
"hostname":"STR-8102-C19-U24",
25+
"afi":"ipv4",
26+
"used":true
27+
}
28+
]
29+
}
30+
] } }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"vrfId": 0,
3+
"vrfName": "default",
4+
"tableVersion": 8972,
5+
"routerId": "10.1.0.32",
6+
"defaultLocPrf": 100,
7+
"localAS": 65100,
8+
"routes": { "10.1.0.32/32": [
9+
{
10+
"valid":true,
11+
"bestpath":true,
12+
"pathFrom":"external",
13+
"prefix":"10.1.0.32",
14+
"prefixLen":32,
15+
"network":"10.1.0.32\/32",
16+
"metric":0,
17+
"weight":32768,
18+
"peerId":"(unspec)",
19+
"path":"",
20+
"origin":"IGP",
21+
"nexthops":[
22+
{
23+
"ip":"0.0.0.0",
24+
"hostname":"STR-8102-C19-U24",
25+
"afi":"ipv4",
26+
"used":true
27+
}
28+
]
29+
}
30+
] } }

tests/mock_tables/no_bgp_neigh.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)