Check number of replaced hosts in update_reservation

InstancePlugin fails to check the number of replaced hosts in
the update_reservation method. It wrongly succeeds to update
the instance reservation via the Lease Update API.

Change-Id: Ib8b436a4a3b40a0d3005d03dcd5af465dd10f088
Closes-Bug: #1786007
This commit is contained in:
Masahito Muroi 2018-08-08 18:58:48 +09:00
parent 3cdd01a3cd
commit 1d8a6a0894
2 changed files with 31 additions and 0 deletions

View File

@ -420,6 +420,10 @@ class VirtualInstancePlugin(base.BasePlugin, nova.NovaClientWrapper):
"active status.")
raise mgr_exceptions.CantUpdateParameter(err_msg)
if (new_values['amount'] - reservation['amount'] !=
(len(changed_hosts['added']) - len(changed_hosts['removed']))):
raise mgr_exceptions.NotEnoughHostsAvailable()
db_api.instance_reservation_update(
reservation['resource_id'],
{key: new_values[key] for key in updatable})

View File

@ -760,6 +760,33 @@ class TestVirtualInstancePlugin(tests.TestCase):
'reservation-id1')
mock_update_resource.assert_called_once_with('reservation-id1')
def test_update_reservation_not_enough_hosts(self):
plugin = instance_plugin.VirtualInstancePlugin()
old_reservation = {
'id': 'reservation-id1',
'status': 'pending',
'lease_id': 'lease-id1',
'resource_id': 'instance-reservation-id1',
'vcpus': 2, 'memory_mb': 1024, 'disk_gb': 100,
'amount': 2, 'affinity': False,
'resource_properties': ''}
mock_reservation_get = self.patch(db_api, 'reservation_get')
mock_reservation_get.return_value = old_reservation
mock_lease_get = self.patch(db_api, 'lease_get')
mock_lease_get.return_value = {'start_date': '2020-07-07 18:00',
'end_date': '2020-07-07 19:00'}
mock_pickup_hosts = self.patch(plugin, 'pickup_hosts')
mock_pickup_hosts.return_value = {
'added': set(), 'removed': set(['host-id2'])}
new_values = {'vcpus': 4, 'disk_gb': 200}
self.assertRaises(mgr_exceptions.NotEnoughHostsAvailable,
plugin.update_reservation, 'reservation-id1',
new_values)
def test_update_flavor_in_active(self):
plugin = instance_plugin.VirtualInstancePlugin()