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