Generalize the reading of test file data
authorIustin Pop <iustin@google.com>
Mon, 20 Oct 2008 18:01:28 +0000 (18:01 +0000)
committerIustin Pop <iustin@google.com>
Mon, 20 Oct 2008 18:01:28 +0000 (18:01 +0000)
Currently we have two methods in ganeti.bdev_unittest.py of computing
the test data file name - and, of course, they don't give the same
results.

The patch moves the functions to compute the test file name and reading
of its contents to the GanetiTestCase class in testutils, which allows
running the tests from the command line as well.

We also change assertFileContent to use utils.ReadFile.

Reviewed-by: imsnah

test/ganeti.bdev_unittest.py
test/testutils.py

index 6973f17..e43fd1e 100755 (executable)
 import os
 import unittest
 
+import testutils
 from ganeti import bdev
 from ganeti import errors
 
 
-class TestDRBD8Runner(unittest.TestCase):
+class TestDRBD8Runner(testutils.GanetiTestCase):
   """Testing case for DRBD8"""
 
   @staticmethod
@@ -46,20 +47,6 @@ class TestDRBD8Runner(unittest.TestCase):
     return retval
 
   @staticmethod
-  def _get_contents(name):
-    """Returns the contents of a file"""
-
-    prefix = os.environ.get("srcdir", None)
-    if prefix:
-      name = prefix + "/test/" + name
-    fh = open(name, "r")
-    try:
-      data = fh.read()
-    finally:
-      fh.close()
-    return data
-
-  @staticmethod
   def _has_net(data, local, remote):
     """Check network connection parameters"""
     retval = (
@@ -76,7 +63,7 @@ class TestDRBD8Runner(unittest.TestCase):
 
   def testParserBoth(self):
     """Test drbdsetup show parser for disk and network"""
-    data = self._get_contents("data/bdev-both.txt")
+    data = self._ReadTestData("bdev-both.txt")
     result = bdev.DRBD8._GetDevInfo(data)
     self.failUnless(self._has_disk(result, "/dev/xenvg/test.data",
                                    "/dev/xenvg/test.meta"),
@@ -87,7 +74,7 @@ class TestDRBD8Runner(unittest.TestCase):
 
   def testParserNet(self):
     """Test drbdsetup show parser for disk and network"""
-    data = self._get_contents("data/bdev-net.txt")
+    data = self._ReadTestData("bdev-net.txt")
     result = bdev.DRBD8._GetDevInfo(data)
     self.failUnless(("local_dev" not in result and
                      "meta_dev" not in result and
@@ -99,7 +86,7 @@ class TestDRBD8Runner(unittest.TestCase):
 
   def testParserDisk(self):
     """Test drbdsetup show parser for disk and network"""
-    data = self._get_contents("data/bdev-disk.txt")
+    data = self._ReadTestData("bdev-disk.txt")
     result = bdev.DRBD8._GetDevInfo(data)
     self.failUnless(self._has_disk(result, "/dev/xenvg/test.data",
                                    "/dev/xenvg/test.meta"),
@@ -109,15 +96,12 @@ class TestDRBD8Runner(unittest.TestCase):
                     "Should not find network info")
 
 
-class TestDRBD8Status(unittest.TestCase):
+class TestDRBD8Status(testutils.GanetiTestCase):
   """Testing case for DRBD8 /proc status"""
 
   def setUp(self):
     """Read in txt data"""
-    proc_data = "test/data/proc_drbd8.txt"
-    prefix = os.environ.get("srcdir", None)
-    if prefix:
-      proc_data = prefix + "/" + proc_data
+    proc_data = self._TestDataFilename("proc_drbd8.txt")
     self.proc_data = bdev.DRBD8._GetProcData(filename=proc_data)
     self.mass_data = bdev.DRBD8._MassageProcData(self.proc_data)
 
index 3eba211..737962e 100644 (file)
 
 """Utilities for unit testing"""
 
+import os
 import unittest
 
+from ganeti import utils
+
 
 class GanetiTestCase(unittest.TestCase):
-  def assertFileContent(self, file_name, content):
-    """Checks the content of a file.
+  def assertFileContent(self, file_name, expected_content):
+    """Checks the content of a file is what we expect.
+
+    @type file_name: str
+    @param file_name: the file whose contents we should check
+    @type expected_content: str
+    @param expected_content: the content we expect
+
+    """
+    actual_content = utils.ReadFile(file_name)
+    self.assertEqual(actual_content, expected_content)
+
+  @staticmethod
+  def _TestDataFilename(name):
+    """Returns the filename of a given test data file.
+
+    @type name: str
+    @param name: the 'base' of the file name, as present in
+        the test/data directory
+    @rtype: str
+    @return: the full path to the filename, such that it can
+        be used in 'make distcheck' rules
 
     """
-    handle = open(file_name, 'r')
-    try:
-      self.assertEqual(handle.read(), content)
-    finally:
-      handle.close()
+    prefix = os.environ.get("srcdir", "")
+    if prefix:
+      prefix = prefix + "/test/"
+    return "%sdata/%s" % (prefix, name)
+
+  @classmethod
+  def _ReadTestData(cls, name):
+    """Returns the contents of a test data file.
+
+    This is just a very simple wrapper over utils.ReadFile with the
+    proper test file name.
+
+    """
+
+    return utils.ReadFile(cls._TestDataFilename(name))