Skip to content

Commit c9f8790

Browse files
authored
Optimize check_interface_status function. (#21055)
Add cache to get_ports_map. Call transceiver presence once for all ports together.
1 parent 1390a58 commit c9f8790

2 files changed

Lines changed: 18 additions & 12 deletions

File tree

tests/common/platform/interface_utils.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import re
88
import logging
99
import json
10+
import functools
1011
from collections import defaultdict
1112
from natsort import natsorted
1213
from .transceiver_utils import all_transceivers_detected
@@ -104,9 +105,13 @@ def check_interface_status(dut, asic_index, interfaces, xcvr_skip_list):
104105
output = dut.command("show interface description")
105106
intf_status = parse_intf_status(output["stdout_lines"][2:])
106107
if dut.is_multi_asic:
107-
check_intf_presence_command = 'show interface transceiver presence -n {} {}'.format(namespace, {})
108+
check_intf_presence_command = 'show interface transceiver presence -n {}'.format(namespace)
108109
else:
109-
check_intf_presence_command = 'show interface transceiver presence {}'
110+
check_intf_presence_command = 'show interface transceiver presence'
111+
check_inerfaces_presence_output = dut.command(check_intf_presence_command)["stdout_lines"][2:]
112+
check_inerfaces_presence_output = (
113+
{ports_presence.split()[0]: ports_presence.split()[1] for ports_presence in check_inerfaces_presence_output}
114+
)
110115
for intf in interfaces:
111116
expected_oper = "up" if intf in mg_ports else "down"
112117
expected_admin = "up" if intf in mg_ports else "down"
@@ -124,10 +129,10 @@ def check_interface_status(dut, asic_index, interfaces, xcvr_skip_list):
124129

125130
# Cross check the interface SFP presence status
126131
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)
132+
assert intf in check_inerfaces_presence_output, "Wrong interface name in the output for: %s" % str(intf)
133+
interface_presence = check_inerfaces_presence_output.get(intf, '')
134+
assert 'Present' in interface_presence, \
135+
"Status is not expected, presence status: %s" % str({intf: interface_presence})
131136

132137
logging.info("Check interface status using the interface_facts module")
133138
intf_facts = dut.interface_facts(up_ports=mg_ports, namespace=namespace)["ansible_facts"]
@@ -167,6 +172,7 @@ def check_interface_information(dut, asic_index, interfaces, xcvr_skip_list):
167172
return True
168173

169174

175+
@functools.lru_cache(maxsize=1)
170176
def get_port_map(dut, asic_index=None):
171177
"""
172178
@summary: Get the port mapping info from the DUT
@@ -219,7 +225,7 @@ def get_physical_port_indices(duthost, logical_intfs=None):
219225
asic_subcommand = f'-n asic{asic_index}' if asic_index is not None else ''
220226
cmd_keys = f'sonic-db-cli {asic_subcommand} CONFIG_DB KEYS "PORT|Ethernet*"'
221227
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'
228+
cmd = f'for key in $({cmd_keys}); do echo "$key : $({cmd_hget})" ; done' # noqa: E702,E203
223229
cmd_out = duthost.command(cmd, _uses_shell=True)["stdout_lines"]
224230
cmd_out_dict = {}
225231
for line in cmd_out:
@@ -285,7 +291,7 @@ def get_fec_eligible_interfaces(duthost, supported_speeds):
285291
if oper == "up" and speed in supported_speeds:
286292
interfaces.append(intf_name)
287293
else:
288-
logging.info(f"Skip for {intf_name}: oper_state:{oper} speed:{speed}")
294+
logging.info(f"Skip for {intf_name}: oper_state: {oper} speed: {speed}")
289295

290296
return interfaces
291297

tests/platform_tests/test_sequential_restart.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ def restart_service_and_check(localhost, dut, enum_frontend_asic_index, service,
7676
interface_wait_time = 300
7777
if dut.facts["platform"] == "x86_64-cel_e1031-r0":
7878
interface_wait_time = 900
79-
pytest_assert(wait_until(interface_wait_time, 20, 0, check_interface_information, dut,
80-
enum_frontend_asic_index, interfaces, xcvr_skip_list),
81-
"Not all interface information are detected within {} seconds".format(interface_wait_time))
82-
79+
if interfaces:
80+
pytest_assert(wait_until(interface_wait_time, 20, 0, check_interface_information, dut,
81+
enum_frontend_asic_index, interfaces, xcvr_skip_list),
82+
"Not all interface information are detected within {} seconds".format(interface_wait_time))
8383
logging.info("Check transceiver status on asic %s" % enum_frontend_asic_index)
8484
check_transceiver_basic(dut, enum_frontend_asic_index, interfaces, xcvr_skip_list)
8585

0 commit comments

Comments
 (0)