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
......@@ -25,11 +25,12 @@
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
......@@ -45,20 +46,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"""
......@@ -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)
......
......@@ -21,16 +21,49 @@
"""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))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment