Statistics
| Branch: | Tag: | Revision:

root / test / py / testutils.py @ 90066780

History | View | Annotate | Download (6.7 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
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 assertFileUid(self, file_name, expected_uid):
140
    """Checks that the user id of a file is what we expect.
141

142
    @type file_name: str
143
    @param file_name: the file whose contents we should check
144
    @type expected_uid: int
145
    @param expected_uid: the user id we expect
146

147
    """
148
    st = os.stat(file_name)
149
    actual_uid = st.st_uid
150
    self.assertEqual(actual_uid, expected_uid)
151

    
152
  def assertFileGid(self, file_name, expected_gid):
153
    """Checks that the group id of a file is what we expect.
154

155
    @type file_name: str
156
    @param file_name: the file whose contents we should check
157
    @type expected_gid: int
158
    @param expected_gid: the group id we expect
159

160
    """
161
    st = os.stat(file_name)
162
    actual_gid = st.st_gid
163
    self.assertEqual(actual_gid, expected_gid)
164

    
165
  def assertEqualValues(self, first, second, msg=None):
166
    """Compares two values whether they're equal.
167

168
    Tuples are automatically converted to lists before comparing.
169

170
    """
171
    return self.assertEqual(UnifyValueType(first),
172
                            UnifyValueType(second),
173
                            msg=msg)
174

    
175
  @staticmethod
176
  def _TestDataFilename(name):
177
    """Returns the filename of a given test data file.
178

179
    @type name: str
180
    @param name: the 'base' of the file name, as present in
181
        the test/data directory
182
    @rtype: str
183
    @return: the full path to the filename, such that it can
184
        be used in 'make distcheck' rules
185

186
    """
187
    return "%s/test/data/%s" % (GetSourceDir(), name)
188

    
189
  @classmethod
190
  def _ReadTestData(cls, name):
191
    """Returns the contents of a test data file.
192

193
    This is just a very simple wrapper over utils.ReadFile with the
194
    proper test file name.
195

196
    """
197
    return utils.ReadFile(cls._TestDataFilename(name))
198

    
199
  def _CreateTempFile(self):
200
    """Creates a temporary file and adds it to the internal cleanup list.
201

202
    This method simplifies the creation and cleanup of temporary files
203
    during tests.
204

205
    """
206
    fh, fname = tempfile.mkstemp(prefix="ganeti-test", suffix=".tmp")
207
    os.close(fh)
208
    self._temp_files.append(fname)
209
    return fname
210

    
211

    
212
def UnifyValueType(data):
213
  """Converts all tuples into lists.
214

215
  This is useful for unittests where an external library doesn't keep types.
216

217
  """
218
  if isinstance(data, (tuple, list)):
219
    return [UnifyValueType(i) for i in data]
220

    
221
  elif isinstance(data, dict):
222
    return dict([(UnifyValueType(key), UnifyValueType(value))
223
                 for (key, value) in data.iteritems()])
224

    
225
  return data
226

    
227

    
228
class CallCounter(object):
229
  """Utility class to count number of calls to a function/method.
230

231
  """
232
  def __init__(self, fn):
233
    """Initializes this class.
234

235
    @type fn: Callable
236

237
    """
238
    self._fn = fn
239
    self._count = 0
240

    
241
  def __call__(self, *args, **kwargs):
242
    """Calls wrapped function with given parameters.
243

244
    """
245
    self._count += 1
246
    return self._fn(*args, **kwargs)
247

    
248
  def Count(self):
249
    """Returns number of calls.
250

251
    @rtype: number
252

253
    """
254
    return self._count