Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[202012][Bugfix] "show vnet routes all" #2654

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions show/vnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,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():
Expand Down Expand Up @@ -374,12 +397,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))

Expand Down
4 changes: 2 additions & 2 deletions tests/mock_tables/appl_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,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",
Expand Down
2 changes: 1 addition & 1 deletion tests/mock_tables/state_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,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": {
Expand Down
45 changes: 45 additions & 0 deletions tests/show_vnet_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,57 @@
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
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()
Expand All @@ -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
Expand Down
22 changes: 2 additions & 20 deletions tests/show_vnet_vxlan_cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down