ganeti-cleaner: Remove expired X509 certs
[ganeti-local] / test / testutils.py
index b4d286e..fd95c76 100644 (file)
 """Utilities for unit testing"""
 
 import os
+import sys
 import stat
 import tempfile
 import unittest
+import logging
 
 from ganeti import utils
 
 
+def GetSourceDir():
+  return os.environ.get("TOP_SRCDIR", ".")
+
+
+class GanetiTestProgram(unittest.TestProgram):
+  def runTests(self):
+    """
+
+    """
+    logging.basicConfig(filename=os.devnull)
+
+    sys.stderr.write("Running %s\n" % self.progName)
+    sys.stderr.flush()
+
+    return unittest.TestProgram.runTests(self)
+
+
 class GanetiTestCase(unittest.TestCase):
   """Helper class for unittesting.
 
@@ -71,6 +90,16 @@ class GanetiTestCase(unittest.TestCase):
     actual_mode = stat.S_IMODE(st.st_mode)
     self.assertEqual(actual_mode, expected_mode)
 
+  def assertEqualValues(self, first, second, msg=None):
+    """Compares two values whether they're equal.
+
+    Tuples are automatically converted to lists before comparing.
+
+    """
+    return self.assertEqual(UnifyValueType(first),
+                            UnifyValueType(second),
+                            msg=msg)
+
   @staticmethod
   def _TestDataFilename(name):
     """Returns the filename of a given test data file.
@@ -83,8 +112,7 @@ class GanetiTestCase(unittest.TestCase):
         be used in 'make distcheck' rules
 
     """
-    prefix = os.environ.get("TOP_SRCDIR", ".")
-    return "%s/test/data/%s" % (prefix, name)
+    return "%s/test/data/%s" % (GetSourceDir(), name)
 
   @classmethod
   def _ReadTestData(cls, name):
@@ -94,7 +122,6 @@ class GanetiTestCase(unittest.TestCase):
     proper test file name.
 
     """
-
     return utils.ReadFile(cls._TestDataFilename(name))
 
   def _CreateTempFile(self):
@@ -108,3 +135,19 @@ class GanetiTestCase(unittest.TestCase):
     os.close(fh)
     self._temp_files.append(fname)
     return fname
+
+
+def UnifyValueType(data):
+  """Converts all tuples into lists.
+
+  This is useful for unittests where an external library doesn't keep types.
+
+  """
+  if isinstance(data, (tuple, list)):
+    return [UnifyValueType(i) for i in data]
+
+  elif isinstance(data, dict):
+    return dict([(UnifyValueType(key), UnifyValueType(value))
+                 for (key, value) in data.iteritems()])
+
+  return data