77import re
88import logging
99import json
10+ import functools
1011from collections import defaultdict
1112from natsort import natsorted
1213from .transceiver_utils import all_transceivers_detected
@@ -101,9 +102,13 @@ def check_interface_status(dut, asic_index, interfaces, xcvr_skip_list):
101102 output = dut .command ("show interface description" )
102103 intf_status = parse_intf_status (output ["stdout_lines" ][2 :])
103104 if dut .is_multi_asic :
104- check_intf_presence_command = 'show interface transceiver presence -n {} {} ' .format (namespace , {} )
105+ check_intf_presence_command = 'show interface transceiver presence -n {}' .format (namespace )
105106 else :
106- check_intf_presence_command = 'show interface transceiver presence {}'
107+ check_intf_presence_command = 'show interface transceiver presence'
108+ check_inerfaces_presence_output = dut .command (check_intf_presence_command )["stdout_lines" ][2 :]
109+ check_inerfaces_presence_output = (
110+ {ports_presence .split ()[0 ]: ports_presence .split ()[1 ] for ports_presence in check_inerfaces_presence_output }
111+ )
107112 for intf in interfaces :
108113 expected_oper = "up" if intf in mg_ports else "down"
109114 expected_admin = "up" if intf in mg_ports else "down"
@@ -121,10 +126,10 @@ def check_interface_status(dut, asic_index, interfaces, xcvr_skip_list):
121126
122127 # Cross check the interface SFP presence status
123128 if intf not in xcvr_skip_list [dut .hostname ]:
124- check_presence_output = dut . command ( check_intf_presence_command . format ( intf ) )
125- presence_list = check_presence_output [ "stdout_lines" ][ 2 ]. split ( )
126- assert intf in presence_list , "Wrong interface name in the output: %s" % str ( presence_list )
127- assert 'Present' in presence_list , "Status is not expected, presence status: %s" % str (presence_list )
129+ assert intf in check_inerfaces_presence_output , "Wrong interface name in the output for: %s" % str ( intf )
130+ interface_presence = check_inerfaces_presence_output . get ( intf , '' )
131+ assert 'Present' in interface_presence , \
132+ "Status is not expected, presence status: %s" % str ({ intf : interface_presence } )
128133
129134 logging .info ("Check interface status using the interface_facts module" )
130135 intf_facts = dut .interface_facts (up_ports = mg_ports , namespace = namespace )["ansible_facts" ]
@@ -164,6 +169,7 @@ def check_interface_information(dut, asic_index, interfaces, xcvr_skip_list):
164169 return True
165170
166171
172+ @functools .lru_cache (maxsize = 1 )
167173def get_port_map (dut , asic_index = None ):
168174 """
169175 @summary: Get the port mapping info from the DUT
@@ -216,7 +222,7 @@ def get_physical_port_indices(duthost, logical_intfs=None):
216222 asic_subcommand = f'-n asic{ asic_index } ' if asic_index is not None else ''
217223 cmd_keys = f'sonic-db-cli { asic_subcommand } CONFIG_DB KEYS "PORT|Ethernet*"'
218224 cmd_hget = f'sonic-db-cli { asic_subcommand } CONFIG_DB HGET $key index'
219- cmd = f'for key in $({ cmd_keys } ); do echo "$key : $({ cmd_hget } )" ; done'
225+ cmd = f'for key in $({ cmd_keys } ); do echo "$key : $({ cmd_hget } )" ; done' # noqa: E702,E203
220226 cmd_out = duthost .command (cmd , _uses_shell = True )["stdout_lines" ]
221227 cmd_out_dict = {}
222228 for line in cmd_out :
@@ -282,7 +288,7 @@ def get_fec_eligible_interfaces(duthost, supported_speeds):
282288 if oper == "up" and speed in supported_speeds :
283289 interfaces .append (intf_name )
284290 else :
285- logging .info (f"Skip for { intf_name } : oper_state:{ oper } speed:{ speed } " )
291+ logging .info (f"Skip for { intf_name } : oper_state: { oper } speed: { speed } " )
286292
287293 return interfaces
288294
0 commit comments