Statistics
| Branch: | Tag: | Revision:

root / test / py / testutils.py @ 74db37c0

History | View | Annotate | Download (7.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
import types
31

    
32
from ganeti import utils
33

    
34

    
35
def GetSourceDir():
36
  return os.environ.get("TOP_SRCDIR", ".")
37

    
38

    
39
def TestDataFilename(name):
40
  """Returns the filename of a given test data file.
41

42
  @type name: str
43
  @param name: the 'base' of the file name, as present in
44
      the test/data directory
45
  @rtype: str
46
  @return: the full path to the filename, such that it can
47
      be used in 'make distcheck' rules
48

49
  """
50
  return "%s/test/data/%s" % (GetSourceDir(), name)
51

    
52

    
53
def ReadTestData(name):
54
  """Returns the content of a test data file.
55

56
  This is just a very simple wrapper over utils.ReadFile with the
57
  proper test file name.
58

59
  """
60
  return utils.ReadFile(TestDataFilename(name))
61

    
62

    
63
def _SetupLogging(verbose):
64
  """Setupup logging infrastructure.
65

66
  """
67
  fmt = logging.Formatter("%(asctime)s: %(threadName)s"
68
                          " %(levelname)s %(message)s")
69

    
70
  if verbose:
71
    handler = logging.StreamHandler()
72
  else:
73
    handler = logging.FileHandler(os.devnull, "a")
74

    
75
  handler.setLevel(logging.NOTSET)
76
  handler.setFormatter(fmt)
77

    
78
  root_logger = logging.getLogger("")
79
  root_logger.setLevel(logging.NOTSET)
80
  root_logger.addHandler(handler)
81

    
82

    
83
class GanetiTestProgram(unittest.TestProgram):
84
  def runTests(self):
85
    """Runs all tests.
86

87
    """
88
    _SetupLogging("LOGTOSTDERR" in os.environ)
89

    
90
    sys.stderr.write("Running %s\n" % self.progName)
91
    sys.stderr.flush()
92

    
93
    # Ensure assertions will be evaluated
94
    if not __debug__:
95
      raise Exception("Not running in debug mode, assertions would not be"
96
                      " evaluated")
97

    
98
    # Check again, this time with a real assertion
99
    try:
100
      assert False
101
    except AssertionError:
102
      pass
103
    else:
104
      raise Exception("Assertion not evaluated")
105

    
106
    # The following piece of code is a backport from Python 2.6. Python 2.4/2.5
107
    # only accept class instances as test runners. Being able to pass classes
108
    # reduces the amount of code necessary for using a custom test runner.
109
    # 2.6 and above should use their own code, however.
110
    if (self.testRunner and sys.hexversion < 0x2060000 and
111
        isinstance(self.testRunner, (type, types.ClassType))):
112
      try:
113
        self.testRunner = self.testRunner(verbosity=self.verbosity)
114
      except TypeError:
115
        # didn't accept the verbosity argument
116
        self.testRunner = self.testRunner()
117

    
118
    return unittest.TestProgram.runTests(self)
119

    
120

    
121
class GanetiTestCase(unittest.TestCase):
122
  """Helper class for unittesting.
123

124
  This class defines a few utility functions that help in building
125
  unittests. Child classes must call the parent setup and cleanup.
126

127
  """
128
  def setUp(self):
129
    self._temp_files = []
130

    
131
  def tearDown(self):
132
    while self._temp_files:
133
      try:
134
        utils.RemoveFile(self._temp_files.pop())
135
      except EnvironmentError, err:
136
        pass
137

    
138
  def assertFileContent(self, file_name, expected_content):
139
    """Checks that the content of a file is what we expect.
140

141
    @type file_name: str
142
    @param file_name: the file whose contents we should check
143
    @type expected_content: str
144
    @param expected_content: the content we expect
145

146
    """
147
    actual_content = utils.ReadFile(file_name)
148
    self.assertEqual(actual_content, expected_content)
149

    
150
  def assertFileMode(self, file_name, expected_mode):
151
    """Checks that the mode of a file is what we expect.
152

153
    @type file_name: str
154
    @param file_name: the file whose contents we should check
155
    @type expected_mode: int
156
    @param expected_mode: the mode we expect
157

158
    """
159
    st = os.stat(file_name)
160
    actual_mode = stat.S_IMODE(st.st_mode)
161
    self.assertEqual(actual_mode, expected_mode)
162

    
163
  def assertFileUid(self, file_name, expected_uid):
164
    """Checks that the user id of a file is what we expect.
165

166
    @type file_name: str
167
    @param file_name: the file whose contents we should check
168
    @type expected_uid: int
169
    @param expected_uid: the user id we expect
170

171
    """
172
    st = os.stat(file_name)
173
    actual_uid = st.st_uid
174
    self.assertEqual(actual_uid, expected_uid)
175

    
176
  def assertFileGid(self, file_name, expected_gid):
177
    """Checks that the group id of a file is what we expect.
178

179
    @type file_name: str
180
    @param file_name: the file whose contents we should check
181
    @type expected_gid: int
182
    @param expected_gid: the group id we expect
183

184
    """
185
    st = os.stat(file_name)
186
    actual_gid = st.st_gid
187
    self.assertEqual(actual_gid, expected_gid)
188

    
189
  def assertEqualValues(self, first, second, msg=None):
190
    """Compares two values whether they're equal.
191

192
    Tuples are automatically converted to lists before comparing.
193

194
    """
195
    return self.assertEqual(UnifyValueType(first),
196
                            UnifyValueType(second),
197
                            msg=msg)
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 patch_object(*args, **kwargs):
213
  """Unified patch_object for various versions of Python Mock.
214

215
  Different Python Mock versions provide incompatible versions of patching an
216
  object. More recent versions use _patch_object, older ones used patch_object.
217
  This function unifies the different variations.
218

219
  """
220
  import mock
221
  try:
222
    return mock._patch_object(*args, **kwargs)
223
  except AttributeError:
224
    return mock.patch_object(*args, **kwargs)
225

    
226

    
227
def UnifyValueType(data):
228
  """Converts all tuples into lists.
229

230
  This is useful for unittests where an external library doesn't keep types.
231

232
  """
233
  if isinstance(data, (tuple, list)):
234
    return [UnifyValueType(i) for i in data]
235

    
236
  elif isinstance(data, dict):
237
    return dict([(UnifyValueType(key), UnifyValueType(value))
238
                 for (key, value) in data.iteritems()])
239

    
240
  return data
241

    
242

    
243
class CallCounter(object):
244
  """Utility class to count number of calls to a function/method.
245

246
  """
247
  def __init__(self, fn):
248
    """Initializes this class.
249

250
    @type fn: Callable
251

252
    """
253
    self._fn = fn
254
    self._count = 0
255

    
256
  def __call__(self, *args, **kwargs):
257
    """Calls wrapped function with given parameters.
258

259
    """
260
    self._count += 1
261
    return self._fn(*args, **kwargs)
262

    
263
  def Count(self):
264
    """Returns number of calls.
265

266
    @rtype: number
267

268
    """
269
    return self._count