Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ansible/group_vars/pdu/pdu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# snmp community strings
snmp_rocommunity: public
snmp_rwcommunity: private
8 changes: 4 additions & 4 deletions tests/common/plugins/psu_controller/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest


def psu_controller_factory(controller_ip, controller_protocol, dut_hostname):
def psu_controller_factory(controller_ip, controller_protocol, dut_hostname, pdu):
"""
@summary: Factory function for creating PSU controller according to different management protocol.
@param controller_ip: IP address of the PSU controller host.
Expand All @@ -13,11 +13,11 @@ def psu_controller_factory(controller_ip, controller_protocol, dut_hostname):
logging.info("Creating psu controller object")
if controller_protocol == "snmp":
import snmp_psu_controllers
return snmp_psu_controllers.get_psu_controller(controller_ip, dut_hostname)
return snmp_psu_controllers.get_psu_controller(controller_ip, dut_hostname, pdu)


@pytest.fixture(scope="module")
def psu_controller(duthost):
def psu_controller(duthost, pdu):
"""
@summary: Fixture for controlling power supply to PSUs of DUT
@param duthost: Fixture duthost defined in sonic-mgmt/tests/conftest.py
Expand Down Expand Up @@ -48,7 +48,7 @@ def psu_controller(duthost):
logging.info("No protocol is defined in inventory file for '%s'. Try to use default 'snmp'" % pdu_host)
controller_protocol = "snmp"

controller = psu_controller_factory(controller_ip, controller_protocol, duthost.hostname)
controller = psu_controller_factory(controller_ip, controller_protocol, duthost.hostname, pdu)

yield controller

Expand Down
18 changes: 10 additions & 8 deletions tests/common/plugins/psu_controller/snmp_psu_controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_psu_controller_type(self):
SYSDESCR = "1.3.6.1.2.1.1.1.0"
psu = None
cmdGen = cmdgen.CommandGenerator()
snmp_auth = cmdgen.CommunityData('public')
snmp_auth = cmdgen.CommunityData(self.snmp_rocommunity)
errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
snmp_auth,
cmdgen.UdpTransportTarget((self.controller, 161), timeout=5.0),
Expand Down Expand Up @@ -92,7 +92,7 @@ def _get_pdu_ports(self):
This method depends on this configuration to find out the PDU ports connected to PSUs of specific DUT.
"""
cmdGen = cmdgen.CommandGenerator()
snmp_auth = cmdgen.CommunityData('public')
snmp_auth = cmdgen.CommunityData(self.snmp_rocommunity)
errorIndication, errorStatus, errorIndex, varTable = cmdGen.nextCmd(
snmp_auth,
cmdgen.UdpTransportTarget((self.controller, 161)),
Expand All @@ -108,11 +108,13 @@ def _get_pdu_ports(self):
# Remove the preceding PORT_NAME_BASE_OID, remaining string is the PDU port ID
self.pdu_ports.append(current_oid.replace(self.PORT_NAME_BASE_OID, ''))

def __init__(self, hostname, controller):
def __init__(self, hostname, controller, pdu):
logging.info("Initializing " + self.__class__.__name__)
PsuControllerBase.__init__(self)
self.hostname = hostname
self.controller = controller
self.snmp_rocommunity = pdu['snmp_rocommunity']
self.snmp_rwcommunity = pdu['snmp_rwcommunity']
self.pdu_ports = []
self.psuType = None
self.get_psu_controller_type()
Expand Down Expand Up @@ -140,7 +142,7 @@ def turn_on_psu(self, psu_id):
port_oid = self.pPORT_CONTROL_BASE_OID + self.pdu_ports[rfc1902.Integer(psu_id)]
errorIndication, errorStatus, _, _ = \
cmdgen.CommandGenerator().setCmd(
cmdgen.CommunityData('private'),
cmdgen.CommunityData(self.snmp_rwcommunity),
cmdgen.UdpTransportTarget((self.controller, 161)),
(port_oid, rfc1902.Integer(self.CONTROL_ON)),
)
Expand Down Expand Up @@ -169,7 +171,7 @@ def turn_off_psu(self, psu_id):
port_oid = self.pPORT_CONTROL_BASE_OID + self.pdu_ports[rfc1902.Integer(psu_id)]
errorIndication, errorStatus, _, _ = \
cmdgen.CommandGenerator().setCmd(
cmdgen.CommunityData('private'),
cmdgen.CommunityData(self.snmp_rwcommunity),
cmdgen.UdpTransportTarget((self.controller, 161)),
(port_oid, rfc1902.Integer(self.CONTROL_OFF)),
)
Expand Down Expand Up @@ -200,7 +202,7 @@ def get_psu_status(self, psu_id=None):
"""
results = []
cmdGen = cmdgen.CommandGenerator()
snmp_auth = cmdgen.CommunityData('public')
snmp_auth = cmdgen.CommunityData(self.snmp_rocommunity)
errorIndication, errorStatus, errorIndex, varTable = cmdGen.nextCmd(
snmp_auth,
cmdgen.UdpTransportTarget((self.controller, 161)),
Expand All @@ -227,9 +229,9 @@ def close(self):
pass


def get_psu_controller(controller_ip, dut_hostname):
def get_psu_controller(controller_ip, dut_hostname, pdu):
"""
@summary: Factory function to create the actual PSU controller object.
@return: The actual PSU controller object. Returns None if something went wrong.
"""
return snmpPsuController(dut_hostname, controller_ip)
return snmpPsuController(dut_hostname, controller_ip, pdu)
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,14 @@ def eos():
return eos


@pytest.fixture(scope='session')
def pdu():
""" read and yield pdu configuration """
with open('../ansible/group_vars/pdu/pdu.yml') as stream:
pdu = yaml.safe_load(stream)
return pdu


@pytest.fixture(scope="module")
def creds(duthost):
""" read credential information according to the dut inventory """
Expand Down