Skip to content

Commit a73a89a

Browse files
modifying snmp psu test to fit format using psu keys instead of index (#11944)
1 parent fed05ec commit a73a89a

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

tests/snmp/test_snmp_psu.py

+20-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
from tests.common.helpers.assertions import pytest_assert
44
from tests.common.helpers.snmp_helpers import get_snmp_facts
5+
from natsort import natsorted
56

67
PSU_STATUS_OK = 2
78
PSU_STATUS_FUNCTIONING_FAIL = 7
@@ -58,11 +59,12 @@ def test_snmp_psu_status(duthosts, enum_supervisor_dut_hostname, localhost, cred
5859
logging.info("No snmp psu info on kvm testbed.")
5960
return
6061

61-
for psu_indx, operstatus in list(snmp_facts['snmp_psu'].items()):
62+
psu_keys = natsorted(redis_get_keys(duthost, 'STATE_DB', 'PSU_INFO|*'))
63+
for psu_indx, operstatus in snmp_facts['snmp_psu'].items():
6264
get_presence = duthost.shell(
63-
"redis-cli -n 6 hget 'PSU_INFO|PSU {}' presence".format(psu_indx))
65+
"redis-cli -n 6 hget '{}' presence".format(psu_keys[int(psu_indx)-1]))
6466
get_status = duthost.shell(
65-
"redis-cli -n 6 hget 'PSU_INFO|PSU {}' status".format(psu_indx))
67+
"redis-cli -n 6 hget '{}' status".format(psu_keys[int(psu_indx)-1]))
6668
status = get_status['stdout'] == 'true'
6769
presence = get_presence['stdout'] == 'true'
6870

@@ -79,3 +81,18 @@ def test_snmp_psu_status(duthosts, enum_supervisor_dut_hostname, localhost, cred
7981

8082
pytest_assert(
8183
psus_on >= 1, "At least one PSU should be with operstatus OK")
84+
85+
86+
def redis_get_keys(duthost, db_id, pattern):
87+
"""
88+
Get all keys for a given pattern in given redis database
89+
:param duthost: DUT host object
90+
:param db_id: ID of redis database
91+
:param pattern: Redis key pattern
92+
:return: A list of key name in string
93+
"""
94+
cmd = 'sonic-db-cli {} KEYS \"{}\"'.format(db_id, pattern)
95+
logging.debug('Getting keys from redis by command: {}'.format(cmd))
96+
output = duthost.shell(cmd)
97+
content = output['stdout'].strip()
98+
return content.split('\n') if content else None

0 commit comments

Comments
 (0)