diff --git a/nova/block_device.py b/nova/block_device.py index cf670a6486a5..38e1c62f80c3 100644 --- a/nova/block_device.py +++ b/nova/block_device.py @@ -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): diff --git a/nova/tests/unit/test_block_device.py b/nova/tests/unit/test_block_device.py index 5e9625c484d6..2777cbc67013 100644 --- a/nova/tests/unit/test_block_device.py +++ b/nova/tests/unit/test_block_device.py @@ -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',