77import re
88import logging
99import json
10+ import functools
1011from collections import defaultdict
1112from natsort import natsorted
1213from .transceiver_utils import all_transceivers_detected
@@ -91,6 +92,11 @@ def check_interface_status(dut, asic_index, interfaces, xcvr_skip_list):
9192 @param dut: The AnsibleHost object of DUT. For interacting with DUT.
9293 @param interfaces: List of interfaces that need to be checked.
9394 """
95+ # If no interfaces to check, return True
96+ if not interfaces :
97+ logging .info ("No interfaces to check on asic %s" % asic_index )
98+ return True
99+
94100 asichost = dut .asic_instance (asic_index )
95101 namespace = asichost .get_asic_namespace ()
96102 logging .info ("Check interface status using cmd 'show interface'" )
@@ -104,9 +110,13 @@ def check_interface_status(dut, asic_index, interfaces, xcvr_skip_list):
104110 output = dut .command ("show interface description" )
105111 intf_status = parse_intf_status (output ["stdout_lines" ][2 :])
106112 if dut .is_multi_asic :
107- check_intf_presence_command = 'show interface transceiver presence -n {} {} ' .format (namespace , {} )
113+ check_intf_presence_command = 'show interface transceiver presence -n {}' .format (namespace )
108114 else :
109- check_intf_presence_command = 'show interface transceiver presence {}'
115+ check_intf_presence_command = 'show interface transceiver presence'
116+ check_inerfaces_presence_output = dut .command (check_intf_presence_command )["stdout_lines" ][2 :]
117+ check_inerfaces_presence_output = (
118+ {ports_presence .split ()[0 ]: ports_presence .split ()[1 ] for ports_presence in check_inerfaces_presence_output }
119+ )
110120 for intf in interfaces :
111121 expected_oper = "up" if intf in mg_ports else "down"
112122 expected_admin = "up" if intf in mg_ports else "down"
@@ -124,10 +134,10 @@ def check_interface_status(dut, asic_index, interfaces, xcvr_skip_list):
124134
125135 # Cross check the interface SFP presence status
126136 if intf not in xcvr_skip_list [dut .hostname ]:
127- check_presence_output = dut . command ( check_intf_presence_command . format ( intf ) )
128- presence_list = check_presence_output [ "stdout_lines" ][ 2 ]. split ( )
129- assert intf in presence_list , "Wrong interface name in the output: %s" % str ( presence_list )
130- assert 'Present' in presence_list , "Status is not expected, presence status: %s" % str (presence_list )
137+ assert intf in check_inerfaces_presence_output , "Wrong interface name in the output for: %s" % str ( intf )
138+ interface_presence = check_inerfaces_presence_output . get ( intf , '' )
139+ assert 'Present' in interface_presence , \
140+ "Status is not expected, presence status: %s" % str ({ intf : interface_presence } )
131141
132142 logging .info ("Check interface status using the interface_facts module" )
133143 intf_facts = dut .interface_facts (up_ports = mg_ports , namespace = namespace )["ansible_facts" ]
@@ -157,6 +167,11 @@ def check_all_interface_information(dut, interfaces, xcvr_skip_list):
157167
158168# This API to check the interface information per asic.
159169def check_interface_information (dut , asic_index , interfaces , xcvr_skip_list ):
170+ # If no interfaces to check, return True
171+ if not interfaces :
172+ logging .info ("No interfaces to check on asic %s" % asic_index )
173+ return True
174+
160175 if not all_transceivers_detected (dut , asic_index , interfaces , xcvr_skip_list ):
161176 logging .info ("Not all transceivers are detected on asic %s" % asic_index )
162177 return False
@@ -167,6 +182,7 @@ def check_interface_information(dut, asic_index, interfaces, xcvr_skip_list):
167182 return True
168183
169184
185+ @functools .lru_cache (maxsize = 1 )
170186def get_port_map (dut , asic_index = None ):
171187 """
172188 @summary: Get the port mapping info from the DUT
@@ -219,7 +235,7 @@ def get_physical_port_indices(duthost, logical_intfs=None):
219235 asic_subcommand = f'-n asic{ asic_index } ' if asic_index is not None else ''
220236 cmd_keys = f'sonic-db-cli { asic_subcommand } CONFIG_DB KEYS "PORT|Ethernet*"'
221237 cmd_hget = f'sonic-db-cli { asic_subcommand } CONFIG_DB HGET $key index'
222- cmd = f'for key in $({ cmd_keys } ); do echo "$key : $({ cmd_hget } )" ; done'
238+ cmd = f'for key in $({ cmd_keys } ); do echo "$key : $({ cmd_hget } )" ; done' # noqa: E702,E203
223239 cmd_out = duthost .command (cmd , _uses_shell = True )["stdout_lines" ]
224240 cmd_out_dict = {}
225241 for line in cmd_out :
@@ -285,7 +301,7 @@ def get_fec_eligible_interfaces(duthost, supported_speeds):
285301 if oper == "up" and speed in supported_speeds :
286302 interfaces .append (intf_name )
287303 else :
288- logging .info (f"Skip for { intf_name } : oper_state:{ oper } speed:{ speed } " )
304+ logging .info (f"Skip for { intf_name } : oper_state: { oper } speed: { speed } " )
289305
290306 return interfaces
291307
0 commit comments