root / test / testutils.py @ d357f531
History | View | Annotate | Download (3.8 kB)
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 stat |
26 |
import tempfile |
27 |
import unittest |
28 |
|
29 |
from ganeti import utils |
30 |
|
31 |
|
32 |
def GetSourceDir(): |
33 |
return os.environ.get("TOP_SRCDIR", ".") |
34 |
|
35 |
|
36 |
class GanetiTestCase(unittest.TestCase): |
37 |
"""Helper class for unittesting.
|
38 |
|
39 |
This class defines a few utility functions that help in building
|
40 |
unittests. Child classes must call the parent setup and cleanup.
|
41 |
|
42 |
"""
|
43 |
def setUp(self): |
44 |
self._temp_files = []
|
45 |
|
46 |
def tearDown(self): |
47 |
while self._temp_files: |
48 |
try:
|
49 |
utils.RemoveFile(self._temp_files.pop())
|
50 |
except EnvironmentError, err: |
51 |
pass
|
52 |
|
53 |
def assertFileContent(self, file_name, expected_content): |
54 |
"""Checks that the content of a file is what we expect.
|
55 |
|
56 |
@type file_name: str
|
57 |
@param file_name: the file whose contents we should check
|
58 |
@type expected_content: str
|
59 |
@param expected_content: the content we expect
|
60 |
|
61 |
"""
|
62 |
actual_content = utils.ReadFile(file_name) |
63 |
self.assertEqual(actual_content, expected_content)
|
64 |
|
65 |
def assertFileMode(self, file_name, expected_mode): |
66 |
"""Checks that the mode of a file is what we expect.
|
67 |
|
68 |
@type file_name: str
|
69 |
@param file_name: the file whose contents we should check
|
70 |
@type expected_mode: int
|
71 |
@param expected_mode: the mode we expect
|
72 |
|
73 |
"""
|
74 |
st = os.stat(file_name) |
75 |
actual_mode = stat.S_IMODE(st.st_mode) |
76 |
self.assertEqual(actual_mode, expected_mode)
|
77 |
|
78 |
def assertEqualValues(self, first, second, msg=None): |
79 |
"""Compares two values whether they're equal.
|
80 |
|
81 |
Tuples are automatically converted to lists before comparing.
|
82 |
|
83 |
"""
|
84 |
return self.assertEqual(UnifyValueType(first), |
85 |
UnifyValueType(second), |
86 |
msg=msg) |
87 |
|
88 |
@staticmethod
|
89 |
def _TestDataFilename(name): |
90 |
"""Returns the filename of a given test data file.
|
91 |
|
92 |
@type name: str
|
93 |
@param name: the 'base' of the file name, as present in
|
94 |
the test/data directory
|
95 |
@rtype: str
|
96 |
@return: the full path to the filename, such that it can
|
97 |
be used in 'make distcheck' rules
|
98 |
|
99 |
"""
|
100 |
return "%s/test/data/%s" % (GetSourceDir(), name) |
101 |
|
102 |
@classmethod
|
103 |
def _ReadTestData(cls, name): |
104 |
"""Returns the contents of a test data file.
|
105 |
|
106 |
This is just a very simple wrapper over utils.ReadFile with the
|
107 |
proper test file name.
|
108 |
|
109 |
"""
|
110 |
return utils.ReadFile(cls._TestDataFilename(name))
|
111 |
|
112 |
def _CreateTempFile(self): |
113 |
"""Creates a temporary file and adds it to the internal cleanup list.
|
114 |
|
115 |
This method simplifies the creation and cleanup of temporary files
|
116 |
during tests.
|
117 |
|
118 |
"""
|
119 |
fh, fname = tempfile.mkstemp(prefix="ganeti-test", suffix=".tmp") |
120 |
os.close(fh) |
121 |
self._temp_files.append(fname)
|
122 |
return fname
|
123 |
|
124 |
|
125 |
def UnifyValueType(data): |
126 |
"""Converts all tuples into lists.
|
127 |
|
128 |
This is useful for unittests where an external library doesn't keep types.
|
129 |
|
130 |
"""
|
131 |
if isinstance(data, (tuple, list)): |
132 |
return [UnifyValueType(i) for i in data] |
133 |
|
134 |
elif isinstance(data, dict): |
135 |
return dict([(UnifyValueType(key), UnifyValueType(value)) |
136 |
for (key, value) in data.iteritems()]) |
137 |
|
138 |
return data
|