Skip to content

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

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

Merged
merged 8 commits into from
Apr 29, 2025
23 changes: 20 additions & 3 deletions tests/snmp/test_snmp_psu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -44,6 +45,7 @@ def test_snmp_psu_status(duthosts, enum_supervisor_dut_hostname, localhost, cred
localhost, host=hostip, version="v2c",
community=creds_all_duts[duthost.hostname]["snmp_rocommunity"], wait=True)['ansible_facts']

psu_keys = natsorted(redis_get_keys(duthost, 'STATE_DB', 'PSU_INFO|*'))
psus_on = 0
msg = "Unexpected operstatus results {} != {} for PSU {}"

Expand All @@ -52,11 +54,11 @@ 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()):
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'

Expand All @@ -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