Currently both of these functions check if the value satisfies
bool(value) == False.
Using this approach, numerical 0 would not be allowed as a value of a
hvparam.
We know that unspecified hvparams have a value of either None or the
zero-length string, so these functions need to check if the value is
the one of these unspecified values.

Signed-off-by: Yuto KAWAMURA(kawamuray) <[email protected]>
---
 lib/hypervisor/hv_base.py | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/lib/hypervisor/hv_base.py b/lib/hypervisor/hv_base.py
index cd74ce7..f72aeff 100644
--- a/lib/hypervisor/hv_base.py
+++ b/lib/hypervisor/hv_base.py
@@ -532,6 +532,23 @@ class BaseHypervisor(object):
     return start_mem
 
   @classmethod
+  def _IsParamValueUnspecified(cls, param_value):
+    """Check if the parameter value is a kind of value meaning unspecified.
+
+    This function checks if the parameter value is a kind of value meaning
+    unspecified.
+
+    @type param_value: any
+    @param param_value: the parameter value that needs to be checked
+    @rtype: bool
+    @return: True if the parameter value is a kind of value meaning 
unspecified,
+      False otherwise
+
+    """
+    return param_value is None \
+      or isinstance(param_value, basestring) and param_value == ""
+
+  @classmethod
   def CheckParameterSyntax(cls, hvparams):
     """Check the given parameters for validity.
 
@@ -552,9 +569,9 @@ class BaseHypervisor(object):
       if name not in hvparams:
         raise errors.HypervisorError("Parameter '%s' is missing" % name)
       value = hvparams[name]
-      if not required and not value:
+      if not required and cls._IsParamValueUnspecified(value):
         continue
-      if not value:
+      if cls._IsParamValueUnspecified(value):
         raise errors.HypervisorError("Parameter '%s' is required but"
                                      " is currently not defined" % (name, ))
       if check_fn is not None and not check_fn(value):
@@ -576,7 +593,7 @@ class BaseHypervisor(object):
     """
     for name, (required, _, _, check_fn, errstr) in cls.PARAMETERS.items():
       value = hvparams[name]
-      if not required and not value:
+      if not required and cls._IsParamValueUnspecified(value):
         continue
       if check_fn is not None and not check_fn(value):
         raise errors.HypervisorError("Parameter '%s' fails"
-- 
2.0.4

Reply via email to