Merge "Code clean up db.instance_get_all_by_host()"

This commit is contained in:
Jenkins 2015-05-21 20:42:15 +00:00 committed by Gerrit Code Review
commit e9c8dfc6b3
3 changed files with 22 additions and 23 deletions

View File

@ -3444,7 +3444,7 @@ class HostAPI(base.Base):
def instance_get_all_by_host(self, context, host_name): def instance_get_all_by_host(self, context, host_name):
"""Return all instances on the given host.""" """Return all instances on the given host."""
return self.db.instance_get_all_by_host(context, host_name) return objects.InstanceList.get_by_host(context, host_name)
def task_log_get_all(self, context, task_name, period_beginning, def task_log_get_all(self, context, task_name, period_beginning,
period_ending, host=None, state=None): period_ending, host=None, state=None):

View File

@ -30,6 +30,8 @@ from nova import exception
from nova import objects from nova import objects
from nova import test from nova import test
from nova.tests.unit.api.openstack import fakes from nova.tests.unit.api.openstack import fakes
from nova.tests.unit import fake_instance
TEST_HYPERS = [ TEST_HYPERS = [
dict(id=1, dict(id=1,
@ -154,7 +156,8 @@ def fake_instance_get_all_by_host(context, host):
results = [] results = []
for inst in TEST_SERVERS: for inst in TEST_SERVERS:
if inst['host'] == host: if inst['host'] == host:
results.append(inst) inst_obj = fake_instance.fake_instance_obj(context, **inst)
results.append(inst_obj)
return results return results
@ -207,8 +210,6 @@ class HypervisorsTestV21(test.NoDBTestCase):
fake_compute_node_get) fake_compute_node_get)
self.stubs.Set(db, 'compute_node_statistics', self.stubs.Set(db, 'compute_node_statistics',
fake_compute_node_statistics) fake_compute_node_statistics)
self.stubs.Set(db, 'instance_get_all_by_host',
fake_instance_get_all_by_host)
def test_view_hypervisor_nodetail_noservers(self): def test_view_hypervisor_nodetail_noservers(self):
result = self.controller._view_hypervisor( result = self.controller._view_hypervisor(
@ -358,18 +359,24 @@ class HypervisorsTestV21(test.NoDBTestCase):
req = self._get_request(True) req = self._get_request(True)
self.assertRaises(exc.HTTPNotFound, self.controller.search, req, 'a') self.assertRaises(exc.HTTPNotFound, self.controller.search, req, 'a')
def test_servers(self): @mock.patch.object(objects.InstanceList, 'get_by_host',
side_effect=fake_instance_get_all_by_host)
def test_servers(self, mock_get):
req = self._get_request(True) req = self._get_request(True)
result = self.controller.servers(req, 'hyper') result = self.controller.servers(req, 'hyper')
expected_dict = copy.deepcopy(self.INDEX_HYPER_DICTS) expected_dict = copy.deepcopy(self.INDEX_HYPER_DICTS)
expected_dict[0].update({'servers': [ expected_dict[0].update({'servers': [
dict(name="inst1", uuid="uuid1"), dict(uuid="uuid1"),
dict(name="inst3", uuid="uuid3")]}) dict(uuid="uuid3")]})
expected_dict[1].update({'servers': [ expected_dict[1].update({'servers': [
dict(name="inst2", uuid="uuid2"), dict(uuid="uuid2"),
dict(name="inst4", uuid="uuid4")]}) dict(uuid="uuid4")]})
for output in result['hypervisors']:
servers = output['servers']
for server in servers:
del server['name']
self.assertEqual(result, dict(hypervisors=expected_dict)) self.assertEqual(result, dict(hypervisors=expected_dict))
def test_servers_non_id(self): def test_servers_non_id(self):

View File

@ -290,13 +290,9 @@ class ComputeHostAPITestCase(test.TestCase):
self.ctxt, host_name, binary, params_to_update) self.ctxt, host_name, binary, params_to_update)
self._compare_obj(result, expected_result) self._compare_obj(result, expected_result)
def test_instance_get_all_by_host(self): @mock.patch.object(objects.InstanceList, 'get_by_host',
self.mox.StubOutWithMock(self.host_api.db, return_value = ['fake-responses'])
'instance_get_all_by_host') def test_instance_get_all_by_host(self, mock_get):
self.host_api.db.instance_get_all_by_host(self.ctxt,
'fake-host').AndReturn(['fake-responses'])
self.mox.ReplayAll()
result = self.host_api.instance_get_all_by_host(self.ctxt, result = self.host_api.instance_get_all_by_host(self.ctxt,
'fake-host') 'fake-host')
self.assertEqual(['fake-responses'], result) self.assertEqual(['fake-responses'], result)
@ -446,17 +442,13 @@ class ComputeHostAPICellsTestCase(ComputeHostAPITestCase):
service_delete.assert_called_once_with( service_delete.assert_called_once_with(
self.ctxt, cell_service_id) self.ctxt, cell_service_id)
def test_instance_get_all_by_host(self): @mock.patch.object(objects.InstanceList, 'get_by_host')
def test_instance_get_all_by_host(self, mock_get):
instances = [dict(id=1, cell_name='cell1', host='host1'), instances = [dict(id=1, cell_name='cell1', host='host1'),
dict(id=2, cell_name='cell2', host='host1'), dict(id=2, cell_name='cell2', host='host1'),
dict(id=3, cell_name='cell1', host='host2')] dict(id=3, cell_name='cell1', host='host2')]
self.mox.StubOutWithMock(self.host_api.db, mock_get.return_value = instances
'instance_get_all_by_host')
self.host_api.db.instance_get_all_by_host(self.ctxt,
'fake-host').AndReturn(instances)
self.mox.ReplayAll()
expected_result = [instances[0], instances[2]] expected_result = [instances[0], instances[2]]
cell_and_host = cells_utils.cell_with_item('cell1', 'fake-host') cell_and_host = cells_utils.cell_with_item('cell1', 'fake-host')
result = self.host_api.instance_get_all_by_host(self.ctxt, result = self.host_api.instance_get_all_by_host(self.ctxt,