Merge "[OVN] Make binding profile validation more robust" into stable/yoga

This commit is contained in:
Zuul 2022-07-04 15:54:19 +00:00 committed by Gerrit Code Review
commit 6ac1a31002
3 changed files with 43 additions and 18 deletions

View File

@ -254,6 +254,11 @@ def validate_and_get_data_from_binding_profile(port):
) % constants.PORT_CAP_PARAM
raise n_exc.InvalidInput(error_message=msg)
# Note that only the keys mentioned in each parameter set, as defined in
# constants.OVN_PORT_BINDING_PROFILE_PARAMS, will be evaluated.
#
# Any surplus keys provided by Nova will be ignored and pruned from the
# Dict returned by this function.
for pbp_param_set in constants.OVN_PORT_BINDING_PROFILE_PARAMS:
if pbp_param_set.vnic_type:
if pbp_param_set.vnic_type != vnic_type:
@ -269,13 +274,10 @@ def validate_and_get_data_from_binding_profile(port):
pass
if len(param_dict) == 0:
continue
if len(param_dict) != len(param_keys):
if param_keys - binding_profile.keys():
msg = _('Invalid binding:profile. %s are all '
'required.') % param_keys
raise n_exc.InvalidInput(error_message=msg)
if (len(binding_profile) != len(param_keys)):
msg = _('Invalid binding:profile. too many parameters')
raise n_exc.InvalidInput(error_message=msg)
break
if not param_dict:

View File

@ -468,7 +468,7 @@ class TestValidateAndGetDataFromBindingProfile(base.BaseTestCase):
constants.OVNPortBindingProfileParamSet(
{
'key': [str],
'other_key': [str],
'other_key': [int],
'third_key': [str]
},
self.VNIC_FAKE_OTHER, constants.PORT_CAP_SWITCHDEV),
@ -584,6 +584,24 @@ class TestValidateAndGetDataFromBindingProfile(base.BaseTestCase):
constants.OVN_PORT_BINDING_PROFILE: expect}),
expect)
def test_valid_input_surplus_keys(self):
# Confirm that extra keys are allowed
binding_profile = {
constants.PORT_CAP_PARAM: [constants.PORT_CAP_SWITCHDEV],
'pci_vendor_info': 'dead:beef',
'pci_slot': '0000:ca:fe.42',
'physical_network': 'physnet1',
'optional_information_provided_by_nova': 'not_consumed_by_neutron',
}
expect = binding_profile.copy()
del(expect[constants.PORT_CAP_PARAM])
del(expect['optional_information_provided_by_nova'])
self.assertDictEqual(
expect,
utils.validate_and_get_data_from_binding_profile(
{portbindings.VNIC_TYPE: portbindings.VNIC_DIRECT,
constants.OVN_PORT_BINDING_PROFILE: binding_profile}))
def test_unknown_profile_items_pruned(self):
# Confirm that unknown profile items are pruned
self.assertEqual(
@ -621,36 +639,41 @@ class TestValidateAndGetDataFromBindingProfile(base.BaseTestCase):
def test_overlapping_param_set_different_vnic_type(self):
# Confirm overlapping param sets discerned by vnic_type
expect = {
binding_profile = {
'key': 'value',
'other_key': 'value',
}
# This param set is not valid for VNIC_FAKE_NORMAL
self.assertRaises(
neutron_lib.exceptions.InvalidInput,
utils.validate_and_get_data_from_binding_profile,
{portbindings.VNIC_TYPE: self.VNIC_FAKE_NORMAL,
constants.OVN_PORT_BINDING_PROFILE: expect})
# This param set is valid for VNIC_FAKE_NORMAL with 'other_key' pruned.
expect = binding_profile.copy()
del(expect['other_key'])
self.assertDictEqual(
expect,
utils.validate_and_get_data_from_binding_profile(
{portbindings.VNIC_TYPE: self.VNIC_FAKE_NORMAL,
constants.OVN_PORT_BINDING_PROFILE: binding_profile}))
# It is valid for VNIC_FAKE_OTHER
expect = binding_profile.copy()
self.assertDictEqual(
expect,
utils.validate_and_get_data_from_binding_profile(
{portbindings.VNIC_TYPE: self.VNIC_FAKE_OTHER,
constants.OVN_PORT_BINDING_PROFILE: expect}))
constants.OVN_PORT_BINDING_PROFILE: binding_profile}))
def test_overlapping_param_set_different_vnic_type_and_capability(self):
# Confirm overlapping param sets discerned by vnic_type and capability
expect = {
binding_profile = {
'key': 'value',
'other_key': 'value',
'other_key': 42,
'third_key': 'value',
}
# This param set is not valid for VNIC_FAKE_OTHER without capability
expect = binding_profile.copy()
del(expect['third_key'])
self.assertRaises(
neutron_lib.exceptions.InvalidInput,
utils.validate_and_get_data_from_binding_profile,
{portbindings.VNIC_TYPE: self.VNIC_FAKE_OTHER,
constants.OVN_PORT_BINDING_PROFILE: expect})
constants.OVN_PORT_BINDING_PROFILE: binding_profile})
# This param set is also not valid as the capabilities do not match
binding_profile = {
constants.PORT_CAP_PARAM: ['fake-capability'],
@ -667,7 +690,7 @@ class TestValidateAndGetDataFromBindingProfile(base.BaseTestCase):
binding_profile = {
constants.PORT_CAP_PARAM: [constants.PORT_CAP_SWITCHDEV],
'key': 'value',
'other_key': 'value',
'other_key': 42,
'third_key': 'value',
}
expect = binding_profile.copy()

View File

@ -4039,7 +4039,7 @@ class TestOVNVtepPortBinding(OVNMechanismDriverTestCase):
with self.subnet(n):
self._create_port(self.fmt, n['network']['id'],
arg_list=(OVN_PROFILE,),
expected_res_status=400,
expected_res_status=404,
**binding)
def test_create_port_with_vtep_options_and_check_vtep_keys(self):