4 # Copyright (C) 2006, 2007, 2008 Google Inc.
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.
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.
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
22 """Utilities for unit testing"""
31 from ganeti import utils
35 return os.environ.get("TOP_SRCDIR", ".")
38 class GanetiTestProgram(unittest.TestProgram):
43 logging.basicConfig(filename=os.devnull)
45 sys.stderr.write("Running %s\n" % self.progName)
48 return unittest.TestProgram.runTests(self)
51 class GanetiTestCase(unittest.TestCase):
52 """Helper class for unittesting.
54 This class defines a few utility functions that help in building
55 unittests. Child classes must call the parent setup and cleanup.
62 while self._temp_files:
64 utils.RemoveFile(self._temp_files.pop())
65 except EnvironmentError, err:
68 def assertFileContent(self, file_name, expected_content):
69 """Checks that the content of a file is what we expect.
72 @param file_name: the file whose contents we should check
73 @type expected_content: str
74 @param expected_content: the content we expect
77 actual_content = utils.ReadFile(file_name)
78 self.assertEqual(actual_content, expected_content)
80 def assertFileMode(self, file_name, expected_mode):
81 """Checks that the mode of a file is what we expect.
84 @param file_name: the file whose contents we should check
85 @type expected_mode: int
86 @param expected_mode: the mode we expect
89 st = os.stat(file_name)
90 actual_mode = stat.S_IMODE(st.st_mode)
91 self.assertEqual(actual_mode, expected_mode)
93 def assertEqualValues(self, first, second, msg=None):
94 """Compares two values whether they're equal.
96 Tuples are automatically converted to lists before comparing.
99 return self.assertEqual(UnifyValueType(first),
100 UnifyValueType(second),
104 def _TestDataFilename(name):
105 """Returns the filename of a given test data file.
108 @param name: the 'base' of the file name, as present in
109 the test/data directory
111 @return: the full path to the filename, such that it can
112 be used in 'make distcheck' rules
115 return "%s/test/data/%s" % (GetSourceDir(), name)
118 def _ReadTestData(cls, name):
119 """Returns the contents of a test data file.
121 This is just a very simple wrapper over utils.ReadFile with the
122 proper test file name.
125 return utils.ReadFile(cls._TestDataFilename(name))
127 def _CreateTempFile(self):
128 """Creates a temporary file and adds it to the internal cleanup list.
130 This method simplifies the creation and cleanup of temporary files
134 fh, fname = tempfile.mkstemp(prefix="ganeti-test", suffix=".tmp")
136 self._temp_files.append(fname)
140 def UnifyValueType(data):
141 """Converts all tuples into lists.
143 This is useful for unittests where an external library doesn't keep types.
146 if isinstance(data, (tuple, list)):
147 return [UnifyValueType(i) for i in data]
149 elif isinstance(data, dict):
150 return dict([(UnifyValueType(key), UnifyValueType(value))
151 for (key, value) in data.iteritems()])