From 7e7fe651c22aed1797c466cbd1692fffe374a999 Mon Sep 17 00:00:00 2001 From: devojha <47282568+developfast@users.noreply.github.com> Date: Thu, 22 Jun 2023 23:06:20 +0000 Subject: [PATCH 01/15] bgp util addition for no neighbors --- utilities_common/bgp_util.py | 128 +++++++++++++++++++++-------------- 1 file changed, 76 insertions(+), 52 deletions(-) diff --git a/utilities_common/bgp_util.py b/utilities_common/bgp_util.py index fd306fdcd0..eb77d13417 100644 --- a/utilities_common/bgp_util.py +++ b/utilities_common/bgp_util.py @@ -194,6 +194,7 @@ def run_bgp_command(vtysh_cmd, bgp_namespace=multi_asic.DEFAULT_NAMESPACE, vtysh return output + def run_bgp_show_command(vtysh_cmd, bgp_namespace=multi_asic.DEFAULT_NAMESPACE): output = run_bgp_command(vtysh_cmd, bgp_namespace, constants.RVTYSH_COMMAND) # handle the the alias mode in the following code @@ -214,6 +215,7 @@ def run_bgp_show_command(vtysh_cmd, bgp_namespace=multi_asic.DEFAULT_NAMESPACE): output= json.dumps(route_info) return output + def get_bgp_summary_from_all_bgp_instances(af, namespace, display): device = multi_asic_util.MultiAsic(display, namespace) @@ -227,21 +229,29 @@ def get_bgp_summary_from_all_bgp_instances(af, namespace, display): bgp_summary = {} cmd_output_json = {} + has_bgp_neighbors = False for ns in device.get_ns_list_based_on_options(): cmd_output = run_bgp_show_command(vtysh_cmd, ns) + device.current_namespace = ns try: cmd_output_json = json.loads(cmd_output) + if cmd_output_json: + has_bgp_neighbors = True except ValueError: ctx.fail("bgp summary from bgp container not in json format") - # exit cli command without printing the error message - if key not in cmd_output_json: - click.echo("No IP{} neighbor is configured".format(af)) - exit() - - device.current_namespace = ns - - process_bgp_summary_json(bgp_summary, cmd_output_json[key], device) + # no bgp neighbors found so print basic device bgp info + if key not in cmd_output_json or not has_bgp_neighbors: + vtysh_cmd = "show ip bgp json" + no_neigh_cmd_output = run_bgp_show_command(vtysh_cmd, ns) + try: + no_neigh_cmd_output_json = json.loads(no_neigh_cmd_output) + except ValueError: + ctx.fail("bgp summary from bgp container not in json format") + + out_cmd = cmd_output_json[key] if has_bgp_neighbors else no_neigh_cmd_output_json + process_bgp_summary_json(bgp_summary, out_cmd, device, has_bgp_neighbors=has_bgp_neighbors) + has_bgp_neighbors = False return bgp_summary @@ -263,7 +273,7 @@ def display_bgp_summary(bgp_summary, af): for router_info in bgp_summary['router_info']: for k in router_info: v = router_info[k] - instance = "{}: ".format(k) if k is not "" else "" + instance = "{}: ".format(k) if k != "" else "" click.echo( "{}BGP router identifier {}, local AS number {} vrf-id {}" .format( instance, v['router_id'], v['as'], v['vrf'])) @@ -287,66 +297,80 @@ def display_bgp_summary(bgp_summary, af): ctx.fail("{} missing in the bgp_summary".format(e.args[0])) -def process_bgp_summary_json(bgp_summary, cmd_output, device): +def process_bgp_summary_json(bgp_summary, cmd_output, device, has_bgp_neighbors=True): ''' This function process the frr output in json format from a bgp instance and stores the need values in the a bgp_summary ''' - static_neighbors, dynamic_neighbors = get_bgp_neighbors_dict( - device.current_namespace) + if has_bgp_neighbors: + static_neighbors, dynamic_neighbors = get_bgp_neighbors_dict( + device.current_namespace) try: # add all the router level fields - bgp_summary['peerCount'] = bgp_summary.get( - 'peerCount', 0) + cmd_output['peerCount'] - bgp_summary['peerMemory'] = bgp_summary.get( - 'peerMemory', 0) + cmd_output['peerMemory'] - bgp_summary['ribCount'] = bgp_summary.get( - 'ribCount', 0) + cmd_output['ribCount'] - bgp_summary['ribMemory'] = bgp_summary.get( - 'ribMemory', 0) + cmd_output['ribMemory'] - bgp_summary['peerGroupCount'] = bgp_summary.get( - 'peerGroupCount', 0) + cmd_output['peerGroupCount'] - bgp_summary['peerGroupMemory'] = bgp_summary.get( - 'peerGroupMemory', 0) + cmd_output['peerGroupMemory'] + if has_bgp_neighbors: + # when there are bgp neighbors, fill information from the dict + bgp_summary['peerCount'] = bgp_summary.get( + 'peerCount', 0) + cmd_output['peerCount'] + bgp_summary['peerMemory'] = bgp_summary.get( + 'peerMemory', 0) + cmd_output['peerMemory'] + bgp_summary['ribCount'] = bgp_summary.get( + 'ribCount', 0) + cmd_output['ribCount'] + bgp_summary['ribMemory'] = bgp_summary.get( + 'ribMemory', 0) + cmd_output['ribMemory'] + bgp_summary['peerGroupCount'] = bgp_summary.get( + 'peerGroupCount', 0) + cmd_output['peerGroupCount'] + bgp_summary['peerGroupMemory'] = bgp_summary.get( + 'peerGroupMemory', 0) + cmd_output['peerGroupMemory'] + else: + # when there are no bgp neighbors, all values are zero + bgp_summary['peerCount'] = 0 + bgp_summary['peerMemory'] = 0 + bgp_summary['ribCount'] = 0 + bgp_summary['ribMemory'] = 0 + bgp_summary['peerGroupCount'] = 0 + bgp_summary['peerGroupMemory'] = 0 # store instance level field is seperate dict router_info = {} router_info['router_id'] = cmd_output['routerId'] router_info['vrf'] = cmd_output['vrfId'] - router_info['as'] = cmd_output['as'] + router_info['as'] = cmd_output['as'] if has_bgp_neighbors else cmd_output['localAS'] router_info['tbl_ver'] = cmd_output['tableVersion'] bgp_summary.setdefault('router_info', []).append( {device.current_namespace: router_info}) # store all the peers in the list - for peer_ip, value in cmd_output['peers'].items(): - peers = [] - # if display option is 'frontend', internal bgp neighbors will not - # be displayed - if device.skip_display(constants.BGP_NEIGH_OBJ, peer_ip): - continue - - peers.append(peer_ip) - peers.append(value['version']) - peers.append(value['remoteAs']) - peers.append(value['msgRcvd']) - peers.append(value['msgSent']) - peers.append(value['tableVersion']) - peers.append(value['inq']) - peers.append(value['outq']) - peers.append(value['peerUptime']) - if value['state'] == 'Established': - peers.append(value['pfxRcd']) - else: - peers.append(value['state']) - - # Get the bgp neighbour name ans store it - neigh_name = get_bgp_neighbor_ip_to_name( - peer_ip, static_neighbors, dynamic_neighbors) - peers.append(neigh_name) - - bgp_summary.setdefault('peers', []).append(peers) + if has_bgp_neighbors: + for peer_ip, value in cmd_output['peers'].items(): + peers = [] + # if display option is 'frontend', internal bgp neighbors will not + # be displayed + if device.skip_display(constants.BGP_NEIGH_OBJ, peer_ip): + continue + + peers.append(peer_ip) + peers.append(value['version']) + peers.append(value['remoteAs']) + peers.append(value['msgRcvd']) + peers.append(value['msgSent']) + peers.append(value['tableVersion']) + peers.append(value['inq']) + peers.append(value['outq']) + peers.append(value['peerUptime']) + if value['state'] == 'Established': + peers.append(value['pfxRcd']) + else: + peers.append(value['state']) + + # Get the bgp neighbour name ans store it + neigh_name = get_bgp_neighbor_ip_to_name( + peer_ip, static_neighbors, dynamic_neighbors) + peers.append(neigh_name) + + bgp_summary.setdefault('peers', []).append(peers) + else: + bgp_summary['peers'] = [] except KeyError as e: ctx = click.get_current_context() ctx.fail("{} missing in the bgp_summary".format(e.args[0])) From 60ab719cafe6214029a21e3a8c816c9f620ac0c9 Mon Sep 17 00:00:00 2001 From: devojha <47282568+developfast@users.noreply.github.com> Date: Fri, 23 Jun 2023 18:12:50 +0000 Subject: [PATCH 02/15] added unit tests for no neighbor bgp util --- tests/bgp_commands_test.py | 40 +++++++++++++++++++++++------ tests/conftest.py | 9 +++---- tests/mock_tables/no_bgp_neigh.json | 30 ++++++++++++++++++++++ utilities_common/bgp_util.py | 7 ++--- 4 files changed, 69 insertions(+), 17 deletions(-) create mode 100644 tests/mock_tables/no_bgp_neigh.json diff --git a/tests/bgp_commands_test.py b/tests/bgp_commands_test.py index 67c05a2fd3..41a83c9691 100644 --- a/tests/bgp_commands_test.py +++ b/tests/bgp_commands_test.py @@ -20,7 +20,7 @@ Peer groups 4, using 256 bytes of memory -Neighbhor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName +Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName ----------- --- ----- --------- --------- -------- ----- ------ --------- -------------- -------------- 10.0.0.1 4 65200 5919 2717 0 0 0 1d21h11m 6402 ARISTA01T2 10.0.0.5 4 65200 5916 2714 0 0 0 1d21h10m 6402 ARISTA03T2 @@ -60,7 +60,7 @@ Peer groups 4, using 256 bytes of memory -Neighbhor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName +Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName ----------- --- ----- --------- --------- -------- ----- ------ --------- -------------- -------------- fc00::1a 4 65200 6665 6672 0 0 0 2d09h39m 6402 ARISTA07T2 fc00::2 4 65200 6666 7913 0 0 0 2d09h39m 6402 ARISTA01T2 @@ -98,11 +98,35 @@ """ show_error_no_v6_neighbor = """\ -No IPv6 neighbor is configured + +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 + + +Neighbor 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 + +IPv4 Unicast Summary: +BGP router identifier 10.1.0.32, local AS number 65100 vrf-id 0 +BGP table version 12811 +RIB entries 0, using 0 bytes of memory +Peers 0, using 0 KiB of memory +Peer groups 0, using 0 bytes of memory + + +Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName +----------- --- ----- --------- --------- -------- ----- ------ --------- -------------- -------------- + +Total number of neighbors 0 """ show_bgp_summary_v4_chassis = """\ @@ -337,10 +361,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 @pytest.mark.parametrize('setup_single_bgp_instance', ['show_bgp_summary_no_neigh'], indirect=['setup_single_bgp_instance']) @@ -351,7 +375,7 @@ def test_bgp_summary_no_v6_neigh( show = setup_bgp_commands runner = CliRunner() result = runner.invoke( - show.cli.commands["ip"].commands["bgp"].commands["summary"], []) + show.cli.commands["ipv6"].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_v6_neighbor diff --git a/tests/conftest.py b/tests/conftest.py index fe99ef47bd..5a58d3aebd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -170,6 +170,9 @@ 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_mocked_json = os.path.join( + test_path, 'mock_tables', 'no_bgp_neigh.json') elif request.param == 'show_run_bgp': bgp_mocked_json = os.path.join( test_path, 'mock_tables', 'show_run_bgp.txt') @@ -187,9 +190,6 @@ 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: @@ -247,9 +247,6 @@ def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVT 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)) diff --git a/tests/mock_tables/no_bgp_neigh.json b/tests/mock_tables/no_bgp_neigh.json new file mode 100644 index 0000000000..5bc28cf8e5 --- /dev/null +++ b/tests/mock_tables/no_bgp_neigh.json @@ -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 + } + ] + } +] } } \ No newline at end of file diff --git a/utilities_common/bgp_util.py b/utilities_common/bgp_util.py index eb77d13417..3a739cc298 100644 --- a/utilities_common/bgp_util.py +++ b/utilities_common/bgp_util.py @@ -229,8 +229,9 @@ def get_bgp_summary_from_all_bgp_instances(af, namespace, display): bgp_summary = {} cmd_output_json = {} - has_bgp_neighbors = False + for ns in device.get_ns_list_based_on_options(): + has_bgp_neighbors = False cmd_output = run_bgp_show_command(vtysh_cmd, ns) device.current_namespace = ns try: @@ -251,7 +252,7 @@ def get_bgp_summary_from_all_bgp_instances(af, namespace, display): out_cmd = cmd_output_json[key] if has_bgp_neighbors else no_neigh_cmd_output_json process_bgp_summary_json(bgp_summary, out_cmd, device, has_bgp_neighbors=has_bgp_neighbors) - has_bgp_neighbors = False + return bgp_summary @@ -264,7 +265,7 @@ def display_bgp_summary(bgp_summary, af): af: IPV4 or IPV6 ''' - headers = ["Neighbhor", "V", "AS", "MsgRcvd", "MsgSent", "TblVer", + headers = ["Neighbor", "V", "AS", "MsgRcvd", "MsgSent", "TblVer", "InQ", "OutQ", "Up/Down", "State/PfxRcd", "NeighborName"] try: From 196cc2996c13c0d5ca7513e4d7dafb180ad16c37 Mon Sep 17 00:00:00 2001 From: devojha <47282568+developfast@users.noreply.github.com> Date: Mon, 26 Jun 2023 20:05:11 +0000 Subject: [PATCH 03/15] neighbhor spelling mismatch --- tests/bgp_commands_test.py | 8 ++++---- utilities_common/bgp_util.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/bgp_commands_test.py b/tests/bgp_commands_test.py index 41a83c9691..fa7bc6e12b 100644 --- a/tests/bgp_commands_test.py +++ b/tests/bgp_commands_test.py @@ -20,7 +20,7 @@ Peer groups 4, using 256 bytes of memory -Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName +Neighbhor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName ----------- --- ----- --------- --------- -------- ----- ------ --------- -------------- -------------- 10.0.0.1 4 65200 5919 2717 0 0 0 1d21h11m 6402 ARISTA01T2 10.0.0.5 4 65200 5916 2714 0 0 0 1d21h10m 6402 ARISTA03T2 @@ -60,7 +60,7 @@ Peer groups 4, using 256 bytes of memory -Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName +Neighbhor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName ----------- --- ----- --------- --------- -------- ----- ------ --------- -------------- -------------- fc00::1a 4 65200 6665 6672 0 0 0 2d09h39m 6402 ARISTA07T2 fc00::2 4 65200 6666 7913 0 0 0 2d09h39m 6402 ARISTA01T2 @@ -107,7 +107,7 @@ Peer groups 0, using 0 bytes of memory -Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName +Neighbhor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName ----------- --- ----- --------- --------- -------- ----- ------ --------- -------------- -------------- Total number of neighbors 0 @@ -123,7 +123,7 @@ Peer groups 0, using 0 bytes of memory -Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName +Neighbhor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName ----------- --- ----- --------- --------- -------- ----- ------ --------- -------------- -------------- Total number of neighbors 0 diff --git a/utilities_common/bgp_util.py b/utilities_common/bgp_util.py index 3a739cc298..dae0aaf94e 100644 --- a/utilities_common/bgp_util.py +++ b/utilities_common/bgp_util.py @@ -265,7 +265,7 @@ def display_bgp_summary(bgp_summary, af): af: IPV4 or IPV6 ''' - headers = ["Neighbor", "V", "AS", "MsgRcvd", "MsgSent", "TblVer", + headers = ["Neighbhor", "V", "AS", "MsgRcvd", "MsgSent", "TblVer", "InQ", "OutQ", "Up/Down", "State/PfxRcd", "NeighborName"] try: From 74a56be8ca72fcb14fe0ae9eb3d2f3515432a632 Mon Sep 17 00:00:00 2001 From: devojha <47282568+developfast@users.noreply.github.com> Date: Mon, 26 Jun 2023 20:28:21 +0000 Subject: [PATCH 04/15] bgp_neigh_changes --- tests/mock_tables/no_bgp_neigh.json | 31 +---------------------------- 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/tests/mock_tables/no_bgp_neigh.json b/tests/mock_tables/no_bgp_neigh.json index 5bc28cf8e5..9e26dfeeb6 100644 --- a/tests/mock_tables/no_bgp_neigh.json +++ b/tests/mock_tables/no_bgp_neigh.json @@ -1,30 +1 @@ -{ -"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 - } - ] - } -] } } \ No newline at end of file +{} \ No newline at end of file From 2edaea7a0644a58ef2bd81b4b5655e87e866f7de Mon Sep 17 00:00:00 2001 From: devojha <47282568+developfast@users.noreply.github.com> Date: Mon, 26 Jun 2023 22:02:57 +0000 Subject: [PATCH 05/15] Revert back no neighbor json output --- tests/mock_tables/no_bgp_neigh.json | 31 ++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/tests/mock_tables/no_bgp_neigh.json b/tests/mock_tables/no_bgp_neigh.json index 9e26dfeeb6..5bc28cf8e5 100644 --- a/tests/mock_tables/no_bgp_neigh.json +++ b/tests/mock_tables/no_bgp_neigh.json @@ -1 +1,30 @@ -{} \ No newline at end of file +{ +"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 + } + ] + } +] } } \ No newline at end of file From f8773640dd86c2f9b3bc80e2afef31c016801103 Mon Sep 17 00:00:00 2001 From: devojha <47282568+developfast@users.noreply.github.com> Date: Tue, 27 Jun 2023 01:14:55 +0000 Subject: [PATCH 06/15] added iter for multiple bgp show return values --- tests/bgp_commands_test.py | 10 ++++---- tests/conftest.py | 34 ++++++++++++-------------- tests/mock_tables/device_bgp_info.json | 30 +++++++++++++++++++++++ tests/mock_tables/no_bgp_neigh.json | 31 +---------------------- 4 files changed, 51 insertions(+), 54 deletions(-) create mode 100644 tests/mock_tables/device_bgp_info.json diff --git a/tests/bgp_commands_test.py b/tests/bgp_commands_test.py index fa7bc6e12b..68c4cb8463 100644 --- a/tests/bgp_commands_test.py +++ b/tests/bgp_commands_test.py @@ -107,8 +107,8 @@ Peer groups 0, using 0 bytes of memory -Neighbhor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName ------------ --- ----- --------- --------- -------- ----- ------ --------- -------------- -------------- +Neighbhor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName +----------- --- ---- --------- --------- -------- ----- ------ --------- -------------- -------------- Total number of neighbors 0 """ @@ -117,14 +117,14 @@ IPv4 Unicast Summary: BGP router identifier 10.1.0.32, local AS number 65100 vrf-id 0 -BGP table version 12811 +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 ------------ --- ----- --------- --------- -------- ----- ------ --------- -------------- -------------- +Neighbhor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName +----------- --- ---- --------- --------- -------- ----- ------ --------- -------------- -------------- Total number of neighbors 0 """ diff --git a/tests/conftest.py b/tests/conftest.py index 5a58d3aebd..5c6cbc6ee4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -171,8 +171,10 @@ def setup_single_bgp_instance(request): bgp_mocked_json = os.path.join( test_path, 'mock_tables', 'ipv6_bgp_summary.json') elif request.param == 'show_bgp_summary_no_neigh': - bgp_mocked_json = os.path.join( + 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') @@ -190,9 +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(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 "" @@ -218,22 +220,12 @@ 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): - bgp_mocked_json_file = os.path.join( - test_path, 'mock_tables', bgp_mocked_json) - if os.path.isfile(bgp_mocked_json_file): - with open(bgp_mocked_json_file) 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 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_command(bgp_mocked_json)) elif request.param.startswith('ipv6_route_err'): bgp_util.run_bgp_command = mock.MagicMock( return_value=mock_run_show_ip_route_commands(request)) @@ -250,9 +242,13 @@ def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVT 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 diff --git a/tests/mock_tables/device_bgp_info.json b/tests/mock_tables/device_bgp_info.json new file mode 100644 index 0000000000..5bc28cf8e5 --- /dev/null +++ b/tests/mock_tables/device_bgp_info.json @@ -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 + } + ] + } +] } } \ No newline at end of file diff --git a/tests/mock_tables/no_bgp_neigh.json b/tests/mock_tables/no_bgp_neigh.json index 5bc28cf8e5..9e26dfeeb6 100644 --- a/tests/mock_tables/no_bgp_neigh.json +++ b/tests/mock_tables/no_bgp_neigh.json @@ -1,30 +1 @@ -{ -"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 - } - ] - } -] } } \ No newline at end of file +{} \ No newline at end of file From 530d4f46ceca7934c939c0888bbbdf5af55632de Mon Sep 17 00:00:00 2001 From: devojha <47282568+developfast@users.noreply.github.com> Date: Wed, 28 Jun 2023 02:16:45 +0000 Subject: [PATCH 07/15] reverted route code --- tests/conftest.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 5c6cbc6ee4..6326dc3e35 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -220,12 +220,22 @@ def mock_run_show_ip_route_commands(request): else: return "" + 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): + with open(bgp_mocked_json_file) 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 any([request.param == 'ip_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(bgp_mocked_json)) + 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)) @@ -234,7 +244,7 @@ def mock_run_show_ip_route_commands(request): 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': From 1b28636787f6fceb15382ec88d36a6a04bfa3d33 Mon Sep 17 00:00:00 2001 From: devojha <47282568+developfast@users.noreply.github.com> Date: Thu, 29 Jun 2023 00:36:52 +0000 Subject: [PATCH 08/15] added multi asic test for bgp no neighbor summary --- tests/bgp_commands_test.py | 28 ++++++++++++++++++ tests/conftest.py | 11 ++++++- tests/mock_tables/asic0/device_bgp_info.json | 30 ++++++++++++++++++++ tests/mock_tables/asic0/no_bgp_neigh.json | 1 + tests/mock_tables/asic1/device_bgp_info.json | 30 ++++++++++++++++++++ tests/mock_tables/asic1/no_bgp_neigh.json | 1 + tests/mock_tables/asic2/device_bgp_info.json | 30 ++++++++++++++++++++ tests/mock_tables/asic2/no_bgp_neigh.json | 1 + 8 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 tests/mock_tables/asic0/device_bgp_info.json create mode 100644 tests/mock_tables/asic0/no_bgp_neigh.json create mode 100644 tests/mock_tables/asic1/device_bgp_info.json create mode 100644 tests/mock_tables/asic1/no_bgp_neigh.json create mode 100644 tests/mock_tables/asic2/device_bgp_info.json create mode 100644 tests/mock_tables/asic2/no_bgp_neigh.json diff --git a/tests/bgp_commands_test.py b/tests/bgp_commands_test.py index 68c4cb8463..faac2ae8e9 100644 --- a/tests/bgp_commands_test.py +++ b/tests/bgp_commands_test.py @@ -379,3 +379,31 @@ def test_bgp_summary_no_v6_neigh( print("{}".format(result.output)) assert result.exit_code == 0 assert result.output == show_error_no_v6_neighbor + + @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 + + @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 diff --git a/tests/conftest.py b/tests/conftest.py index 6326dc3e35..933b6611d9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -296,6 +296,9 @@ def setup_multi_asic_bgp_instance(request): request.param.startswith('bgp_v4_neighbor') or \ request.param.startswith('bgp_v6_neighbor'): m_asic_json_file = request.param + elif request.param == 'show_bgp_summary_no_neigh': + m_asic_no_neigh_json_file = 'no_bgp_neigh.json' + m_asic_basic_device_info_json_file = 'device_bgp_info.json' else: m_asic_json_file = os.path.join( test_path, 'mock_tables', 'dummy.json') @@ -310,7 +313,8 @@ def mock_run_bgp_command_for_static(vtysh_cmd, bgp_namespace="", vtysh_shell_cmd else: return "" - def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVTYSH_COMMAND): + def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVTYSH_COMMAND, + m_asic_json_file=m_asic_json_file): if m_asic_json_file.startswith('bgp_v4_network') or \ m_asic_json_file.startswith('bgp_v6_network'): return mock_show_bgp_network_multi_asic(m_asic_json_file) @@ -331,6 +335,11 @@ def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVT _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': + functions_to_call = [mock_run_bgp_command(m_asic_json_file=m_asic_no_neigh_json_file), + mock_run_bgp_command(m_asic_json_file=m_asic_basic_device_info_json_file)] + bgp_util.run_bgp_command = mock.MagicMock( + side_effect=functions_to_call) else: bgp_util.run_bgp_command = mock_run_bgp_command diff --git a/tests/mock_tables/asic0/device_bgp_info.json b/tests/mock_tables/asic0/device_bgp_info.json new file mode 100644 index 0000000000..5bc28cf8e5 --- /dev/null +++ b/tests/mock_tables/asic0/device_bgp_info.json @@ -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 + } + ] + } +] } } \ No newline at end of file diff --git a/tests/mock_tables/asic0/no_bgp_neigh.json b/tests/mock_tables/asic0/no_bgp_neigh.json new file mode 100644 index 0000000000..9e26dfeeb6 --- /dev/null +++ b/tests/mock_tables/asic0/no_bgp_neigh.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/tests/mock_tables/asic1/device_bgp_info.json b/tests/mock_tables/asic1/device_bgp_info.json new file mode 100644 index 0000000000..5bc28cf8e5 --- /dev/null +++ b/tests/mock_tables/asic1/device_bgp_info.json @@ -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 + } + ] + } +] } } \ No newline at end of file diff --git a/tests/mock_tables/asic1/no_bgp_neigh.json b/tests/mock_tables/asic1/no_bgp_neigh.json new file mode 100644 index 0000000000..9e26dfeeb6 --- /dev/null +++ b/tests/mock_tables/asic1/no_bgp_neigh.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/tests/mock_tables/asic2/device_bgp_info.json b/tests/mock_tables/asic2/device_bgp_info.json new file mode 100644 index 0000000000..5bc28cf8e5 --- /dev/null +++ b/tests/mock_tables/asic2/device_bgp_info.json @@ -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 + } + ] + } +] } } \ No newline at end of file diff --git a/tests/mock_tables/asic2/no_bgp_neigh.json b/tests/mock_tables/asic2/no_bgp_neigh.json new file mode 100644 index 0000000000..9e26dfeeb6 --- /dev/null +++ b/tests/mock_tables/asic2/no_bgp_neigh.json @@ -0,0 +1 @@ +{} \ No newline at end of file From f6c4ef9027a3db03852e0d95cb26b2fa75f7f473 Mon Sep 17 00:00:00 2001 From: devojha <47282568+developfast@users.noreply.github.com> Date: Thu, 29 Jun 2023 00:40:43 +0000 Subject: [PATCH 09/15] local variable 'm_asic_json_file' unbounded --- tests/conftest.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 933b6611d9..ec59b10e1e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -314,17 +314,17 @@ def mock_run_bgp_command_for_static(vtysh_cmd, bgp_namespace="", vtysh_shell_cmd return "" def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVTYSH_COMMAND, - m_asic_json_file=m_asic_json_file): - if m_asic_json_file.startswith('bgp_v4_network') or \ - m_asic_json_file.startswith('bgp_v6_network'): - return mock_show_bgp_network_multi_asic(m_asic_json_file) + m_asic_input_json_file=m_asic_json_file): + if m_asic_input_json_file.startswith('bgp_v4_network') or \ + m_asic_input_json_file.startswith('bgp_v6_network'): + return mock_show_bgp_network_multi_asic(m_asic_input_json_file) - if m_asic_json_file.startswith('bgp_v4_neighbor') or \ - m_asic_json_file.startswith('bgp_v6_neighbor'): - return mock_show_bgp_neighbor_multi_asic(m_asic_json_file, bgp_namespace) + if m_asic_input_json_file.startswith('bgp_v4_neighbor') or \ + m_asic_input_json_file.startswith('bgp_v6_neighbor'): + return mock_show_bgp_neighbor_multi_asic(m_asic_input_json_file, bgp_namespace) bgp_mocked_json = os.path.join( - test_path, 'mock_tables', bgp_namespace, m_asic_json_file) + test_path, 'mock_tables', bgp_namespace, m_asic_input_json_file) if os.path.isfile(bgp_mocked_json): with open(bgp_mocked_json) as json_data: mock_frr_data = json_data.read() @@ -336,8 +336,8 @@ def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVT 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': - functions_to_call = [mock_run_bgp_command(m_asic_json_file=m_asic_no_neigh_json_file), - mock_run_bgp_command(m_asic_json_file=m_asic_basic_device_info_json_file)] + functions_to_call = [mock_run_bgp_command(m_asic_input_json_file=m_asic_no_neigh_json_file), + mock_run_bgp_command(m_asic_input_json_file=m_asic_basic_device_info_json_file)] bgp_util.run_bgp_command = mock.MagicMock( side_effect=functions_to_call) else: From 24871dc5ec6c1205987b4fa0bdf28a29f0cfffa2 Mon Sep 17 00:00:00 2001 From: devojha <47282568+developfast@users.noreply.github.com> Date: Thu, 29 Jun 2023 00:44:58 +0000 Subject: [PATCH 10/15] removed unbounded var and added new func instead --- tests/conftest.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index ec59b10e1e..ffe85abfb6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -313,18 +313,27 @@ def mock_run_bgp_command_for_static(vtysh_cmd, bgp_namespace="", vtysh_shell_cmd else: return "" - def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVTYSH_COMMAND, - m_asic_input_json_file=m_asic_json_file): - if m_asic_input_json_file.startswith('bgp_v4_network') or \ - m_asic_input_json_file.startswith('bgp_v6_network'): - return mock_show_bgp_network_multi_asic(m_asic_input_json_file) + def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVTYSH_COMMAND): + if m_asic_json_file.startswith('bgp_v4_network') or \ + m_asic_json_file.startswith('bgp_v6_network'): + return mock_show_bgp_network_multi_asic(m_asic_json_file) - if m_asic_input_json_file.startswith('bgp_v4_neighbor') or \ - m_asic_input_json_file.startswith('bgp_v6_neighbor'): - return mock_show_bgp_neighbor_multi_asic(m_asic_input_json_file, bgp_namespace) + if m_asic_json_file.startswith('bgp_v4_neighbor') or \ + m_asic_json_file.startswith('bgp_v6_neighbor'): + return mock_show_bgp_neighbor_multi_asic(m_asic_json_file, bgp_namespace) bgp_mocked_json = os.path.join( - test_path, 'mock_tables', bgp_namespace, m_asic_input_json_file) + 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 "" + + def mock_run_show_sum_bgp_command(bgp_namespace, masic_input_json_file): + bgp_mocked_json = os.path.join( + test_path, 'mock_tables', bgp_namespace, masic_input_json_file) if os.path.isfile(bgp_mocked_json): with open(bgp_mocked_json) as json_data: mock_frr_data = json_data.read() @@ -336,8 +345,8 @@ def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVT 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': - functions_to_call = [mock_run_bgp_command(m_asic_input_json_file=m_asic_no_neigh_json_file), - mock_run_bgp_command(m_asic_input_json_file=m_asic_basic_device_info_json_file)] + functions_to_call = [mock_run_show_sum_bgp_command(masic_input_json_file=m_asic_no_neigh_json_file), + mock_run_show_sum_bgp_command(masic_input_json_file=m_asic_basic_device_info_json_file)] bgp_util.run_bgp_command = mock.MagicMock( side_effect=functions_to_call) else: From 68b58eb7ec58e3466904a4951069561914f9b12c Mon Sep 17 00:00:00 2001 From: devojha <47282568+developfast@users.noreply.github.com> Date: Thu, 29 Jun 2023 01:07:42 +0000 Subject: [PATCH 11/15] added ns var --- tests/conftest.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index ffe85abfb6..b35c233a07 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -345,8 +345,12 @@ def mock_run_show_sum_bgp_command(bgp_namespace, masic_input_json_file): 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': - functions_to_call = [mock_run_show_sum_bgp_command(masic_input_json_file=m_asic_no_neigh_json_file), - mock_run_show_sum_bgp_command(masic_input_json_file=m_asic_basic_device_info_json_file)] + functions_to_call = [mock_run_show_sum_bgp_command("asic0", m_asic_no_neigh_json_file), + mock_run_show_sum_bgp_command("asic0", m_asic_basic_device_info_json_file), + mock_run_show_sum_bgp_command("asic1", m_asic_no_neigh_json_file), + mock_run_show_sum_bgp_command("asic1", m_asic_basic_device_info_json_file), + mock_run_show_sum_bgp_command("asic2", m_asic_no_neigh_json_file), + mock_run_show_sum_bgp_command("asic2", m_asic_basic_device_info_json_file)] bgp_util.run_bgp_command = mock.MagicMock( side_effect=functions_to_call) else: From 3c1f99c73e20fbd014c8c807212c32d8ac05bdfe Mon Sep 17 00:00:00 2001 From: devojha <47282568+developfast@users.noreply.github.com> Date: Thu, 29 Jun 2023 01:12:16 +0000 Subject: [PATCH 12/15] remove unnecessary instance of has_bgp_neighbors --- utilities_common/bgp_util.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/utilities_common/bgp_util.py b/utilities_common/bgp_util.py index dae0aaf94e..58e8d9106b 100644 --- a/utilities_common/bgp_util.py +++ b/utilities_common/bgp_util.py @@ -231,18 +231,17 @@ def get_bgp_summary_from_all_bgp_instances(af, namespace, display): cmd_output_json = {} for ns in device.get_ns_list_based_on_options(): - has_bgp_neighbors = False + has_bgp_neighbors = True cmd_output = run_bgp_show_command(vtysh_cmd, ns) device.current_namespace = ns try: cmd_output_json = json.loads(cmd_output) - if cmd_output_json: - has_bgp_neighbors = True except ValueError: ctx.fail("bgp summary from bgp container not in json format") # no bgp neighbors found so print basic device bgp info - if key not in cmd_output_json or not has_bgp_neighbors: + if key not in cmd_output_json: + has_bgp_neighbors = False vtysh_cmd = "show ip bgp json" no_neigh_cmd_output = run_bgp_show_command(vtysh_cmd, ns) try: From fa8e9d542e54964d558bdaf7b4c80851ad39ae3e Mon Sep 17 00:00:00 2001 From: devojha <47282568+developfast@users.noreply.github.com> Date: Thu, 29 Jun 2023 01:49:02 +0000 Subject: [PATCH 13/15] enabled multi asic feature --- tests/bgp_commands_test.py | 19 +++++++++++++++++++ tests/conftest.py | 8 ++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/tests/bgp_commands_test.py b/tests/bgp_commands_test.py index faac2ae8e9..002afe89ff 100644 --- a/tests/bgp_commands_test.py +++ b/tests/bgp_commands_test.py @@ -1,6 +1,7 @@ import os import pytest +import importlib from click.testing import CliRunner @@ -380,6 +381,16 @@ def test_bgp_summary_no_v6_neigh( assert result.exit_code == 0 assert result.output == show_error_no_v6_neighbor + +class TestShowSumBgpMultiAsic(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( @@ -407,3 +418,11 @@ def test_bgp_summary_multi_asic_no_v6_neigh( print("{}".format(result.output)) assert result.exit_code == 0 assert result.output == show_error_no_v6_neighbor + + @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 diff --git a/tests/conftest.py b/tests/conftest.py index b35c233a07..ffe85abfb6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -345,12 +345,8 @@ def mock_run_show_sum_bgp_command(bgp_namespace, masic_input_json_file): 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': - functions_to_call = [mock_run_show_sum_bgp_command("asic0", m_asic_no_neigh_json_file), - mock_run_show_sum_bgp_command("asic0", m_asic_basic_device_info_json_file), - mock_run_show_sum_bgp_command("asic1", m_asic_no_neigh_json_file), - mock_run_show_sum_bgp_command("asic1", m_asic_basic_device_info_json_file), - mock_run_show_sum_bgp_command("asic2", m_asic_no_neigh_json_file), - mock_run_show_sum_bgp_command("asic2", m_asic_basic_device_info_json_file)] + functions_to_call = [mock_run_show_sum_bgp_command(masic_input_json_file=m_asic_no_neigh_json_file), + mock_run_show_sum_bgp_command(masic_input_json_file=m_asic_basic_device_info_json_file)] bgp_util.run_bgp_command = mock.MagicMock( side_effect=functions_to_call) else: From 3fad4dc0c6e8757603a79d53df59a615e4aa452d Mon Sep 17 00:00:00 2001 From: devojha <47282568+developfast@users.noreply.github.com> Date: Thu, 29 Jun 2023 02:15:15 +0000 Subject: [PATCH 14/15] Revert "enabled multi asic feature" This reverts commit fa8e9d542e54964d558bdaf7b4c80851ad39ae3e. --- tests/bgp_commands_test.py | 19 ------------------- tests/conftest.py | 8 ++++++-- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/tests/bgp_commands_test.py b/tests/bgp_commands_test.py index 002afe89ff..faac2ae8e9 100644 --- a/tests/bgp_commands_test.py +++ b/tests/bgp_commands_test.py @@ -1,7 +1,6 @@ import os import pytest -import importlib from click.testing import CliRunner @@ -381,16 +380,6 @@ def test_bgp_summary_no_v6_neigh( assert result.exit_code == 0 assert result.output == show_error_no_v6_neighbor - -class TestShowSumBgpMultiAsic(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( @@ -418,11 +407,3 @@ def test_bgp_summary_multi_asic_no_v6_neigh( print("{}".format(result.output)) assert result.exit_code == 0 assert result.output == show_error_no_v6_neighbor - - @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 diff --git a/tests/conftest.py b/tests/conftest.py index ffe85abfb6..b35c233a07 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -345,8 +345,12 @@ def mock_run_show_sum_bgp_command(bgp_namespace, masic_input_json_file): 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': - functions_to_call = [mock_run_show_sum_bgp_command(masic_input_json_file=m_asic_no_neigh_json_file), - mock_run_show_sum_bgp_command(masic_input_json_file=m_asic_basic_device_info_json_file)] + functions_to_call = [mock_run_show_sum_bgp_command("asic0", m_asic_no_neigh_json_file), + mock_run_show_sum_bgp_command("asic0", m_asic_basic_device_info_json_file), + mock_run_show_sum_bgp_command("asic1", m_asic_no_neigh_json_file), + mock_run_show_sum_bgp_command("asic1", m_asic_basic_device_info_json_file), + mock_run_show_sum_bgp_command("asic2", m_asic_no_neigh_json_file), + mock_run_show_sum_bgp_command("asic2", m_asic_basic_device_info_json_file)] bgp_util.run_bgp_command = mock.MagicMock( side_effect=functions_to_call) else: From b726e4b164b9748ab80f14dc8d925a0f9fafe2a4 Mon Sep 17 00:00:00 2001 From: devojha <47282568+developfast@users.noreply.github.com> Date: Fri, 30 Jun 2023 01:32:52 +0000 Subject: [PATCH 15/15] added ulti-asic tests for bgp show with no nhbr --- tests/bgp_commands_test.py | 81 ++++++++++++++++++-- tests/conftest.py | 21 ++--- tests/mock_tables/asic2/device_bgp_info.json | 30 -------- tests/mock_tables/asic2/no_bgp_neigh.json | 1 - 4 files changed, 82 insertions(+), 51 deletions(-) delete mode 100644 tests/mock_tables/asic2/device_bgp_info.json delete mode 100644 tests/mock_tables/asic2/no_bgp_neigh.json diff --git a/tests/bgp_commands_test.py b/tests/bgp_commands_test.py index faac2ae8e9..837ced3bcc 100644 --- a/tests/bgp_commands_test.py +++ b/tests/bgp_commands_test.py @@ -1,6 +1,7 @@ import os import pytest +import importlib from click.testing import CliRunner @@ -97,7 +98,7 @@ Error: bgp summary from bgp container not in json format """ -show_error_no_v6_neighbor = """\ +show_error_no_v6_neighbor_single_asic = """\ IPv6 Unicast Summary: BGP router identifier 10.1.0.32, local AS number 65100 vrf-id 0 @@ -113,7 +114,7 @@ Total number of neighbors 0 """ -show_error_no_v4_neighbor = """\ +show_error_no_v4_neighbor_single_asic = """\ IPv4 Unicast Summary: BGP router identifier 10.1.0.32, local AS number 65100 vrf-id 0 @@ -123,6 +124,42 @@ 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_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 ----------- --- ---- --------- --------- -------- ----- ------ --------- -------------- -------------- @@ -241,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']) @@ -364,7 +404,7 @@ def test_bgp_summary_no_v4_neigh( 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_single_asic @pytest.mark.parametrize('setup_single_bgp_instance', ['show_bgp_summary_no_neigh'], indirect=['setup_single_bgp_instance']) @@ -378,7 +418,26 @@ def test_bgp_summary_no_v6_neigh( 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 + 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']) @@ -392,7 +451,7 @@ def test_bgp_summary_multi_asic_no_v4_neigh( 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']) @@ -406,4 +465,12 @@ def test_bgp_summary_multi_asic_no_v6_neigh( 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 + 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 diff --git a/tests/conftest.py b/tests/conftest.py index b35c233a07..bfcae47179 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -296,9 +296,6 @@ def setup_multi_asic_bgp_instance(request): request.param.startswith('bgp_v4_neighbor') or \ request.param.startswith('bgp_v6_neighbor'): m_asic_json_file = request.param - elif request.param == 'show_bgp_summary_no_neigh': - m_asic_no_neigh_json_file = 'no_bgp_neigh.json' - m_asic_basic_device_info_json_file = 'device_bgp_info.json' else: m_asic_json_file = os.path.join( test_path, 'mock_tables', 'dummy.json') @@ -331,9 +328,14 @@ def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVT else: return "" - def mock_run_show_sum_bgp_command(bgp_namespace, masic_input_json_file): + 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, masic_input_json_file) + 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() @@ -345,14 +347,7 @@ def mock_run_show_sum_bgp_command(bgp_namespace, masic_input_json_file): 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': - functions_to_call = [mock_run_show_sum_bgp_command("asic0", m_asic_no_neigh_json_file), - mock_run_show_sum_bgp_command("asic0", m_asic_basic_device_info_json_file), - mock_run_show_sum_bgp_command("asic1", m_asic_no_neigh_json_file), - mock_run_show_sum_bgp_command("asic1", m_asic_basic_device_info_json_file), - mock_run_show_sum_bgp_command("asic2", m_asic_no_neigh_json_file), - mock_run_show_sum_bgp_command("asic2", m_asic_basic_device_info_json_file)] - bgp_util.run_bgp_command = mock.MagicMock( - side_effect=functions_to_call) + bgp_util.run_bgp_command = mock_run_show_sum_bgp_command else: bgp_util.run_bgp_command = mock_run_bgp_command diff --git a/tests/mock_tables/asic2/device_bgp_info.json b/tests/mock_tables/asic2/device_bgp_info.json deleted file mode 100644 index 5bc28cf8e5..0000000000 --- a/tests/mock_tables/asic2/device_bgp_info.json +++ /dev/null @@ -1,30 +0,0 @@ -{ -"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 - } - ] - } -] } } \ No newline at end of file diff --git a/tests/mock_tables/asic2/no_bgp_neigh.json b/tests/mock_tables/asic2/no_bgp_neigh.json deleted file mode 100644 index 9e26dfeeb6..0000000000 --- a/tests/mock_tables/asic2/no_bgp_neigh.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file