Skip to content

Commit

Permalink
Removing duplicate kernel/ramdisk check in OSAPI
Browse files Browse the repository at this point in the history
Fixes bug 886169

Change-Id: Id68de3284367e789f1295b61fe6bf9c400470ba8
  • Loading branch information
Brian Waldon committed Nov 4, 2011
1 parent 2915d05 commit 77e1684
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 118 deletions.
52 changes: 1 addition & 51 deletions nova/api/openstack/servers.py
Expand Up @@ -191,45 +191,6 @@ def _validate_server_name(self, value):
msg = _("Server name is an empty string")
raise exc.HTTPBadRequest(explanation=msg)

def _get_kernel_ramdisk_from_image(self, req, image_service, image_id):
"""Fetch an image from the ImageService, then if present, return the
associated kernel and ramdisk image IDs.
"""
context = req.environ['nova.context']
image_meta = image_service.show(context, image_id)
# NOTE(sirp): extracted to a separate method to aid unit-testing, the
# new method doesn't need a request obj or an ImageService stub
kernel_id, ramdisk_id = self._do_get_kernel_ramdisk_from_image(
image_meta)
return kernel_id, ramdisk_id

@staticmethod
def _do_get_kernel_ramdisk_from_image(image_meta):
"""Given an ImageService image_meta, return kernel and ramdisk image
ids if present.
This is only valid for `ami` style images.
"""
image_id = image_meta['id']
if image_meta['status'] != 'active':
raise exception.ImageUnacceptable(image_id=image_id,
reason=_("status is not active"))

if image_meta.get('container_format') != 'ami':
return None, None

try:
kernel_id = image_meta['properties']['kernel_id']
except KeyError:
raise exception.KernelNotFoundForImage(image_id=image_id)

try:
ramdisk_id = image_meta['properties']['ramdisk_id']
except KeyError:
ramdisk_id = None

return kernel_id, ramdisk_id

def _get_injected_files(self, personality):
"""
Create a list of injected files from the personality attribute
Expand Down Expand Up @@ -363,15 +324,6 @@ def create(self, req, body):

if str(image_href).startswith(req.application_url):
image_href = image_href.split('/').pop()
try:
image_service, image_id = image.get_image_service(context,
image_href)
kernel_id, ramdisk_id = self._get_kernel_ramdisk_from_image(
req, image_service, image_id)
except Exception, e:
msg = _("Cannot find requested image %(image_href)s: %(e)s" %
locals())
raise exc.HTTPBadRequest(explanation=msg)

personality = server_dict.get('personality')
config_drive = server_dict.get('config_drive')
Expand Down Expand Up @@ -440,9 +392,7 @@ def create(self, req, body):

(instances, resv_id) = self.compute_api.create(context,
inst_type,
image_id,
kernel_id=kernel_id,
ramdisk_id=ramdisk_id,
image_href,
display_name=name,
display_description=name,
key_name=key_name,
Expand Down
5 changes: 0 additions & 5 deletions nova/tests/api/openstack/contrib/test_createserverext.py
Expand Up @@ -138,11 +138,6 @@ def set_admin_password(self, *args, **kwargs):
compute_api = MockComputeAPI()
self.stubs.Set(nova.compute, 'API',
self._make_stub_method(compute_api))
image_uuid = 'cedef40a-ed67-4d10-800e-17455edce175'
self.stubs.Set(
nova.api.openstack.servers.Controller,
'_get_kernel_ramdisk_from_image',
self._make_stub_method((image_uuid, image_uuid)))
return compute_api

def _setup_mock_network_api(self):
Expand Down
62 changes: 0 additions & 62 deletions nova/tests/api/openstack/test_servers.py
Expand Up @@ -1404,10 +1404,6 @@ def project_get_networks(context, user_id):
def queue_get_for(context, *args):
return 'network_topic'

def kernel_ramdisk_mapping(*args, **kwargs):
image_uuid = 'cedef40a-ed67-4d10-800e-17455edce175'
return (image_uuid, image_uuid)

fakes.stub_out_networking(self.stubs)
fakes.stub_out_rate_limiting(self.stubs)
fakes.stub_out_key_pair_funcs(self.stubs)
Expand All @@ -1426,8 +1422,6 @@ def kernel_ramdisk_mapping(*args, **kwargs):
self.stubs.Set(nova.db, 'queue_get_for', queue_get_for)
self.stubs.Set(nova.network.manager.VlanManager, 'allocate_fixed_ip',
fake_method)
self.stubs.Set(servers.Controller, "_get_kernel_ramdisk_from_image",
kernel_ramdisk_mapping)
self.stubs.Set(nova.compute.api.API, "_find_host", find_host)

def _test_create_instance(self):
Expand Down Expand Up @@ -2380,62 +2374,6 @@ def test_index(self):
str(ip['addr']))


class TestGetKernelRamdiskFromImage(test.TestCase):
"""
If we're building from an AMI-style image, we need to be able to fetch the
kernel and ramdisk associated with the machine image. This information is
stored with the image metadata and return via the ImageService.
These tests ensure that we parse the metadata return the ImageService
correctly and that we handle failure modes appropriately.
"""

def test_status_not_active(self):
"""We should only allow fetching of kernel and ramdisk information if
we have a 'fully-formed' image, aka 'active'
"""
image_meta = {'id': 1, 'status': 'queued'}
self.assertRaises(exception.Invalid, self._get_k_r, image_meta)

def test_not_ami(self):
"""Anything other than ami should return no kernel and no ramdisk"""
image_meta = {'id': 1, 'status': 'active', 'container_format': 'vhd'}
kernel_id, ramdisk_id = self._get_k_r(image_meta)
self.assertEqual(kernel_id, None)
self.assertEqual(ramdisk_id, None)

def test_ami_no_kernel(self):
"""If an ami is missing a kernel it should raise NotFound"""
image_meta = {'id': 1, 'status': 'active', 'container_format': 'ami',
'properties': {'ramdisk_id': 1}}
self.assertRaises(exception.NotFound, self._get_k_r, image_meta)

def test_ami_no_ramdisk(self):
"""If an ami is missing a ramdisk, return kernel ID and None for
ramdisk ID
"""
image_meta = {'id': 1, 'status': 'active', 'container_format': 'ami',
'properties': {'kernel_id': 1}}
kernel_id, ramdisk_id = self._get_k_r(image_meta)
self.assertEqual(kernel_id, 1)
self.assertEqual(ramdisk_id, None)

def test_ami_kernel_ramdisk_present(self):
"""Return IDs if both kernel and ramdisk are present"""
image_meta = {'id': 1, 'status': 'active', 'container_format': 'ami',
'properties': {'kernel_id': 1, 'ramdisk_id': 2}}
kernel_id, ramdisk_id = self._get_k_r(image_meta)
self.assertEqual(kernel_id, 1)
self.assertEqual(ramdisk_id, 2)

@staticmethod
def _get_k_r(image_meta):
"""Rebinding function to a shorter name for convenience"""
kernel_id, ramdisk_id = servers.Controller.\
_do_get_kernel_ramdisk_from_image(image_meta)
return kernel_id, ramdisk_id


class ServersViewBuilderTest(test.TestCase):

def setUp(self):
Expand Down

0 comments on commit 77e1684

Please sign in to comment.