Merge "Fix: volume request body for scheduler hints"

This commit is contained in:
Zuul 2023-09-20 11:56:08 +00:00 committed by Gerrit Code Review
commit 8ef9f74dc9
2 changed files with 51 additions and 0 deletions

View File

@ -298,5 +298,20 @@ class Volume(resource.Resource, metadata.MetadataMixin):
self._action(session, body)
def _prepare_request_body(self, patch, prepend_key):
body = self._body.dirty
# Scheduler hints is external to the standard volume request
# so pass it separately and not under the volume JSON object.
scheduler_hints = None
if 'OS-SCH-HNT:scheduler_hints' in body.keys():
scheduler_hints = body.pop('OS-SCH-HNT:scheduler_hints')
if prepend_key and self.resource_key is not None:
body = {self.resource_key: body}
# If scheduler hints was passed in the request but the value is
# None, it doesn't make a difference to include it.
if scheduler_hints:
body['OS-SCH-HNT:scheduler_hints'] = scheduler_hints
return body
VolumeDetail = Volume

View File

@ -10,6 +10,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import copy
from unittest import mock
from keystoneauth1 import adapter
@ -562,3 +563,38 @@ class TestVolumeActions(TestVolume):
self.sess.post.assert_called_with(
url, json=body, microversion=sot._max_microversion
)
def test__prepare_request_body(self):
sot = volume.Volume(**VOLUME)
body = sot._prepare_request_body(patch=False, prepend_key=True)
original_body = copy.deepcopy(sot._body.dirty)
# Verify that scheduler hints aren't modified after preparing request
# but also not part of 'volume' JSON object
self.assertEqual(
original_body['OS-SCH-HNT:scheduler_hints'],
body['OS-SCH-HNT:scheduler_hints'],
)
# Pop scheduler hints to verify other parameters in body
original_body.pop('OS-SCH-HNT:scheduler_hints')
# Verify that other request parameters are same but in 'volume' JSON
self.assertEqual(original_body, body['volume'])
def test_create_scheduler_hints(self):
sot = volume.Volume(**VOLUME)
sot._translate_response = mock.Mock()
sot.create(self.sess)
url = '/volumes'
volume_body = copy.deepcopy(VOLUME)
scheduler_hints = volume_body.pop('OS-SCH-HNT:scheduler_hints')
body = {
"volume": volume_body,
'OS-SCH-HNT:scheduler_hints': scheduler_hints,
}
self.sess.post.assert_called_with(
url,
json=body,
microversion='3.0',
headers={},
params={},
)