Skip to content

Commit 6d95d9b

Browse files
Make 'show ip bgp summary' work even when we don't have any peer groups (#3739)
* Make 'show ip bgp summary' work even when we don't have any peer groups configured. * fixing the indentation issues
1 parent 7f3957c commit 6d95d9b

File tree

2 files changed

+110
-2
lines changed

2 files changed

+110
-2
lines changed

tests/bgp_commands_test.py

+105
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import os
23

34
import pytest
@@ -481,6 +482,110 @@ def test_bgp_summary_no_v6_neigh(
481482
assert result.exit_code == 0
482483
assert result.output == show_error_no_v6_neighbor_single_asic
483484

485+
@pytest.mark.parametrize('setup_single_bgp_instance',
486+
['v4'], indirect=['setup_single_bgp_instance'])
487+
def test_bgp_summary_raw_missing_peergroup_count(
488+
self,
489+
setup_bgp_commands,
490+
setup_single_bgp_instance):
491+
show = setup_bgp_commands
492+
runner = CliRunner()
493+
# mock vtysh cli output that does not have peergroup count
494+
mock_json = {
495+
"ipv4Unicast": {
496+
"routerId": "10.1.0.32",
497+
"as": 65100,
498+
"localAS": 65100,
499+
"vrfId": 0,
500+
"tableVersion": 1,
501+
"totalPeers": 0,
502+
"dynamicPeers": 0,
503+
"bestPaths": 0,
504+
"peerCount": 2,
505+
"peerMemory": 2048,
506+
"ribCount": 10,
507+
"ribMemory": 1024,
508+
"peers": {
509+
"10.0.0.33": {
510+
"remoteAs": 64001,
511+
"version": 4,
512+
"msgRcvd": 0,
513+
"msgSent": 0,
514+
"tableVersion": 0,
515+
"outq": 0,
516+
"inq": 0,
517+
"peerUptime": "never",
518+
"peerUptimeMsec": 0,
519+
"prefixReceivedCount": 0,
520+
"pfxRcd": 0,
521+
"state": "Active",
522+
"connectionsEstablished": 0,
523+
"connectionsDropped": 0,
524+
"idType": "ipv4"
525+
}
526+
}
527+
}
528+
}
529+
530+
with patch('utilities_common.bgp_util.run_bgp_command', return_value=json.dumps(mock_json)):
531+
result = runner.invoke(
532+
show.cli.commands["ip"].commands["bgp"].commands["summary"], [])
533+
# verify that the CLI handles missing peergroup count gracefully
534+
assert result.exit_code == 0
535+
assert "Peer groups 0, using 0 bytes of memory" in result.output
536+
537+
@pytest.mark.parametrize('setup_single_bgp_instance',
538+
['v6'], indirect=['setup_single_bgp_instance'])
539+
def test_bgp_summary_raw_missing_peergroup_count_v6(
540+
self,
541+
setup_bgp_commands,
542+
setup_single_bgp_instance):
543+
show = setup_bgp_commands
544+
runner = CliRunner()
545+
# mock vtysh cli output that does not have peergroup count
546+
mock_json = {
547+
"ipv6Unicast": {
548+
"routerId": "10.1.0.32",
549+
"as": 65100,
550+
"localAS": 65100,
551+
"vrfId": 0,
552+
"tableVersion": 1,
553+
"totalPeers": 0,
554+
"dynamicPeers": 0,
555+
"bestPaths": 0,
556+
"peerCount": 2,
557+
"peerMemory": 2048,
558+
"ribCount": 10,
559+
"ribMemory": 1024,
560+
"peers": {
561+
"fc00::42": {
562+
"remoteAs": 64001,
563+
"version": 4,
564+
"msgRcvd": 0,
565+
"msgSent": 0,
566+
"tableVersion": 0,
567+
"outq": 0,
568+
"inq": 0,
569+
"peerUptime": "never",
570+
"peerUptimeMsec": 0,
571+
"prefixReceivedCount": 0,
572+
"pfxRcd": 0,
573+
"state": "Active",
574+
"connectionsEstablished": 0,
575+
"connectionsDropped": 0,
576+
"idType": "ipv6"
577+
}
578+
}
579+
}
580+
}
581+
582+
with patch('utilities_common.bgp_util.run_bgp_command', return_value=json.dumps(mock_json)):
583+
result = runner.invoke(
584+
show.cli.commands["ipv6"].commands["bgp"].commands["summary"], [])
585+
# verify that the CLI handles missing peergroup count gracefully
586+
assert result.exit_code == 0
587+
assert "Peer groups 0, using 0 bytes of memory" in result.output
588+
484589
@classmethod
485590
def teardown_class(cls):
486591
print("TEARDOWN")

utilities_common/bgp_util.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,13 @@ def process_bgp_summary_json(bgp_summary, cmd_output, device, has_bgp_neighbors=
357357
'ribCount', 0) + cmd_output['ribCount']
358358
bgp_summary['ribMemory'] = bgp_summary.get(
359359
'ribMemory', 0) + cmd_output['ribMemory']
360+
# Handle the case when we have peers but no peer-groups are configured.
361+
# We still want to display peer information without just showing
362+
# Error: peerGroupCount missing in the bgp_summary
360363
bgp_summary['peerGroupCount'] = bgp_summary.get(
361-
'peerGroupCount', 0) + cmd_output['peerGroupCount']
364+
'peerGroupCount', 0) + cmd_output.get('peerGroupCount', 0)
362365
bgp_summary['peerGroupMemory'] = bgp_summary.get(
363-
'peerGroupMemory', 0) + cmd_output['peerGroupMemory']
366+
'peerGroupMemory', 0) + cmd_output.get('peerGroupMemory', 0)
364367
else:
365368
# when there are no bgp neighbors, all values are zero
366369
bgp_summary['peerCount'] = bgp_summary.get(

0 commit comments

Comments
 (0)