root / test / testutils.py @ c118d1f4
History | View | Annotate | Download (3.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 stat |
26 |
import tempfile |
27 |
import unittest |
28 |
|
29 |
from ganeti import utils |
30 |
|
31 |
|
32 |
class GanetiTestCase(unittest.TestCase): |
33 |
"""Helper class for unittesting.
|
34 |
|
35 |
This class defines a few utility functions that help in building
|
36 |
unittests. Child classes must call the parent setup and cleanup.
|
37 |
|
38 |
"""
|
39 |
def setUp(self): |
40 |
self._temp_files = []
|
41 |
|
42 |
def tearDown(self): |
43 |
while self._temp_files: |
44 |
try:
|
45 |
utils.RemoveFile(self._temp_files.pop())
|
46 |
except EnvironmentError, err: |
47 |
pass
|
48 |
|
49 |
def assertFileContent(self, file_name, expected_content): |
50 |
"""Checks that the content of a file is what we expect.
|
51 |
|
52 |
@type file_name: str
|
53 |
@param file_name: the file whose contents we should check
|
54 |
@type expected_content: str
|
55 |
@param expected_content: the content we expect
|
56 |
|
57 |
"""
|
58 |
actual_content = utils.ReadFile(file_name) |
59 |
self.assertEqual(actual_content, expected_content)
|
60 |
|
61 |
def assertFileMode(self, file_name, expected_mode): |
62 |
"""Checks that the mode of a file is what we expect.
|
63 |
|
64 |
@type file_name: str
|
65 |
@param file_name: the file whose contents we should check
|
66 |
@type expected_mode: int
|
67 |
@param expected_mode: the mode we expect
|
68 |
|
69 |
"""
|
70 |
st = os.stat(file_name) |
71 |
actual_mode = stat.S_IMODE(st.st_mode) |
72 |
self.assertEqual(actual_mode, expected_mode)
|
73 |
|
74 |
@staticmethod
|
75 |
def _TestDataFilename(name): |
76 |
"""Returns the filename of a given test data file.
|
77 |
|
78 |
@type name: str
|
79 |
@param name: the 'base' of the file name, as present in
|
80 |
the test/data directory
|
81 |
@rtype: str
|
82 |
@return: the full path to the filename, such that it can
|
83 |
be used in 'make distcheck' rules
|
84 |
|
85 |
"""
|
86 |
prefix = os.environ.get("srcdir", "") |
87 |
if prefix:
|
88 |
prefix = prefix + "/test/"
|
89 |
return "%sdata/%s" % (prefix, name) |
90 |
|
91 |
@classmethod
|
92 |
def _ReadTestData(cls, name): |
93 |
"""Returns the contents of a test data file.
|
94 |
|
95 |
This is just a very simple wrapper over utils.ReadFile with the
|
96 |
proper test file name.
|
97 |
|
98 |
"""
|
99 |
|
100 |
return utils.ReadFile(cls._TestDataFilename(name))
|
101 |
|
102 |
def _CreateTempFile(self): |
103 |
"""Creates a temporary file and adds it to the internal cleanup list.
|
104 |
|
105 |
This method simplifies the creation and cleanup of temporary files
|
106 |
during tests.
|
107 |
|
108 |
"""
|
109 |
fh, fname = tempfile.mkstemp(prefix="ganeti-test", suffix=".tmp") |
110 |
os.close(fh) |
111 |
self._temp_files.append(fname)
|
112 |
return fname
|