diff --git a/tests/snmp/test_snmp_psu.py b/tests/snmp/test_snmp_psu.py index 05d2ce4942e..8518f218021 100644 --- a/tests/snmp/test_snmp_psu.py +++ b/tests/snmp/test_snmp_psu.py @@ -2,6 +2,7 @@ import logging from tests.common.helpers.assertions import pytest_assert from tests.common.helpers.snmp_helpers import get_snmp_facts +from natsort import natsorted PSU_STATUS_OK = 2 PSU_STATUS_FUNCTIONING_FAIL = 7 @@ -52,11 +53,12 @@ def test_snmp_psu_status(duthosts, enum_supervisor_dut_hostname, localhost, cred logging.info("No snmp psu info on kvm testbed.") return - for psu_indx, operstatus in list(snmp_facts['snmp_psu'].items()): + psu_keys = natsorted(redis_get_keys(duthost, 'STATE_DB', 'PSU_INFO|*')) + for psu_indx, operstatus in snmp_facts['snmp_psu'].items(): get_presence = duthost.shell( - "redis-cli -n 6 hget 'PSU_INFO|PSU {}' presence".format(psu_indx)) + "redis-cli -n 6 hget '{}' presence".format(psu_keys[int(psu_indx)-1])) get_status = duthost.shell( - "redis-cli -n 6 hget 'PSU_INFO|PSU {}' status".format(psu_indx)) + "redis-cli -n 6 hget '{}' status".format(psu_keys[int(psu_indx)-1])) status = get_status['stdout'] == 'true' presence = get_presence['stdout'] == 'true' @@ -73,3 +75,18 @@ def test_snmp_psu_status(duthosts, enum_supervisor_dut_hostname, localhost, cred pytest_assert( psus_on >= 1, "At least one PSU should be with operstatus OK") + + +def redis_get_keys(duthost, db_id, pattern): + """ + Get all keys for a given pattern in given redis database + :param duthost: DUT host object + :param db_id: ID of redis database + :param pattern: Redis key pattern + :return: A list of key name in string + """ + cmd = 'sonic-db-cli {} KEYS \"{}\"'.format(db_id, pattern) + logging.debug('Getting keys from redis by command: {}'.format(cmd)) + output = duthost.shell(cmd) + content = output['stdout'].strip() + return content.split('\n') if content else None