Make compute_api use Service and ComputeNode objects
This converts compute_api (and associated nova-api and test code) to use Service and ComputeNode objects for their work. Note the removal of the nova.db import from compute/api.py. There is still a chunk of code that uses self.db, but it's progress. Related to blueprint compute-api-objects Change-Id: I7d0b4c0d02559ec16b35dd38278cdf738e0fda6c
This commit is contained in:
committed by
Matt Riedemann
parent
61fdbf194d
commit
10593c2eaf
@@ -336,7 +336,7 @@ class HostController(object):
|
||||
except exception.AdminRequired:
|
||||
msg = _("Describe-resource is admin only functionality")
|
||||
raise webob.exc.HTTPForbidden(explanation=msg)
|
||||
compute_node = service['compute_node'][0]
|
||||
compute_node = service['compute_node']
|
||||
instances = self.api.instance_get_all_by_host(context, host_name)
|
||||
resources = [self._get_total_resources(host_name, compute_node)]
|
||||
resources.append(self._get_used_now_resources(host_name,
|
||||
|
||||
@@ -315,7 +315,7 @@ class HostController(wsgi.Controller):
|
||||
# be removed
|
||||
msg = _("Describe-resource is admin only functionality")
|
||||
raise webob.exc.HTTPForbidden(explanation=msg)
|
||||
compute_node = service['compute_node'][0]
|
||||
compute_node = service['compute_node']
|
||||
instances = self.api.instance_get_all_by_host(context, host_name)
|
||||
resources = [self._get_total_resources(host_name, compute_node)]
|
||||
resources.append(self._get_used_now_resources(host_name,
|
||||
|
||||
@@ -41,7 +41,6 @@ from nova.compute import utils as compute_utils
|
||||
from nova.compute import vm_states
|
||||
from nova.consoleauth import rpcapi as consoleauth_rpcapi
|
||||
from nova import crypto
|
||||
from nova import db
|
||||
from nova.db import base
|
||||
from nova import exception
|
||||
from nova import hooks
|
||||
@@ -58,6 +57,7 @@ from nova.objects import instance_info_cache
|
||||
from nova.objects import keypair as keypair_obj
|
||||
from nova.objects import migration as migration_obj
|
||||
from nova.objects import security_group
|
||||
from nova.objects import service as service_obj
|
||||
from nova.openstack.common import excutils
|
||||
from nova.openstack.common.gettextutils import _
|
||||
from nova.openstack.common import log as logging
|
||||
@@ -1191,9 +1191,10 @@ class API(base.Base):
|
||||
def trigger_provider_fw_rules_refresh(self, context):
|
||||
"""Called when a rule is added/removed from a provider firewall."""
|
||||
|
||||
for service in self.db.service_get_all_by_topic(context,
|
||||
CONF.compute_topic):
|
||||
host_name = service['host']
|
||||
services = service_obj.ServiceList.get_all_by_topic(context,
|
||||
CONF.compute_topic)
|
||||
for service in services:
|
||||
host_name = service.host
|
||||
self.compute_rpcapi.refresh_provider_fw_rules(context, host_name)
|
||||
|
||||
@wrap_check_policy
|
||||
@@ -1303,8 +1304,8 @@ class API(base.Base):
|
||||
|
||||
is_up = False
|
||||
try:
|
||||
service = self.db.service_get_by_compute_host(
|
||||
context.elevated(), instance['host'])
|
||||
service = service_obj.Service.get_by_compute_host(
|
||||
context.elevated(), instance.host)
|
||||
if self.servicegroup_api.service_is_up(service):
|
||||
is_up = True
|
||||
|
||||
@@ -2861,7 +2862,7 @@ class API(base.Base):
|
||||
"""
|
||||
LOG.debug(_('vm evacuation scheduled'))
|
||||
inst_host = instance['host']
|
||||
service = self.db.service_get_by_compute_host(context, inst_host)
|
||||
service = service_obj.Service.get_by_compute_host(context, inst_host)
|
||||
if self.servicegroup_api.service_is_up(service):
|
||||
msg = (_('Instance compute service state on %s '
|
||||
'expected to be down, but it was up.') % inst_host)
|
||||
@@ -2900,7 +2901,7 @@ class HostAPI(base.Base):
|
||||
|
||||
def _assert_host_exists(self, context, host_name, must_be_up=False):
|
||||
"""Raise HostNotFound if compute host doesn't exist."""
|
||||
service = self.db.service_get_by_compute_host(context, host_name)
|
||||
service = service_obj.Service.get_by_compute_host(context, host_name)
|
||||
if not service:
|
||||
raise exception.HostNotFound(host=host_name)
|
||||
if must_be_up and not self.servicegroup_api.service_is_up(service):
|
||||
@@ -2943,10 +2944,10 @@ class HostAPI(base.Base):
|
||||
if filters is None:
|
||||
filters = {}
|
||||
disabled = filters.pop('disabled', None)
|
||||
services = self.db.service_get_all(context, disabled=disabled)
|
||||
if set_zones or 'availability_zone' in filters:
|
||||
services = availability_zones.set_availability_zones(context,
|
||||
services)
|
||||
if 'availability_zone' in filters:
|
||||
set_zones = True
|
||||
services = service_obj.ServiceList.get_all(context, disabled,
|
||||
set_zones=set_zones)
|
||||
ret_services = []
|
||||
for service in services:
|
||||
for key, val in filters.iteritems():
|
||||
@@ -2959,7 +2960,7 @@ class HostAPI(base.Base):
|
||||
|
||||
def service_get_by_compute_host(self, context, host_name):
|
||||
"""Get service entry for the given compute hostname."""
|
||||
return self.db.service_get_by_compute_host(context, host_name)
|
||||
return service_obj.Service.get_by_compute_host(context, host_name)
|
||||
|
||||
def service_update(self, context, host_name, binary, params_to_update):
|
||||
"""Enable / Disable a service.
|
||||
@@ -2967,8 +2968,11 @@ class HostAPI(base.Base):
|
||||
For compute services, this stops new builds and migrations going to
|
||||
the host.
|
||||
"""
|
||||
service = db.service_get_by_args(context, host_name, binary)
|
||||
return db.service_update(context, service['id'], params_to_update)
|
||||
service = service_obj.Service.get_by_args(context, host_name,
|
||||
binary)
|
||||
service.update(params_to_update)
|
||||
service.save()
|
||||
return service
|
||||
|
||||
def instance_get_all_by_host(self, context, host_name):
|
||||
"""Return all instances on the given host."""
|
||||
@@ -3143,7 +3147,7 @@ class AggregateAPI(base.Base):
|
||||
"addhost.start",
|
||||
aggregate_payload)
|
||||
# validates the host; ComputeHostNotFound is raised if invalid
|
||||
self.db.service_get_by_compute_host(context, host_name)
|
||||
service_obj.Service.get_by_compute_host(context, host_name)
|
||||
host_az = availability_zones.get_host_availability_zone(context,
|
||||
host_name)
|
||||
if host_az and host_az != CONF.default_availability_zone:
|
||||
@@ -3172,7 +3176,7 @@ class AggregateAPI(base.Base):
|
||||
"removehost.start",
|
||||
aggregate_payload)
|
||||
# validates the host; ComputeHostNotFound is raised if invalid
|
||||
self.db.service_get_by_compute_host(context, host_name)
|
||||
service_obj.Service.get_by_compute_host(context, host_name)
|
||||
self.db.aggregate_host_delete(context, aggregate_id, host_name)
|
||||
aggregate = self.db.aggregate_get(context, aggregate_id)
|
||||
self.compute_rpcapi.remove_aggregate_host(context,
|
||||
|
||||
@@ -22,6 +22,7 @@ from nova.cells import utils as cells_utils
|
||||
from nova.compute import api as compute_api
|
||||
from nova.compute import rpcapi as compute_rpcapi
|
||||
from nova import exception
|
||||
from nova.objects import service as service_obj
|
||||
from nova.openstack.common import excutils
|
||||
|
||||
|
||||
@@ -466,11 +467,24 @@ class HostAPI(compute_api.HostAPI):
|
||||
if zone_filter is not None:
|
||||
services = [s for s in services
|
||||
if s['availability_zone'] == zone_filter]
|
||||
return services
|
||||
# NOTE(danms): Currently cells does not support objects as
|
||||
# return values, so just convert the db-formatted service objects
|
||||
# to new-world objects here
|
||||
return service_obj._make_list(context,
|
||||
service_obj.ServiceList(),
|
||||
service_obj.Service,
|
||||
services)
|
||||
|
||||
def service_get_by_compute_host(self, context, host_name):
|
||||
return self.cells_rpcapi.service_get_by_compute_host(context,
|
||||
host_name)
|
||||
db_service = self.cells_rpcapi.service_get_by_compute_host(context,
|
||||
host_name)
|
||||
# NOTE(danms): Currently cells does not support objects as
|
||||
# return values, so just convert the db-formatted service objects
|
||||
# to new-world objects here
|
||||
if db_service:
|
||||
return service_obj.Service._from_db_object(context,
|
||||
service_obj.Service(),
|
||||
db_service)
|
||||
|
||||
def service_update(self, context, host_name, binary, params_to_update):
|
||||
"""
|
||||
@@ -482,8 +496,15 @@ class HostAPI(compute_api.HostAPI):
|
||||
:param binary: The name of the executable that the service runs as
|
||||
:param params_to_update: eg. {'disabled': True}
|
||||
"""
|
||||
return self.cells_rpcapi.service_update(
|
||||
db_service = self.cells_rpcapi.service_update(
|
||||
context, host_name, binary, params_to_update)
|
||||
# NOTE(danms): Currently cells does not support objects as
|
||||
# return values, so just convert the db-formatted service objects
|
||||
# to new-world objects here
|
||||
if db_service:
|
||||
return service_obj.Service._from_db_object(context,
|
||||
service_obj.Service(),
|
||||
db_service)
|
||||
|
||||
def instance_get_all_by_host(self, context, host_name):
|
||||
"""Get all instances by host. Host might have a cell prepended
|
||||
|
||||
@@ -61,6 +61,10 @@ class DbDriver(api.ServiceGroupDriver):
|
||||
# conductor, then the timestamp will be a string and needs to be
|
||||
# converted back to a datetime.
|
||||
last_heartbeat = timeutils.parse_strtime(last_heartbeat)
|
||||
else:
|
||||
# Objects have proper UTC timezones, but the timeutils comparison
|
||||
# below does not (and will fail)
|
||||
last_heartbeat = last_heartbeat.replace(tzinfo=None)
|
||||
# Timestamps in DB are UTC.
|
||||
elapsed = utils.total_seconds(timeutils.utcnow() - last_heartbeat)
|
||||
LOG.debug('DB_Driver.is_up last_heartbeat = %(lhb)s elapsed = %(el)s',
|
||||
|
||||
@@ -21,14 +21,16 @@ from nova import db
|
||||
from nova.openstack.common import timeutils
|
||||
from nova import test
|
||||
from nova.tests.api.openstack import fakes
|
||||
from nova.tests.objects import test_service
|
||||
from nova import utils
|
||||
|
||||
|
||||
TEST_COMPUTE_SERVICES = [dict(host='foo', topic='compute'),
|
||||
dict(host='bar', topic='compute'),
|
||||
dict(host='baz', topic='compute'),
|
||||
dict(host='plonk', topic='compute'),
|
||||
dict(host='wibble', topic='bogus'),
|
||||
service_base = test_service.fake_service
|
||||
TEST_COMPUTE_SERVICES = [dict(service_base, host='foo', topic='compute'),
|
||||
dict(service_base, host='bar', topic='compute'),
|
||||
dict(service_base, host='baz', topic='compute'),
|
||||
dict(service_base, host='plonk', topic='compute'),
|
||||
dict(service_base, host='wibble', topic='bogus'),
|
||||
]
|
||||
|
||||
|
||||
|
||||
@@ -26,42 +26,47 @@ from nova.openstack.common import timeutils
|
||||
from nova.servicegroup.drivers import db as db_driver
|
||||
from nova import test
|
||||
from nova.tests.api.openstack import fakes
|
||||
from nova.tests.objects import test_service
|
||||
|
||||
|
||||
fake_services_list = [
|
||||
{'binary': 'nova-scheduler',
|
||||
'host': 'host1',
|
||||
'id': 1,
|
||||
'disabled': True,
|
||||
'topic': 'scheduler',
|
||||
'updated_at': datetime.datetime(2012, 10, 29, 13, 42, 2),
|
||||
'created_at': datetime.datetime(2012, 9, 18, 2, 46, 27),
|
||||
'disabled_reason': 'test1'},
|
||||
{'binary': 'nova-compute',
|
||||
'host': 'host1',
|
||||
'id': 2,
|
||||
'disabled': True,
|
||||
'topic': 'compute',
|
||||
'updated_at': datetime.datetime(2012, 10, 29, 13, 42, 5),
|
||||
'created_at': datetime.datetime(2012, 9, 18, 2, 46, 27),
|
||||
'disabled_reason': 'test2'},
|
||||
{'binary': 'nova-scheduler',
|
||||
'host': 'host2',
|
||||
'id': 3,
|
||||
'disabled': False,
|
||||
'topic': 'scheduler',
|
||||
'updated_at': datetime.datetime(2012, 9, 19, 6, 55, 34),
|
||||
'created_at': datetime.datetime(2012, 9, 18, 2, 46, 28),
|
||||
'disabled_reason': ''},
|
||||
{'binary': 'nova-compute',
|
||||
'host': 'host2',
|
||||
'id': 4,
|
||||
'disabled': True,
|
||||
'topic': 'compute',
|
||||
'updated_at': datetime.datetime(2012, 9, 18, 8, 3, 38),
|
||||
'created_at': datetime.datetime(2012, 9, 18, 2, 46, 28),
|
||||
'disabled_reason': 'test4'},
|
||||
]
|
||||
dict(test_service.fake_service,
|
||||
binary='nova-scheduler',
|
||||
host='host1',
|
||||
id=1,
|
||||
disabled=True,
|
||||
topic='scheduler',
|
||||
updated_at=datetime.datetime(2012, 10, 29, 13, 42, 2),
|
||||
created_at=datetime.datetime(2012, 9, 18, 2, 46, 27),
|
||||
disabled_reason='test1'),
|
||||
dict(test_service.fake_service,
|
||||
binary='nova-compute',
|
||||
host='host1',
|
||||
id=2,
|
||||
disabled=True,
|
||||
topic='compute',
|
||||
updated_at=datetime.datetime(2012, 10, 29, 13, 42, 5),
|
||||
created_at=datetime.datetime(2012, 9, 18, 2, 46, 27),
|
||||
disabled_reason='test2'),
|
||||
dict(test_service.fake_service,
|
||||
binary='nova-scheduler',
|
||||
host='host2',
|
||||
id=3,
|
||||
disabled=False,
|
||||
topic='scheduler',
|
||||
updated_at=datetime.datetime(2012, 9, 19, 6, 55, 34),
|
||||
created_at=datetime.datetime(2012, 9, 18, 2, 46, 28),
|
||||
disabled_reason=''),
|
||||
dict(test_service.fake_service,
|
||||
binary='nova-compute',
|
||||
host='host2',
|
||||
id=4,
|
||||
disabled=True,
|
||||
topic='compute',
|
||||
updated_at=datetime.datetime(2012, 9, 18, 8, 3, 38),
|
||||
created_at=datetime.datetime(2012, 9, 18, 2, 46, 28),
|
||||
disabled_reason='test4'),
|
||||
]
|
||||
|
||||
|
||||
class FakeRequest(object):
|
||||
@@ -112,6 +117,7 @@ def fake_service_update(context, service_id, values):
|
||||
service = fake_service_get_by_id(service_id)
|
||||
if service is None:
|
||||
raise exception.ServiceNotFound(service_id=service_id)
|
||||
return service
|
||||
|
||||
|
||||
def fake_utcnow():
|
||||
@@ -322,6 +328,7 @@ class ServicesTest(test.TestCase):
|
||||
def test_services_enable(self):
|
||||
def _service_update(context, service_id, values):
|
||||
self.assertEqual(values['disabled_reason'], None)
|
||||
return dict(test_service.fake_service, **values)
|
||||
|
||||
self.stubs.Set(db, "service_update", _service_update)
|
||||
|
||||
|
||||
@@ -23,15 +23,17 @@ from nova import db
|
||||
from nova.openstack.common import timeutils
|
||||
from nova import test
|
||||
from nova.tests.api.openstack import fakes
|
||||
from nova.tests.objects import test_service
|
||||
from nova import utils
|
||||
import urllib
|
||||
|
||||
|
||||
TEST_COMPUTE_SERVICES = [dict(host='foo', topic='compute'),
|
||||
dict(host='bar', topic='compute'),
|
||||
dict(host='baz', topic='compute'),
|
||||
dict(host='plonk', topic='compute'),
|
||||
dict(host='wibble', topic='bogus'),
|
||||
service_base = test_service.fake_service
|
||||
TEST_COMPUTE_SERVICES = [dict(service_base, host='foo', topic='compute'),
|
||||
dict(service_base, host='bar', topic='compute'),
|
||||
dict(service_base, host='baz', topic='compute'),
|
||||
dict(service_base, host='plonk', topic='compute'),
|
||||
dict(service_base, host='wibble', topic='bogus'),
|
||||
]
|
||||
|
||||
|
||||
|
||||
@@ -25,42 +25,47 @@ from nova.openstack.common import timeutils
|
||||
from nova.servicegroup.drivers import db as db_driver
|
||||
from nova import test
|
||||
from nova.tests.api.openstack import fakes
|
||||
from nova.tests.objects import test_service
|
||||
|
||||
|
||||
fake_services_list = [
|
||||
{'binary': 'nova-scheduler',
|
||||
'host': 'host1',
|
||||
'id': 1,
|
||||
'disabled': True,
|
||||
'topic': 'scheduler',
|
||||
'updated_at': datetime.datetime(2012, 10, 29, 13, 42, 2),
|
||||
'created_at': datetime.datetime(2012, 9, 18, 2, 46, 27),
|
||||
'disabled_reason': 'test1'},
|
||||
{'binary': 'nova-compute',
|
||||
'host': 'host1',
|
||||
'id': 2,
|
||||
'disabled': True,
|
||||
'topic': 'compute',
|
||||
'updated_at': datetime.datetime(2012, 10, 29, 13, 42, 5),
|
||||
'created_at': datetime.datetime(2012, 9, 18, 2, 46, 27),
|
||||
'disabled_reason': 'test2'},
|
||||
{'binary': 'nova-scheduler',
|
||||
'host': 'host2',
|
||||
'id': 3,
|
||||
'disabled': False,
|
||||
'topic': 'scheduler',
|
||||
'updated_at': datetime.datetime(2012, 9, 19, 6, 55, 34),
|
||||
'created_at': datetime.datetime(2012, 9, 18, 2, 46, 28),
|
||||
'disabled_reason': ''},
|
||||
{'binary': 'nova-compute',
|
||||
'host': 'host2',
|
||||
'id': 4,
|
||||
'disabled': True,
|
||||
'topic': 'compute',
|
||||
'updated_at': datetime.datetime(2012, 9, 18, 8, 3, 38),
|
||||
'created_at': datetime.datetime(2012, 9, 18, 2, 46, 28),
|
||||
'disabled_reason': 'test4'},
|
||||
]
|
||||
dict(test_service.fake_service,
|
||||
binary='nova-scheduler',
|
||||
host='host1',
|
||||
id=1,
|
||||
disabled=True,
|
||||
topic='scheduler',
|
||||
updated_at=datetime.datetime(2012, 10, 29, 13, 42, 2),
|
||||
created_at=datetime.datetime(2012, 9, 18, 2, 46, 27),
|
||||
disabled_reason='test1'),
|
||||
dict(test_service.fake_service,
|
||||
binary='nova-compute',
|
||||
host='host1',
|
||||
id=2,
|
||||
disabled=True,
|
||||
topic='compute',
|
||||
updated_at=datetime.datetime(2012, 10, 29, 13, 42, 5),
|
||||
created_at=datetime.datetime(2012, 9, 18, 2, 46, 27),
|
||||
disabled_reason='test2'),
|
||||
dict(test_service.fake_service,
|
||||
binary='nova-scheduler',
|
||||
host='host2',
|
||||
id=3,
|
||||
disabled=False,
|
||||
topic='scheduler',
|
||||
updated_at=datetime.datetime(2012, 9, 19, 6, 55, 34),
|
||||
created_at=datetime.datetime(2012, 9, 18, 2, 46, 28),
|
||||
disabled_reason=''),
|
||||
dict(test_service.fake_service,
|
||||
binary='nova-compute',
|
||||
host='host2',
|
||||
id=4,
|
||||
disabled=True,
|
||||
topic='compute',
|
||||
updated_at=datetime.datetime(2012, 9, 18, 8, 3, 38),
|
||||
created_at=datetime.datetime(2012, 9, 18, 2, 46, 28),
|
||||
disabled_reason='test4'),
|
||||
]
|
||||
|
||||
|
||||
class FakeRequest(object):
|
||||
@@ -111,6 +116,7 @@ def fake_service_update(context, service_id, values):
|
||||
service = fake_service_get_by_id(service_id)
|
||||
if service is None:
|
||||
raise exception.ServiceNotFound(service_id=service_id)
|
||||
return service
|
||||
|
||||
|
||||
def fake_utcnow():
|
||||
@@ -229,6 +235,7 @@ class ServicesTest(test.TestCase):
|
||||
def test_services_enable(self):
|
||||
def _service_update(context, service_id, values):
|
||||
self.assertEqual(values['disabled_reason'], None)
|
||||
return test_service.fake_service
|
||||
|
||||
self.stubs.Set(db, "service_update", _service_update)
|
||||
|
||||
|
||||
@@ -2556,7 +2556,7 @@ class ServersControllerCreateTest(test.TestCase):
|
||||
self.validation_fail_instance_destroy_called = True
|
||||
|
||||
self.stubs.Set(compute_api.API, '_validate_bdm', _validate_bdm)
|
||||
self.stubs.Set(compute_api.db, 'instance_destroy', _instance_destroy)
|
||||
self.stubs.Set(db, 'instance_destroy', _instance_destroy)
|
||||
|
||||
for _ in xrange(len(bdm_exceptions)):
|
||||
params = {'block_device_mapping_v2': [bdm.copy()]}
|
||||
|
||||
@@ -31,11 +31,13 @@ from nova.objects import base as obj_base
|
||||
from nova.objects import instance as instance_obj
|
||||
from nova.objects import instance_info_cache
|
||||
from nova.objects import migration as migration_obj
|
||||
from nova.objects import service as service_obj
|
||||
from nova.openstack.common import timeutils
|
||||
from nova.openstack.common import uuidutils
|
||||
from nova import quota
|
||||
from nova import test
|
||||
from nova.tests.objects import test_migration
|
||||
from nova.tests.objects import test_service
|
||||
|
||||
|
||||
FAKE_IMAGE_REF = 'fake-image-ref'
|
||||
@@ -405,9 +407,9 @@ class _ComputeAPIUnitTestMixIn(object):
|
||||
|
||||
self.context.elevated().MultipleTimes().AndReturn(self.context)
|
||||
db.service_get_by_compute_host(self.context, inst.host).AndReturn(
|
||||
'fake-service')
|
||||
test_service.fake_service)
|
||||
self.compute_api.servicegroup_api.service_is_up(
|
||||
'fake-service').AndReturn(inst.host != 'down-host')
|
||||
mox.IsA(service_obj.Service)).AndReturn(inst.host != 'down-host')
|
||||
|
||||
if self.is_cells:
|
||||
rpcapi = self.compute_api.cells_rpcapi
|
||||
|
||||
@@ -20,6 +20,7 @@ from nova import context
|
||||
from nova import exception
|
||||
from nova.openstack.common import rpc
|
||||
from nova import test
|
||||
from nova.tests.objects import test_service
|
||||
|
||||
|
||||
class ComputeHostAPITestCase(test.TestCase):
|
||||
@@ -28,6 +29,13 @@ class ComputeHostAPITestCase(test.TestCase):
|
||||
self.host_api = compute.HostAPI()
|
||||
self.ctxt = context.get_admin_context()
|
||||
|
||||
def _compare_obj(self, obj, db_obj):
|
||||
test_service.compare(obj, db_obj)
|
||||
|
||||
def _compare_objs(self, obj_list, db_obj_list):
|
||||
for index, obj in enumerate(obj_list):
|
||||
test_service.compare(obj, db_obj_list[index])
|
||||
|
||||
def _mock_rpc_call(self, expected_message, result=None):
|
||||
if result is None:
|
||||
result = 'fake-result'
|
||||
@@ -83,7 +91,7 @@ class ComputeHostAPITestCase(test.TestCase):
|
||||
|
||||
def test_get_host_uptime_service_down(self):
|
||||
def fake_service_get_by_compute_host(context, host_name):
|
||||
return {id: 1}
|
||||
return dict(test_service.fake_service, id=1)
|
||||
self.stubs.Set(self.host_api.db, 'service_get_by_compute_host',
|
||||
fake_service_get_by_compute_host)
|
||||
|
||||
@@ -121,10 +129,10 @@ class ComputeHostAPITestCase(test.TestCase):
|
||||
self.assertEqual('fake-result', result)
|
||||
|
||||
def test_service_get_all_no_zones(self):
|
||||
services = [dict(id=1, key1='val1', key2='val2', topic='compute',
|
||||
host='host1'),
|
||||
dict(id=2, key1='val2', key3='val3', topic='compute',
|
||||
host='host2')]
|
||||
services = [dict(test_service.fake_service,
|
||||
id=1, topic='compute', host='host1'),
|
||||
dict(test_service.fake_service,
|
||||
topic='compute', host='host2')]
|
||||
|
||||
self.mox.StubOutWithMock(self.host_api.db,
|
||||
'service_get_all')
|
||||
@@ -135,7 +143,7 @@ class ComputeHostAPITestCase(test.TestCase):
|
||||
self.mox.ReplayAll()
|
||||
result = self.host_api.service_get_all(self.ctxt)
|
||||
self.mox.VerifyAll()
|
||||
self.assertEqual(services, result)
|
||||
self._compare_objs(result, services)
|
||||
|
||||
# Test no filters #2
|
||||
self.mox.ResetAll()
|
||||
@@ -144,7 +152,7 @@ class ComputeHostAPITestCase(test.TestCase):
|
||||
self.mox.ReplayAll()
|
||||
result = self.host_api.service_get_all(self.ctxt, filters={})
|
||||
self.mox.VerifyAll()
|
||||
self.assertEqual(services, result)
|
||||
self._compare_objs(result, services)
|
||||
|
||||
# Test w/ filter
|
||||
self.mox.ResetAll()
|
||||
@@ -152,15 +160,15 @@ class ComputeHostAPITestCase(test.TestCase):
|
||||
disabled=None).AndReturn(services)
|
||||
self.mox.ReplayAll()
|
||||
result = self.host_api.service_get_all(self.ctxt,
|
||||
filters=dict(key1='val2'))
|
||||
filters=dict(host='host2'))
|
||||
self.mox.VerifyAll()
|
||||
self.assertEqual([services[1]], result)
|
||||
self._compare_objs(result, [services[1]])
|
||||
|
||||
def test_service_get_all(self):
|
||||
services = [dict(id=1, key1='val1', key2='val2', topic='compute',
|
||||
host='host1'),
|
||||
dict(id=2, key1='val2', key3='val3', topic='compute',
|
||||
host='host2')]
|
||||
services = [dict(test_service.fake_service,
|
||||
topic='compute', host='host1'),
|
||||
dict(test_service.fake_service,
|
||||
topic='compute', host='host2')]
|
||||
exp_services = []
|
||||
for service in services:
|
||||
exp_service = {}
|
||||
@@ -176,7 +184,7 @@ class ComputeHostAPITestCase(test.TestCase):
|
||||
self.mox.ReplayAll()
|
||||
result = self.host_api.service_get_all(self.ctxt, set_zones=True)
|
||||
self.mox.VerifyAll()
|
||||
self.assertEqual(exp_services, result)
|
||||
self._compare_objs(result, exp_services)
|
||||
|
||||
# Test no filters #2
|
||||
self.mox.ResetAll()
|
||||
@@ -186,7 +194,7 @@ class ComputeHostAPITestCase(test.TestCase):
|
||||
result = self.host_api.service_get_all(self.ctxt, filters={},
|
||||
set_zones=True)
|
||||
self.mox.VerifyAll()
|
||||
self.assertEqual(exp_services, result)
|
||||
self._compare_objs(result, exp_services)
|
||||
|
||||
# Test w/ filter
|
||||
self.mox.ResetAll()
|
||||
@@ -194,10 +202,10 @@ class ComputeHostAPITestCase(test.TestCase):
|
||||
disabled=None).AndReturn(services)
|
||||
self.mox.ReplayAll()
|
||||
result = self.host_api.service_get_all(self.ctxt,
|
||||
filters=dict(key1='val2'),
|
||||
filters=dict(host='host2'),
|
||||
set_zones=True)
|
||||
self.mox.VerifyAll()
|
||||
self.assertEqual([exp_services[1]], result)
|
||||
self._compare_objs(result, [exp_services[1]])
|
||||
|
||||
# Test w/ zone filter but no set_zones arg.
|
||||
self.mox.ResetAll()
|
||||
@@ -208,29 +216,29 @@ class ComputeHostAPITestCase(test.TestCase):
|
||||
result = self.host_api.service_get_all(self.ctxt,
|
||||
filters=filters)
|
||||
self.mox.VerifyAll()
|
||||
self.assertEqual(exp_services, result)
|
||||
self._compare_objs(result, exp_services)
|
||||
|
||||
def test_service_get_by_compute_host(self):
|
||||
self.mox.StubOutWithMock(self.host_api.db,
|
||||
'service_get_by_compute_host')
|
||||
|
||||
self.host_api.db.service_get_by_compute_host(self.ctxt,
|
||||
'fake-host').AndReturn('fake-response')
|
||||
'fake-host').AndReturn(test_service.fake_service)
|
||||
self.mox.ReplayAll()
|
||||
result = self.host_api.service_get_by_compute_host(self.ctxt,
|
||||
'fake-host')
|
||||
self.assertEqual('fake-response', result)
|
||||
self.assertEqual(test_service.fake_service['id'], result.id)
|
||||
|
||||
def test_service_update(self):
|
||||
host_name = 'fake-host'
|
||||
binary = 'nova-compute'
|
||||
params_to_update = dict(disabled=True)
|
||||
service_id = 42
|
||||
expected_result = {'id': service_id}
|
||||
expected_result = dict(test_service.fake_service, id=service_id)
|
||||
|
||||
self.mox.StubOutWithMock(self.host_api.db, 'service_get_by_args')
|
||||
self.host_api.db.service_get_by_args(self.ctxt,
|
||||
host_name, binary).AndReturn({'id': service_id})
|
||||
host_name, binary).AndReturn(expected_result)
|
||||
|
||||
self.mox.StubOutWithMock(self.host_api.db, 'service_update')
|
||||
self.host_api.db.service_update(
|
||||
@@ -240,7 +248,7 @@ class ComputeHostAPITestCase(test.TestCase):
|
||||
|
||||
result = self.host_api.service_update(
|
||||
self.ctxt, host_name, binary, params_to_update)
|
||||
self.assertEqual(expected_result, result)
|
||||
self._compare_obj(result, expected_result)
|
||||
|
||||
def test_instance_get_all_by_host(self):
|
||||
self.mox.StubOutWithMock(self.host_api.db,
|
||||
@@ -288,12 +296,12 @@ class ComputeHostAPICellsTestCase(ComputeHostAPITestCase):
|
||||
None).AndReturn(result)
|
||||
|
||||
def test_service_get_all_no_zones(self):
|
||||
services = [dict(id=1, key1='val1', key2='val2', topic='compute',
|
||||
host='host1'),
|
||||
dict(id=2, key1='val2', key3='val3', topic='compute',
|
||||
host='host2')]
|
||||
services = [dict(test_service.fake_service,
|
||||
id=1, topic='compute', host='host1'),
|
||||
dict(test_service.fake_service,
|
||||
id=2, topic='compute', host='host2')]
|
||||
|
||||
fake_filters = {'key1': 'val1'}
|
||||
fake_filters = {'host': 'host1'}
|
||||
self.mox.StubOutWithMock(self.host_api.cells_rpcapi,
|
||||
'service_get_all')
|
||||
self.host_api.cells_rpcapi.service_get_all(self.ctxt,
|
||||
@@ -301,12 +309,14 @@ class ComputeHostAPICellsTestCase(ComputeHostAPITestCase):
|
||||
self.mox.ReplayAll()
|
||||
result = self.host_api.service_get_all(self.ctxt,
|
||||
filters=fake_filters)
|
||||
self.assertEqual(services, result)
|
||||
self._compare_objs(result, services)
|
||||
|
||||
def test_service_get_all(self):
|
||||
services = [dict(id=1, key1='val1', key2='val2', topic='compute',
|
||||
services = [dict(test_service.fake_service,
|
||||
id=1, key1='val1', key2='val2', topic='compute',
|
||||
host='host1'),
|
||||
dict(id=2, key1='val2', key3='val3', topic='compute',
|
||||
dict(test_service.fake_service,
|
||||
id=2, key1='val2', key3='val3', topic='compute',
|
||||
host='host2')]
|
||||
exp_services = []
|
||||
for service in services:
|
||||
@@ -324,7 +334,7 @@ class ComputeHostAPICellsTestCase(ComputeHostAPITestCase):
|
||||
filters=fake_filters,
|
||||
set_zones=True)
|
||||
self.mox.VerifyAll()
|
||||
self.assertEqual(exp_services, result)
|
||||
self._compare_objs(result, exp_services)
|
||||
|
||||
# Test w/ zone filter but no set_zones arg.
|
||||
self.mox.ResetAll()
|
||||
@@ -337,25 +347,25 @@ class ComputeHostAPICellsTestCase(ComputeHostAPITestCase):
|
||||
result = self.host_api.service_get_all(self.ctxt,
|
||||
filters=fake_filters)
|
||||
self.mox.VerifyAll()
|
||||
self.assertEqual(exp_services, result)
|
||||
self._compare_objs(result, exp_services)
|
||||
|
||||
def test_service_get_by_compute_host(self):
|
||||
self.mox.StubOutWithMock(self.host_api.cells_rpcapi,
|
||||
'service_get_by_compute_host')
|
||||
|
||||
self.host_api.cells_rpcapi.service_get_by_compute_host(self.ctxt,
|
||||
'fake-host').AndReturn('fake-response')
|
||||
'fake-host').AndReturn(test_service.fake_service)
|
||||
self.mox.ReplayAll()
|
||||
result = self.host_api.service_get_by_compute_host(self.ctxt,
|
||||
'fake-host')
|
||||
self.assertEqual('fake-response', result)
|
||||
self._compare_obj(result, test_service.fake_service)
|
||||
|
||||
def test_service_update(self):
|
||||
host_name = 'fake-host'
|
||||
binary = 'nova-compute'
|
||||
params_to_update = dict(disabled=True)
|
||||
service_id = 42
|
||||
expected_result = {'id': service_id}
|
||||
expected_result = dict(test_service.fake_service, id=service_id)
|
||||
|
||||
self.mox.StubOutWithMock(self.host_api.cells_rpcapi, 'service_update')
|
||||
self.host_api.cells_rpcapi.service_update(
|
||||
@@ -366,7 +376,7 @@ class ComputeHostAPICellsTestCase(ComputeHostAPITestCase):
|
||||
|
||||
result = self.host_api.service_update(
|
||||
self.ctxt, host_name, binary, params_to_update)
|
||||
self.assertEqual(expected_result, result)
|
||||
self._compare_obj(result, expected_result)
|
||||
|
||||
def test_instance_get_all_by_host(self):
|
||||
instances = [dict(id=1, cell_name='cell1', host='host1'),
|
||||
|
||||
@@ -17,6 +17,9 @@
|
||||
Provides some fake hosts to test host and service related functions
|
||||
"""
|
||||
|
||||
from nova.tests.objects import test_service
|
||||
|
||||
|
||||
HOST_LIST = [
|
||||
{"host_name": "host_c1", "service": "compute", "zone": "nova"},
|
||||
{"host_name": "host_c2", "service": "compute", "zone": "nova"}]
|
||||
@@ -27,6 +30,8 @@ HOST_LIST_NOVA_ZONE = [
|
||||
{"host_name": "host_c1", "service": "compute", "zone": "nova"},
|
||||
{"host_name": "host_c2", "service": "compute", "zone": "nova"}]
|
||||
|
||||
service_base = test_service.fake_service
|
||||
|
||||
SERVICES_LIST = [
|
||||
{"host": "host_c1", "topic": "compute"},
|
||||
{"host": "host_c2", "topic": "compute"}]
|
||||
dict(service_base, host='host_c1', topic='compute'),
|
||||
dict(service_base, host='host_c2', topic='compute')]
|
||||
|
||||
Reference in New Issue
Block a user