Revision 149a5439 test/testutils.py
b/test/testutils.py | ||
---|---|---|
21 | 21 |
|
22 | 22 |
"""Utilities for unit testing""" |
23 | 23 |
|
24 |
import os |
|
24 | 25 |
import unittest |
25 | 26 |
|
27 |
from ganeti import utils |
|
28 |
|
|
26 | 29 |
|
27 | 30 |
class GanetiTestCase(unittest.TestCase): |
28 |
def assertFileContent(self, file_name, content): |
|
29 |
"""Checks the content of a file. |
|
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 |
|
30 | 53 |
|
31 | 54 |
""" |
32 |
handle = open(file_name, 'r') |
|
33 |
try: |
|
34 |
self.assertEqual(handle.read(), content) |
|
35 |
finally: |
|
36 |
handle.close() |
|
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)) |
Also available in: Unified diff