commit b01b7a5056cc59734cf58f1bc5e66a877db0cd28
Author: Helga Velroyen <helgav@google.com>
Date:   Thu Jul 4 14:35:49 2013 +0200

    Factor out check for storage params
    
    This patch factors out the sanity checks for storage
    parameters in its own function. Unit tests included.
    
    Signed-off-by: Helga Velroyen <helgav@google.com>
    Reviewed-by: Klaus Aehlig <aehlig@google.com>

diff --git a/lib/backend.py b/lib/backend.py
index 09b9984..8d42030 100644
--- a/lib/backend.py
+++ b/lib/backend.py
@@ -573,6 +573,27 @@ def LeaveCluster(modify_ssh_setup):
   raise errors.QuitGanetiException(True, "Shutdown scheduled")
 
 
+def _CheckStorageParams(params, num_params):
+  """Performs sanity checks for storage parameters.
+
+  @type params: list
+  @param params: list of storage parameters
+  @type num_params: int
+  @param num_params: expected number of parameters
+
+  """
+  if params is None:
+    raise errors.ProgrammerError("No storage parameters for storage"
+                                 " reporting is provided.")
+  if not isinstance(params, list):
+    raise errors.ProgrammerError("The storage parameters are not of type"
+                                 " list: '%s'" % params)
+  if not len(params) == num_params:
+    raise errors.ProgrammerError("Did not receive the expected number of"
+                                 "storage parameters: expected %s,"
+                                 " received '%s'" % (num_params, len(params)))
+
+
 def _GetLvmVgSpaceInfo(name, params):
   """Wrapper around C{_GetVgInfo} which checks the storage parameters.
 
@@ -583,15 +604,7 @@ def _GetLvmVgSpaceInfo(name, params):
     containing only one for exclusive storage
 
   """
-  if params is None:
-    raise errors.ProgrammerError("No storage parameter for LVM vg storage"
-                                 " reporting is provided.")
-  if not isinstance(params, list):
-    raise errors.ProgrammerError("The storage parameters are not of type"
-                                 " list: '%s'" % params)
-  if not len(params) == 1:
-    raise errors.ProgrammerError("Received more than one storage parameter:"
-                                 " '%s'" % params)
+  _CheckStorageParams(params, 1)
   excl_stor = bool(params[0])
   return _GetVgInfo(name, excl_stor)
 
@@ -715,8 +728,7 @@ def GetNodeInfo(storage_units, hv_specs):
   return (bootid, storage_info, hv_info)
 
 
-# pylint: disable=W0613
-def _GetFileStorageSpaceInfo(path, *args):
+def _GetFileStorageSpaceInfo(path, params):
   """Wrapper around filestorage.GetSpaceInfo.
 
   The purpose of this wrapper is to call filestorage.GetFileStorageSpaceInfo
@@ -727,6 +739,7 @@ def _GetFileStorageSpaceInfo(path, *args):
     parameters.
 
   """
+  _CheckStorageParams(params, 0)
   return filestorage.GetFileStorageSpaceInfo(path)
 
 
diff --git a/test/py/ganeti.backend_unittest.py b/test/py/ganeti.backend_unittest.py
index 4e65f6a..24de1d3 100755
--- a/test/py/ganeti.backend_unittest.py
+++ b/test/py/ganeti.backend_unittest.py
@@ -21,19 +21,19 @@
 
 """Script for testing ganeti.backend"""
 
+import mock
 import os
 import shutil
 import tempfile
+import testutils
 import unittest
-import mock
 
-from ganeti import utils
-from ganeti import constants
 from ganeti import backend
-from ganeti import netutils
+from ganeti import constants
+from ganeti import errors
 from ganeti import hypervisor
-
-import testutils
+from ganeti import netutils
+from ganeti import utils
 
 
 class TestX509Certificates(unittest.TestCase):
@@ -618,7 +618,7 @@ class TestGetHvInfo(unittest.TestCase):
 class TestApplyStorageInfoFunction(unittest.TestCase):
 
   _STORAGE_KEY = "some_key"
-  _SOME_ARGS = "some_args"
+  _SOME_ARGS = ["some_args"]
 
   def setUp(self):
     self.mock_storage_fn = mock.Mock()
@@ -650,6 +650,27 @@ class TestApplyStorageInfoFunction(unittest.TestCase):
                       storage_type, self._STORAGE_KEY, self._SOME_ARGS)
 
 
+class TestCheckStorageParams(unittest.TestCase):
+
+  def testParamsNone(self):
+    self.assertRaises(errors.ProgrammerError, backend._CheckStorageParams,
+                      None, NotImplemented)
+
+  def testParamsWrongType(self):
+    self.assertRaises(errors.ProgrammerError, backend._CheckStorageParams,
+                      "string", NotImplemented)
+
+  def testParamsEmpty(self):
+    backend._CheckStorageParams([], 0)
+
+  def testParamsValidNumber(self):
+    backend._CheckStorageParams(["a", True], 2)
+
+  def testParamsInvalidNumber(self):
+    self.assertRaises(errors.ProgrammerError, backend._CheckStorageParams,
+                      ["b", False], 3)
+
+
 class TestGetNodeInfo(unittest.TestCase):
 
   _SOME_RESULT = None
