Generalize the reading of test file data
[ganeti-local] / test / testutils.py
1 #
2 #
3
4 # Copyright (C) 2006, 2007, 2008 Google Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
20
21
22 """Utilities for unit testing"""
23
24 import os
25 import unittest
26
27 from ganeti import utils
28
29
30 class GanetiTestCase(unittest.TestCase):
31   def assertFileContent(self, file_name, expected_content):
32     """Checks the content of a file is what we expect.
33
34     @type file_name: str
35     @param file_name: the file whose contents we should check
36     @type expected_content: str
37     @param expected_content: the content we expect
38
39     """
40     actual_content = utils.ReadFile(file_name)
41     self.assertEqual(actual_content, expected_content)
42
43   @staticmethod
44   def _TestDataFilename(name):
45     """Returns the filename of a given test data file.
46
47     @type name: str
48     @param name: the 'base' of the file name, as present in
49         the test/data directory
50     @rtype: str
51     @return: the full path to the filename, such that it can
52         be used in 'make distcheck' rules
53
54     """
55     prefix = os.environ.get("srcdir", "")
56     if prefix:
57       prefix = prefix + "/test/"
58     return "%sdata/%s" % (prefix, name)
59
60   @classmethod
61   def _ReadTestData(cls, name):
62     """Returns the contents of a test data file.
63
64     This is just a very simple wrapper over utils.ReadFile with the
65     proper test file name.
66
67     """
68
69     return utils.ReadFile(cls._TestDataFilename(name))