Unmap compute nodes when deleting host mapping

When we delete a host mapping in the api database, we need to mark
the compute node record as unmapped. Otherwise we can never re-discover
it if added back.

Change-Id: I9348827c5fd0364727e49cb8fae786cb2a29472a
Closes-Bug: #1735719
(cherry picked from commit 8ad36d15e4)
This commit is contained in:
Dan Smith 2017-12-01 07:20:34 -08:00 committed by Lee Yarwood
parent 83ffc76edd
commit f0525e8f15
3 changed files with 69 additions and 2 deletions

View File

@ -1795,11 +1795,16 @@ class CellV2Commands(object):
with context.target_cell(ctxt, cell_mapping) as cctxt:
instances = objects.InstanceList.get_by_host(cctxt, host)
nodes = objects.ComputeNodeList.get_all_by_host(cctxt, host)
if instances:
print(_('There are instances on the host %s.') % host)
return 4
for node in nodes:
node.mapped = 0
node.save()
host_mapping.destroy()
return 0

View File

@ -264,3 +264,56 @@ class NovaManageDBIronicTest(test.TestCase):
ret = self.commands.ironic_flavor_migration('test', 'fake-host3',
'fake-node4', False, False)
self.assertEqual(2, ret)
class NovaManageCellV2Test(test.TestCase):
def setUp(self):
super(NovaManageCellV2Test, self).setUp()
self.commands = manage.CellV2Commands()
self.context = context.RequestContext('fake-user', 'fake-project')
self.service1 = objects.Service(context=self.context,
host='fake-host1',
binary='nova-compute',
topic='fake-host1',
report_count=1,
disabled=False,
disabled_reason=None,
availability_zone='nova',
forced_down=False)
self.service1.create()
self.cn1 = objects.ComputeNode(context=self.context,
service_id=self.service1.id,
host='fake-host1',
hypervisor_type='ironic',
vcpus=1,
memory_mb=1024,
local_gb=10,
vcpus_used=1,
memory_mb_used=1024,
local_gb_used=10,
hypervisor_version=0,
hypervisor_hostname='fake-node1',
cpu_info='{}')
self.cn1.create()
def test_delete_host(self):
cells = objects.CellMappingList.get_all(self.context)
self.commands.discover_hosts()
# We should have one mapped node
cns = objects.ComputeNodeList.get_all(self.context)
self.assertEqual(1, len(cns))
self.assertEqual(1, cns[0].mapped)
for cell in cells:
r = self.commands.delete_host(cell.uuid, 'fake-host1')
if r == 0:
break
# Our node should now be unmapped
cns = objects.ComputeNodeList.get_all(self.context)
self.assertEqual(1, len(cns))
self.assertEqual(0, cns[0].mapped)

View File

@ -1738,7 +1738,8 @@ class CellV2CommandsTestCase(test.NoDBTestCase):
uuidsentinel.cell1), output)
@mock.patch.object(objects.InstanceList, 'get_by_host')
def test_delete_host_instances_exist(self, mock_get_by_host):
@mock.patch.object(objects.ComputeNodeList, 'get_all_by_host')
def test_delete_host_instances_exist(self, mock_get_cn, mock_get_by_host):
"""Tests trying to delete a host but the host has instances."""
ctxt = context.get_admin_context()
# create the cell mapping
@ -1752,6 +1753,7 @@ class CellV2CommandsTestCase(test.NoDBTestCase):
hm.create()
mock_get_by_host.return_value = [objects.Instance(
ctxt, uuid=uuidsentinel.instance)]
mock_get_cn.return_value = []
self.assertEqual(4, self.commands.delete_host(uuidsentinel.cell1,
'fake-host'))
output = self.output.getvalue().strip()
@ -1762,7 +1764,9 @@ class CellV2CommandsTestCase(test.NoDBTestCase):
@mock.patch.object(objects.InstanceList, 'get_by_host',
return_value=[])
@mock.patch.object(objects.HostMapping, 'destroy')
def test_delete_host_success(self, mock_destroy, mock_get_by_host):
@mock.patch.object(objects.ComputeNodeList, 'get_all_by_host')
def test_delete_host_success(self, mock_get_cn, mock_destroy,
mock_get_by_host):
"""Tests trying to delete a host that has not instances."""
ctxt = context.get_admin_context()
# create the cell mapping
@ -1775,6 +1779,8 @@ class CellV2CommandsTestCase(test.NoDBTestCase):
context=ctxt, host='fake-host', cell_mapping=cm1)
hm.create()
mock_get_cn.return_value = [mock.MagicMock(), mock.MagicMock()]
self.assertEqual(0, self.commands.delete_host(uuidsentinel.cell1,
'fake-host'))
output = self.output.getvalue().strip()
@ -1782,6 +1788,9 @@ class CellV2CommandsTestCase(test.NoDBTestCase):
mock_get_by_host.assert_called_once_with(
test.MatchType(context.RequestContext), 'fake-host')
mock_destroy.assert_called_once_with()
for node in mock_get_cn.return_value:
self.assertEqual(0, node.mapped)
node.save.assert_called_once_with()
class TestNovaManageMain(test.NoDBTestCase):