root / test / testutils.py @ 76e5f8b5
History | View | Annotate | Download (4.1 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 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
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 | 43983a88 | Michael Hanselmann | import sys |
26 | 9b977740 | Guido Trotter | import stat |
27 | 51596eb2 | Iustin Pop | import tempfile |
28 | c9c4f19e | Michael Hanselmann | import unittest |
29 | 25231ec5 | Michael Hanselmann | import logging |
30 | c9c4f19e | Michael Hanselmann | |
31 | 149a5439 | Iustin Pop | from ganeti import utils |
32 | 149a5439 | Iustin Pop | |
33 | c9c4f19e | Michael Hanselmann | |
34 | 3f991867 | Michael Hanselmann | def GetSourceDir(): |
35 | 3f991867 | Michael Hanselmann | return os.environ.get("TOP_SRCDIR", ".") |
36 | 3f991867 | Michael Hanselmann | |
37 | 3f991867 | Michael Hanselmann | |
38 | 25231ec5 | Michael Hanselmann | class GanetiTestProgram(unittest.TestProgram): |
39 | 25231ec5 | Michael Hanselmann | def runTests(self): |
40 | 25231ec5 | Michael Hanselmann | """
|
41 | 25231ec5 | Michael Hanselmann |
|
42 | 25231ec5 | Michael Hanselmann | """
|
43 | 25231ec5 | Michael Hanselmann | logging.basicConfig(filename=os.devnull) |
44 | 43983a88 | Michael Hanselmann | |
45 | 43983a88 | Michael Hanselmann | sys.stderr.write("Running %s\n" % self.progName) |
46 | 43983a88 | Michael Hanselmann | sys.stderr.flush() |
47 | 43983a88 | Michael Hanselmann | |
48 | 25231ec5 | Michael Hanselmann | return unittest.TestProgram.runTests(self) |
49 | 25231ec5 | Michael Hanselmann | |
50 | 25231ec5 | Michael Hanselmann | |
51 | c9c4f19e | Michael Hanselmann | class GanetiTestCase(unittest.TestCase): |
52 | 51596eb2 | Iustin Pop | """Helper class for unittesting.
|
53 | 51596eb2 | Iustin Pop |
|
54 | 51596eb2 | Iustin Pop | This class defines a few utility functions that help in building
|
55 | 51596eb2 | Iustin Pop | unittests. Child classes must call the parent setup and cleanup.
|
56 | 51596eb2 | Iustin Pop |
|
57 | 51596eb2 | Iustin Pop | """
|
58 | 51596eb2 | Iustin Pop | def setUp(self): |
59 | 51596eb2 | Iustin Pop | self._temp_files = []
|
60 | 51596eb2 | Iustin Pop | |
61 | 51596eb2 | Iustin Pop | def tearDown(self): |
62 | 51596eb2 | Iustin Pop | while self._temp_files: |
63 | 51596eb2 | Iustin Pop | try:
|
64 | 51596eb2 | Iustin Pop | utils.RemoveFile(self._temp_files.pop())
|
65 | 51596eb2 | Iustin Pop | except EnvironmentError, err: |
66 | 51596eb2 | Iustin Pop | pass
|
67 | 51596eb2 | Iustin Pop | |
68 | 149a5439 | Iustin Pop | def assertFileContent(self, file_name, expected_content): |
69 | 9b977740 | Guido Trotter | """Checks that the content of a file is what we expect.
|
70 | 149a5439 | Iustin Pop |
|
71 | 149a5439 | Iustin Pop | @type file_name: str
|
72 | 149a5439 | Iustin Pop | @param file_name: the file whose contents we should check
|
73 | 149a5439 | Iustin Pop | @type expected_content: str
|
74 | 149a5439 | Iustin Pop | @param expected_content: the content we expect
|
75 | 149a5439 | Iustin Pop |
|
76 | 149a5439 | Iustin Pop | """
|
77 | 149a5439 | Iustin Pop | actual_content = utils.ReadFile(file_name) |
78 | 149a5439 | Iustin Pop | self.assertEqual(actual_content, expected_content)
|
79 | 149a5439 | Iustin Pop | |
80 | 9b977740 | Guido Trotter | def assertFileMode(self, file_name, expected_mode): |
81 | 9b977740 | Guido Trotter | """Checks that the mode of a file is what we expect.
|
82 | 9b977740 | Guido Trotter |
|
83 | 9b977740 | Guido Trotter | @type file_name: str
|
84 | 9b977740 | Guido Trotter | @param file_name: the file whose contents we should check
|
85 | 9b977740 | Guido Trotter | @type expected_mode: int
|
86 | 9b977740 | Guido Trotter | @param expected_mode: the mode we expect
|
87 | 9b977740 | Guido Trotter |
|
88 | 9b977740 | Guido Trotter | """
|
89 | 9b977740 | Guido Trotter | st = os.stat(file_name) |
90 | 9b977740 | Guido Trotter | actual_mode = stat.S_IMODE(st.st_mode) |
91 | 9b977740 | Guido Trotter | self.assertEqual(actual_mode, expected_mode)
|
92 | 9b977740 | Guido Trotter | |
93 | d357f531 | Michael Hanselmann | def assertEqualValues(self, first, second, msg=None): |
94 | d357f531 | Michael Hanselmann | """Compares two values whether they're equal.
|
95 | d357f531 | Michael Hanselmann |
|
96 | d357f531 | Michael Hanselmann | Tuples are automatically converted to lists before comparing.
|
97 | d357f531 | Michael Hanselmann |
|
98 | d357f531 | Michael Hanselmann | """
|
99 | d357f531 | Michael Hanselmann | return self.assertEqual(UnifyValueType(first), |
100 | d357f531 | Michael Hanselmann | UnifyValueType(second), |
101 | d357f531 | Michael Hanselmann | msg=msg) |
102 | d357f531 | Michael Hanselmann | |
103 | 149a5439 | Iustin Pop | @staticmethod
|
104 | 149a5439 | Iustin Pop | def _TestDataFilename(name): |
105 | 149a5439 | Iustin Pop | """Returns the filename of a given test data file.
|
106 | 149a5439 | Iustin Pop |
|
107 | 149a5439 | Iustin Pop | @type name: str
|
108 | 149a5439 | Iustin Pop | @param name: the 'base' of the file name, as present in
|
109 | 149a5439 | Iustin Pop | the test/data directory
|
110 | 149a5439 | Iustin Pop | @rtype: str
|
111 | 149a5439 | Iustin Pop | @return: the full path to the filename, such that it can
|
112 | 149a5439 | Iustin Pop | be used in 'make distcheck' rules
|
113 | c9c4f19e | Michael Hanselmann |
|
114 | c9c4f19e | Michael Hanselmann | """
|
115 | 3f991867 | Michael Hanselmann | return "%s/test/data/%s" % (GetSourceDir(), name) |
116 | 149a5439 | Iustin Pop | |
117 | 149a5439 | Iustin Pop | @classmethod
|
118 | 149a5439 | Iustin Pop | def _ReadTestData(cls, name): |
119 | 149a5439 | Iustin Pop | """Returns the contents of a test data file.
|
120 | 149a5439 | Iustin Pop |
|
121 | 149a5439 | Iustin Pop | This is just a very simple wrapper over utils.ReadFile with the
|
122 | 149a5439 | Iustin Pop | proper test file name.
|
123 | 149a5439 | Iustin Pop |
|
124 | 149a5439 | Iustin Pop | """
|
125 | 149a5439 | Iustin Pop | return utils.ReadFile(cls._TestDataFilename(name))
|
126 | 51596eb2 | Iustin Pop | |
127 | 51596eb2 | Iustin Pop | def _CreateTempFile(self): |
128 | 51596eb2 | Iustin Pop | """Creates a temporary file and adds it to the internal cleanup list.
|
129 | 51596eb2 | Iustin Pop |
|
130 | 51596eb2 | Iustin Pop | This method simplifies the creation and cleanup of temporary files
|
131 | 51596eb2 | Iustin Pop | during tests.
|
132 | 51596eb2 | Iustin Pop |
|
133 | 51596eb2 | Iustin Pop | """
|
134 | 51596eb2 | Iustin Pop | fh, fname = tempfile.mkstemp(prefix="ganeti-test", suffix=".tmp") |
135 | 51596eb2 | Iustin Pop | os.close(fh) |
136 | 51596eb2 | Iustin Pop | self._temp_files.append(fname)
|
137 | 51596eb2 | Iustin Pop | return fname
|
138 | d357f531 | Michael Hanselmann | |
139 | d357f531 | Michael Hanselmann | |
140 | d357f531 | Michael Hanselmann | def UnifyValueType(data): |
141 | d357f531 | Michael Hanselmann | """Converts all tuples into lists.
|
142 | d357f531 | Michael Hanselmann |
|
143 | d357f531 | Michael Hanselmann | This is useful for unittests where an external library doesn't keep types.
|
144 | d357f531 | Michael Hanselmann |
|
145 | d357f531 | Michael Hanselmann | """
|
146 | d357f531 | Michael Hanselmann | if isinstance(data, (tuple, list)): |
147 | d357f531 | Michael Hanselmann | return [UnifyValueType(i) for i in data] |
148 | d357f531 | Michael Hanselmann | |
149 | d357f531 | Michael Hanselmann | elif isinstance(data, dict): |
150 | d357f531 | Michael Hanselmann | return dict([(UnifyValueType(key), UnifyValueType(value)) |
151 | d357f531 | Michael Hanselmann | for (key, value) in data.iteritems()]) |
152 | d357f531 | Michael Hanselmann | |
153 | d357f531 | Michael Hanselmann | return data |