diff options
author | Brian Waldon <bcwaldon@gmail.com> | 2012-06-25 14:06:08 -0700 |
---|---|---|
committer | Brian Waldon <bcwaldon@gmail.com> | 2012-06-27 14:31:35 -0700 |
commit | 3aaa0b103447d56f8d3b259c693cd9a3a8dcbe36 (patch) | |
tree | 00a15f76716fd9e43ce7c24dfc8b45e38a1b4866 /nova | |
parent | 7e658bd45db6c14b4cd7e3a976caf769873d4ca1 (diff) | |
download | nova-3aaa0b103447d56f8d3b259c693cd9a3a8dcbe36.tar.gz |
Cleanup of image service code
* Remove image_service flag
* Move nova.image.fake to nova.tests.image.fake
* Move nova.image.get_default_image_service to
nova.image.glance.get_default_image_service
* Move nova.image.get_image_service to
nova.image.glance.get_remote_image_service
* Related to bp integrate-python-glanceclient
Change-Id: Iea6db7898328a9060fb88586e042efbc0a4351fc
Diffstat (limited to 'nova')
35 files changed, 177 insertions, 187 deletions
diff --git a/nova/api/openstack/compute/image_metadata.py b/nova/api/openstack/compute/image_metadata.py index 145e0395eb..516b25d88a 100644 --- a/nova/api/openstack/compute/image_metadata.py +++ b/nova/api/openstack/compute/image_metadata.py @@ -21,7 +21,7 @@ from nova.api.openstack import common from nova.api.openstack import wsgi from nova import exception from nova import flags -from nova import image +from nova.image import glance FLAGS = flags.FLAGS @@ -31,7 +31,7 @@ class Controller(object): """The image metadata API controller for the OpenStack API""" def __init__(self): - self.image_service = image.get_default_image_service() + self.image_service = glance.get_default_image_service() def _get_image(self, context, image_id): try: diff --git a/nova/api/openstack/compute/images.py b/nova/api/openstack/compute/images.py index 629ba67d50..9879c7d25a 100644 --- a/nova/api/openstack/compute/images.py +++ b/nova/api/openstack/compute/images.py @@ -21,7 +21,7 @@ from nova.api.openstack import wsgi from nova.api.openstack import xmlutil from nova import exception from nova import flags -import nova.image +import nova.image.glance from nova import log as logging import nova.utils @@ -96,12 +96,12 @@ class Controller(wsgi.Controller): def __init__(self, image_service=None, **kwargs): """Initialize new `ImageController`. - :param image_service: `nova.image.glance:GlancemageService` + :param image_service: `nova.image.glance:GlanceImageService` """ super(Controller, self).__init__(**kwargs) self._image_service = (image_service or - nova.image.get_default_image_service()) + nova.image.glance.get_default_image_service()) def _get_filters(self, req): """ diff --git a/nova/compute/api.py b/nova/compute/api.py index 05f4caa291..557dbb9307 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -39,7 +39,7 @@ from nova import crypto from nova.db import base from nova import exception from nova import flags -import nova.image +from nova.image import glance from nova import log as logging from nova import network from nova import notifications @@ -121,7 +121,7 @@ class API(base.Base): def __init__(self, image_service=None, network_api=None, volume_api=None, security_group_api=None, **kwargs): self.image_service = (image_service or - nova.image.get_default_image_service()) + glance.get_default_image_service()) self.network_api = network_api or network.API() self.volume_api = volume_api or volume.API() @@ -374,8 +374,8 @@ class API(base.Base): self._check_injected_file_quota(context, injected_files) self._check_requested_networks(context, requested_networks) - (image_service, image_id) = nova.image.get_image_service(context, - image_href) + (image_service, image_id) = glance.get_remote_image_service(context, + image_href) image = image_service.show(context, image_id) if instance_type['memory_mb'] < int(image.get('min_ram') or 0): @@ -1219,7 +1219,7 @@ class API(base.Base): def _get_image(self, context, image_href): """Throws an ImageNotFound exception if image_href does not exist.""" - (image_service, image_id) = nova.image.get_image_service(context, + (image_service, image_id) = glance.get_remote_image_service(context, image_href) return image_service.show(context, image_id) diff --git a/nova/compute/manager.py b/nova/compute/manager.py index a9816817b2..deca041771 100644 --- a/nova/compute/manager.py +++ b/nova/compute/manager.py @@ -57,7 +57,7 @@ from nova.compute import vm_states import nova.context from nova import exception from nova import flags -import nova.image +from nova.image import glance from nova import log as logging from nova import manager from nova import network @@ -205,7 +205,8 @@ def wrap_instance_fault(function): def _get_image_meta(context, image_ref): - image_service, image_id = nova.image.get_image_service(context, image_ref) + image_service, image_id = glance.get_remote_image_service(context, + image_ref) return image_service.show(context, image_id) @@ -1056,7 +1057,7 @@ class ComputeManager(manager.SchedulerDependentManager): marker = batch[-1]['id'] return images - image_service = nova.image.get_default_image_service() + image_service = glance.get_default_image_service() filters = {'property-image_type': 'backup', 'property-backup_type': backup_type, 'property-instance_uuid': instance_uuid} diff --git a/nova/flags.py b/nova/flags.py index c427a7e945..bb769c4dd1 100644 --- a/nova/flags.py +++ b/nova/flags.py @@ -322,9 +322,6 @@ global_opts = [ cfg.StrOpt('firewall_driver', default='nova.virt.firewall.IptablesFirewallDriver', help='Firewall driver (defaults to iptables)'), - cfg.StrOpt('image_service', - default='nova.image.glance.GlanceImageService', - help='The service to use for retrieving and searching images.'), cfg.StrOpt('host', default=socket.gethostname(), help='Name of this node. This can be an opaque identifier. ' diff --git a/nova/image/__init__.py b/nova/image/__init__.py index 18dcc2fa2d..e69de29bb2 100644 --- a/nova/image/__init__.py +++ b/nova/image/__init__.py @@ -1,51 +0,0 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 -# -# Copyright 2011 OpenStack LLC. -# All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - - -import nova -from nova import flags -from nova.image import glance -from nova.openstack.common import importutils - -FLAGS = flags.FLAGS - - -def get_default_image_service(): - ImageService = importutils.import_class(FLAGS.image_service) - return ImageService() - - -def get_image_service(context, image_href): - """Get the proper image_service and id for the given image_href. - - The image_href param can be an href of the form - http://myglanceserver:9292/images/42, or just an int such as 42. If the - image_href is an int, then the default image service is returned. - - :param image_href: image ref/id for an image - :returns: a tuple of the form (image_service, image_id) - - """ - # check if this is not a uri - if '/' not in str(image_href): - return (get_default_image_service(), image_href) - - else: - (glance_client, image_id) = glance._get_glance_client(context, - image_href) - image_service = nova.image.glance.GlanceImageService(glance_client) - return (image_service, image_id) diff --git a/nova/image/glance.py b/nova/image/glance.py index bdd30ae916..9ad6da5282 100644 --- a/nova/image/glance.py +++ b/nova/image/glance.py @@ -511,3 +511,31 @@ def _translate_plain_exception(exc_type, exc_value): if exc_type is glance_exception.Invalid: return exception.Invalid(exc_value) return exc_value + + +def get_remote_image_service(context, image_href): + """Create an image_service and parse the id from the given image_href. + + The image_href param can be an href of the form + 'http://example.com:9292/v1/images/b8b2c6f7-7345-4e2f-afa2-eedaba9cbbe3', + or just an id such as 'b8b2c6f7-7345-4e2f-afa2-eedaba9cbbe3'. If the + image_href is a standalone id, then the default image service is returned. + + :param image_href: href that describes the location of an image + :returns: a tuple of the form (image_service, image_id) + + """ + #NOTE(bcwaldon): If image_href doesn't look like a URI, assume its a + # standalone image ID + if '/' not in str(image_href): + image_service = get_default_image_service() + image_id = image_href + else: + (glance_client, image_id) = _get_glance_client(context, image_href) + image_service = GlanceImageService(glance_client) + + return (image_service, image_id) + + +def get_default_image_service(): + return GlanceImageService() diff --git a/nova/image/s3.py b/nova/image/s3.py index 9fcfd5c89b..b5a94cb3fe 100644 --- a/nova/image/s3.py +++ b/nova/image/s3.py @@ -33,7 +33,7 @@ from nova.api.ec2 import ec2utils import nova.cert.rpcapi from nova import exception from nova import flags -from nova import image +from nova.image import glance from nova import log as logging from nova.openstack.common import cfg from nova import utils @@ -69,7 +69,7 @@ class S3ImageService(object): def __init__(self, service=None, *args, **kwargs): self.cert_rpcapi = nova.cert.rpcapi.CertAPI() - self.service = service or image.get_default_image_service() + self.service = service or glance.get_default_image_service() self.service.__init__(*args, **kwargs) def _translate_uuids_to_ids(self, context, images): diff --git a/nova/test.py b/nova/test.py index 1839fd1e4b..da115300eb 100644 --- a/nova/test.py +++ b/nova/test.py @@ -32,7 +32,6 @@ import nose.plugins.skip import stubout from nova import flags -import nova.image.fake from nova import log as logging from nova.openstack.common import cfg from nova.openstack.common import timeutils @@ -150,9 +149,6 @@ class TestCase(unittest.TestCase): self.mox.VerifyAll() super(TestCase, self).tearDown() finally: - if FLAGS.image_service == 'nova.image.fake.FakeImageService': - nova.image.fake.FakeImageService_reset() - # Reset any overridden flags FLAGS.reset() diff --git a/nova/tests/api/ec2/test_cloud.py b/nova/tests/api/ec2/test_cloud.py index 6dd6fe9162..180faeeaa9 100644 --- a/nova/tests/api/ec2/test_cloud.py +++ b/nova/tests/api/ec2/test_cloud.py @@ -36,12 +36,12 @@ from nova import context from nova import db from nova import exception from nova import flags -from nova.image import fake from nova.image import s3 from nova import log as logging from nova.network import api as network_api from nova.openstack.common import rpc from nova import test +from nova.tests.image import fake from nova import utils @@ -94,6 +94,19 @@ class CloudTestCase(test.TestCase): self.flags(compute_driver='nova.virt.fake.FakeDriver', stub_network=True) + def fake_show(meh, context, id): + return {'id': id, + 'container_format': 'ami', + 'properties': { + 'kernel_id': 'cedef40a-ed67-4d10-800e-17455edce175', + 'ramdisk_id': 'cedef40a-ed67-4d10-800e-17455edce175', + 'type': 'machine', + 'image_state': 'available'}} + + self.stubs.Set(fake._FakeImageService, 'show', fake_show) + self.stubs.Set(fake._FakeImageService, 'show_by_name', fake_show) + fake.stub_out_image_service(self.stubs) + def dumb(*args, **kwargs): pass @@ -115,18 +128,6 @@ class CloudTestCase(test.TestCase): self.project_id, is_admin=True) - def fake_show(meh, context, id): - return {'id': id, - 'container_format': 'ami', - 'properties': { - 'kernel_id': 'cedef40a-ed67-4d10-800e-17455edce175', - 'ramdisk_id': 'cedef40a-ed67-4d10-800e-17455edce175', - 'type': 'machine', - 'image_state': 'available'}} - - self.stubs.Set(fake._FakeImageService, 'show', fake_show) - self.stubs.Set(fake._FakeImageService, 'show_by_name', fake_show) - # NOTE(comstud): Make 'cast' behave like a 'call' which will # ensure that operations complete self.stubs.Set(rpc, 'cast', rpc.call) @@ -137,6 +138,10 @@ class CloudTestCase(test.TestCase): db.api.s3_image_create(self.context, '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6') + def tearDown(self): + super(CloudTestCase, self).tearDown() + fake.FakeImageService_reset() + def _stub_instance_get_with_fixed_ips(self, func_name): orig_func = getattr(self.cloud.compute_api, func_name) @@ -1618,7 +1623,6 @@ class CloudTestCase(test.TestCase): 'container_format': 'ami', 'status': 'active'} - self.stubs.UnsetAll() self.stubs.Set(fake._FakeImageService, 'show', fake_show) def dumb(*args, **kwargs): @@ -2352,7 +2356,6 @@ class CloudTestCase(test.TestCase): for i in range(3, 7): db.api.s3_image_create(self.context, 'ami-%d' % i) - self.stubs.UnsetAll() self.stubs.Set(fake._FakeImageService, 'show', fake_show) test_dia_iisb('stop', image_id='ami-3') diff --git a/nova/tests/api/ec2/test_ec2_validate.py b/nova/tests/api/ec2/test_ec2_validate.py index d917c33527..ad0de2ca07 100644 --- a/nova/tests/api/ec2/test_ec2_validate.py +++ b/nova/tests/api/ec2/test_ec2_validate.py @@ -22,7 +22,7 @@ from nova import context from nova import db from nova import exception from nova import flags -from nova.image import fake +from nova.tests.image import fake from nova import log as logging from nova.openstack.common import importutils from nova.openstack.common import rpc @@ -50,7 +50,7 @@ class EC2ValidateTestCase(test.TestCase): self.scheduter = self.start_service('scheduler') self.network = self.start_service('network') self.volume = self.start_service('volume') - self.image_service = importutils.import_object(FLAGS.image_service) + self.image_service = fake.FakeImageService() self.user_id = 'fake' self.project_id = 'fake' @@ -80,6 +80,7 @@ class EC2ValidateTestCase(test.TestCase): 'type': 'machine', 'image_state': 'available'}} + fake.stub_out_image_service(self.stubs) self.stubs.Set(fake._FakeImageService, 'show', fake_show) self.stubs.Set(fake._FakeImageService, 'show_by_name', fake_show) @@ -93,6 +94,10 @@ class EC2ValidateTestCase(test.TestCase): db.api.s3_image_create(self.context, '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6') + def tearDown(self): + super(EC2ValidateTestCase, self).tearDown() + fake.FakeImageService_reset() + #EC2_API tests (InvalidInstanceID.Malformed) def test_console_output(self): for ec2_id, e in self.ec2_id_exception_map: diff --git a/nova/tests/api/openstack/compute/contrib/test_disk_config.py b/nova/tests/api/openstack/compute/contrib/test_disk_config.py index e63c28080a..9999d94da9 100644 --- a/nova/tests/api/openstack/compute/contrib/test_disk_config.py +++ b/nova/tests/api/openstack/compute/contrib/test_disk_config.py @@ -24,6 +24,7 @@ from nova.openstack.common import jsonutils import nova.openstack.common.rpc from nova import test from nova.tests.api.openstack import fakes +import nova.tests.image.fake MANUAL_INSTANCE_UUID = fakes.FAKE_UUID @@ -45,6 +46,8 @@ class DiskConfigTestCase(test.TestCase): def setUp(self): super(DiskConfigTestCase, self).setUp() self.flags(verbose=True) + nova.tests.image.fake.stub_out_image_service(self.stubs) + fakes.stub_out_nw_api(self.stubs) FAKE_INSTANCES = [ @@ -120,6 +123,10 @@ class DiskConfigTestCase(test.TestCase): self.app = compute.APIRouter() + def tearDown(self): + super(DiskConfigTestCase, self).tearDown() + nova.tests.image.fake.FakeImageService_reset() + def assertDiskConfig(self, dict_, value): self.assert_(API_DISK_CONFIG in dict_) self.assertEqual(dict_[API_DISK_CONFIG], value) diff --git a/nova/tests/api/openstack/compute/test_server_actions.py b/nova/tests/api/openstack/compute/test_server_actions.py index 3e3b4eafcc..19d752a9f8 100644 --- a/nova/tests/api/openstack/compute/test_server_actions.py +++ b/nova/tests/api/openstack/compute/test_server_actions.py @@ -27,6 +27,7 @@ from nova import flags from nova.openstack.common import importutils from nova import test from nova.tests.api.openstack import fakes +import nova.tests.image.fake from nova import utils @@ -69,7 +70,7 @@ class ServerActionsControllerTest(test.TestCase): fakes.stub_out_nw_api(self.stubs) fakes.stub_out_rate_limiting(self.stubs) fakes.stub_out_compute_api_snapshot(self.stubs) - fakes.stub_out_image_service(self.stubs) + nova.tests.image.fake.stub_out_image_service(self.stubs) service_class = 'nova.image.glance.GlanceImageService' self.service = importutils.import_object(service_class) self.service.delete_all() diff --git a/nova/tests/api/openstack/compute/test_servers.py b/nova/tests/api/openstack/compute/test_servers.py index e1d898aeb4..91a3ef68d6 100644 --- a/nova/tests/api/openstack/compute/test_servers.py +++ b/nova/tests/api/openstack/compute/test_servers.py @@ -35,12 +35,12 @@ from nova.compute import vm_states import nova.db from nova.db.sqlalchemy import models from nova import flags -import nova.image.fake from nova.openstack.common import jsonutils import nova.openstack.common.rpc from nova import test from nova.tests.api.openstack import fakes from nova.tests import fake_network +import nova.tests.image.fake from nova import utils @@ -99,7 +99,7 @@ class ServersControllerTest(test.TestCase): self.flags(verbose=True, use_ipv6=False) fakes.stub_out_rate_limiting(self.stubs) fakes.stub_out_key_pair_funcs(self.stubs) - fakes.stub_out_image_service(self.stubs) + nova.tests.image.fake.stub_out_image_service(self.stubs) return_server = fakes.fake_instance_get() return_servers = fakes.fake_instance_get_all_by_filters() self.stubs.Set(nova.db, 'instance_get_all_by_filters', @@ -1484,7 +1484,7 @@ class ServersControllerCreateTest(test.TestCase): fakes.stub_out_rate_limiting(self.stubs) fakes.stub_out_key_pair_funcs(self.stubs) - fakes.stub_out_image_service(self.stubs) + nova.tests.image.fake.stub_out_image_service(self.stubs) fakes.stub_out_nw_api(self.stubs) self.stubs.Set(utils, 'gen_uuid', fake_gen_uuid) self.stubs.Set(nova.db, 'instance_add_security_group', diff --git a/nova/tests/api/openstack/compute/test_urlmap.py b/nova/tests/api/openstack/compute/test_urlmap.py index 67524bcf5c..780879e3cd 100644 --- a/nova/tests/api/openstack/compute/test_urlmap.py +++ b/nova/tests/api/openstack/compute/test_urlmap.py @@ -19,6 +19,7 @@ from nova import log as logging from nova.openstack.common import jsonutils from nova import test from nova.tests.api.openstack import fakes +import nova.tests.image.fake LOG = logging.getLogger(__name__) @@ -27,6 +28,11 @@ class UrlmapTest(test.TestCase): def setUp(self): super(UrlmapTest, self).setUp() fakes.stub_out_rate_limiting(self.stubs) + nova.tests.image.fake.stub_out_image_service(self.stubs) + + def tearDown(self): + super(UrlmapTest, self).tearDown() + nova.tests.image.fake.FakeImageService_reset() def test_path_version_v1_1(self): """Test URL path specifying v1.1 returns v2 content.""" diff --git a/nova/tests/api/openstack/fakes.py b/nova/tests/api/openstack/fakes.py index ca6b25c670..a6dcd4d2f0 100644 --- a/nova/tests/api/openstack/fakes.py +++ b/nova/tests/api/openstack/fakes.py @@ -37,7 +37,7 @@ from nova.compute import vm_states from nova import context from nova.db.sqlalchemy import models from nova import exception as exc -import nova.image.fake +import nova.image.glance from nova.openstack.common import jsonutils from nova.openstack.common import timeutils from nova import quota @@ -125,14 +125,6 @@ def stub_out_key_pair_funcs(stubs, have_key_pair=True): stubs.Set(nova.db, 'key_pair_get_all_by_user', no_key_pair) -def stub_out_image_service(stubs): - def fake_get_image_service(context, image_href): - return (nova.image.fake.FakeImageService(), image_href) - stubs.Set(nova.image, 'get_image_service', fake_get_image_service) - stubs.Set(nova.image, 'get_default_image_service', - lambda: nova.image.fake.FakeImageService()) - - def stub_out_rate_limiting(stubs): def fake_rate_init(self, app): super(limits.RateLimitingMiddleware, self).__init__(app) @@ -276,10 +268,12 @@ def stub_out_glance_add_image(stubs, sent_to_glance): def stub_out_glance(stubs): - def fake_get_image_service(): + def fake_get_remote_image_service(): client = glance_stubs.StubGlanceClient(_make_image_fixtures()) return nova.image.glance.GlanceImageService(client) - stubs.Set(nova.image, 'get_default_image_service', fake_get_image_service) + stubs.Set(nova.image.glance, + 'get_default_image_service', + fake_get_remote_image_service) class FakeToken(object): diff --git a/nova/tests/compute/test_compute.py b/nova/tests/compute/test_compute.py index e77b0fcdb4..9ae099b607 100644 --- a/nova/tests/compute/test_compute.py +++ b/nova/tests/compute/test_compute.py @@ -39,7 +39,7 @@ from nova import context from nova import db from nova import exception from nova import flags -from nova.image import fake as fake_image +from nova.tests.image import fake as fake_image from nova import log as logging from nova.notifier import test_notifier from nova.openstack.common import importutils @@ -125,11 +125,13 @@ class BaseTestCase(test.TestCase): 'ramdisk_id': 'fake_ramdisk_id', 'something_else': 'meow'}} + fake_image.stub_out_image_service(self.stubs) self.stubs.Set(fake_image._FakeImageService, 'show', fake_show) self.stubs.Set(rpc, 'call', rpc_call_wrapper) self.stubs.Set(rpc, 'cast', rpc_cast_wrapper) def tearDown(self): + fake_image.FakeImageService_reset() instances = db.instance_get_all(self.context.elevated()) for instance in instances: db.instance_destroy(self.context.elevated(), instance['uuid']) diff --git a/nova/tests/fake_flags.py b/nova/tests/fake_flags.py index 3d05d7605b..5bcfb4c715 100644 --- a/nova/tests/fake_flags.py +++ b/nova/tests/fake_flags.py @@ -38,7 +38,6 @@ def set_defaults(conf): conf.set_default('fake_network', True) conf.set_default('fake_rabbit', True) conf.set_default('flat_network_bridge', 'br100') - conf.set_default('image_service', 'nova.image.fake.FakeImageService') conf.set_default('iscsi_num_targets', 8) conf.set_default('network_size', 8) conf.set_default('num_networks', 2) diff --git a/nova/image/fake.py b/nova/tests/image/fake.py index 996459081e..017611aa90 100644 --- a/nova/image/fake.py +++ b/nova/tests/image/fake.py @@ -1,6 +1,7 @@ # vim: tabstop=4 shiftwidth=4 softtabstop=4 # Copyright 2011 Justin Santa Barbara +# Copyright 2012 OpenStack LLC # All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may @@ -22,6 +23,7 @@ import datetime from nova import exception from nova import flags +import nova.image.glance from nova import log as logging from nova import utils @@ -41,7 +43,6 @@ class _FakeImageService(object): # So, make sure we've got one.. timestamp = datetime.datetime(2011, 01, 01, 01, 02, 03) - # NOTE(bcwaldon): was image '123456' image1 = {'id': '155d900f-4e14-4e4c-a73d-069cbf4541e6', 'name': 'fakeimage123456', 'created_at': timestamp, @@ -56,7 +57,6 @@ class _FakeImageService(object): 'ramdisk_id': FLAGS.null_kernel, 'architecture': 'x86_64'}} - # NOTE(bcwaldon): was image 'fake' image2 = {'id': 'a2459075-d96c-40d5-893e-577ff92e721c', 'name': 'fakeimage123456', 'created_at': timestamp, @@ -70,7 +70,6 @@ class _FakeImageService(object): 'properties': {'kernel_id': FLAGS.null_kernel, 'ramdisk_id': FLAGS.null_kernel}} - # NOTE(bcwaldon): was image '2' image3 = {'id': '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6', 'name': 'fakeimage123456', 'created_at': timestamp, @@ -84,7 +83,6 @@ class _FakeImageService(object): 'properties': {'kernel_id': FLAGS.null_kernel, 'ramdisk_id': FLAGS.null_kernel}} - # NOTE(bcwaldon): was image '1' image4 = {'id': 'cedef40a-ed67-4d10-800e-17455edce175', 'name': 'fakeimage123456', 'created_at': timestamp, @@ -98,7 +96,6 @@ class _FakeImageService(object): 'properties': {'kernel_id': FLAGS.null_kernel, 'ramdisk_id': FLAGS.null_kernel}} - # NOTE(bcwaldon): was image '3' image5 = {'id': 'c905cedb-7281-47e4-8a62-f26bc5fc4c77', 'name': 'fakeimage123456', 'created_at': timestamp, @@ -113,7 +110,6 @@ class _FakeImageService(object): '155d900f-4e14-4e4c-a73d-069cbf4541e6', 'ramdisk_id': None}} - # NOTE(sirp): was image '6' image6 = {'id': 'a440c04b-79fa-479c-bed1-0b816eaec379', 'name': 'fakeimage6', 'created_at': timestamp, @@ -129,7 +125,6 @@ class _FakeImageService(object): 'architecture': 'x86_64', 'auto_disk_config': 'False'}} - # NOTE(sirp): was image '7' image7 = {'id': '70a599e0-31e7-49b7-b260-868f441e862b', 'name': 'fakeimage7', 'created_at': timestamp, @@ -258,3 +253,12 @@ def FakeImageService(): def FakeImageService_reset(): global _fakeImageService _fakeImageService = _FakeImageService() + + +def stub_out_image_service(stubs): + def fake_get_remote_image_service(context, image_href): + return (FakeImageService(), image_href) + stubs.Set(nova.image.glance, 'get_remote_image_service', + lambda x, y: (FakeImageService(), y)) + stubs.Set(nova.image.glance, 'get_default_image_service', + lambda: FakeImageService()) diff --git a/nova/tests/test_image.py b/nova/tests/image/test_fake.py index 6482d779b2..eb11368946 100644 --- a/nova/tests/test_image.py +++ b/nova/tests/image/test_fake.py @@ -20,15 +20,20 @@ import StringIO from nova import context from nova import exception -import nova.image +import nova.tests.image.fake from nova import test -class _ImageTestCase(test.TestCase): +class FakeImageServiceTestCase(test.TestCase): def setUp(self): - super(_ImageTestCase, self).setUp() + super(FakeImageServiceTestCase, self).setUp() + self.image_service = nova.tests.image.fake.FakeImageService() self.context = context.get_admin_context() + def tearDown(self): + super(FakeImageServiceTestCase, self).setUp() + nova.tests.image.fake.FakeImageService_reset() + def test_index(self): res = self.image_service.index(self.context) for image in res: @@ -138,9 +143,3 @@ class _ImageTestCase(test.TestCase): s2 = StringIO.StringIO() self.image_service.get(self.context, '32', data=s2) self.assertEquals(s2.getvalue(), blob, 'Did not get blob back intact') - - -class FakeImageTestCase(_ImageTestCase): - def setUp(self): - super(FakeImageTestCase, self).setUp() - self.image_service = nova.image.fake.FakeImageService() diff --git a/nova/tests/image/test_s3.py b/nova/tests/image/test_s3.py index 9f078fc497..9b4b5757f9 100644 --- a/nova/tests/image/test_s3.py +++ b/nova/tests/image/test_s3.py @@ -24,6 +24,7 @@ import tempfile from nova import context import nova.db.api from nova import exception +from nova.tests.image import fake from nova.image import s3 from nova import test @@ -81,14 +82,19 @@ file_manifest_xml = """<?xml version="1.0" ?> class TestS3ImageService(test.TestCase): def setUp(self): super(TestS3ImageService, self).setUp() - self.flags(image_service='nova.image.fake.FakeImageService') - self.image_service = s3.S3ImageService() self.context = context.RequestContext(None, None) # set up one fixture to test shows, should have id '1' nova.db.api.s3_image_create(self.context, '155d900f-4e14-4e4c-a73d-069cbf4541e6') + fake.stub_out_image_service(self.stubs) + self.image_service = s3.S3ImageService() + + def tearDown(self): + super(TestS3ImageService, self).tearDown() + fake.FakeImageService_reset() + def _assertEqualList(self, list0, list1, keys): self.assertEqual(len(list0), len(list1)) key = keys[0] @@ -183,8 +189,8 @@ class TestS3ImageService(test.TestCase): eventlet.sleep() translated = self.image_service._translate_id_to_uuid(context, img) uuid = translated['id'] - self.glance_service = nova.image.get_default_image_service() - updated_image = self.glance_service.update(self.context, uuid, + image_service = fake.FakeImageService() + updated_image = image_service.update(self.context, uuid, {'is_public': True}, None, {'x-glance-registry-purge-props': False}) self.assertTrue(updated_image['is_public']) diff --git a/nova/tests/integrated/integrated_helpers.py b/nova/tests/integrated/integrated_helpers.py index e205dfb18a..a514de6775 100644 --- a/nova/tests/integrated/integrated_helpers.py +++ b/nova/tests/integrated/integrated_helpers.py @@ -22,10 +22,10 @@ Provides common functionality for integrated unit tests import random import string -import nova.image.glance from nova.log import logging from nova import service from nova import test # For the flags +import nova.tests.image.fake from nova.tests.integrated.api import client from nova import utils @@ -65,10 +65,7 @@ class _IntegratedTestBase(test.TestCase): self.flags(**f) self.flags(verbose=True) - def fake_get_image_service(context, image_href): - image_id = str(image_href).split('/')[-1] - return (nova.image.fake.FakeImageService(), image_id) - self.stubs.Set(nova.image, 'get_image_service', fake_get_image_service) + nova.tests.image.fake.stub_out_image_service(self.stubs) self.flags(compute_scheduler_driver='nova.scheduler.' 'chance.ChanceScheduler') @@ -84,6 +81,7 @@ class _IntegratedTestBase(test.TestCase): def tearDown(self): self.osapi.stop() + nova.tests.image.fake.FakeImageService_reset() super(_IntegratedTestBase, self).tearDown() def _start_api_service(self): @@ -108,7 +106,6 @@ class _IntegratedTestBase(test.TestCase): f['osapi_volume_listen_port'] = 0 f['metadata_listen_port'] = 0 - f['image_service'] = 'nova.image.fake.FakeImageService' f['fake_network'] = True return f diff --git a/nova/tests/test_compute_utils.py b/nova/tests/test_compute_utils.py index 50c85073f8..0955134baf 100644 --- a/nova/tests/test_compute_utils.py +++ b/nova/tests/test_compute_utils.py @@ -22,12 +22,12 @@ from nova.compute import utils as compute_utils from nova import context from nova import db from nova import flags -import nova.image.fake from nova import log as logging from nova.notifier import test_notifier from nova.openstack.common import importutils from nova import test from nova.tests import fake_network +import nova.tests.image.fake from nova import utils @@ -61,7 +61,8 @@ class UsageInfoTestCase(test.TestCase): def fake_show(meh, context, id): return {'id': 1, 'properties': {'kernel_id': 1, 'ramdisk_id': 1}} - self.stubs.Set(nova.image.fake._FakeImageService, 'show', fake_show) + self.stubs.Set(nova.tests.image.fake._FakeImageService, + 'show', fake_show) def _create_instance(self, params={}): """Create a test instance""" diff --git a/nova/tests/test_libvirt.py b/nova/tests/test_libvirt.py index 75ab99383d..510bcad7c8 100644 --- a/nova/tests/test_libvirt.py +++ b/nova/tests/test_libvirt.py @@ -40,6 +40,7 @@ from nova.openstack.common import jsonutils from nova import test from nova.tests import fake_libvirt_utils from nova.tests import fake_network +import nova.tests.image.fake from nova import utils from nova.virt import driver from nova.virt import firewall as base_firewall @@ -427,8 +428,11 @@ class LibvirtConnTestCase(test.TestCase): self.stubs.Set(libvirt_driver.disk, 'extend', fake_extend) + nova.tests.image.fake.stub_out_image_service(self.stubs) + def tearDown(self): libvirt_driver.libvirt_utils = libvirt_utils + nova.tests.image.fake.FakeImageService_reset() super(LibvirtConnTestCase, self).tearDown() test_instance = {'memory_kb': '1024000', @@ -458,7 +462,6 @@ class LibvirtConnTestCase(test.TestCase): for key, val in kwargs.items(): fake.__setattr__(key, val) - self.flags(image_service='nova.image.fake.FakeImageService') self.flags(libvirt_vif_driver="nova.tests.fake_network.FakeVIFDriver") self.mox.StubOutWithMock(libvirt_driver.LibvirtDriver, '_conn') @@ -807,10 +810,8 @@ class LibvirtConnTestCase(test.TestCase): @test.skip_if(missing_libvirt(), "Test requires libvirt") def test_snapshot_in_ami_format(self): - self.flags(image_service='nova.image.fake.FakeImageService') - # Start test - image_service = importutils.import_object(FLAGS.image_service) + image_service = nova.tests.image.fake.FakeImageService() # Assign different image_ref from nova/images/fakes for testing ami test_instance = copy.deepcopy(self.test_instance) @@ -845,10 +846,8 @@ class LibvirtConnTestCase(test.TestCase): @test.skip_if(missing_libvirt(), "Test requires libvirt") def test_snapshot_in_raw_format(self): - self.flags(image_service='nova.image.fake.FakeImageService') - # Start test - image_service = importutils.import_object(FLAGS.image_service) + image_service = nova.tests.image.fake.FakeImageService() # Assuming that base image already exists in image_service instance_ref = db.instance_create(self.context, self.test_instance) @@ -879,11 +878,10 @@ class LibvirtConnTestCase(test.TestCase): @test.skip_if(missing_libvirt(), "Test requires libvirt") def test_snapshot_in_qcow2_format(self): - self.flags(image_service='nova.image.fake.FakeImageService') self.flags(snapshot_image_format='qcow2') # Start test - image_service = importutils.import_object(FLAGS.image_service) + image_service = nova.tests.image.fake.FakeImageService() # Assuming that base image already exists in image_service instance_ref = db.instance_create(self.context, self.test_instance) @@ -914,10 +912,8 @@ class LibvirtConnTestCase(test.TestCase): @test.skip_if(missing_libvirt(), "Test requires libvirt") def test_snapshot_no_image_architecture(self): - self.flags(image_service='nova.image.fake.FakeImageService') - # Start test - image_service = importutils.import_object(FLAGS.image_service) + image_service = nova.tests.image.fake.FakeImageService() # Assign different image_ref from nova/images/fakes for # testing different base image @@ -952,10 +948,8 @@ class LibvirtConnTestCase(test.TestCase): @test.skip_if(missing_libvirt(), "Test requires libvirt") def test_snapshot_no_original_image(self): - self.flags(image_service='nova.image.fake.FakeImageService') - # Start test - image_service = importutils.import_object(FLAGS.image_service) + image_service = nova.tests.image.fake.FakeImageService() # Assign a non-existent image test_instance = copy.deepcopy(self.test_instance) diff --git a/nova/tests/test_notifications.py b/nova/tests/test_notifications.py index 5ce45d7770..ff6acb1559 100644 --- a/nova/tests/test_notifications.py +++ b/nova/tests/test_notifications.py @@ -25,9 +25,9 @@ from nova.compute import vm_states from nova import context from nova import db from nova import flags -import nova.image.fake from nova import log as logging from nova import notifications +import nova.network from nova.notifier import test_notifier from nova import test from nova.tests import fake_network diff --git a/nova/tests/test_quota.py b/nova/tests/test_quota.py index 54489918bc..eefde358e0 100644 --- a/nova/tests/test_quota.py +++ b/nova/tests/test_quota.py @@ -31,6 +31,7 @@ from nova.openstack.common import timeutils from nova import quota from nova.scheduler import driver as scheduler_driver from nova import test +import nova.tests.image.fake from nova import volume @@ -39,11 +40,6 @@ FLAGS = flags.FLAGS class QuotaIntegrationTestCase(test.TestCase): - class StubImageService(object): - - def show(self, *args, **kwargs): - return {"properties": {}} - def setUp(self): super(QuotaIntegrationTestCase, self).setUp() self.flags(compute_driver='nova.virt.fake.FakeDriver', @@ -78,6 +74,11 @@ class QuotaIntegrationTestCase(test.TestCase): return orig_rpc_call(context, topic, msg) self.stubs.Set(rpc, 'call', rpc_call_wrapper) + nova.tests.image.fake.stub_out_image_service(self.stubs) + + def tearDown(self): + super(QuotaIntegrationTestCase, self).tearDown() + nova.tests.image.fake.FakeImageService_reset() def _create_instance(self, cores=2): """Create a test instance""" @@ -173,8 +174,7 @@ class QuotaIntegrationTestCase(test.TestCase): metadata=metadata) def _create_with_injected_files(self, files): - self.flags(image_service='nova.image.fake.FakeImageService') - api = compute.API(image_service=self.StubImageService()) + api = compute.API() inst_type = instance_types.get_instance_type_by_name('m1.small') image_uuid = 'cedef40a-ed67-4d10-800e-17455edce175' api.create(self.context, min_count=1, max_count=1, @@ -182,8 +182,7 @@ class QuotaIntegrationTestCase(test.TestCase): injected_files=files) def test_no_injected_files(self): - self.flags(image_service='nova.image.fake.FakeImageService') - api = compute.API(image_service=self.StubImageService()) + api = compute.API() inst_type = instance_types.get_instance_type_by_name('m1.small') image_uuid = 'cedef40a-ed67-4d10-800e-17455edce175' api.create(self.context, diff --git a/nova/tests/test_virt_drivers.py b/nova/tests/test_virt_drivers.py index 9f54089bf5..6443c67217 100644 --- a/nova/tests/test_virt_drivers.py +++ b/nova/tests/test_virt_drivers.py @@ -21,10 +21,10 @@ import traceback from nova.compute.manager import ComputeManager from nova import exception -from nova import image from nova import log as logging from nova.openstack.common import importutils from nova import test +from nova.tests.image import fake as fake_image from nova.tests import utils as test_utils LOG = logging.getLogger(__name__) @@ -102,9 +102,11 @@ class _FakeDriverBackendTestCase(test.TestCase): # TODO(sdague): it would be nice to do this in a way that only # the relevant backends where replaced for tests, though this # should not harm anything by doing it for all backends + fake_image.stub_out_image_service(self.stubs) self._setup_fakelibvirt() def tearDown(self): + fake_image.FakeImageService_reset() self._teardown_fakelibvirt() super(_FakeDriverBackendTestCase, self).tearDown() @@ -177,7 +179,7 @@ class _VirtDriverTestCase(_FakeDriverBackendTestCase): super(_VirtDriverTestCase, self).setUp() self.connection = importutils.import_object(self.driver_module, '') self.ctxt = test_utils.get_test_admin_context() - self.image_service = image.get_default_image_service() + self.image_service = fake_image.FakeImageService() def _get_running_instance(self): instance_ref = test_utils.get_test_instance() diff --git a/nova/tests/test_vmwareapi.py b/nova/tests/test_vmwareapi.py index 7c2653f84c..647a9d0a09 100644 --- a/nova/tests/test_vmwareapi.py +++ b/nova/tests/test_vmwareapi.py @@ -24,9 +24,8 @@ from nova import context from nova import db from nova import exception from nova import flags -import nova.image.fake from nova import test -import nova.tests.api.openstack.fakes as api_fakes +import nova.tests.image.fake from nova.tests.vmwareapi import db_fakes from nova.tests.vmwareapi import stubs from nova.virt.vmwareapi import fake as vmwareapi_fake @@ -78,12 +77,12 @@ class VMWareAPIVMTestCase(test.TestCase): 'disk_format': 'vhd', 'size': 512, } - api_fakes.stub_out_image_service(self.stubs) + nova.tests.image.fake.stub_out_image_service(self.stubs) def tearDown(self): super(VMWareAPIVMTestCase, self).tearDown() vmwareapi_fake.cleanup() - nova.image.fake.FakeImageService_reset() + nova.tests.image.fake.FakeImageService_reset() def _create_instance_in_the_db(self): values = {'name': 1, diff --git a/nova/tests/test_volume_utils.py b/nova/tests/test_volume_utils.py index 222f12266d..b2ccb41b89 100644 --- a/nova/tests/test_volume_utils.py +++ b/nova/tests/test_volume_utils.py @@ -20,7 +20,6 @@ from nova import context from nova import db from nova import flags -import nova.image.fake from nova import log as logging from nova.notifier import test_notifier from nova.openstack.common import importutils @@ -39,7 +38,7 @@ class UsageInfoTestCase(test.TestCase): self.flags(compute_driver='nova.virt.fake.FakeDriver', stub_network=True, host='fake') - self.stubs.Set(nova.flags.FLAGS, 'notification_driver', + self.stubs.Set(flags.FLAGS, 'notification_driver', 'nova.notifier.test_notifier') self.volume = importutils.import_object(FLAGS.volume_manager) self.user_id = 'fake' diff --git a/nova/tests/test_xenapi.py b/nova/tests/test_xenapi.py index bdef5c6d40..ca3fb3263e 100644 --- a/nova/tests/test_xenapi.py +++ b/nova/tests/test_xenapi.py @@ -29,7 +29,6 @@ from nova import context from nova import db from nova import exception from nova import flags -import nova.image.fake from nova import log as logging from nova.openstack.common import importutils from nova.openstack.common import timeutils @@ -38,6 +37,7 @@ import nova.tests.api.openstack.fakes as api_fakes from nova.tests.db import fakes as db_fakes from nova.tests import fake_network from nova.tests import fake_utils +import nova.tests.image.fake from nova.tests.xenapi import stubs from nova.virt.xenapi import connection as xenapi_conn from nova.virt.xenapi import fake as xenapi_fake @@ -92,7 +92,7 @@ IMAGE_FIXTURES = { def set_image_fixtures(): - image_service = nova.image.fake.FakeImageService() + image_service = nova.tests.image.fake.FakeImageService() image_service.delete_all() for image_id, image_meta in IMAGE_FIXTURES.items(): image_meta = image_meta['image_meta'] @@ -113,23 +113,23 @@ def stub_vm_utils_with_vdi_attached_here(function, should_return=True): fake_dev = 'fakedev' yield fake_dev - def fake_image_service_get(*args, **kwargs): + def fake_image_get(*args, **kwargs): pass def fake_is_vdi_pv(*args, **kwargs): return should_return orig_vdi_attached_here = vm_utils.vdi_attached_here - orig_image_service_get = nova.image.fake._FakeImageService.get + orig_image_get = nova.tests.image.fake._FakeImageService.get orig_is_vdi_pv = vm_utils._is_vdi_pv try: vm_utils.vdi_attached_here = fake_vdi_attached_here - nova.image.fake._FakeImageService.get = fake_image_service_get + nova.tests.image.fake._FakeImageService.get = fake_image_get vm_utils._is_vdi_pv = fake_is_vdi_pv return function(self, *args, **kwargs) finally: vm_utils._is_vdi_pv = orig_is_vdi_pv - nova.image.fake._FakeImageService.get = orig_image_service_get + nova.tests.image.fake._FakeImageService.get = orig_image_get vm_utils.vdi_attached_here = orig_vdi_attached_here return decorated_function @@ -278,14 +278,14 @@ class XenAPIVMTestCase(test.TestCase): self.context = context.RequestContext(self.user_id, self.project_id) self.conn = xenapi_conn.XenAPIDriver(False) - api_fakes.stub_out_image_service(self.stubs) + nova.tests.image.fake.stub_out_image_service(self.stubs) set_image_fixtures() stubs.stubout_image_service_get(self.stubs) stubs.stubout_stream_disk(self.stubs) def tearDown(self): super(XenAPIVMTestCase, self).tearDown() - nova.image.fake.FakeImageService_reset() + nova.tests.image.fake.FakeImageService_reset() def test_init_host(self): session = xenapi_conn.XenAPISession('test_url', 'root', 'test_pass') diff --git a/nova/tests/utils.py b/nova/tests/utils.py index 17fa2a049b..46feca99e6 100644 --- a/nova/tests/utils.py +++ b/nova/tests/utils.py @@ -17,6 +17,7 @@ import nova.context import nova.db import nova.flags +from nova.image import glance FLAGS = nova.flags.FLAGS @@ -30,7 +31,8 @@ def get_test_image_info(context, instance_ref): context = get_test_admin_context() image_ref = instance_ref['image_ref'] - image_service, image_id = nova.image.get_image_service(context, image_ref) + image_service, image_id = glance.get_remote_image_service(context, + image_ref) return image_service.show(context, image_id) diff --git a/nova/tests/xenapi/stubs.py b/nova/tests/xenapi/stubs.py index 7f32e96fbc..0116c4d99a 100644 --- a/nova/tests/xenapi/stubs.py +++ b/nova/tests/xenapi/stubs.py @@ -19,8 +19,8 @@ import random from eventlet import tpool -import nova.image.fake from nova.openstack.common import jsonutils +import nova.tests.image.fake from nova.virt.xenapi import connection as xenapi_conn from nova.virt.xenapi import fake from nova.virt.xenapi import vm_utils @@ -85,7 +85,7 @@ def stubout_get_this_vm_uuid(stubs): def stubout_image_service_get(stubs): def fake_get(*args, **kwargs): pass - stubs.Set(nova.image.fake._FakeImageService, 'get', fake_get) + stubs.Set(nova.tests.image.fake._FakeImageService, 'get', fake_get) def stubout_stream_disk(stubs): diff --git a/nova/virt/images.py b/nova/virt/images.py index e89aed627f..ffb2e8e367 100644 --- a/nova/virt/images.py +++ b/nova/virt/images.py @@ -25,7 +25,7 @@ import os from nova import exception from nova import flags -import nova.image +from nova.image import glance from nova import log as logging from nova.openstack.common import cfg from nova import utils @@ -48,8 +48,8 @@ def fetch(context, image_href, path, _user_id, _project_id): # when it is added to glance. Right now there is no # auth checking in glance, so we assume that access was # checked before we got here. - (image_service, image_id) = nova.image.get_image_service(context, - image_href) + (image_service, image_id) = glance.get_remote_image_service(context, + image_href) with utils.remove_path_on_error(path): with open(path, "wb") as image_file: metadata = image_service.get(context, image_id, image_file) diff --git a/nova/virt/libvirt/driver.py b/nova/virt/libvirt/driver.py index 4f81ed7b8c..d0000186bc 100644 --- a/nova/virt/libvirt/driver.py +++ b/nova/virt/libvirt/driver.py @@ -60,7 +60,7 @@ from nova import context as nova_context from nova import db from nova import exception from nova import flags -import nova.image +from nova.image import glance from nova import log as logging from nova.openstack.common import cfg from nova.openstack.common import excutils @@ -698,14 +698,14 @@ class LibvirtDriver(driver.ComputeDriver): except exception.InstanceNotFound: raise exception.InstanceNotRunning() - (image_service, image_id) = nova.image.get_image_service( + (image_service, image_id) = glance.get_remote_image_service( context, instance['image_ref']) try: base = image_service.show(context, image_id) except exception.ImageNotFound: base = {} - _image_service = nova.image.get_image_service(context, image_href) + _image_service = glance.get_remote_image_service(context, image_href) snapshot_image_service, snapshot_image_id = _image_service snapshot = snapshot_image_service.show(context, snapshot_image_id) diff --git a/nova/virt/xenapi/vm_utils.py b/nova/virt/xenapi/vm_utils.py index 64a479fe1f..76958c6d0f 100644 --- a/nova/virt/xenapi/vm_utils.py +++ b/nova/virt/xenapi/vm_utils.py @@ -39,7 +39,6 @@ from nova.compute import power_state from nova import db from nova import exception from nova import flags -import nova.image from nova.image import glance from nova import log as logging from nova.openstack.common import cfg @@ -869,7 +868,8 @@ def _fetch_image_glance_disk(context, session, instance, image_id, image_type): else: sr_ref = safe_find_sr(session) - image_service, image_id = nova.image.get_image_service(context, image_id) + image_service, image_id = glance.get_remote_image_service( + context, image_id) meta = image_service.show(context, image_id) virtual_size = int(meta['size']) vdi_size = virtual_size |