Fixed the false InvalidConfigException for vfid=0

The InvalidConfigException is raised due to incorrect validation
of the parameters.

Change-Id: Icd1e55117caf65989cf91149d686080b0447d9bd
Closes-Bug: 1831853
This commit is contained in:
Karthik S 2019-06-06 07:11:01 +00:00
parent 115c36b892
commit 4919946e2c
2 changed files with 66 additions and 6 deletions

View File

@ -91,11 +91,16 @@ def object_from_json(json):
return SriovVF.from_json(json)
def _get_required_field(json, name, object_name):
def _get_required_field(json, name, object_name, datatype=None):
field = json.get(name)
if not field:
msg = '%s JSON objects require \'%s\' to be configured.' \
% (object_name, name)
if not datatype:
if field is None:
msg = '%s JSON objects require \'%s\' to be configured.' \
% (object_name, name)
raise InvalidConfigException(msg)
elif not isinstance(field, datatype):
msg = '%s JSON objects require \'%s\' to be configured as %s'\
% (object_name, name, str(datatype))
raise InvalidConfigException(msg)
return field
@ -1402,7 +1407,7 @@ class SriovVF(_BaseOpts):
@staticmethod
def from_json(json):
# Get the VF id
vfid = _get_required_field(json, 'vfid', 'SriovVF')
vfid = _get_required_field(json, 'vfid', 'SriovVF', datatype=int)
# Get the PF device name
device = _get_required_field(json, 'device', 'SriovVF')
opts = _BaseOpts.base_opts_from_json(json)

View File

@ -140,7 +140,7 @@ class TestAddress(base.TestCase):
{})
data = '{"ip_netmask": false}'
json_data = json.loads(data)
self.assertRaises(objects.InvalidConfigException,
self.assertRaises(TypeError,
objects.Address.from_json,
json_data)
@ -1889,6 +1889,61 @@ class TestSriovVF(base.TestCase):
def tearDown(self):
super(TestSriovVF, self).tearDown()
def test_from_json_zero_vfid(self):
def test_get_vf_devname(device, vfid):
return device + '_' + str(vfid)
def test_get_pci_address(ifname, noop):
return '0000:79:10.2'
self.stub_out('os_net_config.utils.get_vf_devname',
test_get_vf_devname)
self.stub_out('os_net_config.utils.get_pci_address',
test_get_pci_address)
data = '{"type": "sriov_vf", "device": "em1", "vfid": 0}'
vf = objects.object_from_json(json.loads(data))
self.assertEqual("em1", vf.device)
self.assertEqual(0, vf.vfid)
self.assertEqual("em1_0", vf.name)
def test_from_json_invalid_vfid(self):
def test_get_vf_devname(device, vfid):
return device + '_' + str(vfid)
def test_get_pci_address(ifname, noop):
return '0000:79:10.2'
self.stub_out('os_net_config.utils.get_vf_devname',
test_get_vf_devname)
self.stub_out('os_net_config.utils.get_pci_address',
test_get_pci_address)
data = '{"type": "sriov_vf", "device": "em1", "vfid": "0"}'
err = self.assertRaises(objects.InvalidConfigException,
objects.object_from_json,
json.loads(data))
expected = 'SriovVF JSON objects require \'vfid\' to be configured ' \
'as %s' % (str(int))
self.assertIn(expected, six.text_type(err))
def test_from_json_no_vfid(self):
def test_get_vf_devname(device, vfid):
return device + '_' + str(vfid)
def test_get_pci_address(ifname, noop):
return '0000:79:10.2'
self.stub_out('os_net_config.utils.get_vf_devname',
test_get_vf_devname)
self.stub_out('os_net_config.utils.get_pci_address',
test_get_pci_address)
data = '{"type": "sriov_vf", "device": "em1"}'
err = self.assertRaises(objects.InvalidConfigException,
objects.object_from_json,
json.loads(data))
expected = 'SriovVF JSON objects require \'vfid\' to be configured ' \
'as %s' % (str(int))
self.assertIn(expected, six.text_type(err))
def test_from_json_vfid(self):
def test_get_vf_devname(device, vfid):
return device + '_' + str(vfid)