Statistics
| Branch: | Tag: | Revision:

root / test / testutils.py @ 26d3fd2f

History | View | Annotate | Download (4.9 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

    
31
from ganeti import utils
32

    
33

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

    
37

    
38
def _SetupLogging(verbose):
39
  """Setupup logging infrastructure.
40

41
  """
42
  fmt = logging.Formatter("%(asctime)s: %(threadName)s"
43
                          " %(levelname)s %(message)s")
44

    
45
  if verbose:
46
    handler = logging.StreamHandler()
47
  else:
48
    handler = logging.FileHandler(os.devnull, "a")
49

    
50
  handler.setLevel(logging.NOTSET)
51
  handler.setFormatter(fmt)
52

    
53
  root_logger = logging.getLogger("")
54
  root_logger.setLevel(logging.NOTSET)
55
  root_logger.addHandler(handler)
56

    
57

    
58
class GanetiTestProgram(unittest.TestProgram):
59
  def runTests(self):
60
    """Runs all tests.
61

62
    """
63
    _SetupLogging("LOGTOSTDERR" in os.environ)
64

    
65
    sys.stderr.write("Running %s\n" % self.progName)
66
    sys.stderr.flush()
67

    
68
    # Ensure assertions will be evaluated
69
    if not __debug__:
70
      raise Exception("Not running in debug mode, assertions would not be"
71
                      " evaluated")
72

    
73
    # Check again, this time with a real assertion
74
    try:
75
      assert False
76
    except AssertionError:
77
      pass
78
    else:
79
      raise Exception("Assertion not evaluated")
80

    
81
    return unittest.TestProgram.runTests(self)
82

    
83

    
84
class GanetiTestCase(unittest.TestCase):
85
  """Helper class for unittesting.
86

87
  This class defines a few utility functions that help in building
88
  unittests. Child classes must call the parent setup and cleanup.
89

90
  """
91
  def setUp(self):
92
    self._temp_files = []
93

    
94
  def tearDown(self):
95
    while self._temp_files:
96
      try:
97
        utils.RemoveFile(self._temp_files.pop())
98
      except EnvironmentError, err:
99
        pass
100

    
101
  def assertFileContent(self, file_name, expected_content):
102
    """Checks that the content of a file is what we expect.
103

104
    @type file_name: str
105
    @param file_name: the file whose contents we should check
106
    @type expected_content: str
107
    @param expected_content: the content we expect
108

109
    """
110
    actual_content = utils.ReadFile(file_name)
111
    self.assertEqual(actual_content, expected_content)
112

    
113
  def assertFileMode(self, file_name, expected_mode):
114
    """Checks that the mode of a file is what we expect.
115

116
    @type file_name: str
117
    @param file_name: the file whose contents we should check
118
    @type expected_mode: int
119
    @param expected_mode: the mode we expect
120

121
    """
122
    st = os.stat(file_name)
123
    actual_mode = stat.S_IMODE(st.st_mode)
124
    self.assertEqual(actual_mode, expected_mode)
125

    
126
  def assertEqualValues(self, first, second, msg=None):
127
    """Compares two values whether they're equal.
128

129
    Tuples are automatically converted to lists before comparing.
130

131
    """
132
    return self.assertEqual(UnifyValueType(first),
133
                            UnifyValueType(second),
134
                            msg=msg)
135

    
136
  @staticmethod
137
  def _TestDataFilename(name):
138
    """Returns the filename of a given test data file.
139

140
    @type name: str
141
    @param name: the 'base' of the file name, as present in
142
        the test/data directory
143
    @rtype: str
144
    @return: the full path to the filename, such that it can
145
        be used in 'make distcheck' rules
146

147
    """
148
    return "%s/test/data/%s" % (GetSourceDir(), name)
149

    
150
  @classmethod
151
  def _ReadTestData(cls, name):
152
    """Returns the contents of a test data file.
153

154
    This is just a very simple wrapper over utils.ReadFile with the
155
    proper test file name.
156

157
    """
158
    return utils.ReadFile(cls._TestDataFilename(name))
159

    
160
  def _CreateTempFile(self):
161
    """Creates a temporary file and adds it to the internal cleanup list.
162

163
    This method simplifies the creation and cleanup of temporary files
164
    during tests.
165

166
    """
167
    fh, fname = tempfile.mkstemp(prefix="ganeti-test", suffix=".tmp")
168
    os.close(fh)
169
    self._temp_files.append(fname)
170
    return fname
171

    
172

    
173
def UnifyValueType(data):
174
  """Converts all tuples into lists.
175

176
  This is useful for unittests where an external library doesn't keep types.
177

178
  """
179
  if isinstance(data, (tuple, list)):
180
    return [UnifyValueType(i) for i in data]
181

    
182
  elif isinstance(data, dict):
183
    return dict([(UnifyValueType(key), UnifyValueType(value))
184
                 for (key, value) in data.iteritems()])
185

    
186
  return data