X-Git-Url: https://code.grnet.gr/git/ganeti-local/blobdiff_plain/149a54392714573f3586b55f374ec24a935fb3ea..ba55d062da8dfb89a37afc2f13f2e689d0094829:/test/testutils.py diff --git a/test/testutils.py b/test/testutils.py index 737962e..c0932cc 100644 --- a/test/testutils.py +++ b/test/testutils.py @@ -22,14 +22,32 @@ """Utilities for unit testing""" import os +import stat +import tempfile import unittest from ganeti import utils class GanetiTestCase(unittest.TestCase): + """Helper class for unittesting. + + This class defines a few utility functions that help in building + unittests. Child classes must call the parent setup and cleanup. + + """ + def setUp(self): + self._temp_files = [] + + def tearDown(self): + while self._temp_files: + try: + utils.RemoveFile(self._temp_files.pop()) + except EnvironmentError, err: + pass + def assertFileContent(self, file_name, expected_content): - """Checks the content of a file is what we expect. + """Checks that the content of a file is what we expect. @type file_name: str @param file_name: the file whose contents we should check @@ -40,6 +58,19 @@ class GanetiTestCase(unittest.TestCase): actual_content = utils.ReadFile(file_name) self.assertEqual(actual_content, expected_content) + def assertFileMode(self, file_name, expected_mode): + """Checks that the mode of a file is what we expect. + + @type file_name: str + @param file_name: the file whose contents we should check + @type expected_mode: int + @param expected_mode: the mode we expect + + """ + st = os.stat(file_name) + actual_mode = stat.S_IMODE(st.st_mode) + self.assertEqual(actual_mode, expected_mode) + @staticmethod def _TestDataFilename(name): """Returns the filename of a given test data file. @@ -67,3 +98,15 @@ class GanetiTestCase(unittest.TestCase): """ return utils.ReadFile(cls._TestDataFilename(name)) + + def _CreateTempFile(self): + """Creates a temporary file and adds it to the internal cleanup list. + + This method simplifies the creation and cleanup of temporary files + during tests. + + """ + fh, fname = tempfile.mkstemp(prefix="ganeti-test", suffix=".tmp") + os.close(fh) + self._temp_files.append(fname) + return fname