Skip to content

Commit f3262f7

Browse files
sanjair-gitgshemesh2
authored andcommitted
[GITPR_TBD] Fix for parse_show_copp_configuration for multi-asic dut (sonic-net#20946)
Revert is_trap_uninstalled method deletion Signed-off-by: Guy Shemesh <[email protected]>
1 parent dfeb438 commit f3262f7

2 files changed

Lines changed: 27 additions & 13 deletions

File tree

tests/copp/copp_utils.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -492,16 +492,18 @@ def get_copp_trap_capabilities(duthost):
492492
return trap_ids.split(",")
493493

494494

495-
def parse_show_copp_configuration(duthost):
495+
def parse_show_copp_configuration(duthost, namespace):
496496
"""
497497
Parses the output of the `show copp configuration` command into a structured dictionary.
498498
Args:
499499
duthost (SonicHost): The target device.
500500
Returns:
501501
dict: A dictionary mapping trap IDs to their configuration details.
502502
"""
503-
504-
copp_config_output = duthost.shell("show copp configuration")["stdout"]
503+
command = "show copp configuration"
504+
if namespace is not None:
505+
command = namespace.ns_arg + ' ' + command
506+
copp_config_output = duthost.shell(command)["stdout"]
505507
copp_config_lines = copp_config_output.splitlines()
506508

507509
# Parse the command output into a structured format
@@ -523,7 +525,7 @@ def parse_show_copp_configuration(duthost):
523525
return copp_config_data
524526

525527

526-
def is_trap_installed(duthost, trap_id):
528+
def is_trap_installed(duthost, trap_id, namespace=None):
527529
"""
528530
Checks if a specific trap is installed by parsing the output of `show copp configuration`.
529531
Args:
@@ -533,7 +535,7 @@ def is_trap_installed(duthost, trap_id):
533535
bool: True if the trap is installed, False otherwise.
534536
"""
535537

536-
output = parse_show_copp_configuration(duthost)
538+
output = parse_show_copp_configuration(duthost, namespace)
537539
assert trap_id in output, f"Trap {trap_id} not found in the configuration"
538540
assert "hw_status" in output[trap_id], f"hw_status not found for trap {trap_id}"
539541

@@ -552,22 +554,27 @@ def is_trap_uninstalled(duthost, trap_id):
552554
return not is_trap_installed(duthost, trap_id)
553555

554556

555-
def get_trap_hw_status(duthost):
557+
def get_trap_hw_status(duthost, namespace):
556558
"""
557559
Retrieves the hw_status for traps from the STATE_DB.
558560
Args:
559561
dut (SonicHost): The target device
560562
Returns:
561563
dict: A dictionary mapping trap IDs to their hw_status.
562564
"""
563-
564-
state_db_data = duthost.shell("sonic-db-cli STATE_DB KEYS 'COPP_TRAP_TABLE|*'")["stdout"]
565+
if namespace is None:
566+
state_db_cmd = "sonic-db-cli STATE_DB KEYS 'COPP_TRAP_TABLE|*'"
567+
trap_data_cmd = "sonic-db-cli STATE_DB HGETALL "
568+
else:
569+
state_db_cmd = "sonic-db-cli -n {} STATE_DB KEYS 'COPP_TRAP_TABLE|*'".format(namespace.namespace)
570+
trap_data_cmd = "sonic-db-cli -n {} STATE_DB HGETALL ".format(namespace.namespace)
571+
state_db_data = duthost.shell(state_db_cmd)["stdout"]
565572
state_db_data = state_db_data.splitlines()
566573
hw_status = {}
567574

568575
for key in state_db_data:
569576
trap_id = key.split("|")[-1]
570-
trap_data = duthost.shell(f"sonic-db-cli STATE_DB HGETALL '{key}'")["stdout"]
577+
trap_data = duthost.shell(trap_data_cmd + f"'{key}'")["stdout"]
571578
trap_data_dict = ast.literal_eval(trap_data)
572579
hw_status[trap_id] = trap_data_dict.get("hw_status", "not-installed")
573580

tests/copp/test_copp.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from tests.common.utilities import skip_release
3636
from tests.common.utilities import wait_until
3737
from tests.common.helpers.assertions import pytest_assert
38+
from tests.common.helpers.constants import DEFAULT_NAMESPACE
3839
from tests.common.utilities import find_duthost_on_role
3940
from tests.common.utilities import get_upstream_neigh_type
4041

@@ -119,17 +120,20 @@ def test_policer(self, protocol, duthosts, enum_rand_one_per_hwsku_frontend_host
119120
pytest.skip("Skip UDLD test for Arista-7060x6 fanout without UDLD forward support")
120121

121122
duthost = duthosts[enum_rand_one_per_hwsku_frontend_hostname]
123+
namespace = DEFAULT_NAMESPACE
124+
if duthost.is_multi_asic:
125+
namespace = random.choice(duthost.asics)
122126

123127
# Skip the check if the protocol is "Default"
124128
if protocol != "Default":
125129
trap_ids = PROTOCOL_TO_TRAP_ID.get(protocol)
126130
is_always_enabled, feature_name = copp_utils.get_feature_name_from_trap_id(duthost, trap_ids[0])
127131
if is_always_enabled:
128-
pytest_assert(copp_utils.is_trap_installed(duthost, trap_ids[0]),
132+
pytest_assert(copp_utils.is_trap_installed(duthost, trap_ids[0], namespace),
129133
f"Trap {trap_ids[0]} for protocol {protocol} is not installed")
130134
else:
131135
feature_list, _ = duthost.get_feature_status()
132-
trap_installed = copp_utils.is_trap_installed(duthost, trap_ids[0])
136+
trap_installed = copp_utils.is_trap_installed(duthost, trap_ids[0], namespace)
133137
if feature_name in feature_list and feature_list[feature_name] == "enabled":
134138
pytest_assert(trap_installed,
135139
f"Trap {trap_ids[0]} for protocol {protocol} is not installed")
@@ -303,10 +307,13 @@ def test_verify_copp_configuration_cli(duthosts, enum_rand_one_per_hwsku_fronten
303307
"""
304308

305309
duthost = duthosts[enum_rand_one_per_hwsku_frontend_hostname]
310+
namespace = DEFAULT_NAMESPACE
311+
if duthost.is_multi_asic:
312+
namespace = random.choice(duthost.asics)
306313

307314
trap, trap_group, copp_group_cfg = copp_utils.get_random_copp_trap_config(duthost)
308-
hw_status = copp_utils.get_trap_hw_status(duthost)
309-
show_copp_config = copp_utils.parse_show_copp_configuration(duthost)
315+
hw_status = copp_utils.get_trap_hw_status(duthost, namespace)
316+
show_copp_config = copp_utils.parse_show_copp_configuration(duthost, namespace)
310317

311318
pytest_assert(trap in show_copp_config,
312319
f"Trap {trap} not found in show copp configuration output")

0 commit comments

Comments
 (0)