hail: Fix result for node evacuation
[ganeti-local] / test / testutils.py
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 import types
31
32 from ganeti import utils
33
34
35 def GetSourceDir():
36   return os.environ.get("TOP_SRCDIR", ".")
37
38
39 def _SetupLogging(verbose):
40   """Setupup logging infrastructure.
41
42   """
43   fmt = logging.Formatter("%(asctime)s: %(threadName)s"
44                           " %(levelname)s %(message)s")
45
46   if verbose:
47     handler = logging.StreamHandler()
48   else:
49     handler = logging.FileHandler(os.devnull, "a")
50
51   handler.setLevel(logging.NOTSET)
52   handler.setFormatter(fmt)
53
54   root_logger = logging.getLogger("")
55   root_logger.setLevel(logging.NOTSET)
56   root_logger.addHandler(handler)
57
58
59 class GanetiTestProgram(unittest.TestProgram):
60   def runTests(self):
61     """Runs all tests.
62
63     """
64     _SetupLogging("LOGTOSTDERR" in os.environ)
65
66     sys.stderr.write("Running %s\n" % self.progName)
67     sys.stderr.flush()
68
69     # Ensure assertions will be evaluated
70     if not __debug__:
71       raise Exception("Not running in debug mode, assertions would not be"
72                       " evaluated")
73
74     # Check again, this time with a real assertion
75     try:
76       assert False
77     except AssertionError:
78       pass
79     else:
80       raise Exception("Assertion not evaluated")
81
82     # The following piece of code is a backport from Python 2.6. Python 2.4/2.5
83     # only accept class instances as test runners. Being able to pass classes
84     # reduces the amount of code necessary for using a custom test runner.
85     # 2.6 and above should use their own code, however.
86     if (self.testRunner and sys.hexversion < 0x2060000 and
87         isinstance(self.testRunner, (type, types.ClassType))):
88       try:
89         self.testRunner = self.testRunner(verbosity=self.verbosity)
90       except TypeError:
91         # didn't accept the verbosity argument
92         self.testRunner = self.testRunner()
93
94     return unittest.TestProgram.runTests(self)
95
96
97 class GanetiTestCase(unittest.TestCase):
98   """Helper class for unittesting.
99
100   This class defines a few utility functions that help in building
101   unittests. Child classes must call the parent setup and cleanup.
102
103   """
104   def setUp(self):
105     self._temp_files = []
106
107   def tearDown(self):
108     while self._temp_files:
109       try:
110         utils.RemoveFile(self._temp_files.pop())
111       except EnvironmentError, err:
112         pass
113
114   def assertFileContent(self, file_name, expected_content):
115     """Checks that the content of a file is what we expect.
116
117     @type file_name: str
118     @param file_name: the file whose contents we should check
119     @type expected_content: str
120     @param expected_content: the content we expect
121
122     """
123     actual_content = utils.ReadFile(file_name)
124     self.assertEqual(actual_content, expected_content)
125
126   def assertFileMode(self, file_name, expected_mode):
127     """Checks that the mode of a file is what we expect.
128
129     @type file_name: str
130     @param file_name: the file whose contents we should check
131     @type expected_mode: int
132     @param expected_mode: the mode we expect
133
134     """
135     st = os.stat(file_name)
136     actual_mode = stat.S_IMODE(st.st_mode)
137     self.assertEqual(actual_mode, expected_mode)
138
139   def assertEqualValues(self, first, second, msg=None):
140     """Compares two values whether they're equal.
141
142     Tuples are automatically converted to lists before comparing.
143
144     """
145     return self.assertEqual(UnifyValueType(first),
146                             UnifyValueType(second),
147                             msg=msg)
148
149   @staticmethod
150   def _TestDataFilename(name):
151     """Returns the filename of a given test data file.
152
153     @type name: str
154     @param name: the 'base' of the file name, as present in
155         the test/data directory
156     @rtype: str
157     @return: the full path to the filename, such that it can
158         be used in 'make distcheck' rules
159
160     """
161     return "%s/test/data/%s" % (GetSourceDir(), name)
162
163   @classmethod
164   def _ReadTestData(cls, name):
165     """Returns the contents of a test data file.
166
167     This is just a very simple wrapper over utils.ReadFile with the
168     proper test file name.
169
170     """
171     return utils.ReadFile(cls._TestDataFilename(name))
172
173   def _CreateTempFile(self):
174     """Creates a temporary file and adds it to the internal cleanup list.
175
176     This method simplifies the creation and cleanup of temporary files
177     during tests.
178
179     """
180     fh, fname = tempfile.mkstemp(prefix="ganeti-test", suffix=".tmp")
181     os.close(fh)
182     self._temp_files.append(fname)
183     return fname
184
185
186 def UnifyValueType(data):
187   """Converts all tuples into lists.
188
189   This is useful for unittests where an external library doesn't keep types.
190
191   """
192   if isinstance(data, (tuple, list)):
193     return [UnifyValueType(i) for i in data]
194
195   elif isinstance(data, dict):
196     return dict([(UnifyValueType(key), UnifyValueType(value))
197                  for (key, value) in data.iteritems()])
198
199   return data