From 23d3b8fbc9d793b4faba472ec6a0f7b95be1e70f Mon Sep 17 00:00:00 2001 From: Balazs Gibizer Date: Wed, 1 May 2019 23:06:52 +0200 Subject: [PATCH] Remove global state from the FakeDriver The virt driver FakeDriver used in both the functional and in the unit test used a global state to configure the host and node names the driver reports. This was hard to use when more then one compute service is started. Also global state is dangerous. It turned out that only a set of unit tests are using multiple nodes per compute the rest of the tests can simply use host=, nodes=[] setup. So this removes the global state. On stable/stein: * Changes in functional/regressions/test_bug_1764556.py just removes the usage of the global state as that test was backported before the patch being backported was introduced * Changes in functional/regressions/test_bug_1839560.py is a re-backport of If822509e906d5094f13a8700b2b9ed3c40580431 adapted to the current patch. Related-Bug: #1859766 Change-Id: I2cf2fcbaebc706f897ce5dfbff47d32117064f9c (cherry picked from commit b5666fb49239dbed6777481be63276db35ed8118) --- .../v2.53/hypervisors-list-resp.json | 2 +- nova/test.py | 9 ++-- .../v2.53/hypervisors-list-resp.json.tpl | 2 +- .../api_sample_tests/test_hypervisors.py | 5 -- .../functional/compute/test_live_migration.py | 3 -- nova/tests/functional/integrated_helpers.py | 3 -- .../test_instance.py | 3 -- .../regressions/test_bug_1669054.py | 3 -- .../regressions/test_bug_1671648.py | 10 ---- .../regressions/test_bug_1702454.py | 11 ----- .../regressions/test_bug_1718455.py | 10 ---- .../regressions/test_bug_1718512.py | 3 -- .../regressions/test_bug_1719730.py | 4 -- .../regressions/test_bug_1735407.py | 5 -- .../regressions/test_bug_1741307.py | 3 -- .../regressions/test_bug_1746483.py | 3 -- .../regressions/test_bug_1764556.py | 5 -- .../regressions/test_bug_1764883.py | 4 -- .../regressions/test_bug_1781710.py | 4 -- .../regressions/test_bug_1784353.py | 4 -- .../regressions/test_bug_1797580.py | 3 -- .../regressions/test_bug_1815153.py | 3 -- .../regressions/test_bug_1830747.py | 3 -- .../regressions/test_bug_1839560.py | 13 ++++-- nova/tests/functional/test_aggregates.py | 5 -- .../functional/test_availability_zones.py | 3 -- nova/tests/functional/test_server_group.py | 14 ------ nova/tests/functional/test_servers.py | 10 ---- nova/tests/unit/compute/test_compute.py | 6 ++- nova/tests/unit/compute/test_compute_mgr.py | 2 +- nova/tests/unit/virt/test_virt_drivers.py | 2 +- nova/virt/fake.py | 46 ++++++------------- 32 files changed, 35 insertions(+), 171 deletions(-) diff --git a/doc/api_samples/os-hypervisors/v2.53/hypervisors-list-resp.json b/doc/api_samples/os-hypervisors/v2.53/hypervisors-list-resp.json index ec10b4a106e0..ad6d9c4edfc6 100644 --- a/doc/api_samples/os-hypervisors/v2.53/hypervisors-list-resp.json +++ b/doc/api_samples/os-hypervisors/v2.53/hypervisors-list-resp.json @@ -1,7 +1,7 @@ { "hypervisors": [ { - "hypervisor_hostname": "fake-mini", + "hypervisor_hostname": "host2", "id": "1bb62a04-c576-402c-8147-9e89757a09e3", "state": "up", "status": "enabled" diff --git a/nova/test.py b/nova/test.py index b9433b42acee..917b735c780e 100644 --- a/nova/test.py +++ b/nova/test.py @@ -384,6 +384,12 @@ class TestCase(testtools.TestCase): def start_service(self, name, host=None, **kwargs): cell = None + # if the host is None then the CONF.host remains defaulted to + # 'fake-mini' (originally done in ConfFixture) + if host is not None: + # Make sure that CONF.host is relevant to the right hostname + self.useFixture(nova_fixtures.ConfPatcher(host=host)) + if name == 'compute' and self.USES_DB: # NOTE(danms): We need to create the HostMapping first, because # otherwise we'll fail to update the scheduler while running @@ -396,9 +402,6 @@ class TestCase(testtools.TestCase): cell_mapping=cell) hm.create() self.host_mappings[hm.host] = hm - if host is not None: - # Make sure that CONF.host is relevant to the right hostname - self.useFixture(nova_fixtures.ConfPatcher(host=host)) svc = self.useFixture( nova_fixtures.ServiceFixture(name, host, cell=cell, **kwargs)) diff --git a/nova/tests/functional/api_sample_tests/api_samples/os-hypervisors/v2.53/hypervisors-list-resp.json.tpl b/nova/tests/functional/api_sample_tests/api_samples/os-hypervisors/v2.53/hypervisors-list-resp.json.tpl index 38337772dc15..af2b6fbdb7c7 100644 --- a/nova/tests/functional/api_sample_tests/api_samples/os-hypervisors/v2.53/hypervisors-list-resp.json.tpl +++ b/nova/tests/functional/api_sample_tests/api_samples/os-hypervisors/v2.53/hypervisors-list-resp.json.tpl @@ -1,7 +1,7 @@ { "hypervisors": [ { - "hypervisor_hostname": "fake-mini", + "hypervisor_hostname": "host2", "id": "%(hypervisor_id)s", "state": "up", "status": "enabled" diff --git a/nova/tests/functional/api_sample_tests/test_hypervisors.py b/nova/tests/functional/api_sample_tests/test_hypervisors.py index e330d6e6ed4f..53a35b9eecd1 100644 --- a/nova/tests/functional/api_sample_tests/test_hypervisors.py +++ b/nova/tests/functional/api_sample_tests/test_hypervisors.py @@ -18,7 +18,6 @@ import mock from nova.cells import utils as cells_utils from nova import objects from nova.tests.functional.api_sample_tests import api_sample_base -from nova.virt import fake class HypervisorsSampleJsonTests(api_sample_base.ApiSampleTestBaseV21): @@ -157,8 +156,6 @@ class HypervisorsSampleJson233Tests(api_sample_base.ApiSampleTestBaseV21): # Start a new compute service to fake a record with hypervisor id=2 # for pagination test. host = 'host1' - fake.set_nodes([host]) - self.addCleanup(fake.restore_nodes) self.start_service('compute', host=host) def test_hypervisors_list(self): @@ -205,8 +202,6 @@ class HypervisorsSampleJson253Tests(HypervisorsSampleJson228Tests): def test_hypervisors_detail(self): # Start another compute service to get a 2nd compute for paging tests. host = 'host2' - fake.set_nodes([host]) - self.addCleanup(fake.restore_nodes) service_2 = self.start_service('compute', host=host).service_ref compute_node_2 = service_2.compute_node marker = self.compute_node_1.uuid diff --git a/nova/tests/functional/compute/test_live_migration.py b/nova/tests/functional/compute/test_live_migration.py index fd44a710183b..f88846d4f6ed 100644 --- a/nova/tests/functional/compute/test_live_migration.py +++ b/nova/tests/functional/compute/test_live_migration.py @@ -20,7 +20,6 @@ from nova import exception from nova.tests import fixtures as nova_fixtures from nova.tests.functional import integrated_helpers from nova.tests.unit import fake_notifier -from nova.virt import fake class FakeCinderError(object): @@ -54,8 +53,6 @@ class LiveMigrationCinderFailure(integrated_helpers._IntegratedTestBase, # Start a second compte node (the first one was started for us by # _IntegratedTestBase. set_nodes() is needed to avoid duplicate # nodenames. See comments in test_bug_1702454.py. - fake.set_nodes(['host2']) - self.addCleanup(fake.restore_nodes) self.compute2 = self.start_service('compute', host='host2') # To get the old Cinder flow we need to hack the service version, otherwise diff --git a/nova/tests/functional/integrated_helpers.py b/nova/tests/functional/integrated_helpers.py index af00f99a5b75..21bd397abb32 100644 --- a/nova/tests/functional/integrated_helpers.py +++ b/nova/tests/functional/integrated_helpers.py @@ -41,7 +41,6 @@ from nova.tests.unit import fake_notifier import nova.tests.unit.image.fake from nova.tests.unit import policy_fixture from nova import utils -from nova.virt import fake CONF = nova.conf.CONF @@ -443,8 +442,6 @@ class ProviderUsageBaseTestCase(test.TestCase, InstanceHelperMixin): compute service (defaults to cell1) :return: the nova compute service object """ - fake.set_nodes([host]) - self.addCleanup(fake.restore_nodes) compute = self.start_service('compute', host=host, cell=cell_name) self.computes[host] = compute return compute diff --git a/nova/tests/functional/notification_sample_tests/test_instance.py b/nova/tests/functional/notification_sample_tests/test_instance.py index ab4fcab4dc7e..3bc13540108c 100644 --- a/nova/tests/functional/notification_sample_tests/test_instance.py +++ b/nova/tests/functional/notification_sample_tests/test_instance.py @@ -22,7 +22,6 @@ from nova.tests.functional.api import client from nova.tests.functional.notification_sample_tests \ import notification_sample_base from nova.tests.unit import fake_notifier -from nova.virt import fake COMPUTE_VERSION_OLD_ATTACH_FLOW = \ compute_api.CINDER_V3_ATTACH_MIN_COMPUTE_VERSION - 1 @@ -51,8 +50,6 @@ class TestInstanceNotificationSampleWithMultipleCompute( self._wait_for_notification('instance.create.end') self._attach_volume_to_server(server, self.cinder.SWAP_OLD_VOL) # server will boot on the 'compute' host - fake.set_nodes(['host2']) - self.addCleanup(fake.restore_nodes) self.compute2 = self.start_service('compute', host='host2') actions = [ diff --git a/nova/tests/functional/regressions/test_bug_1669054.py b/nova/tests/functional/regressions/test_bug_1669054.py index 73b8fcc18503..7f2b6ef1e936 100644 --- a/nova/tests/functional/regressions/test_bug_1669054.py +++ b/nova/tests/functional/regressions/test_bug_1669054.py @@ -13,7 +13,6 @@ from nova import context from nova import objects from nova.tests.functional import integrated_helpers -from nova.virt import fake class ResizeEvacuateTestCase(integrated_helpers._IntegratedTestBase, @@ -47,8 +46,6 @@ class ResizeEvacuateTestCase(integrated_helpers._IntegratedTestBase, self._wait_for_state_change(self.api, server, 'ACTIVE') # Start up another compute service so we can resize. - fake.set_nodes(['host2']) - self.addCleanup(fake.restore_nodes) host2 = self.start_service('compute', host='host2') # Now resize the server to move it to host2. diff --git a/nova/tests/functional/regressions/test_bug_1671648.py b/nova/tests/functional/regressions/test_bug_1671648.py index ce1ffb500ad8..6164595a845c 100644 --- a/nova/tests/functional/regressions/test_bug_1671648.py +++ b/nova/tests/functional/regressions/test_bug_1671648.py @@ -23,7 +23,6 @@ from nova.tests.unit import cast_as_call from nova.tests.unit import fake_network import nova.tests.unit.image.fake from nova.tests.unit import policy_fixture -from nova.virt import fake class TestRetryBetweenComputeNodeBuilds(test.TestCase): @@ -67,16 +66,7 @@ class TestRetryBetweenComputeNodeBuilds(test.TestCase): # We start two compute services because we're going to fake one # of them to fail the build so we can trigger the retry code. - # set_nodes() is needed to have each compute service return a - # different nodename, so we get two hosts in the list of candidates - # for scheduling. Otherwise both hosts will have the same default - # nodename "fake-mini". The host passed to start_service controls the - # "host" attribute and set_nodes() sets the "nodename" attribute. - # We set_nodes() to make host and nodename the same for each compute. - fake.set_nodes(['host1']) - self.addCleanup(fake.restore_nodes) self.start_service('compute', host='host1') - fake.set_nodes(['host2']) self.start_service('compute', host='host2') self.scheduler_service = self.start_service('scheduler') diff --git a/nova/tests/functional/regressions/test_bug_1702454.py b/nova/tests/functional/regressions/test_bug_1702454.py index 6724a9a4a6bd..0aca1bed728f 100644 --- a/nova/tests/functional/regressions/test_bug_1702454.py +++ b/nova/tests/functional/regressions/test_bug_1702454.py @@ -18,7 +18,6 @@ from nova.tests.functional import integrated_helpers from nova.tests.unit import cast_as_call from nova.tests.unit.image import fake as image_fake from nova.tests.unit import policy_fixture -from nova.virt import fake class HostNameWeigher(weights.BaseHostWeigher): @@ -100,18 +99,8 @@ class SchedulerOnlyChecksTargetTest(test.TestCase, self.start_service('scheduler') # Let's now start three compute nodes as we said above. - # set_nodes() is needed to have each compute service return a - # different nodename, so we get two hosts in the list of candidates - # for scheduling. Otherwise both hosts will have the same default - # nodename "fake-mini". The host passed to start_service controls the - # "host" attribute and set_nodes() sets the "nodename" attribute. - # We set_nodes() to make host and nodename the same for each compute. - fake.set_nodes(['host1']) - self.addCleanup(fake.restore_nodes) self.start_service('compute', host='host1') - fake.set_nodes(['host2']) self.start_service('compute', host='host2') - fake.set_nodes(['host3']) self.start_service('compute', host='host3') self.useFixture(cast_as_call.CastAsCall(self)) diff --git a/nova/tests/functional/regressions/test_bug_1718455.py b/nova/tests/functional/regressions/test_bug_1718455.py index 2f5310ecc218..a166d4bfb534 100644 --- a/nova/tests/functional/regressions/test_bug_1718455.py +++ b/nova/tests/functional/regressions/test_bug_1718455.py @@ -19,7 +19,6 @@ from nova.tests.functional import integrated_helpers from nova.tests.unit import fake_network import nova.tests.unit.image.fake from nova.tests.unit import policy_fixture -from nova.virt import fake class TestLiveMigrateOneOfConcurrentlyCreatedInstances( @@ -61,16 +60,7 @@ class TestLiveMigrateOneOfConcurrentlyCreatedInstances( self.start_service('conductor') self.start_service('scheduler') - # set_nodes() is needed to have each compute service return a - # different nodename, so we get two hosts in the list of candidates - # for scheduling. Otherwise both hosts will have the same default - # nodename "fake-mini". The host passed to start_service controls the - # "host" attribute and set_nodes() sets the "nodename" attribute. - # We set_nodes() to make host and nodename the same for each compute. - fake.set_nodes(['host1']) - self.addCleanup(fake.restore_nodes) self.start_service('compute', host='host1') - fake.set_nodes(['host2']) self.start_service('compute', host='host2') fake_network.set_stub_network_methods(self) diff --git a/nova/tests/functional/regressions/test_bug_1718512.py b/nova/tests/functional/regressions/test_bug_1718512.py index 9786f1b71cde..e1e1ed385d24 100644 --- a/nova/tests/functional/regressions/test_bug_1718512.py +++ b/nova/tests/functional/regressions/test_bug_1718512.py @@ -20,7 +20,6 @@ from nova.tests.functional import fixtures as func_fixtures from nova.tests.functional import integrated_helpers from nova.tests.unit.image import fake as image_fake from nova.tests.unit import policy_fixture -from nova.virt import fake class HostNameWeigher(weights.BaseHostWeigher): @@ -86,9 +85,7 @@ class TestRequestSpecRetryReschedule(test.TestCase, self.start_service('scheduler') # Let's now start three compute nodes as we said above. - self.addCleanup(fake.restore_nodes) for host in ['host1', 'host2', 'host3']: - fake.set_nodes([host]) self.start_service('compute', host=host) def _stub_resize_failure(self, failed_host): diff --git a/nova/tests/functional/regressions/test_bug_1719730.py b/nova/tests/functional/regressions/test_bug_1719730.py index 46e05c84668f..1cf09df8b9ae 100644 --- a/nova/tests/functional/regressions/test_bug_1719730.py +++ b/nova/tests/functional/regressions/test_bug_1719730.py @@ -18,7 +18,6 @@ from nova.tests.functional import integrated_helpers from nova.tests.unit import fake_network import nova.tests.unit.image.fake from nova.tests.unit import policy_fixture -from nova.virt import fake class TestRescheduleWithServerGroup(test.TestCase, @@ -65,10 +64,7 @@ class TestRescheduleWithServerGroup(test.TestCase, # We start two compute services because we're going to fake one raising # RescheduledException to trigger a retry to the other compute host. - fake.set_nodes(['host1']) - self.addCleanup(fake.restore_nodes) self.start_service('compute', host='host1') - fake.set_nodes(['host2']) self.start_service('compute', host='host2') self.image_id = self.api.get_images()[0]['id'] diff --git a/nova/tests/functional/regressions/test_bug_1735407.py b/nova/tests/functional/regressions/test_bug_1735407.py index 657c0a8f1f56..846915ab3a2d 100644 --- a/nova/tests/functional/regressions/test_bug_1735407.py +++ b/nova/tests/functional/regressions/test_bug_1735407.py @@ -18,7 +18,6 @@ from nova.tests.unit import fake_network from nova.tests.unit import fake_notifier import nova.tests.unit.image.fake from nova.tests.unit import policy_fixture -from nova.virt import fake class TestParallelEvacuationWithServerGroup( @@ -61,10 +60,7 @@ class TestParallelEvacuationWithServerGroup( # We start two compute services because we need two instances with # anti-affinity server group policy to be booted - fake.set_nodes(['host1']) - self.addCleanup(fake.restore_nodes) self.compute1 = self.start_service('compute', host='host1') - fake.set_nodes(['host2']) self.compute2 = self.start_service('compute', host='host2') self.image_id = self.api.get_images()[0]['id'] @@ -124,7 +120,6 @@ class TestParallelEvacuationWithServerGroup( self.api.force_down_service('host2', 'nova-compute', True) # start a third compute to have place for one of the instances - fake.set_nodes(['host3']) self.compute3 = self.start_service('compute', host='host3') # evacuate both instances diff --git a/nova/tests/functional/regressions/test_bug_1741307.py b/nova/tests/functional/regressions/test_bug_1741307.py index 2c8c1cc17847..c829aa0cd205 100644 --- a/nova/tests/functional/regressions/test_bug_1741307.py +++ b/nova/tests/functional/regressions/test_bug_1741307.py @@ -16,7 +16,6 @@ from nova.tests.functional import fixtures as func_fixtures from nova.tests.functional import integrated_helpers import nova.tests.unit.image.fake from nova.tests.unit import policy_fixture -from nova.virt import fake class TestResizeWithNoAllocationScheduler( @@ -63,9 +62,7 @@ class TestResizeWithNoAllocationScheduler( self.start_service('conductor') # Create two compute nodes/services. - self.addCleanup(fake.restore_nodes) for host in ('host1', 'host2'): - fake.set_nodes([host]) self.start_service('compute', host=host) scheduler_service = self.start_service('scheduler') diff --git a/nova/tests/functional/regressions/test_bug_1746483.py b/nova/tests/functional/regressions/test_bug_1746483.py index 34936d6e3fa6..37ce48495eae 100644 --- a/nova/tests/functional/regressions/test_bug_1746483.py +++ b/nova/tests/functional/regressions/test_bug_1746483.py @@ -18,7 +18,6 @@ from nova.tests.functional import integrated_helpers from nova.tests.unit.image import fake as image_fakes from nova.tests.unit import policy_fixture from nova import utils -from nova.virt import fake CONF = config.CONF @@ -67,9 +66,7 @@ class TestBootFromVolumeIsolatedHostsFilter( # Create two compute nodes/services so we can restrict the image # we'll use to one of the hosts. - self.addCleanup(fake.restore_nodes) for host in ('host1', 'host2'): - fake.set_nodes([host]) self.start_service('compute', host=host) def test_boot_from_volume_with_isolated_image(self): diff --git a/nova/tests/functional/regressions/test_bug_1764556.py b/nova/tests/functional/regressions/test_bug_1764556.py index 8dceeaf3684d..2a9cce0c50c2 100644 --- a/nova/tests/functional/regressions/test_bug_1764556.py +++ b/nova/tests/functional/regressions/test_bug_1764556.py @@ -19,7 +19,6 @@ from nova.tests.functional import integrated_helpers from nova.tests.unit.image import fake as fake_image from nova.tests.unit import policy_fixture from nova import utils -from nova.virt import fake as fake_virt class InstanceListWithDeletedServicesTestCase( @@ -84,8 +83,6 @@ class InstanceListWithDeletedServicesTestCase( 5. migrate the instance back to the host1 service 6. list instances which will try to online migrate the old service uuid """ - fake_virt.set_nodes(['host1']) - self.addCleanup(fake_virt.restore_nodes) host1 = self.start_service('compute', host='host1') # Create an instance which will be on host1 since it's the only host. @@ -97,8 +94,6 @@ class InstanceListWithDeletedServicesTestCase( # Now we start a 2nd compute which is "upgraded" (has a uuid) and # we'll migrate the instance to that host. - fake_virt.set_nodes(['host2']) - self.addCleanup(fake_virt.restore_nodes) host2 = self.start_service('compute', host='host2') self.assertIsNotNone(host2.service_ref.uuid) diff --git a/nova/tests/functional/regressions/test_bug_1764883.py b/nova/tests/functional/regressions/test_bug_1764883.py index d3eab3f2570f..2c77a72e56aa 100644 --- a/nova/tests/functional/regressions/test_bug_1764883.py +++ b/nova/tests/functional/regressions/test_bug_1764883.py @@ -19,7 +19,6 @@ from nova.tests.unit import fake_network from nova.tests.unit import fake_notifier import nova.tests.unit.image.fake from nova.tests.unit import policy_fixture -from nova.virt import fake class TestEvacuationWithSourceReturningDuringRebuild( @@ -63,11 +62,8 @@ class TestEvacuationWithSourceReturningDuringRebuild( # Start two computes self.computes = {} - fake.set_nodes(['host1']) - self.addCleanup(fake.restore_nodes) self.computes['host1'] = self.start_service('compute', host='host1') - fake.set_nodes(['host2']) self.computes['host2'] = self.start_service('compute', host='host2') self.image_id = self.api.get_images()[0]['id'] diff --git a/nova/tests/functional/regressions/test_bug_1781710.py b/nova/tests/functional/regressions/test_bug_1781710.py index e29e88d85697..70b55d2a736b 100644 --- a/nova/tests/functional/regressions/test_bug_1781710.py +++ b/nova/tests/functional/regressions/test_bug_1781710.py @@ -18,7 +18,6 @@ from nova.tests.functional import fixtures as func_fixtures from nova.tests.functional import integrated_helpers from nova.tests.unit.image import fake as image_fake from nova.tests.unit import policy_fixture -from nova.virt import fake class HostNameWeigher(weights.BaseHostWeigher): @@ -77,10 +76,7 @@ class AntiAffinityMultiCreateRequest(test.TestCase, group='workarounds') self.start_service('scheduler') - fake.set_nodes(['host1']) - self.addCleanup(fake.restore_nodes) self.start_service('compute', host='host1') - fake.set_nodes(['host2']) self.start_service('compute', host='host2') def test_anti_affinity_multi_create(self): diff --git a/nova/tests/functional/regressions/test_bug_1784353.py b/nova/tests/functional/regressions/test_bug_1784353.py index 04c1cf0a8317..2ce4fb95cdd9 100644 --- a/nova/tests/functional/regressions/test_bug_1784353.py +++ b/nova/tests/functional/regressions/test_bug_1784353.py @@ -17,7 +17,6 @@ from nova.tests.functional import integrated_helpers from nova.tests.unit import fake_network import nova.tests.unit.image.fake from nova.tests.unit import policy_fixture -from nova.virt import fake class TestRescheduleWithVolumesAttached( @@ -55,11 +54,8 @@ class TestRescheduleWithVolumesAttached( self.start_service('scheduler') # Start two computes to allow the instance to be rescheduled - fake.set_nodes(['host1']) - self.addCleanup(fake.restore_nodes) self.host1 = self.start_service('compute', host='host1') - fake.set_nodes(['host2']) self.host2 = self.start_service('compute', host='host2') self.image_id = self.api.get_images()[0]['id'] diff --git a/nova/tests/functional/regressions/test_bug_1797580.py b/nova/tests/functional/regressions/test_bug_1797580.py index bbe3bb8183f5..b752a9e8dc1a 100644 --- a/nova/tests/functional/regressions/test_bug_1797580.py +++ b/nova/tests/functional/regressions/test_bug_1797580.py @@ -16,7 +16,6 @@ from nova.tests.functional import fixtures as func_fixtures from nova.tests.functional import integrated_helpers from nova.tests.unit.image import fake as image_fake from nova.tests.unit import policy_fixture -from nova.virt import fake class ColdMigrateTargetHostThenLiveMigrateTest( @@ -60,9 +59,7 @@ class ColdMigrateTargetHostThenLiveMigrateTest( self.start_service('conductor') self.start_service('scheduler') - self.addCleanup(fake.restore_nodes) for host in ('host1', 'host2'): - fake.set_nodes([host]) self.start_service('compute', host=host) def test_cold_migrate_target_host_then_live_migrate(self): diff --git a/nova/tests/functional/regressions/test_bug_1815153.py b/nova/tests/functional/regressions/test_bug_1815153.py index 15012b097137..f4f42c38ddfb 100644 --- a/nova/tests/functional/regressions/test_bug_1815153.py +++ b/nova/tests/functional/regressions/test_bug_1815153.py @@ -23,7 +23,6 @@ from nova.tests.functional import fixtures as func_fixtures from nova.tests.functional import integrated_helpers from nova.tests.unit.image import fake as image_fake from nova.tests.unit import policy_fixture -from nova.virt import fake class NonPersistentFieldNotResetTest( @@ -69,9 +68,7 @@ class NonPersistentFieldNotResetTest( self.compute = {} - self.addCleanup(fake.restore_nodes) for host in ('host1', 'host2', 'host3'): - fake.set_nodes([host]) compute_service = self.start_service('compute', host=host) self.compute.update({host: compute_service}) diff --git a/nova/tests/functional/regressions/test_bug_1830747.py b/nova/tests/functional/regressions/test_bug_1830747.py index 1e22df6caf66..877546a5a85e 100644 --- a/nova/tests/functional/regressions/test_bug_1830747.py +++ b/nova/tests/functional/regressions/test_bug_1830747.py @@ -21,7 +21,6 @@ from nova.tests import fixtures as nova_fixtures from nova.tests.functional import fixtures as func_fixtures from nova.tests.functional import integrated_helpers from nova.tests.unit.image import fake as fake_image -from nova.virt import fake as fake_virt class HostNameWeigher(weights.BaseHostWeigher): @@ -79,10 +78,8 @@ class MissingReqSpecInstanceGroupUUIDTestCase( self.start_service('scheduler') # Start two computes, one where the server will be created and another # where we'll cold migrate it. - self.addCleanup(fake_virt.restore_nodes) self.computes = {} # keep track of the compute services per host name for host in ('host1', 'host2'): - fake_virt.set_nodes([host]) compute_service = self.start_service('compute', host=host) self.computes[host] = compute_service diff --git a/nova/tests/functional/regressions/test_bug_1839560.py b/nova/tests/functional/regressions/test_bug_1839560.py index 94e507e1ba8d..5cf0e6da42e2 100644 --- a/nova/tests/functional/regressions/test_bug_1839560.py +++ b/nova/tests/functional/regressions/test_bug_1839560.py @@ -20,7 +20,6 @@ from nova.tests import fixtures as nova_fixtures from nova.tests.functional import fixtures as func_fixtures from nova.tests.functional import integrated_helpers from nova import utils -from nova.virt import fake as fake_virt LOG = logging.getLogger(__name__) @@ -58,11 +57,15 @@ class PeriodicNodeRecreateTestCase(test.TestCase, def test_update_available_resource_node_recreate(self): # First we create a compute service to manage a couple of fake nodes. - # When start_service runs, it will create the node1 and node2 - # ComputeNodes. - fake_virt.set_nodes(['node1', 'node2']) - self.addCleanup(fake_virt.restore_nodes) compute = self.start_service('compute', 'node1') + # When start_service runs, it will create the node1 ComputeNode. + compute.manager.driver._set_nodes(['node1', 'node2']) + # Run the update_available_resource periodic to register node2. + ctxt = context.get_admin_context() + compute.manager.update_available_resource(ctxt) + # Make sure no compute nodes were orphaned or deleted. + self.assertNotIn('Deleting orphan compute node', + self.stdlog.logger.output) # Now we should have two compute nodes, make sure the hypervisors API # shows them. hypervisors = self.api.api_get('/os-hypervisors').body['hypervisors'] diff --git a/nova/tests/functional/test_aggregates.py b/nova/tests/functional/test_aggregates.py index 0a23839ba96d..45fc17dfb4b9 100644 --- a/nova/tests/functional/test_aggregates.py +++ b/nova/tests/functional/test_aggregates.py @@ -27,7 +27,6 @@ from nova.tests.functional import integrated_helpers import nova.tests.unit.image.fake from nova.tests.unit import policy_fixture from nova import utils -from nova.virt import fake CONF = nova.conf.CONF @@ -117,8 +116,6 @@ class AggregateRequestFiltersTest(test.TestCase, compute service. :return: the nova compute service object """ - fake.set_nodes([host]) - self.addCleanup(fake.restore_nodes) compute = self.start_service('compute', host=host) self.computes[host] = compute return compute @@ -471,8 +468,6 @@ class TestAggregateMultiTenancyIsolationFilter( test.TestCase, integrated_helpers.InstanceHelperMixin): def _start_compute(self, host): - fake.set_nodes([host]) - self.addCleanup(fake.restore_nodes) self.start_service('compute', host=host) def setUp(self): diff --git a/nova/tests/functional/test_availability_zones.py b/nova/tests/functional/test_availability_zones.py index 93705e4aa30e..b4d0b18994c6 100644 --- a/nova/tests/functional/test_availability_zones.py +++ b/nova/tests/functional/test_availability_zones.py @@ -18,7 +18,6 @@ from nova.tests.functional import fixtures as func_fixtures from nova.tests.functional import integrated_helpers from nova.tests.unit.image import fake as fake_image from nova.tests.unit import policy_fixture -from nova.virt import fake class TestAvailabilityZoneScheduling( @@ -53,8 +52,6 @@ class TestAvailabilityZoneScheduling( def _start_host_in_zone(self, host, zone): # Start the nova-compute service. - fake.set_nodes([host]) - self.addCleanup(fake.restore_nodes) self.start_service('compute', host=host) # Create a host aggregate with a zone in which to put this host. aggregate_body = { diff --git a/nova/tests/functional/test_server_group.py b/nova/tests/functional/test_server_group.py index d778571fece6..b160b566aea3 100644 --- a/nova/tests/functional/test_server_group.py +++ b/nova/tests/functional/test_server_group.py @@ -136,14 +136,9 @@ class ServerGroupTestV21(ServerGroupTestBase): # tree. self.stub_out('nova.virt.driver.load_compute_driver', _fake_load_compute_driver) - fake.set_nodes(['compute']) - self.addCleanup(fake.restore_nodes) self.compute = self.start_service('compute', host='compute') # NOTE(gibi): start a second compute host to be able to test affinity - # NOTE(sbauza): Make sure the FakeDriver returns a different nodename - # for the second compute node. - fake.set_nodes(['host2']) self.compute2 = self.start_service('compute', host='host2') def test_get_no_groups(self): @@ -363,7 +358,6 @@ class ServerGroupTestV21(ServerGroupTestBase): def test_migrate_with_anti_affinity(self): # Start additional host to test migration with anti-affinity - fake.set_nodes(['host3']) self.start_service('compute', host='host3') created_group = self.api.post_server_groups(self.anti_affinity) @@ -422,7 +416,6 @@ class ServerGroupTestV21(ServerGroupTestBase): self._set_forced_down(host, True) # Start additional host to test evacuation - fake.set_nodes(['host3']) self.start_service('compute', host='host3') post = {'evacuate': {'onSharedStorage': False}} @@ -607,7 +600,6 @@ class ServerGroupTestV215(ServerGroupTestV21): self._set_forced_down(host, True) # Start additional host to test evacuation - fake.set_nodes(['host3']) compute3 = self.start_service('compute', host='host3') post = {'evacuate': {}} @@ -877,11 +869,8 @@ class ServerGroupTestMultiCell(ServerGroupTestBase): def setUp(self): super(ServerGroupTestMultiCell, self).setUp() # Start two compute services, one per cell - fake.set_nodes(['host1']) - self.addCleanup(fake.restore_nodes) self.compute1 = self.start_service('compute', host='host1', cell='cell1') - fake.set_nodes(['host2']) self.compute2 = self.start_service('compute', host='host2', cell='cell2') # This is needed to find a server that is still booting with multiple @@ -965,9 +954,7 @@ class TestAntiAffinityLiveMigration(test.TestCase, # Start conductor, scheduler and two computes. self.start_service('conductor') self.start_service('scheduler') - self.addCleanup(fake.restore_nodes) for host in ('host1', 'host2'): - fake.set_nodes([host]) self.start_service('compute', host=host) def test_serial_no_valid_host_then_pass_with_third_host(self): @@ -1029,7 +1016,6 @@ class TestAntiAffinityLiveMigration(test.TestCase, # Now start up a 3rd compute service and retry the live migration which # should work this time. - fake.set_nodes(['host3']) self.start_service('compute', host='host3') self.admin_api.post_server_action(server['id'], body) server = self._wait_for_state_change(self.admin_api, server, 'ACTIVE') diff --git a/nova/tests/functional/test_servers.py b/nova/tests/functional/test_servers.py index a99c5a163a73..452259e9e60e 100644 --- a/nova/tests/functional/test_servers.py +++ b/nova/tests/functional/test_servers.py @@ -196,9 +196,6 @@ class ServersTest(ServersTestBase): def _test_create_server_with_error_with_retries(self): # Create a server which will enter error state. - fake.set_nodes(['host2']) - self.addCleanup(fake.restore_nodes) - self.flags(host='host2') self.compute2 = self.start_service('compute', host='host2') self.computes['compute2'] = self.compute2 @@ -1491,9 +1488,6 @@ class ServerRebuildTestCase(integrated_helpers._IntegratedTestBase, default so that should filter out the host based on the image meta. """ - fake.set_nodes(['host2']) - self.addCleanup(fake.restore_nodes) - self.flags(host='host2') self.compute2 = self.start_service('compute', host='host2') # We hard-code from a fake image since we can't get images @@ -5023,9 +5017,7 @@ class ServerTestV256Common(ServersTestBase): def _setup_compute_service(self): # Set up 3 compute services in the same cell - self.addCleanup(fake.restore_nodes) for host in ('host1', 'host2', 'host3'): - fake.set_nodes([host]) self.start_service('compute', host=host) def _create_server(self, target_host=None): @@ -5057,9 +5049,7 @@ class ServerTestV256MultiCellTestCase(ServerTestV256Common): host_to_cell_mappings = { 'host1': 'cell1', 'host2': 'cell2'} - self.addCleanup(fake.restore_nodes) for host in sorted(host_to_cell_mappings): - fake.set_nodes([host]) self.start_service('compute', host=host, cell=host_to_cell_mappings[host]) diff --git a/nova/tests/unit/compute/test_compute.py b/nova/tests/unit/compute/test_compute.py index e22bbe872269..07e3f748bdad 100644 --- a/nova/tests/unit/compute/test_compute.py +++ b/nova/tests/unit/compute/test_compute.py @@ -157,12 +157,15 @@ class BaseTestCase(test.TestCase): def setUp(self): super(BaseTestCase, self).setUp() self.flags(network_manager='nova.network.manager.FlatManager') - fake.set_nodes([NODENAME, NODENAME2]) fake_notifier.stub_notifier(self) self.addCleanup(fake_notifier.reset) self.compute = compute_manager.ComputeManager() + # NOTE(gibi): this is a hack to make the fake virt driver use the nodes + # needed for these tests. + self.compute.driver._set_nodes([NODENAME, NODENAME2]) + # execute power syncing synchronously for testing: self.compute._sync_power_pool = eventlet_utils.SyncPool() @@ -279,7 +282,6 @@ class BaseTestCase(test.TestCase): instances = db.instance_get_all(ctxt) for instance in instances: db.instance_destroy(ctxt, instance['uuid']) - fake.restore_nodes() super(BaseTestCase, self).tearDown() def _fake_instance(self, updates): diff --git a/nova/tests/unit/compute/test_compute_mgr.py b/nova/tests/unit/compute/test_compute_mgr.py index 433692f4cf15..2d6c6ad6627a 100644 --- a/nova/tests/unit/compute/test_compute_mgr.py +++ b/nova/tests/unit/compute/test_compute_mgr.py @@ -9162,7 +9162,7 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase, """ instance = fake_instance.fake_instance_obj( self.context, host=self.compute.host, vm_state=vm_states.STOPPED, - expected_attrs=['system_metadata', 'flavor']) + node='fake-node', expected_attrs=['system_metadata', 'flavor']) migration = mock.MagicMock(spec='nova.objects.Migration') request_spec = mock.MagicMock(spec='nova.objects.RequestSpec') ex = exception.InstanceFaultRollback( diff --git a/nova/tests/unit/virt/test_virt_drivers.py b/nova/tests/unit/virt/test_virt_drivers.py index 7a9f8db559ae..14bb4bde9bbe 100644 --- a/nova/tests/unit/virt/test_virt_drivers.py +++ b/nova/tests/unit/virt/test_virt_drivers.py @@ -867,8 +867,8 @@ class AbstractDriverTestCase(_VirtDriverTestCase, test.TestCase): class FakeConnectionTestCase(_VirtDriverTestCase, test.TestCase): def setUp(self): self.driver_module = 'nova.virt.fake.FakeDriver' - fake.set_nodes(['myhostname']) super(FakeConnectionTestCase, self).setUp() + self.connection.init_host('myhostname') def _check_available_resource_fields(self, host_status): super(FakeConnectionTestCase, self)._check_available_resource_fields( diff --git a/nova/virt/fake.py b/nova/virt/fake.py index 1014d6083adb..fc0c4180b49e 100644 --- a/nova/virt/fake.py +++ b/nova/virt/fake.py @@ -25,7 +25,6 @@ semantics of real hypervisor connections. import collections import contextlib -import copy import time import uuid @@ -53,31 +52,6 @@ CONF = nova.conf.CONF LOG = logging.getLogger(__name__) -_FAKE_NODES = None - - -def set_nodes(nodes): - """Sets FakeDriver's node.list. - - It has effect on the following methods: - get_available_nodes() - get_available_resource - - To restore the change, call restore_nodes() - """ - global _FAKE_NODES - _FAKE_NODES = nodes - - -def restore_nodes(): - """Resets FakeDriver's node list modified by set_nodes(). - - Usually called from tearDown(). - """ - global _FAKE_NODES - _FAKE_NODES = [CONF.host] - - class FakeInstance(object): def __init__(self, name, state, uuid): @@ -168,15 +142,21 @@ class FakeDriver(driver.ComputeDriver): self._mounts = {} self._interfaces = {} self.active_migrations = {} - self._nodes = self._init_nodes() - - def _init_nodes(self): - if not _FAKE_NODES: - set_nodes([CONF.host]) - return copy.copy(_FAKE_NODES) + self._host = None + self._nodes = None def init_host(self, host): - return + self._host = host + # NOTE(gibi): this is unnecessary complex and fragile but this is + # how many current functional sample tests expect the node name. + self._nodes = (['fake-mini'] if self._host == 'compute' + else [self._host]) + + def _set_nodes(self, nodes): + # NOTE(gibi): this is not part of the driver interface but used + # by our tests to customize the discovered nodes by the fake + # driver. + self._nodes = nodes def list_instances(self): return [self.instances[uuid].name for uuid in self.instances.keys()]