diff --git a/ironic/drivers/modules/snmp.py b/ironic/drivers/modules/snmp.py index 73fa40b53c..63bbc58f99 100644 --- a/ironic/drivers/modules/snmp.py +++ b/ironic/drivers/modules/snmp.py @@ -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 diff --git a/ironic/tests/unit/drivers/modules/test_snmp.py b/ironic/tests/unit/drivers/modules/test_snmp.py index 485e523c40..ca2da7ea50 100644 --- a/ironic/tests/unit/drivers/modules/test_snmp.py +++ b/ironic/tests/unit/drivers/modules/test_snmp.py @@ -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) diff --git a/releasenotes/notes/snmp-outlet-validate-ffbe8e6687172efc.yaml b/releasenotes/notes/snmp-outlet-validate-ffbe8e6687172efc.yaml new file mode 100644 index 0000000000..c4554c9284 --- /dev/null +++ b/releasenotes/notes/snmp-outlet-validate-ffbe8e6687172efc.yaml @@ -0,0 +1,3 @@ +--- +fixes: + - Adds validation of ``snmp_outlet`` parameter to SNMP driver.