Commit 255e19d4 authored by Guido Trotter's avatar Guido Trotter
Browse files

Add NIC.CheckParameterSyntax



This function will be used to check the NIC parameters for validity.
Unittests are included.
Signed-off-by: default avatarGuido Trotter <ultrotter@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent ac061be9
......@@ -312,6 +312,24 @@ class NIC(ConfigObject):
"""Config object representing a network card."""
__slots__ = ["mac", "ip", "bridge"]
@classmethod
def CheckParameterSyntax(cls, nicparams):
"""Check the given parameters for validity.
@type nicparams: dict
@param nicparams: dictionary with parameter names/value
@raise errors.ConfigurationError: when a parameter is not valid
"""
if nicparams[constants.NIC_MODE] not in constants.NIC_VALID_MODES:
err = "Invalid nic mode: %s" % nicparams[constants.NIC_MODE]
raise errors.ConfigurationError(err)
if (nicparams[constants.NIC_MODE] is constants.NIC_MODE_BRIDGED and
not nicparams[constants.NIC_LINK]):
err = "Missing bridged nic link"
raise errors.ConfigurationError(err)
class Disk(ConfigObject):
"""Config object representing a block device."""
......
......@@ -152,6 +152,24 @@ class TestConfigRunner(unittest.TestCase):
# but the fake_instance update should still fail
self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_instance)
def testNICParameterSyntaxCheck(self):
"""Test the NIC's CheckParameterSyntax function"""
mode = constants.NIC_MODE
link = constants.NIC_LINK
m_bridged = constants.NIC_MODE_BRIDGED
m_routed = constants.NIC_MODE_ROUTED
CheckSyntax = objects.NIC.CheckParameterSyntax
CheckSyntax(constants.NICC_DEFAULTS)
CheckSyntax({mode: m_bridged, link: 'br1'})
CheckSyntax({mode: m_routed, link: 'default'})
self.assertRaises(errors.ConfigurationError,
CheckSyntax, {mode: '000invalid', link: 'any'})
self.assertRaises(errors.ConfigurationError,
CheckSyntax, {mode: m_bridged, link: None})
self.assertRaises(errors.ConfigurationError,
CheckSyntax, {mode: m_bridged, link: ''})
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment