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