Statistics
| Branch: | Tag: | Revision:

root / test / py / testutils.py @ 2fe9deec

History | View | Annotate | Download (6.5 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
    return unittest.TestProgram.runTests(self)
107

    
108

    
109
class GanetiTestCase(unittest.TestCase):
110
  """Helper class for unittesting.
111

112
  This class defines a few utility functions that help in building
113
  unittests. Child classes must call the parent setup and cleanup.
114

115
  """
116
  def setUp(self):
117
    self._temp_files = []
118

    
119
  def tearDown(self):
120
    while self._temp_files:
121
      try:
122
        utils.RemoveFile(self._temp_files.pop())
123
      except EnvironmentError, err:
124
        pass
125

    
126
  def assertFileContent(self, file_name, expected_content):
127
    """Checks that the content 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_content: str
132
    @param expected_content: the content we expect
133

134
    """
135
    actual_content = utils.ReadFile(file_name)
136
    self.assertEqual(actual_content, expected_content)
137

    
138
  def assertFileMode(self, file_name, expected_mode):
139
    """Checks that the mode 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_mode: int
144
    @param expected_mode: the mode we expect
145

146
    """
147
    st = os.stat(file_name)
148
    actual_mode = stat.S_IMODE(st.st_mode)
149
    self.assertEqual(actual_mode, expected_mode)
150

    
151
  def assertFileUid(self, file_name, expected_uid):
152
    """Checks that the user id of a file is what we expect.
153

154
    @type file_name: str
155
    @param file_name: the file whose contents we should check
156
    @type expected_uid: int
157
    @param expected_uid: the user id we expect
158

159
    """
160
    st = os.stat(file_name)
161
    actual_uid = st.st_uid
162
    self.assertEqual(actual_uid, expected_uid)
163

    
164
  def assertFileGid(self, file_name, expected_gid):
165
    """Checks that the group id of a file is what we expect.
166

167
    @type file_name: str
168
    @param file_name: the file whose contents we should check
169
    @type expected_gid: int
170
    @param expected_gid: the group id we expect
171

172
    """
173
    st = os.stat(file_name)
174
    actual_gid = st.st_gid
175
    self.assertEqual(actual_gid, expected_gid)
176

    
177
  def assertEqualValues(self, first, second, msg=None):
178
    """Compares two values whether they're equal.
179

180
    Tuples are automatically converted to lists before comparing.
181

182
    """
183
    return self.assertEqual(UnifyValueType(first),
184
                            UnifyValueType(second),
185
                            msg=msg)
186

    
187
  def _CreateTempFile(self):
188
    """Creates a temporary file and adds it to the internal cleanup list.
189

190
    This method simplifies the creation and cleanup of temporary files
191
    during tests.
192

193
    """
194
    fh, fname = tempfile.mkstemp(prefix="ganeti-test", suffix=".tmp")
195
    os.close(fh)
196
    self._temp_files.append(fname)
197
    return fname
198

    
199

    
200
def patch_object(*args, **kwargs):
201
  """Unified patch_object for various versions of Python Mock.
202

203
  Different Python Mock versions provide incompatible versions of patching an
204
  object. More recent versions use _patch_object, older ones used patch_object.
205
  This function unifies the different variations.
206

207
  """
208
  import mock
209
  try:
210
    return mock._patch_object(*args, **kwargs)
211
  except AttributeError:
212
    return mock.patch_object(*args, **kwargs)
213

    
214

    
215
def UnifyValueType(data):
216
  """Converts all tuples into lists.
217

218
  This is useful for unittests where an external library doesn't keep types.
219

220
  """
221
  if isinstance(data, (tuple, list)):
222
    return [UnifyValueType(i) for i in data]
223

    
224
  elif isinstance(data, dict):
225
    return dict([(UnifyValueType(key), UnifyValueType(value))
226
                 for (key, value) in data.iteritems()])
227

    
228
  return data
229

    
230

    
231
class CallCounter(object):
232
  """Utility class to count number of calls to a function/method.
233

234
  """
235
  def __init__(self, fn):
236
    """Initializes this class.
237

238
    @type fn: Callable
239

240
    """
241
    self._fn = fn
242
    self._count = 0
243

    
244
  def __call__(self, *args, **kwargs):
245
    """Calls wrapped function with given parameters.
246

247
    """
248
    self._count += 1
249
    return self._fn(*args, **kwargs)
250

    
251
  def Count(self):
252
    """Returns number of calls.
253

254
    @rtype: number
255

256
    """
257
    return self._count