Skip to content

Commit

Permalink
Make 'show ip bgp summary' work even when we don't have any peer grou…
Browse files Browse the repository at this point in the history
…ps configured.
  • Loading branch information
kalash-nexthop committed Jan 28, 2025
1 parent 97c20cc commit 21f16bf
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 2 deletions.
107 changes: 107 additions & 0 deletions tests/bgp_commands_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os

import pytest
Expand Down Expand Up @@ -481,6 +482,112 @@ def test_bgp_summary_no_v6_neigh(
assert result.exit_code == 0
assert result.output == show_error_no_v6_neighbor_single_asic

@pytest.mark.parametrize('setup_single_bgp_instance',
['v4'],
indirect=['setup_single_bgp_instance'])
def test_bgp_summary_raw_missing_peergroup_count(
self,
setup_bgp_commands,
setup_single_bgp_instance):
show = setup_bgp_commands
runner = CliRunner()
# mock vtysh cli output that does not have peergroup count
mock_json = {
"ipv4Unicast": {
"routerId": "10.1.0.32",
"as": 65100,
"localAS": 65100,
"vrfId": 0,
"tableVersion": 1,
"totalPeers": 0,
"dynamicPeers": 0,
"bestPaths": 0,
"peerCount": 2,
"peerMemory": 2048,
"ribCount": 10,
"ribMemory": 1024,
"peers":{
"10.0.0.33":{
"remoteAs":64001,
"version":4,
"msgRcvd":0,
"msgSent":0,
"tableVersion":0,
"outq":0,
"inq":0,
"peerUptime":"never",
"peerUptimeMsec":0,
"prefixReceivedCount":0,
"pfxRcd":0,
"state":"Active",
"connectionsEstablished":0,
"connectionsDropped":0,
"idType":"ipv4"
}
}
}
}

with patch('utilities_common.bgp_util.run_bgp_command', return_value=json.dumps(mock_json)):
result = runner.invoke(
show.cli.commands["ip"].commands["bgp"].commands["summary"], [])
# verify that the CLI handles missing peergroup count gracefully
assert result.exit_code == 0
assert "Peer groups 0, using 0 bytes of memory" in result.output

@pytest.mark.parametrize('setup_single_bgp_instance',
['v6'],
indirect=['setup_single_bgp_instance'])
def test_bgp_summary_raw_missing_peergroup_count_v6(
self,
setup_bgp_commands,
setup_single_bgp_instance):
show = setup_bgp_commands
runner = CliRunner()
# mock vtysh cli output that does not have peergroup count
mock_json = {
"ipv6Unicast": {
"routerId": "10.1.0.32",
"as": 65100,
"localAS": 65100,
"vrfId": 0,
"tableVersion": 1,
"totalPeers": 0,
"dynamicPeers": 0,
"bestPaths": 0,
"peerCount": 2,
"peerMemory": 2048,
"ribCount": 10,
"ribMemory": 1024,
"peers":{
"fc00::42":{
"remoteAs":64001,
"version":4,
"msgRcvd":0,
"msgSent":0,
"tableVersion":0,
"outq":0,
"inq":0,
"peerUptime":"never",
"peerUptimeMsec":0,
"prefixReceivedCount":0,
"pfxRcd":0,
"state":"Active",
"connectionsEstablished":0,
"connectionsDropped":0,
"idType":"ipv6"
}
}
}
}

with patch('utilities_common.bgp_util.run_bgp_command', return_value=json.dumps(mock_json)):
result = runner.invoke(
show.cli.commands["ipv6"].commands["bgp"].commands["summary"], [])
# verify that the CLI handles missing peergroup count gracefully
assert result.exit_code == 0
assert "Peer groups 0, using 0 bytes of memory" in result.output

@classmethod
def teardown_class(cls):
print("TEARDOWN")
Expand Down
7 changes: 5 additions & 2 deletions utilities_common/bgp_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,13 @@ def process_bgp_summary_json(bgp_summary, cmd_output, device, has_bgp_neighbors=
'ribCount', 0) + cmd_output['ribCount']
bgp_summary['ribMemory'] = bgp_summary.get(
'ribMemory', 0) + cmd_output['ribMemory']
# Handle the case when we have peers but no peer-groups are configured.
# We still want to display peer information without just showing
# Error: peerGroupCount missing in the bgp_summary
bgp_summary['peerGroupCount'] = bgp_summary.get(
'peerGroupCount', 0) + cmd_output['peerGroupCount']
'peerGroupCount', 0) + cmd_output.get('peerGroupCount', 0)
bgp_summary['peerGroupMemory'] = bgp_summary.get(
'peerGroupMemory', 0) + cmd_output['peerGroupMemory']
'peerGroupMemory', 0) + cmd_output.get('peerGroupMemory', 0)
else:
# when there are no bgp neighbors, all values are zero
bgp_summary['peerCount'] = bgp_summary.get(
Expand Down

0 comments on commit 21f16bf

Please sign in to comment.