summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNaveed Massjouni <naveedm9@gmail.com>2011-05-20 04:14:02 -0400
committerNaveed Massjouni <naveedm9@gmail.com>2011-05-20 04:14:02 -0400
commite16b2d22dc4e6e24c3bf5150a0830661933aad29 (patch)
treef2ccb5b54289c237dc4a15ddb247782f26941b93
parenta1869741689817168c75046f2f81ee9761956cbc (diff)
Fixed some tests.
-rw-r--r--nova/api/openstack/common.py28
-rw-r--r--nova/api/openstack/servers.py6
-rw-r--r--nova/exception.py4
-rw-r--r--nova/flags.py3
-rw-r--r--nova/image/fake.py12
-rw-r--r--nova/tests/api/openstack/test_servers.py14
-rw-r--r--nova/tests/integrated/integrated_helpers.py7
-rw-r--r--nova/tests/test_quota.py8
-rw-r--r--nova/utils.py16
9 files changed, 45 insertions, 53 deletions
diff --git a/nova/api/openstack/common.py b/nova/api/openstack/common.py
index 32cd689ca..a89594c13 100644
--- a/nova/api/openstack/common.py
+++ b/nova/api/openstack/common.py
@@ -100,34 +100,6 @@ def limited_by_marker(items, request, max_limit=FLAGS.osapi_max_limit):
return items[start_index:range_end]
-def get_image_id_from_image_hash(image_service, context, image_hash):
- """Given an Image ID Hash, return an objectstore Image ID.
-
- image_service - reference to objectstore compatible image service.
- context - security context for image service requests.
- image_hash - hash of the image ID.
- """
-
- # FIX(sandy): This is terribly inefficient. It pulls all images
- # from objectstore in order to find the match. ObjectStore
- # should have a numeric counterpart to the string ID.
- try:
- items = image_service.detail(context)
- except NotImplementedError:
- items = image_service.index(context)
- for image in items:
- image_id = image['id']
- try:
- if abs(hash(image_id)) == int(image_hash):
- return image_id
- except ValueError:
- msg = _("Requested image_id has wrong format: %s,"
- "should have numerical format") % image_id
- LOG.error(msg)
- raise Exception(msg)
- raise exception.ImageNotFound(image_id=image_hash)
-
-
def get_id_from_href(href):
"""Return the id portion of a url as an int.
diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py
index 337c6ced8..31c1e86c0 100644
--- a/nova/api/openstack/servers.py
+++ b/nova/api/openstack/servers.py
@@ -144,10 +144,10 @@ class Controller(common.OpenstackController):
(image_service, image_id) = utils.get_image_service(image_ref)
kernel_id, ramdisk_id = self._get_kernel_ramdisk_from_image(
req, image_id)
- image_set = set([x['id'] for x in image_service.index(context)])
- assert image_id in image_set
+ images = set([str(x['id']) for x in image_service.index(context)])
+ assert str(image_id) in images
except:
- msg = _("Can not find requested image")
+ msg = _("Cannot find requested image %s") % image_ref
return faults.Fault(exc.HTTPBadRequest(msg))
personality = env['server'].get('personality')
diff --git a/nova/exception.py b/nova/exception.py
index cf6069454..4c977aca0 100644
--- a/nova/exception.py
+++ b/nova/exception.py
@@ -279,6 +279,10 @@ class DiskNotFound(NotFound):
message = _("No disk at %(location)s")
+class InvalidImageRef(Invalid):
+ message = _("Invalid image ref %(image_ref)s.")
+
+
class ImageNotFound(NotFound):
message = _("Image %(image_id)s could not be found.")
diff --git a/nova/flags.py b/nova/flags.py
index ee5adae32..32cb6efa8 100644
--- a/nova/flags.py
+++ b/nova/flags.py
@@ -362,9 +362,6 @@ DEFINE_string('scheduler_manager', 'nova.scheduler.manager.SchedulerManager',
# The service to use for image search and retrieval
DEFINE_string('image_service', 'nova.image.local.LocalImageService',
'The service to use for retrieving and searching for images.')
-DEFINE_string('glance_image_service', 'nova.image.glance.GlanceImageService',
- 'The service to use for retrieving and searching for ' +
- 'glance images.')
DEFINE_string('host', socket.gethostname(),
'name of this node')
diff --git a/nova/image/fake.py b/nova/image/fake.py
index 2a60c7743..659c16557 100644
--- a/nova/image/fake.py
+++ b/nova/image/fake.py
@@ -79,10 +79,22 @@ class FakeImageService(service.BaseImageService):
'disk_format': 'raw',
'properties': {'kernel_id': FLAGS.null_kernel,
'ramdisk_id': FLAGS.null_kernel}}
+
+ image5 = {'id': '3',
+ 'name': 'fakeimage123456',
+ 'created_at': timestamp,
+ 'updated_at': timestamp,
+ 'status': 'active',
+ 'container_format': 'ami',
+ 'disk_format': 'raw',
+ 'properties': {'kernel_id': FLAGS.null_kernel,
+ 'ramdisk_id': FLAGS.null_kernel}}
+
self.create(None, image1)
self.create(None, image2)
self.create(None, image3)
self.create(None, image4)
+ self.create(None, image5)
super(FakeImageService, self).__init__()
def index(self, context):
diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py
index bced2b910..22beef05f 100644
--- a/nova/tests/api/openstack/test_servers.py
+++ b/nova/tests/api/openstack/test_servers.py
@@ -29,6 +29,7 @@ from nova import db
from nova import exception
from nova import flags
from nova import test
+from nova import utils
import nova.api.openstack
from nova.api.openstack import servers
import nova.compute.api
@@ -37,6 +38,7 @@ from nova.compute import power_state
import nova.db.api
from nova.db.sqlalchemy.models import Instance
from nova.db.sqlalchemy.models import InstanceMetadata
+import nova.image.fake
import nova.rpc
from nova.tests.api.openstack import common
from nova.tests.api.openstack import fakes
@@ -464,7 +466,12 @@ class ServersTest(test.TestCase):
def image_id_from_hash(*args, **kwargs):
return 2
- FLAGS.glance_image_service = 'nova.image.fake.FakeImageService'
+ def fake_image_service(*args):
+ return nova.image.fake.FakeImageService()
+
+ FLAGS.image_service = 'nova.image.fake.FakeImageService'
+ self.stubs.Set(
+ nova.image.glance, 'GlanceImageService', fake_image_service)
self.stubs.Set(nova.db.api, 'project_get_network', project_get_network)
self.stubs.Set(nova.db.api, 'instance_create', instance_create)
self.stubs.Set(nova.rpc, 'cast', fake_method)
@@ -476,8 +483,6 @@ class ServersTest(test.TestCase):
fake_method)
self.stubs.Set(nova.api.openstack.servers.Controller,
"_get_kernel_ramdisk_from_image", kernel_ramdisk_mapping)
- self.stubs.Set(nova.api.openstack.common,
- "get_image_id_from_image_hash", image_id_from_hash)
self.stubs.Set(nova.compute.api.API, "_find_host", find_host)
def _test_create_instance_helper(self):
@@ -1707,11 +1712,10 @@ class TestServerInstanceCreation(test.TestCase):
return stub_method
compute_api = MockComputeAPI()
+ FLAGS.image_service = 'nova.image.fake.FakeImageService'
self.stubs.Set(nova.compute, 'API', make_stub_method(compute_api))
self.stubs.Set(nova.api.openstack.servers.Controller,
'_get_kernel_ramdisk_from_image', make_stub_method((1, 1)))
- self.stubs.Set(nova.api.openstack.common,
- 'get_image_id_from_image_hash', make_stub_method(2))
return compute_api
def _create_personality_request_dict(self, personality_files):
diff --git a/nova/tests/integrated/integrated_helpers.py b/nova/tests/integrated/integrated_helpers.py
index e6efc16c5..5871a498c 100644
--- a/nova/tests/integrated/integrated_helpers.py
+++ b/nova/tests/integrated/integrated_helpers.py
@@ -27,6 +27,7 @@ from nova import flags
from nova import service
from nova import test # For the flags
from nova.auth import manager
+import nova.image.glance
from nova.log import logging
from nova.tests.integrated.api import client
@@ -151,6 +152,11 @@ class _IntegratedTestBase(test.TestCase):
f = self._get_flags()
self.flags(**f)
+ def fake_image_service(*args):
+ return nova.image.fake.FakeImageService()
+ self.stubs.Set(
+ nova.image.glance, 'GlanceImageService', fake_image_service)
+
# set up services
self.start_service('compute')
self.start_service('volume')
@@ -185,7 +191,6 @@ class _IntegratedTestBase(test.TestCase):
"""An opportunity to setup flags, before the services are started."""
f = {}
f['image_service'] = 'nova.image.fake.FakeImageService'
- f['glance_image_service'] = 'nova.image.fake.FakeImageService'
f['fake_network'] = True
return f
diff --git a/nova/tests/test_quota.py b/nova/tests/test_quota.py
index 9ede0786f..02b641a47 100644
--- a/nova/tests/test_quota.py
+++ b/nova/tests/test_quota.py
@@ -280,18 +280,18 @@ class QuotaTestCase(test.TestCase):
FLAGS.quota_max_injected_files)
def _create_with_injected_files(self, files):
- FLAGS.glance_image_service = 'nova.image.fake.FakeImageService'
+ FLAGS.image_service = 'nova.image.fake.FakeImageService'
api = compute.API(image_service=self.StubImageService())
inst_type = instance_types.get_instance_type_by_name('m1.small')
api.create(self.context, min_count=1, max_count=1,
- instance_type=inst_type, image_id='fake',
+ instance_type=inst_type, image_id='3',
injected_files=files)
def test_no_injected_files(self):
- FLAGS.glance_image_service = 'nova.image.fake.FakeImageService'
+ FLAGS.image_service = 'nova.image.fake.FakeImageService'
api = compute.API(image_service=self.StubImageService())
inst_type = instance_types.get_instance_type_by_name('m1.small')
- api.create(self.context, instance_type=inst_type, image_id='fake')
+ api.create(self.context, instance_type=inst_type, image_id='3')
def test_max_injected_files(self):
files = []
diff --git a/nova/utils.py b/nova/utils.py
index 85934813e..3802f50c4 100644
--- a/nova/utils.py
+++ b/nova/utils.py
@@ -748,11 +748,7 @@ def parse_image_ref(image_ref):
o = urlparse(image_ref)
port = o.port or 80
host = o.netloc.split(':', 1)[0]
- image_id = o.path.split('/')[-1]
-
- if is_int(image_id):
- image_id = int(image_id)
-
+ image_id = int(o.path.split('/')[-1])
return (image_id, host, port)
@@ -776,8 +772,10 @@ def get_image_service(image_ref):
if is_int(image_ref):
return (get_default_image_service(), int(image_ref))
- (image_id, host, port) = parse_image_ref(image_ref)
- glance_client = import_class('nova.image.glance.GlanceClient')(host,
- port)
- image_service = import_class(FLAGS.glance_image_service)(glance_client)
+ try:
+ (image_id, host, port) = parse_image_ref(image_ref)
+ except:
+ raise exception.InvalidImageRef(image_ref=image_ref)
+ glance_client = nova.image.glance.GlanceClient(host, port)
+ image_service = nova.image.glance.GlanceImageService(glance_client)
return (image_service, image_id)