Skip to content
Snippets Groups Projects
Commit 149a5439 authored by Iustin Pop's avatar Iustin Pop
Browse files

Generalize the reading of test file data

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
parent 02691904
No related branches found
No related tags found
No related merge requests found
...@@ -25,11 +25,12 @@ ...@@ -25,11 +25,12 @@
import os import os
import unittest import unittest
import testutils
from ganeti import bdev from ganeti import bdev
from ganeti import errors from ganeti import errors
class TestDRBD8Runner(unittest.TestCase): class TestDRBD8Runner(testutils.GanetiTestCase):
"""Testing case for DRBD8""" """Testing case for DRBD8"""
@staticmethod @staticmethod
...@@ -45,20 +46,6 @@ class TestDRBD8Runner(unittest.TestCase): ...@@ -45,20 +46,6 @@ class TestDRBD8Runner(unittest.TestCase):
) )
return retval 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 @staticmethod
def _has_net(data, local, remote): def _has_net(data, local, remote):
"""Check network connection parameters""" """Check network connection parameters"""
...@@ -76,7 +63,7 @@ class TestDRBD8Runner(unittest.TestCase): ...@@ -76,7 +63,7 @@ class TestDRBD8Runner(unittest.TestCase):
def testParserBoth(self): def testParserBoth(self):
"""Test drbdsetup show parser for disk and network""" """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) result = bdev.DRBD8._GetDevInfo(data)
self.failUnless(self._has_disk(result, "/dev/xenvg/test.data", self.failUnless(self._has_disk(result, "/dev/xenvg/test.data",
"/dev/xenvg/test.meta"), "/dev/xenvg/test.meta"),
...@@ -87,7 +74,7 @@ class TestDRBD8Runner(unittest.TestCase): ...@@ -87,7 +74,7 @@ class TestDRBD8Runner(unittest.TestCase):
def testParserNet(self): def testParserNet(self):
"""Test drbdsetup show parser for disk and network""" """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) result = bdev.DRBD8._GetDevInfo(data)
self.failUnless(("local_dev" not in result and self.failUnless(("local_dev" not in result and
"meta_dev" not in result and "meta_dev" not in result and
...@@ -99,7 +86,7 @@ class TestDRBD8Runner(unittest.TestCase): ...@@ -99,7 +86,7 @@ class TestDRBD8Runner(unittest.TestCase):
def testParserDisk(self): def testParserDisk(self):
"""Test drbdsetup show parser for disk and network""" """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) result = bdev.DRBD8._GetDevInfo(data)
self.failUnless(self._has_disk(result, "/dev/xenvg/test.data", self.failUnless(self._has_disk(result, "/dev/xenvg/test.data",
"/dev/xenvg/test.meta"), "/dev/xenvg/test.meta"),
...@@ -109,15 +96,12 @@ class TestDRBD8Runner(unittest.TestCase): ...@@ -109,15 +96,12 @@ class TestDRBD8Runner(unittest.TestCase):
"Should not find network info") "Should not find network info")
class TestDRBD8Status(unittest.TestCase): class TestDRBD8Status(testutils.GanetiTestCase):
"""Testing case for DRBD8 /proc status""" """Testing case for DRBD8 /proc status"""
def setUp(self): def setUp(self):
"""Read in txt data""" """Read in txt data"""
proc_data = "test/data/proc_drbd8.txt" proc_data = self._TestDataFilename("proc_drbd8.txt")
prefix = os.environ.get("srcdir", None)
if prefix:
proc_data = prefix + "/" + proc_data
self.proc_data = bdev.DRBD8._GetProcData(filename=proc_data) self.proc_data = bdev.DRBD8._GetProcData(filename=proc_data)
self.mass_data = bdev.DRBD8._MassageProcData(self.proc_data) self.mass_data = bdev.DRBD8._MassageProcData(self.proc_data)
......
...@@ -21,16 +21,49 @@ ...@@ -21,16 +21,49 @@
"""Utilities for unit testing""" """Utilities for unit testing"""
import os
import unittest import unittest
from ganeti import utils
class GanetiTestCase(unittest.TestCase): class GanetiTestCase(unittest.TestCase):
def assertFileContent(self, file_name, content): def assertFileContent(self, file_name, expected_content):
"""Checks the content of a file. """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') prefix = os.environ.get("srcdir", "")
try: if prefix:
self.assertEqual(handle.read(), content) prefix = prefix + "/test/"
finally: return "%sdata/%s" % (prefix, name)
handle.close()
@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))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment