Adding guard on None value for some helpers method

We use some helper methods to strip and extrapolate details from the
device name string, some of these methods are missing the check on None
values, if a None is passed the code raise a TypeError exception. This
change adds a check for managing None values correctly.

Change-Id: Iebfaa09e494dac085b78dbaffa867f8047c2106b
This commit is contained in:
Andrea Rosa 2016-01-07 16:20:50 +00:00
parent 42e4126f05
commit 3f98021271
2 changed files with 5 additions and 2 deletions

View File

@ -494,7 +494,7 @@ _pref = re.compile('^((x?v|s|h)d)')
def strip_prefix(device_name):
"""remove both leading /dev/ and xvd or sd or vd or hd."""
device_name = strip_dev(device_name)
return _pref.sub('', device_name)
return _pref.sub('', device_name) if device_name else device_name
_nums = re.compile('\d+')
@ -504,7 +504,7 @@ def get_device_letter(device_name):
letter = strip_prefix(device_name)
# NOTE(vish): delete numbers in case we have something like
# /dev/sda1
return _nums.sub('', letter)
return _nums.sub('', letter) if device_name else device_name
def instance_block_mapping(instance, bdms):

View File

@ -125,6 +125,7 @@ class BlockDeviceTestCase(test.NoDBTestCase):
def test_strip_dev(self):
self.assertEqual('sda', block_device.strip_dev('/dev/sda'))
self.assertEqual('sda', block_device.strip_dev('sda'))
self.assertIsNone(block_device.strip_dev(None))
def test_strip_prefix(self):
self.assertEqual('a', block_device.strip_prefix('/dev/sda'))
@ -132,6 +133,7 @@ class BlockDeviceTestCase(test.NoDBTestCase):
self.assertEqual('a', block_device.strip_prefix('xvda'))
self.assertEqual('a', block_device.strip_prefix('vda'))
self.assertEqual('a', block_device.strip_prefix('hda'))
self.assertIsNone(block_device.strip_prefix(None))
def test_get_device_letter(self):
self.assertEqual('', block_device.get_device_letter(''))
@ -142,6 +144,7 @@ class BlockDeviceTestCase(test.NoDBTestCase):
self.assertEqual('b', block_device.get_device_letter('sdb2'))
self.assertEqual('c', block_device.get_device_letter('vdc'))
self.assertEqual('c', block_device.get_device_letter('hdc'))
self.assertIsNone(block_device.get_device_letter(None))
def test_volume_in_mapping(self):
swap = {'device_name': '/dev/sdb',