diff --git a/tests/common/helpers/drop_counters/drop_counters.py b/tests/common/helpers/drop_counters/drop_counters.py index b19dd9b9a1..40a82d69bd 100644 --- a/tests/common/helpers/drop_counters/drop_counters.py +++ b/tests/common/helpers/drop_counters/drop_counters.py @@ -23,7 +23,7 @@ COMBINED_ACL_DROP_COUNTER = False -def get_pkt_drops(duthost, cli_cmd, asic_index): +def get_pkt_drops(duthost, cli_cmd, asic_index=None): """ @summary: Parse output of "portstat" or "intfstat" commands and convert it to the dictionary. @param module: The AnsibleModule object @@ -31,32 +31,38 @@ def get_pkt_drops(duthost, cli_cmd, asic_index): @return: Return dictionary of parsed counters """ # Get namespace from asic_index. - namespace = duthost.get_namespace_from_asic_id(asic_index) - - # Frame the correct cli command - # the L2 commands need _SUFFIX and L3 commands need _PREFIX - if cli_cmd == GET_L3_COUNTERS: - CMD_PREFIX = NAMESPACE_PREFIX if (namespace is not None and duthost.is_multi_asic) else '' - cli_cmd = CMD_PREFIX + cli_cmd - elif cli_cmd == GET_L2_COUNTERS: - CMD_SUFFIX = NAMESPACE_SUFFIX if (namespace is not None and duthost.is_multi_asic) else '' - cli_cmd = cli_cmd + CMD_SUFFIX - - stdout = duthost.command(cli_cmd.format(namespace)) - stdout = stdout["stdout"] - match = re.search("Last cached time was.*\n", stdout) - if match: - stdout = re.sub("Last cached time was.*\n", "", stdout) - - try: - return json.loads(stdout) - except Exception as err: - raise Exception("Failed to parse output of '{}', err={}".format(cli_cmd, str(err))) - - -def ensure_no_l3_drops(duthost, asic_index, packets_count): + result = {} + for asic_id in duthost.get_asic_ids(): + if asic_index is not None and asic_index != asic_id: + continue + namespace = duthost.get_namespace_from_asic_id(asic_id) + + # Frame the correct cli command + # the L2 commands need _SUFFIX and L3 commands need _PREFIX + if cli_cmd == GET_L3_COUNTERS: + CMD_PREFIX = NAMESPACE_PREFIX if (namespace is not None and duthost.is_multi_asic) else '' + cli_cmd = CMD_PREFIX + cli_cmd + elif cli_cmd == GET_L2_COUNTERS: + CMD_SUFFIX = NAMESPACE_SUFFIX if (namespace is not None and duthost.is_multi_asic) else '' + cli_cmd = cli_cmd + CMD_SUFFIX + + stdout = duthost.command(cli_cmd.format(namespace)) + stdout = stdout["stdout"] + match = re.search("Last cached time was.*\n", stdout) + if match: + stdout = re.sub("Last cached time was.*\n", "", stdout) + + try: + namespace_result = json.loads(stdout) + result.update(namespace_result) + except Exception as err: + raise Exception("Failed to parse output of '{}', err={}".format(cli_cmd, str(err))) + return result + + +def ensure_no_l3_drops(duthost, packets_count): """ Verify L3 drop counters were not incremented """ - intf_l3_counters = get_pkt_drops(duthost, GET_L3_COUNTERS, asic_index) + intf_l3_counters = get_pkt_drops(duthost, GET_L3_COUNTERS) unexpected_drops = {} for iface, value in list(intf_l3_counters.items()): try: @@ -71,9 +77,9 @@ def ensure_no_l3_drops(duthost, asic_index, packets_count): pytest.fail("L3 'RX_ERR' was incremented for the following interfaces:\n{}".format(unexpected_drops)) -def ensure_no_l2_drops(duthost, asic_index, packets_count): +def ensure_no_l2_drops(duthost, packets_count): """ Verify L2 drop counters were not incremented """ - intf_l2_counters = get_pkt_drops(duthost, GET_L2_COUNTERS, asic_index) + intf_l2_counters = get_pkt_drops(duthost, GET_L2_COUNTERS) unexpected_drops = {} for iface, value in list(intf_l2_counters.items()): try: @@ -92,7 +98,7 @@ def verify_drop_counters(duthosts, asic_index, dut_iface, get_cnt_cli_cmd, colum def _get_drops_across_all_duthosts(): drop_list = [] for duthost in duthosts.frontend_nodes: - pkt_drops = get_pkt_drops(duthost, get_cnt_cli_cmd, asic_index) + pkt_drops = get_pkt_drops(duthost, get_cnt_cli_cmd) # we cannot assume the iface name will be same on all the devices for SONiC chassis # if the dut_iface is not found ignore this device if dut_iface not in pkt_drops: diff --git a/tests/drop_packets/test_drop_counters.py b/tests/drop_packets/test_drop_counters.py index bab671071c..d302b75149 100755 --- a/tests/drop_packets/test_drop_counters.py +++ b/tests/drop_packets/test_drop_counters.py @@ -119,13 +119,13 @@ def base_verification(discard_group, pkt, ptfadapter, duthosts, asic_index, port verify_drop_counters(duthosts, asic_index, ports_info["dut_iface"], GET_L2_COUNTERS, L2_COL_KEY, packets_count=PKT_NUMBER) for duthost in duthosts.frontend_nodes: - ensure_no_l3_drops(duthost, asic_index, packets_count=PKT_NUMBER) + ensure_no_l3_drops(duthost, packets_count=PKT_NUMBER) elif discard_group == "L3": if COMBINED_L2L3_DROP_COUNTER: verify_drop_counters(duthosts, asic_index, ports_info["dut_iface"], GET_L2_COUNTERS, L2_COL_KEY, packets_count=PKT_NUMBER) for duthost in duthosts.frontend_nodes: - ensure_no_l3_drops(duthost, asic_index, packets_count=PKT_NUMBER) + ensure_no_l3_drops(duthost, packets_count=PKT_NUMBER) else: if not tx_dut_ports: pytest.fail("No L3 interface specified") @@ -133,7 +133,7 @@ def base_verification(discard_group, pkt, ptfadapter, duthosts, asic_index, port verify_drop_counters(duthosts, asic_index, tx_dut_ports[ports_info["dut_iface"]], GET_L3_COUNTERS, L3_COL_KEY, packets_count=PKT_NUMBER) for duthost in duthosts.frontend_nodes: - ensure_no_l2_drops(duthost, asic_index, packets_count=PKT_NUMBER) + ensure_no_l2_drops(duthost, packets_count=PKT_NUMBER) elif discard_group == "ACL": if not tx_dut_ports: pytest.fail("No L3 interface specified") @@ -154,12 +154,12 @@ def base_verification(discard_group, pkt, ptfadapter, duthosts, asic_index, port pytest.fail(fail_msg) if not COMBINED_ACL_DROP_COUNTER: for duthost in duthosts.frontend_nodes: - ensure_no_l3_drops(duthost, asic_index, packets_count=PKT_NUMBER) - ensure_no_l2_drops(duthost, asic_index, packets_count=PKT_NUMBER) + ensure_no_l3_drops(duthost, packets_count=PKT_NUMBER) + ensure_no_l2_drops(duthost, packets_count=PKT_NUMBER) elif discard_group == "NO_DROPS": for duthost in duthosts.frontend_nodes: - ensure_no_l2_drops(duthost, asic_index, packets_count=PKT_NUMBER) - ensure_no_l3_drops(duthost, asic_index, packets_count=PKT_NUMBER) + ensure_no_l2_drops(duthost, packets_count=PKT_NUMBER) + ensure_no_l3_drops(duthost, packets_count=PKT_NUMBER) else: pytest.fail("Incorrect 'discard_group' specified. Supported values: 'L2', 'L3', 'ACL' or 'NO_DROPS'")