diff options
author | zhangbailin <zhangbailin@inspur.com> | 2020-08-28 09:48:49 +0800 |
---|---|---|
committer | Brin Zhang <zhangbailin@inspur.com> | 2020-09-08 05:48:10 +0000 |
commit | 3f466079f92084f62f0d0f58b0f0c1bb4fb0ac5b (patch) | |
tree | ddb02621011115f9943638d16898c9386f87696a | |
parent | 1c28a88afdfd012fa8b962a25a4b75f0198ada1b (diff) | |
download | nova-3f466079f92084f62f0d0f58b0f0c1bb4fb0ac5b.tar.gz |
Reject resize operation for accelerator
When resize with a new flavor with accelerator, we should raise
forbidden exception to reject the resize operation.
Add related unit test.
Follow up the comment:
https://review.opendev.org/#/c/715326/22/nova/compute/api.py@3907
Conflicts:
nova/compute/api.py
nova/tests/unit/compute/test_compute_api.py
NOTE(brinzhang): Conflicts are due to change
eef4b5435e7cdfe53ee9d9265d96c7dd278d9e93 ("api: Reject
non-spawn operations for vTPM") which modified the same code.
Add 'extra_specs={}' as a request parameter in test_resize_quota_check()
in nova/tests/unit/compute/test_compute_api.py. Actually this change is
required due to not having patch
I147bf4d95e6d86ff1f967a8ce37260730f21d236 in stable ussuri.
Co-Authored-By: Wenping Song <songwenping@inspur.com>
Closes-Bug: #1893751
Change-Id: Ie20a620588e3b0d18055c34ec7671e91e6bf6ee0
(cherry picked from commit 97267fc05c60b60761ebe8ec2773e8920148ae06)
-rw-r--r-- | nova/compute/api.py | 6 | ||||
-rw-r--r-- | nova/tests/unit/compute/test_compute_api.py | 21 |
2 files changed, 26 insertions, 1 deletions
diff --git a/nova/compute/api.py b/nova/compute/api.py index 2b9ae0f982..0ec92174ec 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -3875,6 +3875,12 @@ class API(base.Base): else: new_instance_type = flavors.get_flavor_by_flavor_id( flavor_id, read_deleted="no") + # NOTE(wenping): We use this instead of the 'block_accelerator' + # decorator since the operation can differ depending on args, + # and for resize we have two flavors to worry about, we should + # reject resize with new flavor with accelerator. + if new_instance_type.extra_specs.get('accel:device_profile'): + raise exception.ForbiddenWithAccelerators() # Check to see if we're resizing to a zero-disk flavor which is # only supported with volume-backed servers. if (new_instance_type.get('root_gb') == 0 and diff --git a/nova/tests/unit/compute/test_compute_api.py b/nova/tests/unit/compute/test_compute_api.py index 624fdf2d74..c7515fedfc 100644 --- a/nova/tests/unit/compute/test_compute_api.py +++ b/nova/tests/unit/compute/test_compute_api.py @@ -2063,7 +2063,7 @@ class _ComputeAPIUnitTestMixIn(object): fake_inst = self._create_instance_obj() fake_inst.flavor = cur_flavor new_flavor = objects.Flavor(id=2, name='bar', vcpus=1, memory_mb=2048, - root_gb=10, disabled=False) + root_gb=10, disabled=False, extra_specs={}) mock_get.return_value = new_flavor mock_check.side_effect = exception.OverQuota( overs=['ram'], quotas={'cores': 1, 'ram': 2048}, @@ -2078,6 +2078,25 @@ class _ComputeAPIUnitTestMixIn(object): project_values={'cores': 1, 'ram': 2560}, project_id=fake_inst.project_id, user_id=fake_inst.user_id) + @mock.patch('nova.compute.api.API.get_instance_host_status', + new=mock.Mock(return_value=fields_obj.HostStatus.UP)) + @mock.patch('nova.compute.utils.is_volume_backed_instance', + new=mock.Mock(return_value=False)) + @mock.patch.object(flavors, 'get_flavor_by_flavor_id') + def test_resize__with_accelerator(self, mock_get_flavor): + """Ensure resizes are rejected if either flavor requests accelerator. + """ + fake_inst = self._create_instance_obj() + new_flavor = self._create_flavor( + id=200, flavorid='new-flavor-id', name='new_flavor', + disabled=False, extra_specs={'accel:device_profile': 'dp'}) + mock_get_flavor.return_value = new_flavor + + self.assertRaises( + exception.ForbiddenWithAccelerators, + self.compute_api.resize, + self.context, fake_inst, flavor_id=new_flavor.flavorid) + def test_migrate(self): self._test_migrate() |