Merge "Validate outlet index in SNMP driver"

This commit is contained in:
Jenkins 2017-05-01 20:00:54 +00:00 committed by Gerrit Code Review
commit 6f237ecfd9
3 changed files with 25 additions and 7 deletions

View File

@ -365,7 +365,7 @@ class SNMPDriverSimple(SNMPDriverBase):
:returns: Power state object OID as a tuple of integers.
"""
outlet = int(self.snmp_info['outlet'])
outlet = self.snmp_info['outlet']
return self.oid_enterprise + self.oid_device + (outlet,)
def _snmp_power_state(self):
@ -411,7 +411,7 @@ class SNMPDriverAten(SNMPDriverSimple):
:returns: Power state object OID as a tuple of integers.
"""
outlet = int(self.snmp_info['outlet'])
outlet = self.snmp_info['outlet']
return self.oid_enterprise + self.oid_device + (outlet, 0,)
@ -530,7 +530,7 @@ class SNMPDriverEatonPower(SNMPDriverBase):
integers.
:returns: The full OID as a tuple of integers.
"""
outlet = int(self.snmp_info['outlet'])
outlet = self.snmp_info['outlet']
return self.oid_base + oid + (outlet,)
def _snmp_power_state(self):
@ -597,7 +597,7 @@ def _parse_driver_info(node):
snmp_info = {}
# Validate PDU driver type
snmp_info['driver'] = info.get('snmp_driver')
snmp_info['driver'] = info['snmp_driver']
if snmp_info['driver'] not in DRIVER_CLASSES:
raise exception.InvalidParameterValue(_(
"SNMPPowerDriver: unknown driver: '%s'") % snmp_info['driver'])
@ -633,8 +633,14 @@ def _parse_driver_info(node):
snmp_info['security'] = info.get('snmp_security')
# Target PDU IP address and power outlet identification
snmp_info['address'] = info.get('snmp_address')
snmp_info['outlet'] = info.get('snmp_outlet')
snmp_info['address'] = info['snmp_address']
outlet = info['snmp_outlet']
try:
snmp_info['outlet'] = int(outlet)
except ValueError:
raise exception.InvalidParameterValue(_(
"SNMPPowerDriver: PDU power outlet index is not an integer: %s")
% outlet)
return snmp_info

View File

@ -211,7 +211,7 @@ class SNMPValidateParametersTestCase(db_base.DbTestCase):
self.assertEqual(INFO_DICT['snmp_driver'], info['driver'])
self.assertEqual(INFO_DICT['snmp_address'], info['address'])
self.assertEqual(INFO_DICT['snmp_port'], str(info['port']))
self.assertEqual(INFO_DICT['snmp_outlet'], info['outlet'])
self.assertEqual(INFO_DICT['snmp_outlet'], str(info['outlet']))
self.assertEqual(INFO_DICT['snmp_version'], info['version'])
self.assertEqual(INFO_DICT['snmp_community'], info['community'])
self.assertNotIn('security', info)
@ -349,6 +349,15 @@ class SNMPValidateParametersTestCase(db_base.DbTestCase):
snmp._parse_driver_info,
node)
def test__parse_driver_info_invalid_outlet(self):
# Make sure exception is raised when the outlet is not integer.
info = dict(INFO_DICT)
info['snmp_outlet'] = 'nn'
node = self._get_test_node(info)
self.assertRaises(exception.InvalidParameterValue,
snmp._parse_driver_info,
node)
def test__parse_driver_info_default_version(self):
# Make sure version defaults to 1 when it is missing.
info = dict(INFO_DICT)

View File

@ -0,0 +1,3 @@
---
fixes:
- Adds validation of ``snmp_outlet`` parameter to SNMP driver.