Skip to content

Commit 0e96c99

Browse files
authored
[show]: split show interfaces commands into separate file (sonic-net#1044)
add extensive test coverage for show interfaces commands. Signed-off-by: Guohan Lu <[email protected]>
1 parent fdda5ac commit 0e96c99

11 files changed

Lines changed: 1144 additions & 640 deletions

File tree

scripts/intfstat

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#####################################################################
44
#
5-
# intfstat is a tool for summarizing l3 network statistics.
5+
# intfstat is a tool for summarizing l3 network statistics.
66
#
77
#####################################################################
88

@@ -17,7 +17,7 @@ import time
1717

1818
# mock the redis for unit test purposes #
1919
try:
20-
if os.environ["UTILITIES_UNIT_TESTING"] == "1":
20+
if os.environ["UTILITIES_UNIT_TESTING"] == "2":
2121
modules_path = os.path.join(os.path.dirname(__file__), "..")
2222
test_path = os.path.join(modules_path, "tests")
2323
sys.path.insert(0, modules_path)
@@ -34,7 +34,7 @@ from utilities_common.netstat import ns_diff, ns_brate, ns_prate, table_as_json,
3434
NStats = namedtuple("NStats", "rx_b_ok, rx_p_ok, tx_b_ok, tx_p_ok,\
3535
rx_b_err, rx_p_err, tx_b_err, tx_p_err,")
3636

37-
header = ['IFACE', 'RX_OK', 'RX_BPS', 'RX_PPS', 'RX_ERR',
37+
header = ['IFACE', 'RX_OK', 'RX_BPS', 'RX_PPS', 'RX_ERR',
3838
'TX_OK', 'TX_BPS', 'TX_PPS', 'TX_ERR']
3939

4040
counter_names = (
@@ -95,7 +95,7 @@ class Intfstat(object):
9595
if counter_rif_name_map is None:
9696
print("No %s in the DB!" % COUNTERS_RIF_NAME_MAP)
9797
sys.exit(1)
98-
98+
9999
if rif and not rif in counter_rif_name_map:
100100
print("Interface %s missing from %s! Make sure it exists" % (rif, COUNTERS_RIF_NAME_MAP))
101101
sys.exit(2)
@@ -105,7 +105,7 @@ class Intfstat(object):
105105
return cnstat_dict
106106

107107
for rif in natsorted(counter_rif_name_map):
108-
cnstat_dict[rif] = get_counters(counter_rif_name_map[rif])
108+
cnstat_dict[rif] = get_counters(counter_rif_name_map[rif])
109109
return cnstat_dict
110110

111111
def get_intf_state(self, port_name):
@@ -187,12 +187,12 @@ class Intfstat(object):
187187
print(tabulate(table, header, tablefmt='simple', stralign='right'))
188188

189189
def cnstat_single_interface(self, rif, cnstat_new_dict, cnstat_old_dict):
190-
190+
191191
header = rif + '\n' + '-'*len(rif)
192192
body = """
193193
RX:
194-
%10s packets
195-
%10s bytes
194+
%10s packets
195+
%10s bytes
196196
%10s error packets
197197
%10s error bytes
198198
TX:
@@ -202,7 +202,7 @@ class Intfstat(object):
202202
%10s error bytes"""
203203

204204
cntr = cnstat_new_dict.get(rif)
205-
205+
206206
if cnstat_old_dict:
207207
old_cntr = cnstat_old_dict.get(rif)
208208
if old_cntr:

scripts/intfutil

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import os
1414

1515
# mock the redis for unit test purposes #
1616
try:
17-
if os.environ["UTILITIES_UNIT_TESTING"] == "1":
17+
if os.environ["UTILITIES_UNIT_TESTING"] == "2":
1818
modules_path = os.path.join(os.path.dirname(__file__), "..")
1919
tests_path = os.path.join(modules_path, "tests")
2020
sys.path.insert(0, modules_path)
@@ -49,10 +49,10 @@ def db_connect_configdb():
4949
Connect to configdb
5050
"""
5151
config_db = ConfigDBConnector()
52-
if config_db is None:
53-
return None
52+
if config_db is None:
53+
return None
5454
config_db.connect()
55-
return config_db
55+
return config_db
5656

5757
def get_frontpanel_port_list(config_db):
5858
ports_dict = config_db.get_table('PORT')
@@ -96,7 +96,7 @@ def config_db_vlan_port_keys_get(int_to_vlan_dict, front_panel_ports_list, intf_
9696
if intf_name in int_to_vlan_dict.keys():
9797
vlan = int_to_vlan_dict[intf_name]
9898
if "Vlan" in vlan:
99-
vlan = "trunk"
99+
vlan = "trunk"
100100
return vlan
101101

102102

@@ -169,9 +169,9 @@ def state_db_port_optics_get(state_db, intf_name, type):
169169
"""
170170
full_table_id = PORT_TRANSCEIVER_TABLE_PREFIX + intf_name
171171
optics_type = state_db.get(state_db.STATE_DB, full_table_id, type)
172-
if optics_type is None:
173-
return "N/A"
174-
return optics_type
172+
if optics_type is None:
173+
return "N/A"
174+
return optics_type
175175

176176
def merge_dicts(x,y):
177177
# store a copy of x, but overwrite with y's values where applicable
@@ -188,7 +188,7 @@ def merge_dicts(x,y):
188188
def tuple_to_dict(tup, new_dict):
189189
"""
190190
From a tuple create a dictionary that uses the first item in the tuple as a key
191-
and the 2nd item in the tuple as a value.
191+
and the 2nd item in the tuple as a value.
192192
"""
193193
for a, b in tup:
194194
new_dict.setdefault(a, []).append(b)
@@ -216,7 +216,7 @@ def get_portchannel_list(get_raw_po_int_configdb_info):
216216
>>> portchannel_list = get_portchannel_list(get_raw_po_int_configdb_info)
217217
>>> pprint(portchannel_list)
218218
['PortChannel0001', 'PortChannel0002', 'PortChannel0003', 'PortChannel0004']
219-
>>>
219+
>>>
220220
"""
221221
portchannel_list = []
222222
for po in get_raw_po_int_configdb_info:
@@ -234,7 +234,7 @@ def create_po_int_tuple_list(get_raw_po_int_configdb_info):
234234
('PortChannel0004', 'Ethernet124'),
235235
('PortChannel0003', 'Ethernet120'),
236236
('PortChannel0001', 'Ethernet112')]
237-
>>>
237+
>>>
238238
"""
239239
po_int_tuple_list = get_raw_po_int_configdb_info.keys()
240240
return po_int_tuple_list
@@ -262,11 +262,11 @@ def create_int_to_portchannel_dict(po_int_tuple_list):
262262

263263
def po_speed_dict(po_int_dict, appl_db):
264264
"""
265-
This function takes the portchannel to interface dictionary
265+
This function takes the portchannel to interface dictionary
266266
and the appl_db and then creates a portchannel to speed
267-
dictionary.
267+
dictionary.
268268
"""
269-
if po_int_dict:
269+
if po_int_dict:
270270
po_list = []
271271
for key, value in po_int_dict.iteritems():
272272
agg_speed_list = []
@@ -311,9 +311,9 @@ def appl_db_portchannel_status_get(appl_db, config_db, po_name, status_type, por
311311
return status
312312
status = appl_db.get(appl_db.APPL_DB, full_table_id, status_type)
313313
#print(status)
314-
if status is None:
314+
if status is None:
315315
return "N/A"
316-
return status
316+
return status
317317

318318
def appl_db_sub_intf_status_get(appl_db, config_db, front_panel_ports_list, portchannel_speed_dict, sub_intf_name, status_type):
319319
sub_intf_sep_idx = sub_intf_name.find(VLAN_SUB_INTERFACE_SEPARATOR)
@@ -415,16 +415,16 @@ class IntfStatus(object):
415415
def __init__(self, intf_name):
416416
"""
417417
Class constructor method
418-
:param self:
418+
:param self:
419419
:param intf_name: string of interface
420-
:return:
420+
:return:
421421
"""
422422
self.appl_db = db_connect_appl()
423423
self.state_db = db_connect_state()
424424
self.config_db = db_connect_configdb()
425425
if self.appl_db is None:
426426
return
427-
if self.state_db is None:
427+
if self.state_db is None:
428428
return
429429
if self.config_db is None:
430430
return

scripts/sfpshow

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/python
22
"""
33
Script to show sfp eeprom and presence status.
4-
Not like sfputil this scripts get the sfp data from DB directly.
4+
Not like sfputil this scripts get the sfp data from DB directly.
55
"""
66
import sys
77
import click
@@ -15,14 +15,14 @@ from tabulate import tabulate
1515

1616
# Mock the redis for unit test purposes #
1717
try:
18-
if os.environ["UTILITIES_UNIT_TESTING"] == "1":
18+
if os.environ["UTILITIES_UNIT_TESTING"] == "2":
1919
modules_path = os.path.join(os.path.dirname(__file__), "..")
2020
test_path = os.path.join(modules_path, "tests")
2121
sys.path.insert(0, modules_path)
2222
sys.path.insert(0, test_path)
2323
import mock_tables.dbconnector
2424
except KeyError:
25-
pass
25+
pass
2626

2727
qsfp_data_map = {'model': 'Vendor PN',
2828
'vendor_oui': 'Vendor OUI',
@@ -159,7 +159,7 @@ class SFPShow(object):
159159
dom_unit_map[key])
160160
out_put = out_put + current_val + '\n'
161161
return out_put
162-
162+
163163
# Convert dom sensor info in DB to cli output string
164164
def convert_dom_to_output_string(self, sfp_type, dom_info_dict):
165165
ident = ' '
@@ -253,7 +253,7 @@ class SFPShow(object):
253253
def convert_sfp_info_to_output_string(self, sfp_info_dict):
254254
ident = ' '
255255
out_put = ''
256-
256+
257257
sorted_qsfp_data_map = sorted(qsfp_data_map.items(), key=operator.itemgetter(1))
258258
for key in sorted_qsfp_data_map:
259259
key1 = key[0]
@@ -299,7 +299,7 @@ class SFPShow(object):
299299
if presence:
300300
out_put = self.convert_interface_sfp_info_to_cli_output_string(self.sdb, interfacename, dump_dom)
301301
else:
302-
out_put = out_put + interfacename + ': ' + 'SFP EEPROM Not detected' + '\n'
302+
out_put = out_put + interfacename + ': ' + 'SFP EEPROM Not detected' + '\n'
303303
else:
304304
port_table_keys = self.adb.keys(self.adb.APPL_DB, "PORT_TABLE:*")
305305
sorted_table_keys = natsorted(port_table_keys)
@@ -310,9 +310,9 @@ class SFPShow(object):
310310
if presence:
311311
out_put = out_put + self.convert_interface_sfp_info_to_cli_output_string(self.sdb, interface, dump_dom)
312312
else:
313-
out_put = out_put + interface + ': ' + 'SFP EEPROM Not detected' + '\n'
313+
out_put = out_put + interface + ': ' + 'SFP EEPROM Not detected' + '\n'
314314

315-
out_put = out_put + '\n'
315+
out_put = out_put + '\n'
316316

317317
click.echo(out_put)
318318

@@ -336,7 +336,7 @@ class SFPShow(object):
336336
port_table.append((key,'Present'))
337337
else:
338338
port_table.append((key,'Not present'))
339-
339+
340340
sorted_port_table = natsorted(port_table)
341341
click.echo(tabulate(sorted_port_table, header))
342342

0 commit comments

Comments
 (0)