Merge "Code clean up db.instance_get_all_by_host()"
This commit is contained in:
commit
e9c8dfc6b3
@ -3444,7 +3444,7 @@ class HostAPI(base.Base):
|
||||
|
||||
def instance_get_all_by_host(self, context, host_name):
|
||||
"""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,
|
||||
period_ending, host=None, state=None):
|
||||
|
@ -30,6 +30,8 @@ from nova import exception
|
||||
from nova import objects
|
||||
from nova import test
|
||||
from nova.tests.unit.api.openstack import fakes
|
||||
from nova.tests.unit import fake_instance
|
||||
|
||||
|
||||
TEST_HYPERS = [
|
||||
dict(id=1,
|
||||
@ -154,7 +156,8 @@ def fake_instance_get_all_by_host(context, host):
|
||||
results = []
|
||||
for inst in TEST_SERVERS:
|
||||
if inst['host'] == host:
|
||||
results.append(inst)
|
||||
inst_obj = fake_instance.fake_instance_obj(context, **inst)
|
||||
results.append(inst_obj)
|
||||
return results
|
||||
|
||||
|
||||
@ -207,8 +210,6 @@ class HypervisorsTestV21(test.NoDBTestCase):
|
||||
fake_compute_node_get)
|
||||
self.stubs.Set(db, '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):
|
||||
result = self.controller._view_hypervisor(
|
||||
@ -358,18 +359,24 @@ class HypervisorsTestV21(test.NoDBTestCase):
|
||||
req = self._get_request(True)
|
||||
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)
|
||||
result = self.controller.servers(req, 'hyper')
|
||||
|
||||
expected_dict = copy.deepcopy(self.INDEX_HYPER_DICTS)
|
||||
expected_dict[0].update({'servers': [
|
||||
dict(name="inst1", uuid="uuid1"),
|
||||
dict(name="inst3", uuid="uuid3")]})
|
||||
dict(uuid="uuid1"),
|
||||
dict(uuid="uuid3")]})
|
||||
expected_dict[1].update({'servers': [
|
||||
dict(name="inst2", uuid="uuid2"),
|
||||
dict(name="inst4", uuid="uuid4")]})
|
||||
dict(uuid="uuid2"),
|
||||
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))
|
||||
|
||||
def test_servers_non_id(self):
|
||||
|
@ -290,13 +290,9 @@ class ComputeHostAPITestCase(test.TestCase):
|
||||
self.ctxt, host_name, binary, params_to_update)
|
||||
self._compare_obj(result, expected_result)
|
||||
|
||||
def test_instance_get_all_by_host(self):
|
||||
self.mox.StubOutWithMock(self.host_api.db,
|
||||
'instance_get_all_by_host')
|
||||
|
||||
self.host_api.db.instance_get_all_by_host(self.ctxt,
|
||||
'fake-host').AndReturn(['fake-responses'])
|
||||
self.mox.ReplayAll()
|
||||
@mock.patch.object(objects.InstanceList, 'get_by_host',
|
||||
return_value = ['fake-responses'])
|
||||
def test_instance_get_all_by_host(self, mock_get):
|
||||
result = self.host_api.instance_get_all_by_host(self.ctxt,
|
||||
'fake-host')
|
||||
self.assertEqual(['fake-responses'], result)
|
||||
@ -446,17 +442,13 @@ class ComputeHostAPICellsTestCase(ComputeHostAPITestCase):
|
||||
service_delete.assert_called_once_with(
|
||||
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'),
|
||||
dict(id=2, cell_name='cell2', host='host1'),
|
||||
dict(id=3, cell_name='cell1', host='host2')]
|
||||
|
||||
self.mox.StubOutWithMock(self.host_api.db,
|
||||
'instance_get_all_by_host')
|
||||
|
||||
self.host_api.db.instance_get_all_by_host(self.ctxt,
|
||||
'fake-host').AndReturn(instances)
|
||||
self.mox.ReplayAll()
|
||||
mock_get.return_value = instances
|
||||
expected_result = [instances[0], instances[2]]
|
||||
cell_and_host = cells_utils.cell_with_item('cell1', 'fake-host')
|
||||
result = self.host_api.instance_get_all_by_host(self.ctxt,
|
||||
|
Loading…
Reference in New Issue
Block a user