Fix the request context in ServiceFixture

Multiple cells are not considered in ServiceFixture.
So all compute nodes are registered to only one cell (cell1)
database regardless of their cells.
So change the ServiceFixture to use a request context
for the cell where the service runs.

Closes-Bug: #1759792

Change-Id: I6e940518c6ec27c02c714db6c124eb88c72214ab
(cherry picked from commit 6a06fd28c2)
This commit is contained in:
Takashi NATSUME 2018-04-02 11:55:53 +09:00 committed by melanie witt
parent fdc3d22ea7
commit ab6f78b33b
2 changed files with 11 additions and 4 deletions

View File

@ -409,6 +409,7 @@ class TestCase(testtools.TestCase):
CONF.set_override(k, v, group) CONF.set_override(k, v, group)
def start_service(self, name, host=None, **kwargs): def start_service(self, name, host=None, **kwargs):
cell = None
if name == 'compute' and self.USES_DB: if name == 'compute' and self.USES_DB:
# NOTE(danms): We need to create the HostMapping first, because # NOTE(danms): We need to create the HostMapping first, because
# otherwise we'll fail to update the scheduler while running # otherwise we'll fail to update the scheduler while running
@ -424,7 +425,7 @@ class TestCase(testtools.TestCase):
# Make sure that CONF.host is relevant to the right hostname # Make sure that CONF.host is relevant to the right hostname
self.useFixture(nova_fixtures.ConfPatcher(host=host)) self.useFixture(nova_fixtures.ConfPatcher(host=host))
svc = self.useFixture( svc = self.useFixture(
nova_fixtures.ServiceFixture(name, host, **kwargs)) nova_fixtures.ServiceFixture(name, host, cell=cell, **kwargs))
return svc.service return svc.service

View File

@ -65,7 +65,7 @@ SESSION_CONFIGURED = False
class ServiceFixture(fixtures.Fixture): class ServiceFixture(fixtures.Fixture):
"""Run a service as a test fixture.""" """Run a service as a test fixture."""
def __init__(self, name, host=None, **kwargs): def __init__(self, name, host=None, cell=None, **kwargs):
name = name name = name
# If not otherwise specified, the host will default to the # If not otherwise specified, the host will default to the
# name of the service. Some things like aggregates care that # name of the service. Some things like aggregates care that
@ -73,12 +73,18 @@ class ServiceFixture(fixtures.Fixture):
host = host or name host = host or name
kwargs.setdefault('host', host) kwargs.setdefault('host', host)
kwargs.setdefault('binary', 'nova-%s' % name) kwargs.setdefault('binary', 'nova-%s' % name)
self.cell = cell
self.kwargs = kwargs self.kwargs = kwargs
def setUp(self): def setUp(self):
super(ServiceFixture, self).setUp() super(ServiceFixture, self).setUp()
self.service = service.Service.create(**self.kwargs) self.ctxt = context.get_admin_context()
self.service.start() if self.cell:
context.set_target_cell(self.ctxt, self.cell)
with mock.patch('nova.context.get_admin_context',
return_value=self.ctxt):
self.service = service.Service.create(**self.kwargs)
self.service.start()
self.addCleanup(self.service.kill) self.addCleanup(self.service.kill)