Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modifying snmp psu test to fit format using psu keys instead of index #11944

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
23 changes: 20 additions & 3 deletions tests/snmp/test_snmp_psu.py
Original file line number Diff line number Diff line change
@@ -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
Loading