From e44751162b09c5b57557b89db27656b5bd23341c Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Mon, 6 Aug 2012 12:17:43 -0700 Subject: Allow nova to guess device if not passed to attach partial fix for bug 1004328 Only the xen hypervisor actually respects the device name that is passed in attach_volume. For other hypervisors it makes much more sense to automatically generate a unique name. This patch generates a non-conflicting device name if one is not passed to attach_volume. It also validates the passed in volume name to make sure another device isn't already attached there. A corresponding change to novaclient and horizon will greatly improve the user experience of attaching a volume. It moves some common code out of metadata/base so that it can be used to get a list of block devices. The code was functionally tested as well and block device name generation works properly. This adds a new method to the rpcapi to validate a device name. It also adds server_id to the volumes extension, since it was omitted by mistake. The next step is to modify the libvirt driver to match the serial number of the device to the volume uuid so that the volume can always be found at /dev/disk/by-id/virtio-. DocImpact Change-Id: I0b9454fc50a5c93b4aea38545dcee98f68d7e511 --- nova/tests/compute/test_compute_utils.py | 94 ++++++++++++++++++++++++++++++++ nova/tests/compute/test_rpcapi.py | 4 ++ nova/tests/test_metadata.py | 3 +- 3 files changed, 100 insertions(+), 1 deletion(-) (limited to 'nova/tests') diff --git a/nova/tests/compute/test_compute_utils.py b/nova/tests/compute/test_compute_utils.py index 40e1947e6..7ae692bb6 100644 --- a/nova/tests/compute/test_compute_utils.py +++ b/nova/tests/compute/test_compute_utils.py @@ -17,10 +17,13 @@ """Tests For miscellaneous util methods used with compute.""" +import string + from nova.compute import instance_types from nova.compute import utils as compute_utils from nova import context from nova import db +from nova import exception from nova import flags from nova.openstack.common import importutils from nova.openstack.common import log as logging @@ -37,6 +40,97 @@ FLAGS = flags.FLAGS flags.DECLARE('stub_network', 'nova.compute.manager') +class ComputeValidateDeviceTestCase(test.TestCase): + def setUp(self): + super(ComputeValidateDeviceTestCase, self).setUp() + self.context = context.RequestContext('fake', 'fake') + self.instance = { + 'uuid': 'fake', + 'root_device_name': '/dev/vda', + 'default_ephemeral_device': '/dev/vdb' + } + + def _validate_device(self, device=None): + return compute_utils.get_device_name_for_instance(self.context, + self.instance, + device) + + @staticmethod + def _fake_bdm(device): + return { + 'device_name': device, + 'no_device': None, + 'volume_id': 'fake', + 'snapshot_id': None + } + + def test_wrap(self): + data = [] + for letter in string.ascii_lowercase[2:]: + data.append(self._fake_bdm('/dev/vd' + letter)) + self.stubs.Set(db, 'block_device_mapping_get_all_by_instance', + lambda context, instance: data) + device = self._validate_device() + self.assertEqual(device, '/dev/vdaa') + + def test_wrap_plus_one(self): + data = [] + for letter in string.ascii_lowercase[2:]: + data.append(self._fake_bdm('/dev/vd' + letter)) + data.append(self._fake_bdm('/dev/vdaa')) + self.stubs.Set(db, 'block_device_mapping_get_all_by_instance', + lambda context, instance: data) + device = self._validate_device() + self.assertEqual(device, '/dev/vdab') + + def test_later(self): + data = [ + self._fake_bdm('/dev/vdc'), + self._fake_bdm('/dev/vdd'), + self._fake_bdm('/dev/vde'), + ] + self.stubs.Set(db, 'block_device_mapping_get_all_by_instance', + lambda context, instance: data) + device = self._validate_device() + self.assertEqual(device, '/dev/vdf') + + def test_gap(self): + data = [ + self._fake_bdm('/dev/vdc'), + self._fake_bdm('/dev/vde'), + ] + self.stubs.Set(db, 'block_device_mapping_get_all_by_instance', + lambda context, instance: data) + device = self._validate_device() + self.assertEqual(device, '/dev/vdd') + + def test_no_bdms(self): + data = [] + self.stubs.Set(db, 'block_device_mapping_get_all_by_instance', + lambda context, instance: data) + device = self._validate_device() + self.assertEqual(device, '/dev/vdc') + + def test_invalid_bdms(self): + self.stubs.Set(db, 'block_device_mapping_get_all_by_instance', + lambda context, instance: []) + self.instance['root_device_name'] = "baddata" + self.assertRaises(exception.InvalidDevicePath, + self._validate_device) + + def test_invalid_device_prefix(self): + self.stubs.Set(db, 'block_device_mapping_get_all_by_instance', + lambda context, instance: []) + self.assertRaises(exception.InvalidDevicePath, + self._validate_device, '/baddata/vdc') + + def test_device_in_use(self): + self.stubs.Set(db, 'block_device_mapping_get_all_by_instance', + lambda context, instance: []) + self.assertRaises(exception.DevicePathInUse, + self._validate_device, '/dev/vdb') + + class UsageInfoTestCase(test.TestCase): def setUp(self): diff --git a/nova/tests/compute/test_rpcapi.py b/nova/tests/compute/test_rpcapi.py index e88cb2096..38849d3ed 100644 --- a/nova/tests/compute/test_rpcapi.py +++ b/nova/tests/compute/test_rpcapi.py @@ -232,6 +232,10 @@ class ComputeRpcAPITestCase(test.TestCase): injected_files='files', image_ref='ref', orig_image_ref='orig_ref', version='1.24') + def test_reserve_block_device_name(self): + self._test_compute_api('reserve_block_device_name', 'call', + instance=self.fake_instance, device='device', version='1.44') + def refresh_provider_fw_rules(self): self._test_compute_api('refresh_provider_fw_rules', 'cast', host='host') diff --git a/nova/tests/test_metadata.py b/nova/tests/test_metadata.py index b9b67326c..13be9e056 100644 --- a/nova/tests/test_metadata.py +++ b/nova/tests/test_metadata.py @@ -27,6 +27,7 @@ import webob from nova.api.metadata import base from nova.api.metadata import handler +from nova import block_device from nova import db from nova.db.sqlalchemy import api from nova import exception @@ -183,7 +184,7 @@ class MetadataTestCase(test.TestCase): 'ebs0': '/dev/sdh'} self.assertEqual(base._format_instance_mapping(ctxt, instance_ref0), - base._DEFAULT_MAPPINGS) + block_device._DEFAULT_MAPPINGS) self.assertEqual(base._format_instance_mapping(ctxt, instance_ref1), expected) -- cgit