From 19ef3655b1b32c64d35892d63c7699b0a462994a Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Tue, 11 Aug 2020 17:19:59 +0100 Subject: [PATCH] tests: Remove '_FakeImageService' Migrate unit tests over to the new GlanceFixture, allowing us to fully delete '_FakeImageService' and various helpers. Change-Id: I3e3d9bbd32aa2fe0777681f21ba827610d5b3040 Signed-off-by: Stephen Finucane --- .../api/openstack/compute/test_access_ips.py | 4 +- .../compute/test_availability_zone.py | 4 +- .../api/openstack/compute/test_disk_config.py | 9 +- .../openstack/compute/test_server_actions.py | 8 +- .../api/openstack/compute/test_servers.py | 17 +- .../unit/api/openstack/compute/test_urlmap.py | 8 +- nova/tests/unit/compute/test_api.py | 11 +- nova/tests/unit/compute/test_compute.py | 89 ++---- nova/tests/unit/compute/test_compute_utils.py | 5 - nova/tests/unit/compute/test_shelve.py | 7 +- nova/tests/unit/image/fake.py | 301 ------------------ nova/tests/unit/image/test_fake.py | 119 ------- nova/tests/unit/test_quota.py | 8 +- .../tests/unit/virt/libvirt/test_blockinfo.py | 4 +- nova/tests/unit/virt/libvirt/test_driver.py | 7 +- nova/tests/unit/virt/test_virt_drivers.py | 6 +- .../unit/virt/vmwareapi/test_configdrive.py | 7 +- .../unit/virt/vmwareapi/test_driver_api.py | 8 +- nova/tests/unit/virt/vmwareapi/test_images.py | 27 +- nova/tests/unit/virt/vmwareapi/test_vmops.py | 46 ++- .../unit/virt/vmwareapi/test_volumeops.py | 7 +- 21 files changed, 110 insertions(+), 592 deletions(-) delete mode 100644 nova/tests/unit/image/test_fake.py diff --git a/nova/tests/unit/api/openstack/compute/test_access_ips.py b/nova/tests/unit/api/openstack/compute/test_access_ips.py index 15b42bf9c782..57fc1428282e 100644 --- a/nova/tests/unit/api/openstack/compute/test_access_ips.py +++ b/nova/tests/unit/api/openstack/compute/test_access_ips.py @@ -17,8 +17,8 @@ from oslo_serialization import jsonutils from nova.api.openstack.compute import servers as servers_v21 from nova import exception from nova import test +from nova.tests import fixtures from nova.tests.unit.api.openstack import fakes -from nova.tests.unit.image import fake v4_key = "accessIPv4" v6_key = "accessIPv6" @@ -38,7 +38,7 @@ class AccessIPsAPIValidationTestV21(test.TestCase): fakes.stub_out_nw_api(self) self._set_up_controller() - fake.stub_out_image_service(self) + self.useFixture(fixtures.GlanceFixture(self)) self.stub_out('nova.compute.api.API.get', # This project_id matches fakes.HTTPRequest.blank. fakes.fake_compute_get(project_id=fakes.FAKE_PROJECT_ID)) diff --git a/nova/tests/unit/api/openstack/compute/test_availability_zone.py b/nova/tests/unit/api/openstack/compute/test_availability_zone.py index da49f699e116..9f2471d19dfd 100644 --- a/nova/tests/unit/api/openstack/compute/test_availability_zone.py +++ b/nova/tests/unit/api/openstack/compute/test_availability_zone.py @@ -28,8 +28,8 @@ from nova.db import api as db from nova import exception from nova import objects from nova import test +from nova.tests import fixtures from nova.tests.unit.api.openstack import fakes -from nova.tests.unit.image import fake from nova.tests.unit import matchers from nova.tests.unit.objects import test_service @@ -208,7 +208,7 @@ class ServersControllerCreateTestV21(test.TestCase): instance.uuid = FAKE_UUID return instance - fake.stub_out_image_service(self) + self.useFixture(fixtures.GlanceFixture(self)) self.stub_out('nova.compute.api.API.create_db_entry_for_new_instance', create_db_entry_for_new_instance) diff --git a/nova/tests/unit/api/openstack/compute/test_disk_config.py b/nova/tests/unit/api/openstack/compute/test_disk_config.py index 8f764d2b1795..ccbc29187e7c 100644 --- a/nova/tests/unit/api/openstack/compute/test_disk_config.py +++ b/nova/tests/unit/api/openstack/compute/test_disk_config.py @@ -24,9 +24,9 @@ from nova import context as nova_context from nova import exception from nova import objects from nova import test +from nova.tests import fixtures from nova.tests.unit.api.openstack import fakes from nova.tests.unit import fake_instance -import nova.tests.unit.image.fake MANUAL_INSTANCE_UUID = fakes.FAKE_UUID @@ -100,8 +100,7 @@ class DiskConfigTestCaseV21(test.TestCase): ' not one of [\'AUTO\', \'MANUAL\']", "code": 400}}}}') def _setup_fake_image_service(self): - self.image_service = nova.tests.unit.image.fake.stub_out_image_service( - self) + self.image_service = self.useFixture(fixtures.GlanceFixture(self)) timestamp = datetime.datetime(2011, 1, 1, 1, 2, 3) image = {'id': '88580842-f50a-11e2-8d3a-f23c91aec05e', 'name': 'fakeimage7', @@ -117,10 +116,6 @@ class DiskConfigTestCaseV21(test.TestCase): 'properties': {'auto_disk_config': 'Disabled'}} self.image_service.create(None, image) - def tearDown(self): - super(DiskConfigTestCaseV21, self).tearDown() - nova.tests.unit.image.fake.FakeImageService_reset() - def assertDiskConfig(self, dict_, value): self.assertIn(API_DISK_CONFIG, dict_) self.assertEqual(dict_[API_DISK_CONFIG], value) diff --git a/nova/tests/unit/api/openstack/compute/test_server_actions.py b/nova/tests/unit/api/openstack/compute/test_server_actions.py index 08f698bbe36d..a5e7caecdb32 100644 --- a/nova/tests/unit/api/openstack/compute/test_server_actions.py +++ b/nova/tests/unit/api/openstack/compute/test_server_actions.py @@ -29,10 +29,10 @@ from nova import exception from nova.image import glance from nova import objects from nova import test +from nova.tests import fixtures as nova_fixtures from nova.tests.unit.api.openstack import fakes from nova.tests.unit import fake_block_device from nova.tests.unit import fake_instance -from nova.tests.unit.image import fake CONF = nova.conf.CONF @@ -69,7 +69,7 @@ class ServerActionsControllerTestV21(test.TestCase): self.stub_out('nova.objects.Instance.save', lambda *a, **kw: None) fakes.stub_out_compute_api_snapshot(self) - fake.stub_out_image_service(self) + self.useFixture(nova_fixtures.GlanceFixture(self)) self.flags(enable_instance_password=True, group='api') # TODO(stephenfin): Use uuidsentinel instead of this self._image_href = '155d900f-4e14-4e4c-a73d-069cbf4541e6' @@ -591,7 +591,7 @@ class ServerActionsControllerTestV21(test.TestCase): return image_meta - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', + self.stub_out('nova.tests.fixtures.GlanceFixture.show', return_image_meta) body = { "rebuild": { @@ -643,7 +643,7 @@ class ServerActionsControllerTestV21(test.TestCase): return image_meta - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', + self.stub_out('nova.tests.fixtures.GlanceFixture.show', return_image_meta) self.stub_out('nova.compute.api.API.get', wrap_get) self.stub_out('nova.objects.Instance.save', fake_save) diff --git a/nova/tests/unit/api/openstack/compute/test_servers.py b/nova/tests/unit/api/openstack/compute/test_servers.py index bb8b4915db42..a13ad1515340 100644 --- a/nova/tests/unit/api/openstack/compute/test_servers.py +++ b/nova/tests/unit/api/openstack/compute/test_servers.py @@ -70,7 +70,6 @@ from nova.tests.unit.api.openstack import fakes from nova.tests.unit import fake_block_device from nova.tests.unit import fake_flavor from nova.tests.unit import fake_instance -from nova.tests.unit.image import fake from nova.tests.unit import matchers from nova import utils as nova_utils @@ -228,7 +227,7 @@ class ControllerTest(test.TestCase): super(ControllerTest, self).setUp() fakes.stub_out_nw_api(self) fakes.stub_out_key_pair_funcs(self) - fake.stub_out_image_service(self) + self.useFixture(nova_fixtures.GlanceFixture(self)) fakes.stub_out_secgroup_api( self, security_groups=[{'name': 'default'}]) return_server = fakes.fake_compute_get(id=2, availability_zone='nova', @@ -3066,7 +3065,7 @@ class ServersControllerRebuildInstanceTest(ControllerTest): self.controller._action_rebuild, self.req, FAKE_UUID, body=self.body) - @mock.patch.object(fake._FakeImageService, 'show', + @mock.patch.object(nova_fixtures.GlanceFixture, 'show', return_value=dict( id='76fa36fc-c930-4bf3-8c8a-ea2a2420deb6', name='public image', is_public=True, @@ -3082,7 +3081,7 @@ class ServersControllerRebuildInstanceTest(ControllerTest): self.req.environ['nova.context'], self.image_uuid, include_locations=False, show_deleted=True) - @mock.patch.object(fake._FakeImageService, 'show', + @mock.patch.object(nova_fixtures.GlanceFixture, 'show', return_value=dict( id='76fa36fc-c930-4bf3-8c8a-ea2a2420deb6', name='public image', is_public=True, @@ -3098,7 +3097,7 @@ class ServersControllerRebuildInstanceTest(ControllerTest): self.req.environ['nova.context'], self.image_uuid, include_locations=False, show_deleted=True) - @mock.patch.object(fake._FakeImageService, 'show', + @mock.patch.object(nova_fixtures.GlanceFixture, 'show', return_value=dict( id='76fa36fc-c930-4bf3-8c8a-ea2a2420deb6', name='public image', is_public=True, @@ -3120,7 +3119,7 @@ class ServersControllerRebuildInstanceTest(ControllerTest): self.controller._action_rebuild, self.req, FAKE_UUID, body=self.body) - @mock.patch.object(fake._FakeImageService, 'show', + @mock.patch.object(nova_fixtures.GlanceFixture, 'show', return_value=dict( id='76fa36fc-c930-4bf3-8c8a-ea2a2420deb6', name='public image', is_public=True, @@ -3140,7 +3139,7 @@ class ServersControllerRebuildInstanceTest(ControllerTest): name='public image', is_public=True, status='active') with test.nested( - mock.patch.object(fake._FakeImageService, 'show', + mock.patch.object(nova_fixtures.GlanceFixture, 'show', side_effect=fake_get_image), mock.patch.object(self.controller.compute_api, 'rebuild', side_effect=exception.OnsetFileLimitExceeded) @@ -4251,7 +4250,7 @@ class ServersControllerCreateTest(test.TestCase): return (inst, inst) fakes.stub_out_key_pair_funcs(self) - fake.stub_out_image_service(self) + self.useFixture(nova_fixtures.GlanceFixture(self)) self.stub_out('nova.db.api.instance_create', instance_create) self.stub_out('nova.db.api.instance_system_metadata_update', lambda *a, **kw: None) @@ -4384,7 +4383,7 @@ class ServersControllerCreateTest(test.TestCase): "Flavor's disk is too small for requested image."): self.controller.create(self.req, body=self.body) - @mock.patch.object(fake._FakeImageService, 'show', + @mock.patch.object(nova_fixtures.GlanceFixture, 'show', return_value=dict( id='76fa36fc-c930-4bf3-8c8a-ea2a2420deb6', status='active', diff --git a/nova/tests/unit/api/openstack/compute/test_urlmap.py b/nova/tests/unit/api/openstack/compute/test_urlmap.py index 44e2d1907940..4ab837713a14 100644 --- a/nova/tests/unit/api/openstack/compute/test_urlmap.py +++ b/nova/tests/unit/api/openstack/compute/test_urlmap.py @@ -16,18 +16,14 @@ from oslo_serialization import jsonutils from nova import test +from nova.tests import fixtures from nova.tests.unit.api.openstack import fakes -import nova.tests.unit.image.fake class UrlmapTest(test.NoDBTestCase): def setUp(self): super(UrlmapTest, self).setUp() - nova.tests.unit.image.fake.stub_out_image_service(self) - - def tearDown(self): - super(UrlmapTest, self).tearDown() - nova.tests.unit.image.fake.FakeImageService_reset() + self.useFixture(fixtures.GlanceFixture(self)) def test_path_version_v2(self): # Test URL path specifying v2 returns v2 content. diff --git a/nova/tests/unit/compute/test_api.py b/nova/tests/unit/compute/test_api.py index de56d94b368a..88dd94ac703f 100644 --- a/nova/tests/unit/compute/test_api.py +++ b/nova/tests/unit/compute/test_api.py @@ -61,7 +61,6 @@ from nova.tests.unit import fake_build_request from nova.tests.unit import fake_instance from nova.tests.unit import fake_request_spec from nova.tests.unit import fake_volume -from nova.tests.unit.image import fake as fake_image from nova.tests.unit import matchers from nova.tests.unit.objects import test_flavor from nova.tests.unit.objects import test_migration @@ -3263,7 +3262,7 @@ class _ComputeAPIUnitTestMixIn(object): self.useFixture(fixtures.MockPatchObject( self.compute_api.compute_rpcapi, 'unquiesce_instance', side_effect=fake_unquiesce_instance)) - fake_image.stub_out_image_service(self) + self.useFixture(nova_fixtures.GlanceFixture(self)) with test.nested( mock.patch.object(compute_api.API, '_record_action_start'), @@ -3636,8 +3635,8 @@ class _ComputeAPIUnitTestMixIn(object): 'properties': {"auto_disk_config": "Disabled"}, } - fake_image.stub_out_image_service(self) - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', + self.useFixture(nova_fixtures.GlanceFixture(self)) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', lambda obj, context, image_id, **kwargs: self.fake_image) return self.fake_image['id'] @@ -3649,8 +3648,8 @@ class _ComputeAPIUnitTestMixIn(object): 'properties': {"hw_architecture": "arm64"}, } - fake_image.stub_out_image_service(self) - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', + self.useFixture(nova_fixtures.GlanceFixture(self)) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', lambda obj, context, image_id, **kwargs: self.fake_image) return self.fake_image['id'] diff --git a/nova/tests/unit/compute/test_compute.py b/nova/tests/unit/compute/test_compute.py index 81bb05e6b239..fadf14f7feeb 100644 --- a/nova/tests/unit/compute/test_compute.py +++ b/nova/tests/unit/compute/test_compute.py @@ -79,7 +79,6 @@ from nova.tests.unit import fake_network from nova.tests.unit import fake_network_cache_model from nova.tests.unit import fake_notifier from nova.tests.unit import fake_server_actions -from nova.tests.unit.image import fake as fake_image from nova.tests.unit import matchers from nova.tests.unit.objects import test_diagnostics from nova.tests.unit.objects import test_flavor @@ -231,9 +230,8 @@ class BaseTestCase(test.TestCase): else: raise exception.ImageNotFound(image_id=id) - fake_image.stub_out_image_service(self) - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', - fake_show) + self.useFixture(fixtures.GlanceFixture(self)) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', fake_show) fake_network.set_stub_network_methods(self) fake_server_actions.stub_out_action_events(self) @@ -266,7 +264,6 @@ class BaseTestCase(test.TestCase): def tearDown(self): ctxt = context.get_admin_context() - fake_image.FakeImageService_reset() instances = db.instance_get_all(ctxt) for instance in instances: db.instance_destroy(ctxt, instance['uuid']) @@ -2184,9 +2181,8 @@ class ComputeTestCase(BaseTestCase, def fake_delete(self_, ctxt, image_id): self.deleted_image_id = image_id - fake_image.stub_out_image_service(self) - self.stub_out('nova.tests.unit.image.fake._FakeImageService.delete', - fake_delete) + self.useFixture(fixtures.GlanceFixture(self)) + self.stub_out('nova.tests.fixtures.GlanceFixture.delete', fake_delete) instance = self._create_fake_instance_obj() image = {'id': uuids.image} @@ -3429,9 +3425,8 @@ class ComputeTestCase(BaseTestCase, raise Exception() self.stub_out('nova.virt.fake.FakeDriver.snapshot', fake_snapshot) - fake_image.stub_out_image_service(self) - self.stub_out('nova.tests.unit.image.fake._FakeImageService.delete', - fake_delete) + self.useFixture(fixtures.GlanceFixture(self)) + self.stub_out('nova.tests.fixtures.GlanceFixture.delete', fake_delete) inst_obj = self._get_snapshotting_instance() with mock.patch.object(compute_utils, @@ -3494,22 +3489,20 @@ class ComputeTestCase(BaseTestCase, 'status': status} return image - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', - fake_show) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', fake_show) def fake_delete(self_, context, image_id): self.fake_image_delete_called = True self.assertEqual(uuids.snapshot, image_id) - self.stub_out('nova.tests.unit.image.fake._FakeImageService.delete', - fake_delete) + self.stub_out('nova.tests.fixtures.GlanceFixture.delete', fake_delete) def fake_snapshot(*args, **kwargs): raise exc self.stub_out('nova.virt.fake.FakeDriver.snapshot', fake_snapshot) - fake_image.stub_out_image_service(self) + self.useFixture(fixtures.GlanceFixture(self)) inst_obj = self._get_snapshotting_instance() @@ -8563,8 +8556,7 @@ class ComputeAPITestCase(BaseTestCase): inst_type['memory_mb'] = 1 self.fake_image['min_ram'] = 2 - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', - self.fake_show) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', self.fake_show) self.assertRaises(exception.FlavorMemoryTooSmall, self.compute_api.create, self.context, @@ -8582,8 +8574,7 @@ class ComputeAPITestCase(BaseTestCase): inst_type['root_gb'] = 1 self.fake_image['min_disk'] = 2 - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', - self.fake_show) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', self.fake_show) self.assertRaises(exception.FlavorDiskSmallerThanMinDisk, self.compute_api.create, self.context, @@ -8602,8 +8593,7 @@ class ComputeAPITestCase(BaseTestCase): self.fake_image['size'] = '1073741825' - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', - self.fake_show) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', self.fake_show) self.assertRaises(exception.FlavorDiskSmallerThanImage, self.compute_api.create, self.context, @@ -8624,8 +8614,7 @@ class ComputeAPITestCase(BaseTestCase): self.fake_image['min_ram'] = 2 self.fake_image['min_disk'] = 2 self.fake_image['name'] = 'fake_name' - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', - self.fake_show) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', self.fake_show) (refs, resv_id) = self.compute_api.create(self.context, inst_type, self.fake_image['id']) @@ -8637,8 +8626,7 @@ class ComputeAPITestCase(BaseTestCase): inst_type['root_gb'] = 1 inst_type['memory_mb'] = 1 - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', - self.fake_show) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', self.fake_show) (refs, resv_id) = self.compute_api.create(self.context, inst_type, self.fake_image['id']) @@ -8648,8 +8636,7 @@ class ComputeAPITestCase(BaseTestCase): # build from it self.fake_image['name'] = 'fake_name' self.fake_image['status'] = 'DELETED' - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', - self.fake_show) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', self.fake_show) expected_message = ( exception.ImageNotActive.msg_fmt % {'image_id': @@ -8768,8 +8755,7 @@ class ComputeAPITestCase(BaseTestCase): # Test an instance type with malformed user data. self.fake_image['min_ram'] = 2 - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', - self.fake_show) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', self.fake_show) self.assertRaises(exception.InstanceUserDataMalformed, self.compute_api.create, self.context, self.default_flavor, @@ -8778,8 +8764,7 @@ class ComputeAPITestCase(BaseTestCase): def test_create_with_base64_user_data(self): # Test an instance type with ok much user data. self.fake_image['min_ram'] = 2 - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', - self.fake_show) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', self.fake_show) # NOTE(mikal): a string of length 48510 encodes to 65532 characters of # base64 @@ -8866,8 +8851,7 @@ class ComputeAPITestCase(BaseTestCase): @mock.patch('nova.compute.api.API._get_requested_instance_group') def test_instance_create_adds_to_instance_group(self, get_group_mock): - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', - self.fake_show) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', self.fake_show) group = objects.InstanceGroup(self.context) group.uuid = uuids.fake @@ -8888,8 +8872,7 @@ class ComputeAPITestCase(BaseTestCase): @mock.patch('nova.compute.api.API._get_requested_instance_group') def test_create_instance_group_members_over_quota_during_recheck( self, get_group_mock, check_deltas_mock): - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', - self.fake_show) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', self.fake_show) # Simulate a race where the first check passes and the recheck fails. # The first call is for checking instances/cores/ram, second and third @@ -8927,8 +8910,7 @@ class ComputeAPITestCase(BaseTestCase): def test_create_instance_group_members_no_quota_recheck(self, get_group_mock, check_deltas_mock): - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', - self.fake_show) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', self.fake_show) # Disable recheck_quota. self.flags(recheck_quota=False, group='quota') @@ -8963,8 +8945,7 @@ class ComputeAPITestCase(BaseTestCase): self.assertIn(refs[0]['uuid'], group.members) def test_instance_create_with_group_uuid_fails_group_not_exist(self): - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', - self.fake_show) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', self.fake_show) self.assertRaises( exception.InstanceGroupNotFound, @@ -9040,8 +9021,7 @@ class ComputeAPITestCase(BaseTestCase): def test_rebuild_in_error_not_launched(self): instance = self._create_fake_instance_obj(params={'image_ref': ''}) - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', - self.fake_show) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', self.fake_show) self.compute.build_and_run_instance(self.context, instance, {}, {}, {}, block_device_mapping=[]) @@ -9063,8 +9043,7 @@ class ComputeAPITestCase(BaseTestCase): without an image_ref (volume-backed instance). """ instance = self._create_fake_instance_obj(params={'image_ref': ''}) - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', - self.fake_show) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', self.fake_show) # The API request schema validates that a UUID is passed for the # imageRef parameter so we need to provide an image. ex = self.assertRaises(exception.NovaException, @@ -9081,8 +9060,7 @@ class ComputeAPITestCase(BaseTestCase): params={'image_ref': FAKE_IMAGE_REF}) self.fake_image['name'] = 'fake_name' self.fake_image['status'] = 'DELETED' - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', - self.fake_show) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', self.fake_show) expected_message = ( exception.ImageNotActive.msg_fmt % {'image_id': @@ -9100,7 +9078,7 @@ class ComputeAPITestCase(BaseTestCase): instance.flavor.root_gb = 1 self.fake_image['min_ram'] = 128 - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', + self.stub_out('nova.tests.fixtures.GlanceFixture.show', self.fake_show) self.assertRaises(exception.FlavorMemoryTooSmall, @@ -9131,8 +9109,7 @@ class ComputeAPITestCase(BaseTestCase): fake_extract_flavor) self.fake_image['min_disk'] = 2 - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', - self.fake_show) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', self.fake_show) self.assertRaises(exception.FlavorDiskSmallerThanMinDisk, self.compute_api.rebuild, self.context, @@ -9163,8 +9140,7 @@ class ComputeAPITestCase(BaseTestCase): self.fake_image['min_ram'] = 64 self.fake_image['min_disk'] = 1 - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', - self.fake_show) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', self.fake_show) self.compute_api.rebuild(self.context, instance, self.fake_image['id'], 'new_password') @@ -9185,8 +9161,7 @@ class ComputeAPITestCase(BaseTestCase): self.stub_out('nova.compute.flavors.extract_flavor', fake_extract_flavor) - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', - self.fake_show) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', self.fake_show) self.compute_api.rebuild(self.context, instance, self.fake_image['id'], 'new_password') @@ -9209,8 +9184,7 @@ class ComputeAPITestCase(BaseTestCase): fake_extract_flavor) self.fake_image['size'] = '1073741825' - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', - self.fake_show) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', self.fake_show) self.assertRaises(exception.FlavorDiskSmallerThanImage, self.compute_api.rebuild, self.context, @@ -13047,9 +13021,8 @@ class ComputeInactiveImageTestCase(BaseTestCase): 'ramdisk_id': uuids.ramdisk_id, 'something_else': 'meow'}} - fake_image.stub_out_image_service(self) - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', - fake_show) + self.useFixture(fixtures.GlanceFixture(self)) + self.stub_out('nova.tests.fixtures.GlanceFixture.show', fake_show) self.compute_api = compute.API() def test_create_instance_with_deleted_image(self): diff --git a/nova/tests/unit/compute/test_compute_utils.py b/nova/tests/unit/compute/test_compute_utils.py index b9a8bf1d494e..5207002ab112 100644 --- a/nova/tests/unit/compute/test_compute_utils.py +++ b/nova/tests/unit/compute/test_compute_utils.py @@ -371,12 +371,7 @@ class UsageInfoTestCase(test.TestCase): self.context = context.RequestContext(self.user_id, self.project_id) self.flavor = objects.Flavor.get_by_name(self.context, 'm1.tiny') - def fake_show(meh, context, id, **kwargs): - return {'id': 1, 'properties': {'kernel_id': 1, 'ramdisk_id': 1}} - self.flags(group='glance', api_servers=['http://localhost:9292']) - self.stub_out('nova.tests.unit.image.fake._FakeImageService.show', - fake_show) fake_network.set_stub_network_methods(self) fake_server_actions.stub_out_action_events(self) diff --git a/nova/tests/unit/compute/test_shelve.py b/nova/tests/unit/compute/test_shelve.py index 0623f9751487..fba5a8876ea6 100644 --- a/nova/tests/unit/compute/test_shelve.py +++ b/nova/tests/unit/compute/test_shelve.py @@ -28,8 +28,8 @@ from nova import exception from nova.network import neutron as neutron_api from nova import objects from nova import test +from nova.tests import fixtures from nova.tests.unit.compute import test_compute -from nova.tests.unit.image import fake as fake_image CONF = nova.conf.CONF @@ -330,9 +330,8 @@ class ShelveComputeManagerTestCase(test_compute.BaseTestCase): else: self.fail('Unexpected save!') - fake_image.stub_out_image_service(self) - self.stub_out('nova.tests.unit.image.fake._FakeImageService.delete', - fake_delete) + self.useFixture(fixtures.GlanceFixture(self)) + self.stub_out('nova.tests.fixtures.GlanceFixture.delete', fake_delete) with mock.patch.object(self.rt, 'instance_claim', side_effect=fake_claim), \ diff --git a/nova/tests/unit/image/fake.py b/nova/tests/unit/image/fake.py index aa6cabee8793..63ebc287de9e 100644 --- a/nova/tests/unit/image/fake.py +++ b/nova/tests/unit/image/fake.py @@ -16,314 +16,13 @@ """Implementation of a fake image service.""" -import copy -import datetime - from oslo_log import log as logging -from oslo_utils import uuidutils import nova.conf -from nova import exception from nova import objects -from nova.objects import fields as obj_fields -from nova.tests import fixtures as nova_fixtures CONF = nova.conf.CONF LOG = logging.getLogger(__name__) -AUTO_DISK_CONFIG_ENABLED_IMAGE_UUID = '70a599e0-31e7-49b7-b260-868f441e862b' - - -class _FakeImageService(object): - """Mock (fake) image service for unit testing.""" - - def __init__(self): - self.images = {} - # NOTE(justinsb): The OpenStack API can't upload an image? - # So, make sure we've got one.. - timestamp = datetime.datetime(2011, 1, 1, 1, 2, 3) - - image1 = {'id': '155d900f-4e14-4e4c-a73d-069cbf4541e6', - 'name': 'fakeimage123456', - 'created_at': timestamp, - 'updated_at': timestamp, - 'deleted_at': None, - 'deleted': False, - 'status': 'active', - 'is_public': False, - 'container_format': 'raw', - 'disk_format': 'raw', - 'size': '25165824', - 'min_ram': 0, - 'min_disk': 0, - 'protected': False, - 'visibility': 'public', - 'tags': ['tag1', 'tag2'], - 'properties': { - 'kernel_id': 'nokernel', - 'ramdisk_id': 'nokernel', - 'architecture': obj_fields.Architecture.X86_64}} - - image2 = {'id': 'a2459075-d96c-40d5-893e-577ff92e721c', - 'name': 'fakeimage123456', - 'created_at': timestamp, - 'updated_at': timestamp, - 'deleted_at': None, - 'deleted': False, - 'status': 'active', - 'is_public': True, - 'container_format': 'ami', - 'disk_format': 'ami', - 'size': '58145823', - 'min_ram': 0, - 'min_disk': 0, - 'protected': False, - 'visibility': 'public', - 'tags': [], - 'properties': {'kernel_id': 'nokernel', - 'ramdisk_id': 'nokernel'}} - - image3 = {'id': '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6', - 'name': 'fakeimage123456', - 'created_at': timestamp, - 'updated_at': timestamp, - 'deleted_at': None, - 'deleted': False, - 'status': 'active', - 'is_public': True, - 'container_format': 'bare', - 'disk_format': 'raw', - 'size': '83594576', - 'min_ram': 0, - 'min_disk': 0, - 'protected': False, - 'visibility': 'public', - 'tags': ['tag3', 'tag4'], - 'properties': { - 'kernel_id': 'nokernel', - 'ramdisk_id': 'nokernel', - 'architecture': obj_fields.Architecture.X86_64}} - - image4 = {'id': 'cedef40a-ed67-4d10-800e-17455edce175', - 'name': 'fakeimage123456', - 'created_at': timestamp, - 'updated_at': timestamp, - 'deleted_at': None, - 'deleted': False, - 'status': 'active', - 'is_public': True, - 'container_format': 'ami', - 'disk_format': 'ami', - 'size': '84035174', - 'min_ram': 0, - 'min_disk': 0, - 'protected': False, - 'visibility': 'public', - 'tags': [], - 'properties': {'kernel_id': 'nokernel', - 'ramdisk_id': 'nokernel'}} - - image5 = {'id': 'c905cedb-7281-47e4-8a62-f26bc5fc4c77', - 'name': 'fakeimage123456', - 'created_at': timestamp, - 'updated_at': timestamp, - 'deleted_at': None, - 'deleted': False, - 'status': 'active', - 'is_public': True, - 'container_format': 'ami', - 'disk_format': 'ami', - 'size': '26360814', - 'min_ram': 0, - 'min_disk': 0, - 'protected': False, - 'visibility': 'public', - 'tags': [], - 'properties': {'kernel_id': - '155d900f-4e14-4e4c-a73d-069cbf4541e6', - 'ramdisk_id': None}} - - image6 = {'id': 'a440c04b-79fa-479c-bed1-0b816eaec379', - 'name': 'fakeimage6', - 'created_at': timestamp, - 'updated_at': timestamp, - 'deleted_at': None, - 'deleted': False, - 'status': 'active', - 'is_public': False, - 'container_format': 'ova', - 'disk_format': 'vhd', - 'size': '49163826', - 'min_ram': 0, - 'min_disk': 0, - 'protected': False, - 'visibility': 'public', - 'tags': [], - 'properties': { - 'kernel_id': 'nokernel', - 'ramdisk_id': 'nokernel', - 'architecture': obj_fields.Architecture.X86_64, - 'auto_disk_config': 'False'}} - - image7 = {'id': AUTO_DISK_CONFIG_ENABLED_IMAGE_UUID, - 'name': 'fakeimage7', - 'created_at': timestamp, - 'updated_at': timestamp, - 'deleted_at': None, - 'deleted': False, - 'status': 'active', - 'is_public': False, - 'container_format': 'ova', - 'disk_format': 'vhd', - 'size': '74185822', - 'min_ram': 0, - 'min_disk': 0, - 'protected': False, - 'visibility': 'public', - 'tags': [], - 'properties': { - 'kernel_id': 'nokernel', - 'ramdisk_id': 'nokernel', - 'architecture': obj_fields.Architecture.X86_64, - 'auto_disk_config': 'True'}} - - self.create(None, image1) - self.create(None, image2) - self.create(None, image3) - self.create(None, image4) - self.create(None, image5) - self.create(None, image6) - self.create(None, image7) - self._imagedata = {} - super(_FakeImageService, self).__init__() - - # TODO(bcwaldon): implement optional kwargs such as limit, sort_dir - def detail(self, context, **kwargs): - """Return list of detailed image information.""" - return copy.deepcopy(list(self.images.values())) - - def download(self, context, image_id, data=None, dst_path=None, - trusted_certs=None): - self.show(context, image_id) - if data: - data.write(self._imagedata.get(image_id, b'')) - elif dst_path: - with open(dst_path, 'wb') as data: - data.write(self._imagedata.get(image_id, b'')) - - def show(self, context, image_id, include_locations=False, - show_deleted=True): - """Get data about specified image. - - Returns a dict containing image data for the given opaque image id. - - """ - image = self.images.get(str(image_id)) - if image: - return copy.deepcopy(image) - LOG.warning('Unable to find image id %s. Have images: %s', - image_id, self.images) - raise exception.ImageNotFound(image_id=image_id) - - def create(self, context, metadata, data=None): - """Store the image data and return the new image id. - - :raises: Duplicate if the image already exist. - - """ - image_id = str(metadata.get('id', uuidutils.generate_uuid())) - metadata['id'] = image_id - if image_id in self.images: - raise exception.CouldNotUploadImage(image_id=image_id) - image_meta = copy.deepcopy(metadata) - # Glance sets the size value when an image is created, so we - # need to do that here to fake things out if it's not provided - # by the caller. This is needed to avoid a KeyError in the - # image-size API. - if 'size' not in image_meta: - image_meta['size'] = None - # Similarly, Glance provides the status on the image once it's created - # and this is checked in the compute API when booting a server from - # this image, so we just fake it out to be 'active' even though this - # is mostly a lie on a newly created image. - if 'status' not in metadata: - image_meta['status'] = 'active' - # The owner of the image is by default the request context project_id. - if context and 'owner' not in image_meta.get('properties', {}): - # Note that normally "owner" is a top-level field in an image - # resource in glance but we have to fake this out for the images - # proxy API by throwing it into the generic "properties" dict. - image_meta.get('properties', {})['owner'] = context.project_id - self.images[image_id] = image_meta - if data: - self._imagedata[image_id] = data.read() - return self.images[image_id] - - def update(self, context, image_id, metadata, data=None, - purge_props=False): - """Replace the contents of the given image with the new data. - - :raises: ImageNotFound if the image does not exist. - - """ - if not self.images.get(image_id): - raise exception.ImageNotFound(image_id=image_id) - if purge_props: - self.images[image_id] = copy.deepcopy(metadata) - else: - image = self.images[image_id] - try: - image['properties'].update(metadata.pop('properties')) - except KeyError: - pass - image.update(metadata) - return self.images[image_id] - - def delete(self, context, image_id): - """Delete the given image. - - :raises: ImageNotFound if the image does not exist. - - """ - removed = self.images.pop(image_id, None) - if not removed: - raise exception.ImageNotFound(image_id=image_id) - - def get_location(self, context, image_id): - if image_id in self.images: - return 'fake_location' - return None - - -_fakeImageService = _FakeImageService() - - -def FakeImageService(): - return _fakeImageService - - -def FakeImageService_reset(): - global _fakeImageService - _fakeImageService = _FakeImageService() - - -def get_valid_image_id(): - return AUTO_DISK_CONFIG_ENABLED_IMAGE_UUID - - -def stub_out_image_service(test): - """Stubs out the image service for the test with the FakeImageService - - :param test: instance of nova.test.TestCase - :returns: The stubbed out FakeImageService object - """ - image_service = FakeImageService() - test.stub_out('nova.image.glance.get_remote_image_service', - lambda x, y: (image_service, y)) - test.stub_out('nova.image.glance.get_default_image_service', - lambda: image_service) - test.useFixture(nova_fixtures.ConfPatcher( - group="glance", api_servers=['http://localhost:9292'])) - return image_service def fake_image_obj(default_image_meta=None, default_image_props=None, diff --git a/nova/tests/unit/image/test_fake.py b/nova/tests/unit/image/test_fake.py deleted file mode 100644 index a2644e8c5872..000000000000 --- a/nova/tests/unit/image/test_fake.py +++ /dev/null @@ -1,119 +0,0 @@ -# Copyright 2011 OpenStack Foundation -# -# 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 datetime - -from six.moves import StringIO - -from nova import context -from nova import exception -from nova import test -import nova.tests.unit.image.fake - - -class FakeImageServiceTestCase(test.NoDBTestCase): - def setUp(self): - super(FakeImageServiceTestCase, self).setUp() - self.image_service = nova.tests.unit.image.fake.FakeImageService() - self.context = context.get_admin_context() - - def tearDown(self): - super(FakeImageServiceTestCase, self).tearDown() - nova.tests.unit.image.fake.FakeImageService_reset() - - def test_detail(self): - res = self.image_service.detail(self.context) - for image in res: - keys = set(image.keys()) - self.assertEqual(keys, set(['id', 'name', 'created_at', - 'updated_at', 'deleted_at', 'deleted', - 'status', 'is_public', 'properties', - 'disk_format', 'container_format', - 'size', 'min_disk', 'min_ram', - 'protected', 'tags', 'visibility'])) - self.assertIsInstance(image['created_at'], datetime.datetime) - self.assertIsInstance(image['updated_at'], datetime.datetime) - - if not (isinstance(image['deleted_at'], datetime.datetime) or - image['deleted_at'] is None): - self.fail('image\'s "deleted_at" attribute was neither a ' - 'datetime object nor None') - - def check_is_bool(image, key): - val = image.get('deleted') - if not isinstance(val, bool): - self.fail('image\'s "%s" attribute wasn\'t ' - 'a bool: %r' % (key, val)) - - check_is_bool(image, 'deleted') - check_is_bool(image, 'is_public') - - def test_show_raises_imagenotfound_for_invalid_id(self): - self.assertRaises(exception.ImageNotFound, - self.image_service.show, - self.context, - 'this image does not exist') - - def test_create_adds_id(self): - index = self.image_service.detail(self.context) - image_count = len(index) - - self.image_service.create(self.context, {}) - - index = self.image_service.detail(self.context) - self.assertEqual(len(index), image_count + 1) - - self.assertTrue(index[0]['id']) - - def test_create_keeps_id(self): - self.image_service.create(self.context, {'id': '34'}) - self.image_service.show(self.context, '34') - - def test_create_rejects_duplicate_ids(self): - self.image_service.create(self.context, {'id': '34'}) - self.assertRaises(exception.CouldNotUploadImage, - self.image_service.create, - self.context, - {'id': '34'}) - - # Make sure there's still one left - self.image_service.show(self.context, '34') - - def test_update(self): - self.image_service.create(self.context, - {'id': '34', 'foo': 'bar'}) - - self.image_service.update(self.context, '34', - {'id': '34', 'foo': 'baz'}) - - img = self.image_service.show(self.context, '34') - self.assertEqual(img['foo'], 'baz') - - def test_delete(self): - self.image_service.create(self.context, {'id': '34', 'foo': 'bar'}) - self.image_service.delete(self.context, '34') - self.assertRaises(exception.NotFound, - self.image_service.show, - self.context, - '34') - - def test_create_then_get(self): - blob = 'some data' - s1 = StringIO(blob) - self.image_service.create(self.context, - {'id': '32', 'foo': 'bar'}, - data=s1) - s2 = StringIO() - self.image_service.download(self.context, '32', data=s2) - self.assertEqual(s2.getvalue(), blob, 'Did not get blob back intact') diff --git a/nova/tests/unit/test_quota.py b/nova/tests/unit/test_quota.py index 04d23563d426..238d5e97bd30 100644 --- a/nova/tests/unit/test_quota.py +++ b/nova/tests/unit/test_quota.py @@ -27,7 +27,7 @@ from nova import exception from nova import objects from nova import quota from nova import test -import nova.tests.unit.image.fake +from nova.tests import fixtures as nova_fixtures CONF = nova.conf.CONF @@ -67,7 +67,7 @@ class QuotaIntegrationTestCase(test.TestCase): is_admin=True) self.inst_type = objects.Flavor.get_by_name(self.context, 'm1.small') - nova.tests.unit.image.fake.stub_out_image_service(self) + self.useFixture(nova_fixtures.GlanceFixture(self)) self.compute_api = compute.API() @@ -79,10 +79,6 @@ class QuotaIntegrationTestCase(test.TestCase): self.compute_api.network_api.validate_networks = ( mock.Mock(side_effect=fake_validate_networks)) - def tearDown(self): - super(QuotaIntegrationTestCase, self).tearDown() - nova.tests.unit.image.fake.FakeImageService_reset() - def _create_instance(self, flavor_name='m1.large'): """Create a test instance in cell1 with an instance mapping.""" cell1 = self.cell_mappings[test.CELL1_NAME] diff --git a/nova/tests/unit/virt/libvirt/test_blockinfo.py b/nova/tests/unit/virt/libvirt/test_blockinfo.py index 966e179280e3..9723c683f1cc 100644 --- a/nova/tests/unit/virt/libvirt/test_blockinfo.py +++ b/nova/tests/unit/virt/libvirt/test_blockinfo.py @@ -25,8 +25,8 @@ from nova import exception from nova import objects from nova.objects import fields as obj_fields from nova import test +from nova.tests import fixtures as nova_fixtures from nova.tests.unit import fake_block_device -import nova.tests.unit.image.fake from nova.tests.unit.virt import fakelibosinfo from nova.virt import block_device as driver_block_device from nova.virt import driver @@ -41,7 +41,7 @@ class LibvirtBlockInfoTest(test.NoDBTestCase): self.user_id = 'fake' self.project_id = 'fake' self.context = context.get_admin_context() - nova.tests.unit.image.fake.stub_out_image_service(self) + self.useFixture(nova_fixtures.GlanceFixture(self)) self.test_instance = { 'uuid': '32dfcb37-5af1-552b-357c-be8c3aa38310', 'memory_kb': '1024000', diff --git a/nova/tests/unit/virt/libvirt/test_driver.py b/nova/tests/unit/virt/libvirt/test_driver.py index 53e7a13beeff..865489b0410d 100644 --- a/nova/tests/unit/virt/libvirt/test_driver.py +++ b/nova/tests/unit/virt/libvirt/test_driver.py @@ -988,8 +988,7 @@ class LibvirtConnTestCase(test.NoDBTestCase, self.test_image_meta = { "disk_format": "raw", } - self.image_service = nova.tests.unit.image.fake.stub_out_image_service( - self) + self.image_service = self.useFixture(nova_fixtures.GlanceFixture(self)) self.device_xml_tmpl = """ @@ -1009,7 +1008,6 @@ class LibvirtConnTestCase(test.NoDBTestCase, return os.path.relpath(path, CONF.instances_path) def tearDown(self): - nova.tests.unit.image.fake.FakeImageService_reset() super(LibvirtConnTestCase, self).tearDown() def test_driver_capabilities(self): @@ -26977,8 +26975,7 @@ class _BaseSnapshotTests(test.NoDBTestCase): self.useFixture(fakelibvirt.FakeLibvirtFixture()) - self.image_service = nova.tests.unit.image.fake.stub_out_image_service( - self) + self.image_service = self.useFixture(nova_fixtures.GlanceFixture(self)) self.mock_update_task_state = mock.Mock() diff --git a/nova/tests/unit/virt/test_virt_drivers.py b/nova/tests/unit/virt/test_virt_drivers.py index f14d6f60df73..9b2d1e14f41b 100644 --- a/nova/tests/unit/virt/test_virt_drivers.py +++ b/nova/tests/unit/virt/test_virt_drivers.py @@ -36,7 +36,6 @@ from nova import objects from nova import test from nova.tests import fixtures as nova_fixtures from nova.tests.unit import fake_block_device -from nova.tests.unit.image import fake as fake_image from nova.tests.unit import utils as test_utils from nova.tests.unit.virt.libvirt import fakelibvirt from nova.virt import block_device as driver_block_device @@ -146,11 +145,10 @@ class _FakeDriverBackendTestCase(object): # 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) + self.useFixture(nova_fixtures.GlanceFixture(self)) self._setup_fakelibvirt() def tearDown(self): - fake_image.FakeImageService_reset() self._teardown_fakelibvirt() super(_FakeDriverBackendTestCase, self).tearDown() @@ -195,7 +193,7 @@ class _VirtDriverTestCase(_FakeDriverBackendTestCase): self.connection = importutils.import_object(self.driver_module, fake.FakeVirtAPI()) self.ctxt = test_utils.get_test_admin_context() - self.image_service = fake_image.FakeImageService() + self.image_service = self.useFixture(nova_fixtures.GlanceFixture(self)) # NOTE(dripton): resolve_driver_format does some file reading and # writing and chowning that complicate testing too much by requiring # using real directories with proper permissions. Just stub it out diff --git a/nova/tests/unit/virt/vmwareapi/test_configdrive.py b/nova/tests/unit/virt/vmwareapi/test_configdrive.py index 40568445f836..de07444ddb53 100644 --- a/nova/tests/unit/virt/vmwareapi/test_configdrive.py +++ b/nova/tests/unit/virt/vmwareapi/test_configdrive.py @@ -21,8 +21,8 @@ from nova import context from nova.image import glance from nova import objects from nova import test +from nova.tests import fixtures as nova_fixtures from nova.tests.unit import fake_instance -import nova.tests.unit.image.fake from nova.tests.unit import utils from nova.tests.unit.virt.vmwareapi import fake as vmwareapi_fake from nova.tests.unit.virt.vmwareapi import stubs @@ -50,11 +50,11 @@ class ConfigDriveTestCase(test.NoDBTestCase): self.flags(enabled=False, group='vnc') vmwareapi_fake.reset() stubs.set_stubs(self) - nova.tests.unit.image.fake.stub_out_image_service(self) + self.glance = self.useFixture(nova_fixtures.GlanceFixture(self)) self.conn = driver.VMwareVCDriver(fake.FakeVirtAPI) self.network_info = utils.get_test_network_info() self.node_name = self.conn._nodename - image_ref = nova.tests.unit.image.fake.get_valid_image_id() + image_ref = self.glance.auto_disk_config_enabled_image['id'] instance_values = { 'vm_state': 'building', 'project_id': 'fake', @@ -121,7 +121,6 @@ class ConfigDriveTestCase(test.NoDBTestCase): def tearDown(self): super(ConfigDriveTestCase, self).tearDown() vmwareapi_fake.cleanup() - nova.tests.unit.image.fake.FakeImageService_reset() @mock.patch.object(vmops.VMwareVMOps, '_get_instance_metadata', return_value='fake_metadata') diff --git a/nova/tests/unit/virt/vmwareapi/test_driver_api.py b/nova/tests/unit/virt/vmwareapi/test_driver_api.py index 375d29cf495e..a73b7194c461 100644 --- a/nova/tests/unit/virt/vmwareapi/test_driver_api.py +++ b/nova/tests/unit/virt/vmwareapi/test_driver_api.py @@ -46,9 +46,9 @@ from nova.image import glance from nova.network import model as network_model from nova import objects from nova import test +from nova.tests import fixtures as nova_fixtures from nova.tests.unit import fake_diagnostics from nova.tests.unit import fake_instance -import nova.tests.unit.image.fake from nova.tests.unit import matchers from nova.tests.unit.objects import test_diagnostics from nova.tests.unit import utils @@ -219,7 +219,7 @@ class VMwareAPIVMTestCase(test.NoDBTestCase, self.context = context.RequestContext(self.user_id, self.project_id) stubs.set_stubs(self) vmwareapi_fake.reset() - nova.tests.unit.image.fake.stub_out_image_service(self) + self.glance = self.useFixture(nova_fixtures.GlanceFixture(self)) service = self._create_service(host=HOST) self.conn = driver.VMwareVCDriver(None, False) @@ -234,7 +234,7 @@ class VMwareAPIVMTestCase(test.NoDBTestCase, # NOTE(vish): none of the network plugging code is actually # being tested self.network_info = utils.get_test_network_info() - image_ref = nova.tests.unit.image.fake.get_valid_image_id() + image_ref = self.glance.auto_disk_config_enabled_image['id'] (image_service, image_id) = glance.get_remote_image_service( self.context, image_ref) metadata = image_service.show(self.context, image_id) @@ -244,7 +244,6 @@ class VMwareAPIVMTestCase(test.NoDBTestCase, 'size': int(metadata['size']), }) self.fake_image_uuid = self.image.id - nova.tests.unit.image.fake.stub_out_image_service(self) self.vnc_host = 'ha-host' # create compute node resource provider @@ -266,7 +265,6 @@ class VMwareAPIVMTestCase(test.NoDBTestCase, def tearDown(self): super(VMwareAPIVMTestCase, self).tearDown() vmwareapi_fake.cleanup() - nova.tests.unit.image.fake.FakeImageService_reset() def test_legacy_block_device_info(self): self.assertFalse(self.conn.need_legacy_block_device_info) diff --git a/nova/tests/unit/virt/vmwareapi/test_images.py b/nova/tests/unit/virt/vmwareapi/test_images.py index 7dd9b7b06df8..7cfec00c97fe 100644 --- a/nova/tests/unit/virt/vmwareapi/test_images.py +++ b/nova/tests/unit/virt/vmwareapi/test_images.py @@ -19,14 +19,13 @@ import os import tarfile import mock -from oslo_utils.fixture import uuidsentinel +from oslo_utils.fixture import uuidsentinel as uuids from oslo_utils import units from oslo_vmware import rw_handles from nova import exception from nova import objects from nova import test -import nova.tests.unit.image.fake from nova.virt.vmwareapi import constants from nova.virt.vmwareapi import images from nova.virt.vmwareapi import vm_util @@ -46,15 +45,15 @@ class VMwareImagesTestCase(test.NoDBTestCase): context = mock.MagicMock() image_data = { - 'id': nova.tests.unit.image.fake.get_valid_image_id(), - 'disk_format': 'vmdk', - 'size': 512, - } + 'id': uuids.image, + 'disk_format': 'vmdk', + 'size': 512, + } read_file_handle = mock.MagicMock() write_file_handle = mock.MagicMock() read_iter = mock.MagicMock() instance = objects.Instance(id=1, - uuid=uuidsentinel.foo, + uuid=uuids.foo, image_ref=image_data['id']) def fake_read_handle(read_iter): @@ -229,7 +228,6 @@ class VMwareImagesTestCase(test.NoDBTestCase): def test_from_image_with_image_ref(self): raw_disk_size_in_gb = 83 raw_disk_size_in_bytes = raw_disk_size_in_gb * units.Gi - image_id = nova.tests.unit.image.fake.get_valid_image_id() mdata = {'size': raw_disk_size_in_bytes, 'disk_format': 'vmdk', 'properties': { @@ -239,9 +237,10 @@ class VMwareImagesTestCase(test.NoDBTestCase): "hw_vif_model": constants.DEFAULT_VIF_MODEL, "vmware_linked_clone": True}} mdata = objects.ImageMeta.from_dict(mdata) - with mock.patch.object(images, 'get_vsphere_location', - return_value=None): - img_props = images.VMwareImage.from_image(None, image_id, mdata) + with mock.patch.object( + images, 'get_vsphere_location', return_value=None, + ): + img_props = images.VMwareImage.from_image(None, uuids.image, mdata) image_size_in_kb = raw_disk_size_in_bytes / units.Ki @@ -265,7 +264,6 @@ class VMwareImagesTestCase(test.NoDBTestCase): raw_disk_size_in_gb = 93 raw_disk_size_in_btyes = raw_disk_size_in_gb * units.Gi - image_id = nova.tests.unit.image.fake.get_valid_image_id() mdata = {'size': raw_disk_size_in_btyes, 'disk_format': disk_format, 'properties': { @@ -280,8 +278,9 @@ class VMwareImagesTestCase(test.NoDBTestCase): context = mock.Mock() mdata = objects.ImageMeta.from_dict(mdata) with mock.patch.object( - images, 'get_vsphere_location', return_value=vsphere_location): - return images.VMwareImage.from_image(context, image_id, mdata) + images, 'get_vsphere_location', return_value=vsphere_location, + ): + return images.VMwareImage.from_image(context, uuids.image, mdata) def test_use_linked_clone_override_nf(self): image_props = self._image_build(None, False) diff --git a/nova/tests/unit/virt/vmwareapi/test_vmops.py b/nova/tests/unit/virt/vmwareapi/test_vmops.py index e15a0e1f3c72..102f6e505975 100644 --- a/nova/tests/unit/virt/vmwareapi/test_vmops.py +++ b/nova/tests/unit/virt/vmwareapi/test_vmops.py @@ -17,7 +17,7 @@ import time import mock from oslo_serialization import jsonutils -from oslo_utils.fixture import uuidsentinel +from oslo_utils.fixture import uuidsentinel as uuids from oslo_utils import units from oslo_utils import uuidutils from oslo_vmware import exceptions as vexc @@ -33,7 +33,6 @@ from nova import objects from nova import test from nova.tests.unit import fake_flavor from nova.tests.unit import fake_instance -import nova.tests.unit.image.fake from nova.tests.unit.virt.vmwareapi import fake as vmwareapi_fake from nova.tests.unit.virt.vmwareapi import stubs from nova import version @@ -70,7 +69,7 @@ class VMwareVMOpsTestCase(test.NoDBTestCase): self._session = driver.VMwareAPISession() self._virtapi = mock.Mock() - self._image_id = nova.tests.unit.image.fake.get_valid_image_id() + self._image_id = uuids.image fake_ds_ref = vmwareapi_fake.ManagedObjectReference(value='fake-ds') self._ds = ds_obj.Datastore( ref=fake_ds_ref, name='fake_ds', @@ -80,7 +79,7 @@ class VMwareVMOpsTestCase(test.NoDBTestCase): ref='fake_dc_ref', name='fake_dc', vmFolder='fake_vm_folder') cluster = vmwareapi_fake.create_cluster('fake_cluster', fake_ds_ref) - self._uuid = uuidsentinel.foo + self._uuid = uuids.foo fake_info_cache = { 'created_at': None, 'updated_at': None, @@ -173,8 +172,10 @@ class VMwareVMOpsTestCase(test.NoDBTestCase): "flavor:ephemeral_gb:8128\n" "flavor:root_gb:496\n" "flavor:swap:33550336\n" - "imageid:70a599e0-31e7-49b7-b260-868f441e862b\n" - "package:%s\n" % version.version_string_with_package()) + "imageid:%s\n" + "package:%s\n" % ( + uuids.image, + version.version_string_with_package())) def test_get_machine_id_str(self): result = vmops.VMwareVMOps._get_machine_id_str(self.network_info) @@ -754,8 +755,10 @@ class VMwareVMOpsTestCase(test.NoDBTestCase): 'flavor:ephemeral_gb:0\n' 'flavor:root_gb:10\n' 'flavor:swap:0\n' - 'imageid:70a599e0-31e7-49b7-b260-868f441e862b\n' - 'package:%s\n' % version.version_string_with_package()) + 'imageid:%s\n' + 'package:%s\n' % ( + uuids.image, + version.version_string_with_package())) fake_resize_spec.assert_called_once_with( self._session.vim.client.factory, int(self._instance.vcpus), @@ -1191,10 +1194,6 @@ class VMwareVMOpsTestCase(test.NoDBTestCase): self.assertTrue(self.password_logged) def _get_metadata(self, is_image_used=True): - if is_image_used: - image_id = '70a599e0-31e7-49b7-b260-868f441e862b' - else: - image_id = None return ("name:fake_display_name\n" "userid:fake_user\n" "username:None\n" @@ -1208,7 +1207,7 @@ class VMwareVMOpsTestCase(test.NoDBTestCase): "flavor:swap:0\n" "imageid:%(image_id)s\n" "package:%(version)s\n" % { - 'image_id': image_id, + 'image_id': uuids.image if is_image_used else None, 'version': version.version_string_with_package()}) @mock.patch.object(vm_util, 'rename_vm') @@ -1222,8 +1221,7 @@ class VMwareVMOpsTestCase(test.NoDBTestCase): @mock.patch.object(vmops.VMwareVMOps, 'build_virtual_machine') @mock.patch.object(vmops.VMwareVMOps, '_get_vm_config_info') @mock.patch.object(vmops.VMwareVMOps, '_get_extra_specs') - @mock.patch.object(nova.virt.vmwareapi.images.VMwareImage, - 'from_image') + @mock.patch.object(images.VMwareImage, 'from_image') def test_spawn_non_root_block_device(self, from_image, get_extra_specs, get_vm_config_info, @@ -1284,8 +1282,7 @@ class VMwareVMOpsTestCase(test.NoDBTestCase): @mock.patch.object(vmops.VMwareVMOps, 'build_virtual_machine') @mock.patch.object(vmops.VMwareVMOps, '_get_vm_config_info') @mock.patch.object(vmops.VMwareVMOps, '_get_extra_specs') - @mock.patch.object(nova.virt.vmwareapi.images.VMwareImage, - 'from_image') + @mock.patch.object(images.VMwareImage, 'from_image') def test_spawn_with_no_image_and_block_devices(self, from_image, get_extra_specs, get_vm_config_info, @@ -1347,8 +1344,7 @@ class VMwareVMOpsTestCase(test.NoDBTestCase): @mock.patch.object(vmops.VMwareVMOps, 'build_virtual_machine') @mock.patch.object(vmops.VMwareVMOps, '_get_vm_config_info') @mock.patch.object(vmops.VMwareVMOps, '_get_extra_specs') - @mock.patch.object(nova.virt.vmwareapi.images.VMwareImage, - 'from_image') + @mock.patch.object(images.VMwareImage, 'from_image') def test_spawn_unsupported_hardware(self, from_image, get_extra_specs, get_vm_config_info, @@ -1859,8 +1855,7 @@ class VMwareVMOpsTestCase(test.NoDBTestCase): @mock.patch.object(vmops.VMwareVMOps, 'build_virtual_machine') @mock.patch.object(vmops.VMwareVMOps, '_get_vm_config_info') @mock.patch.object(vmops.VMwareVMOps, '_get_extra_specs') - @mock.patch.object(nova.virt.vmwareapi.images.VMwareImage, - 'from_image') + @mock.patch.object(images.VMwareImage, 'from_image') def test_spawn_with_ephemerals_and_swap(self, from_image, get_extra_specs, get_vm_config_info, @@ -2048,8 +2043,7 @@ class VMwareVMOpsTestCase(test.NoDBTestCase): @mock.patch.object(vmops.VMwareVMOps, '_create_folders', return_value='fake_vm_folder') def test_build_virtual_machine(self, mock_create_folder): - image_id = nova.tests.unit.image.fake.get_valid_image_id() - image = images.VMwareImage(image_id=image_id) + image = images.VMwareImage(image_id=self._image_id) extra_specs = vm_util.ExtraSpecs() @@ -2805,8 +2799,10 @@ class VMwareVMOpsTestCase(test.NoDBTestCase): "flavor:ephemeral_gb:8128\n" "flavor:root_gb:496\n" "flavor:swap:33550336\n" - "imageid:70a599e0-31e7-49b7-b260-868f441e862b\n" - "package:%s\n" % version.version_string_with_package()) + "imageid:%s\n" + "package:%s\n" % ( + uuids.image, + version.version_string_with_package())) self.assertEqual(expected, metadata) def test_get_instance_metadata_flavor(self): diff --git a/nova/tests/unit/virt/vmwareapi/test_volumeops.py b/nova/tests/unit/virt/vmwareapi/test_volumeops.py index 96c7fe9754f3..0a051d62f548 100644 --- a/nova/tests/unit/virt/vmwareapi/test_volumeops.py +++ b/nova/tests/unit/virt/vmwareapi/test_volumeops.py @@ -13,7 +13,7 @@ # under the License. import mock -from oslo_utils.fixture import uuidsentinel +from oslo_utils.fixture import uuidsentinel as uuids from oslo_vmware import exceptions as oslo_vmw_exceptions from oslo_vmware import vim_util as vutil @@ -23,7 +23,6 @@ from nova import context from nova import exception from nova import test from nova.tests.unit import fake_instance -from nova.tests.unit.image import fake as image_fake from nova.tests.unit.virt.vmwareapi import fake as vmwareapi_fake from nova.tests.unit.virt.vmwareapi import stubs from nova.virt.vmwareapi import constants @@ -43,10 +42,10 @@ class VMwareVolumeOpsTestCase(test.NoDBTestCase): self._context = context.RequestContext('fake_user', 'fake_project') self._volumeops = volumeops.VMwareVolumeOps(self._session) - self._image_id = image_fake.get_valid_image_id() + self._image_id = uuids.image self._instance_values = { 'name': 'fake_name', - 'uuid': uuidsentinel.foo, + 'uuid': uuids.foo, 'vcpus': 1, 'memory_mb': 512, 'image_ref': self._image_id,