From a2520e60d471ebc4791a50d5bb9ca9691bd5c456 Mon Sep 17 00:00:00 2001 From: siqbal1986 Date: Mon, 6 Feb 2023 12:00:09 -0800 Subject: [PATCH] Fixed a bug in "show vnet routes all" causing screen overrun. (#2644) Signed-off-by: siqbal1486 --- show/vnet.py | 40 ++++++++++++++++++++++----- tests/mock_tables/appl_db.json | 4 +-- tests/mock_tables/state_db.json | 2 +- tests/show_vnet_test.py | 45 +++++++++++++++++++++++++++++++ tests/show_vnet_vxlan_cli_test.py | 22 ++------------- 5 files changed, 84 insertions(+), 29 deletions(-) diff --git a/show/vnet.py b/show/vnet.py index ba6f81ce8d..239e6d2206 100644 --- a/show/vnet.py +++ b/show/vnet.py @@ -333,6 +333,29 @@ def routes(): """Show vnet routes related information""" pass +def pretty_print(table, r, epval, mac_addr, vni, state): + endpoints = epval.split(',') + row_width = 3 + max_len = 0 + for ep in endpoints: + max_len = len(ep) if len(ep) > max_len else max_len + if max_len > 15: + row_width = 2 + iter = 0 + while iter < len(endpoints): + if iter +row_width > len(endpoints): + r.append(",".join(endpoints[iter:])) + else: + r.append(",".join(endpoints[iter:iter + row_width])) + if iter == 0: + r.append(mac_addr) + r.append(vni) + r.append(state) + else: + r.extend(["", "", ""]) + iter += row_width + table.append(r) + r = ["",""] @routes.command() def all(): @@ -373,12 +396,17 @@ def all(): state_db_key = '|'.join(k.split(":",2)) val = appl_db.get_all(appl_db.APPL_DB, k) val_state = state_db.get_all(state_db.STATE_DB, state_db_key) - r.append(val.get('endpoint')) - r.append(val.get('mac_address')) - r.append(val.get('vni')) - if val_state: - r.append(val_state.get('state')) - table.append(r) + epval = val.get('endpoint') + if len(epval) < 40: + r.append(epval) + r.append(val.get('mac_address')) + r.append(val.get('vni')) + if val_state: + r.append(val_state.get('state')) + table.append(r) + continue + state = val_state.get('state') if val_state else "" + pretty_print(table, r, epval, val.get('mac_address'), val.get('vni'), state ) click.echo(tabulate(table, header)) diff --git a/tests/mock_tables/appl_db.json b/tests/mock_tables/appl_db.json index 8554a07eaf..e330bdaddc 100644 --- a/tests/mock_tables/appl_db.json +++ b/tests/mock_tables/appl_db.json @@ -329,8 +329,8 @@ "endpoint_monitor":"100.251.7.1" }, "VNET_ROUTE_TUNNEL_TABLE:Vnet_v6_in_v6-0:fddd:a156:a251::a6:1/128": { - "endpoint": "fddd:a100:a251::a10:1,fddd:a101:a251::a10:1", - "endpoint_monitor":"fddd:a100:a251::a10:1,fddd:a101:a251::a10:1" + "endpoint": "fddd:a100:a251::a10:1,fddd:a101:a251::a10:1,fddd:a102:a251::a10:1,fddd:a103:a251::a10:1", + "endpoint_monitor":"fddd:a100:a251::a10:1,fddd:a101:a251::a10:1,fddd:a102:a251::a10:1,fddd:a103:a251::a10:1" }, "VNET_ROUTE_TUNNEL_TABLE:test_v4_in_v4-0:160.162.191.1/32": { "endpoint":"100.251.7.1", diff --git a/tests/mock_tables/state_db.json b/tests/mock_tables/state_db.json index d32836aecd..8462248a92 100644 --- a/tests/mock_tables/state_db.json +++ b/tests/mock_tables/state_db.json @@ -1086,7 +1086,7 @@ "state":"active" }, "VNET_ROUTE_TUNNEL_TABLE|Vnet_v6_in_v6-0|fddd:a156:a251::a6:1/128": { - "active_endpoints":"fddd:a100:a251::a10:1,fddd:a101:a251::a10:1", + "active_endpoints":"fddd:a100:a251::a10:1,fddd:a101:a251::a10:1,fddd:a102:a251::a10:1,fddd:a103:a251::a10:1", "state":"active" }, "BFD_SESSION_TABLE|default|default|100.251.7.1": { diff --git a/tests/show_vnet_test.py b/tests/show_vnet_test.py index 5317b9b3ff..eff75a583f 100644 --- a/tests/show_vnet_test.py +++ b/tests/show_vnet_test.py @@ -2,6 +2,7 @@ from click.testing import CliRunner from utilities_common.db import Db import show.main as show +import show.vnet as vnet class TestShowVnetRoutesAll(object): @classmethod @@ -9,6 +10,49 @@ def setup_class(cls): print("SETUP") os.environ["UTILITIES_UNIT_TESTING"] = "1" + def test_Preety_print(self): + table =[] + row = ["Vnet_v6_in_v6-0", "fddd:a156:a251::a6:1/128"] + mac_addr = "" + vni = "" + state = "active" + epval = "fddd:a100:a251::a10:1,fddd:a101:a251::a10:1" + + vnet.pretty_print(table, row, epval, mac_addr, vni, state) + expected_output = [['Vnet_v6_in_v6-0', 'fddd:a156:a251::a6:1/128', 'fddd:a100:a251::a10:1,fddd:a101:a251::a10:1', '', '', 'active']] + assert table == expected_output + + table =[] + row = ["Vnet_v6_in_v6-0", "fddd:a156:a251::a6:1/128"] + epval = "fddd:a100:a251::a10:1,fddd:a101:a251::a10:1,fddd:a100:a251::a11:1,fddd:a100:a251::a12:1,fddd:a100:a251::a13:1" + vnet.pretty_print(table, row, epval, mac_addr, vni, state) + expected_output = [ + ['Vnet_v6_in_v6-0', 'fddd:a156:a251::a6:1/128', 'fddd:a100:a251::a10:1,fddd:a101:a251::a10:1', '', '', 'active'], + ['', '', 'fddd:a100:a251::a11:1,fddd:a100:a251::a12:1', '', '', ''], + ['', '', 'fddd:a100:a251::a13:1', '', '', ''] + ] + assert table == expected_output + + table =[] + row = ["Vnet_v6_in_v6-0", "fddd:a156:a251::a6:1/128"] + epval = "192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4,192.168.1.5,192.168.1.6,192.168.1.7,192.168.1.8,192.168.1.9,192.168.1.10,192.168.1.11,192.168.1.12,192.168.1.13,192.168.1.14,192.168.1.15" + vnet.pretty_print(table, row, epval, mac_addr, vni, state) + expected_output =[ + ['Vnet_v6_in_v6-0', 'fddd:a156:a251::a6:1/128', '192.168.1.1,192.168.1.2,192.168.1.3', '', '', 'active'], + ['', '', '192.168.1.4,192.168.1.5,192.168.1.6', '', '', ''], + ['', '', '192.168.1.7,192.168.1.8,192.168.1.9', '', '', ''], + ['', '', '192.168.1.10,192.168.1.11,192.168.1.12', '', '', ''], + ['', '', '192.168.1.13,192.168.1.14,192.168.1.15', '', '', '']] + assert table == expected_output + + table =[] + row = ["Vnet_v6_in_v6-0", "fddd:a156:a251::a6:1/128"] + epval = "192.168.1.1" + vnet.pretty_print(table, row, epval, mac_addr, vni, state) + expected_output =[ + ['Vnet_v6_in_v6-0', 'fddd:a156:a251::a6:1/128', '192.168.1.1', '', '', 'active']] + assert table == expected_output + def test_show_vnet_routes_all_basic(self): runner = CliRunner() db = Db() @@ -22,6 +66,7 @@ def test_show_vnet_routes_all_basic(self): vnet name prefix endpoint mac address vni status --------------- ------------------------ ------------------------------------------- ------------- ----- -------- Vnet_v6_in_v6-0 fddd:a156:a251::a6:1/128 fddd:a100:a251::a10:1,fddd:a101:a251::a10:1 active + fddd:a102:a251::a10:1,fddd:a103:a251::a10:1 test_v4_in_v4-0 160.162.191.1/32 100.251.7.1 active test_v4_in_v4-0 160.163.191.1/32 100.251.7.1 active test_v4_in_v4-0 160.164.191.1/32 100.251.7.1 diff --git a/tests/show_vnet_vxlan_cli_test.py b/tests/show_vnet_vxlan_cli_test.py index f0cee3b257..c9aa5b6223 100644 --- a/tests/show_vnet_vxlan_cli_test.py +++ b/tests/show_vnet_vxlan_cli_test.py @@ -9,32 +9,12 @@ #test_path = os.path.dirname(os.path.abspath(__file__)) - - class TestShowVnet(object): @classmethod def setup_class(cls): print("SETUP") os.environ["UTILITIES_UNIT_TESTING"] = "1" - def test_show_vnet_routes_all_basic(self): - runner = CliRunner() - db = Db() - result = runner.invoke(show.cli.commands['vnet'].commands['routes'].commands['all'], [], obj=db) - assert result.exit_code == 0 - expected_output = """\ -vnet name prefix nexthop interface ------------ -------- --------- ----------- - -vnet name prefix endpoint mac address vni status ---------------- ------------------------ ------------------------------------------- ------------- ----- -------- -Vnet_v6_in_v6-0 fddd:a156:a251::a6:1/128 fddd:a100:a251::a10:1,fddd:a101:a251::a10:1 active -test_v4_in_v4-0 160.162.191.1/32 100.251.7.1 active -test_v4_in_v4-0 160.163.191.1/32 100.251.7.1 active -test_v4_in_v4-0 160.164.191.1/32 100.251.7.1 -""" - assert result.output == expected_output - def test_show_vnet_endpoint(self): runner = CliRunner() db = Db() @@ -45,6 +25,8 @@ def test_show_vnet_endpoint(self): --------------------- --------------------- -------------- -------- fddd:a100:a251::a10:1 fddd:a100:a251::a10:1 1 Unknown fddd:a101:a251::a10:1 fddd:a101:a251::a10:1 1 Down +fddd:a102:a251::a10:1 fddd:a102:a251::a10:1 1 Unknown +fddd:a103:a251::a10:1 fddd:a103:a251::a10:1 1 Unknown 100.251.7.1 100.251.7.1 3 Up """ assert result.output == expected_output