2
2
import logging
3
3
from tests .common .helpers .assertions import pytest_assert
4
4
from tests .common .helpers .snmp_helpers import get_snmp_facts
5
+ from natsort import natsorted
5
6
6
7
PSU_STATUS_OK = 2
7
8
PSU_STATUS_FUNCTIONING_FAIL = 7
@@ -58,11 +59,12 @@ def test_snmp_psu_status(duthosts, enum_supervisor_dut_hostname, localhost, cred
58
59
logging .info ("No snmp psu info on kvm testbed." )
59
60
return
60
61
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 ():
62
64
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 ] ))
64
66
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 ] ))
66
68
status = get_status ['stdout' ] == 'true'
67
69
presence = get_presence ['stdout' ] == 'true'
68
70
@@ -79,3 +81,18 @@ def test_snmp_psu_status(duthosts, enum_supervisor_dut_hostname, localhost, cred
79
81
80
82
pytest_assert (
81
83
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