| Branch: | Tag: | Revision:

root / test / @ d357f531

History | View | Annotate | Download (3.8 kB)

1 c9c4f19e Michael Hanselmann
2 c9c4f19e Michael Hanselmann
3 c9c4f19e Michael Hanselmann
4 c9c4f19e Michael Hanselmann
# Copyright (C) 2006, 2007, 2008 Google Inc.
5 c9c4f19e Michael Hanselmann
6 c9c4f19e Michael Hanselmann
# This program is free software; you can redistribute it and/or modify
7 c9c4f19e Michael Hanselmann
# it under the terms of the GNU General Public License as published by
8 c9c4f19e Michael Hanselmann
# the Free Software Foundation; either version 2 of the License, or
9 c9c4f19e Michael Hanselmann
# (at your option) any later version.
10 c9c4f19e Michael Hanselmann
11 c9c4f19e Michael Hanselmann
# This program is distributed in the hope that it will be useful, but
12 c9c4f19e Michael Hanselmann
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 c9c4f19e Michael Hanselmann
14 c9c4f19e Michael Hanselmann
# General Public License for more details.
15 c9c4f19e Michael Hanselmann
16 c9c4f19e Michael Hanselmann
# You should have received a copy of the GNU General Public License
17 c9c4f19e Michael Hanselmann
# along with this program; if not, write to the Free Software
18 c9c4f19e Michael Hanselmann
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 c9c4f19e Michael Hanselmann
# 02110-1301, USA.
20 c9c4f19e Michael Hanselmann
21 c9c4f19e Michael Hanselmann
22 c9c4f19e Michael Hanselmann
"""Utilities for unit testing"""
23 c9c4f19e Michael Hanselmann
24 149a5439 Iustin Pop
import os
25 9b977740 Guido Trotter
import stat
26 51596eb2 Iustin Pop
import tempfile
27 c9c4f19e Michael Hanselmann
import unittest
28 c9c4f19e Michael Hanselmann
29 149a5439 Iustin Pop
from ganeti import utils
30 149a5439 Iustin Pop
31 c9c4f19e Michael Hanselmann
32 3f991867 Michael Hanselmann
def GetSourceDir():
33 3f991867 Michael Hanselmann
  return os.environ.get("TOP_SRCDIR", ".")
34 3f991867 Michael Hanselmann
35 3f991867 Michael Hanselmann
36 c9c4f19e Michael Hanselmann
class GanetiTestCase(unittest.TestCase):
37 51596eb2 Iustin Pop
  """Helper class for unittesting.
38 51596eb2 Iustin Pop

39 51596eb2 Iustin Pop
  This class defines a few utility functions that help in building
40 51596eb2 Iustin Pop
  unittests. Child classes must call the parent setup and cleanup.
41 51596eb2 Iustin Pop

42 51596eb2 Iustin Pop
43 51596eb2 Iustin Pop
  def setUp(self):
44 51596eb2 Iustin Pop
    self._temp_files = []
45 51596eb2 Iustin Pop
46 51596eb2 Iustin Pop
  def tearDown(self):
47 51596eb2 Iustin Pop
    while self._temp_files:
48 51596eb2 Iustin Pop
49 51596eb2 Iustin Pop
50 51596eb2 Iustin Pop
      except EnvironmentError, err:
51 51596eb2 Iustin Pop
52 51596eb2 Iustin Pop
53 149a5439 Iustin Pop
  def assertFileContent(self, file_name, expected_content):
54 9b977740 Guido Trotter
    """Checks that the content of a file is what we expect.
55 149a5439 Iustin Pop

56 149a5439 Iustin Pop
    @type file_name: str
57 149a5439 Iustin Pop
    @param file_name: the file whose contents we should check
58 149a5439 Iustin Pop
    @type expected_content: str
59 149a5439 Iustin Pop
    @param expected_content: the content we expect
60 149a5439 Iustin Pop

61 149a5439 Iustin Pop
62 149a5439 Iustin Pop
    actual_content = utils.ReadFile(file_name)
63 149a5439 Iustin Pop
    self.assertEqual(actual_content, expected_content)
64 149a5439 Iustin Pop
65 9b977740 Guido Trotter
  def assertFileMode(self, file_name, expected_mode):
66 9b977740 Guido Trotter
    """Checks that the mode of a file is what we expect.
67 9b977740 Guido Trotter

68 9b977740 Guido Trotter
    @type file_name: str
69 9b977740 Guido Trotter
    @param file_name: the file whose contents we should check
70 9b977740 Guido Trotter
    @type expected_mode: int
71 9b977740 Guido Trotter
    @param expected_mode: the mode we expect
72 9b977740 Guido Trotter

73 9b977740 Guido Trotter
74 9b977740 Guido Trotter
    st = os.stat(file_name)
75 9b977740 Guido Trotter
    actual_mode = stat.S_IMODE(st.st_mode)
76 9b977740 Guido Trotter
    self.assertEqual(actual_mode, expected_mode)
77 9b977740 Guido Trotter
78 d357f531 Michael Hanselmann
  def assertEqualValues(self, first, second, msg=None):
79 d357f531 Michael Hanselmann
    """Compares two values whether they're equal.
80 d357f531 Michael Hanselmann

81 d357f531 Michael Hanselmann
    Tuples are automatically converted to lists before comparing.
82 d357f531 Michael Hanselmann

83 d357f531 Michael Hanselmann
84 d357f531 Michael Hanselmann
    return self.assertEqual(UnifyValueType(first),
85 d357f531 Michael Hanselmann
86 d357f531 Michael Hanselmann
87 d357f531 Michael Hanselmann
88 149a5439 Iustin Pop
89 149a5439 Iustin Pop
  def _TestDataFilename(name):
90 149a5439 Iustin Pop
    """Returns the filename of a given test data file.
91 149a5439 Iustin Pop

92 149a5439 Iustin Pop
    @type name: str
93 149a5439 Iustin Pop
    @param name: the 'base' of the file name, as present in
94 149a5439 Iustin Pop
        the test/data directory
95 149a5439 Iustin Pop
    @rtype: str
96 149a5439 Iustin Pop
    @return: the full path to the filename, such that it can
97 149a5439 Iustin Pop
        be used in 'make distcheck' rules
98 c9c4f19e Michael Hanselmann

99 c9c4f19e Michael Hanselmann
100 3f991867 Michael Hanselmann
    return "%s/test/data/%s" % (GetSourceDir(), name)
101 149a5439 Iustin Pop
102 149a5439 Iustin Pop
103 149a5439 Iustin Pop
  def _ReadTestData(cls, name):
104 149a5439 Iustin Pop
    """Returns the contents of a test data file.
105 149a5439 Iustin Pop

106 149a5439 Iustin Pop
    This is just a very simple wrapper over utils.ReadFile with the
107 149a5439 Iustin Pop
    proper test file name.
108 149a5439 Iustin Pop

109 149a5439 Iustin Pop
110 149a5439 Iustin Pop
    return utils.ReadFile(cls._TestDataFilename(name))
111 51596eb2 Iustin Pop
112 51596eb2 Iustin Pop
  def _CreateTempFile(self):
113 51596eb2 Iustin Pop
    """Creates a temporary file and adds it to the internal cleanup list.
114 51596eb2 Iustin Pop

115 51596eb2 Iustin Pop
    This method simplifies the creation and cleanup of temporary files
116 51596eb2 Iustin Pop
    during tests.
117 51596eb2 Iustin Pop

118 51596eb2 Iustin Pop
119 51596eb2 Iustin Pop
    fh, fname = tempfile.mkstemp(prefix="ganeti-test", suffix=".tmp")
120 51596eb2 Iustin Pop
121 51596eb2 Iustin Pop
122 51596eb2 Iustin Pop
    return fname
123 d357f531 Michael Hanselmann
124 d357f531 Michael Hanselmann
125 d357f531 Michael Hanselmann
def UnifyValueType(data):
126 d357f531 Michael Hanselmann
  """Converts all tuples into lists.
127 d357f531 Michael Hanselmann

128 d357f531 Michael Hanselmann
  This is useful for unittests where an external library doesn't keep types.
129 d357f531 Michael Hanselmann

130 d357f531 Michael Hanselmann
131 d357f531 Michael Hanselmann
  if isinstance(data, (tuple, list)):
132 d357f531 Michael Hanselmann
    return [UnifyValueType(i) for i in data]
133 d357f531 Michael Hanselmann
134 d357f531 Michael Hanselmann
  elif isinstance(data, dict):
135 d357f531 Michael Hanselmann
    return dict([(UnifyValueType(key), UnifyValueType(value))
136 d357f531 Michael Hanselmann
                 for (key, value) in data.iteritems()])
137 d357f531 Michael Hanselmann
138 d357f531 Michael Hanselmann
  return data