root / test / testutils.py @ e7d6946c
History | View | Annotate | Download (3.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 | 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 | c9c4f19e | Michael Hanselmann | class GanetiTestCase(unittest.TestCase): |
33 | 51596eb2 | Iustin Pop | """Helper class for unittesting.
|
34 | 51596eb2 | Iustin Pop |
|
35 | 51596eb2 | Iustin Pop | This class defines a few utility functions that help in building
|
36 | 51596eb2 | Iustin Pop | unittests. Child classes must call the parent setup and cleanup.
|
37 | 51596eb2 | Iustin Pop |
|
38 | 51596eb2 | Iustin Pop | """
|
39 | 51596eb2 | Iustin Pop | def setUp(self): |
40 | 51596eb2 | Iustin Pop | self._temp_files = []
|
41 | 51596eb2 | Iustin Pop | |
42 | 51596eb2 | Iustin Pop | def tearDown(self): |
43 | 51596eb2 | Iustin Pop | while self._temp_files: |
44 | 51596eb2 | Iustin Pop | try:
|
45 | 51596eb2 | Iustin Pop | utils.RemoveFile(self._temp_files.pop())
|
46 | 51596eb2 | Iustin Pop | except EnvironmentError, err: |
47 | 51596eb2 | Iustin Pop | pass
|
48 | 51596eb2 | Iustin Pop | |
49 | 149a5439 | Iustin Pop | def assertFileContent(self, file_name, expected_content): |
50 | 9b977740 | Guido Trotter | """Checks that the content of a file is what we expect.
|
51 | 149a5439 | Iustin Pop |
|
52 | 149a5439 | Iustin Pop | @type file_name: str
|
53 | 149a5439 | Iustin Pop | @param file_name: the file whose contents we should check
|
54 | 149a5439 | Iustin Pop | @type expected_content: str
|
55 | 149a5439 | Iustin Pop | @param expected_content: the content we expect
|
56 | 149a5439 | Iustin Pop |
|
57 | 149a5439 | Iustin Pop | """
|
58 | 149a5439 | Iustin Pop | actual_content = utils.ReadFile(file_name) |
59 | 149a5439 | Iustin Pop | self.assertEqual(actual_content, expected_content)
|
60 | 149a5439 | Iustin Pop | |
61 | 9b977740 | Guido Trotter | def assertFileMode(self, file_name, expected_mode): |
62 | 9b977740 | Guido Trotter | """Checks that the mode of a file is what we expect.
|
63 | 9b977740 | Guido Trotter |
|
64 | 9b977740 | Guido Trotter | @type file_name: str
|
65 | 9b977740 | Guido Trotter | @param file_name: the file whose contents we should check
|
66 | 9b977740 | Guido Trotter | @type expected_mode: int
|
67 | 9b977740 | Guido Trotter | @param expected_mode: the mode we expect
|
68 | 9b977740 | Guido Trotter |
|
69 | 9b977740 | Guido Trotter | """
|
70 | 9b977740 | Guido Trotter | st = os.stat(file_name) |
71 | 9b977740 | Guido Trotter | actual_mode = stat.S_IMODE(st.st_mode) |
72 | 9b977740 | Guido Trotter | self.assertEqual(actual_mode, expected_mode)
|
73 | 9b977740 | Guido Trotter | |
74 | 149a5439 | Iustin Pop | @staticmethod
|
75 | 149a5439 | Iustin Pop | def _TestDataFilename(name): |
76 | 149a5439 | Iustin Pop | """Returns the filename of a given test data file.
|
77 | 149a5439 | Iustin Pop |
|
78 | 149a5439 | Iustin Pop | @type name: str
|
79 | 149a5439 | Iustin Pop | @param name: the 'base' of the file name, as present in
|
80 | 149a5439 | Iustin Pop | the test/data directory
|
81 | 149a5439 | Iustin Pop | @rtype: str
|
82 | 149a5439 | Iustin Pop | @return: the full path to the filename, such that it can
|
83 | 149a5439 | Iustin Pop | be used in 'make distcheck' rules
|
84 | c9c4f19e | Michael Hanselmann |
|
85 | c9c4f19e | Michael Hanselmann | """
|
86 | 149a5439 | Iustin Pop | prefix = os.environ.get("srcdir", "") |
87 | 149a5439 | Iustin Pop | if prefix:
|
88 | 149a5439 | Iustin Pop | prefix = prefix + "/test/"
|
89 | 149a5439 | Iustin Pop | return "%sdata/%s" % (prefix, name) |
90 | 149a5439 | Iustin Pop | |
91 | 149a5439 | Iustin Pop | @classmethod
|
92 | 149a5439 | Iustin Pop | def _ReadTestData(cls, name): |
93 | 149a5439 | Iustin Pop | """Returns the contents of a test data file.
|
94 | 149a5439 | Iustin Pop |
|
95 | 149a5439 | Iustin Pop | This is just a very simple wrapper over utils.ReadFile with the
|
96 | 149a5439 | Iustin Pop | proper test file name.
|
97 | 149a5439 | Iustin Pop |
|
98 | 149a5439 | Iustin Pop | """
|
99 | 149a5439 | Iustin Pop | |
100 | 149a5439 | Iustin Pop | return utils.ReadFile(cls._TestDataFilename(name))
|
101 | 51596eb2 | Iustin Pop | |
102 | 51596eb2 | Iustin Pop | def _CreateTempFile(self): |
103 | 51596eb2 | Iustin Pop | """Creates a temporary file and adds it to the internal cleanup list.
|
104 | 51596eb2 | Iustin Pop |
|
105 | 51596eb2 | Iustin Pop | This method simplifies the creation and cleanup of temporary files
|
106 | 51596eb2 | Iustin Pop | during tests.
|
107 | 51596eb2 | Iustin Pop |
|
108 | 51596eb2 | Iustin Pop | """
|
109 | 51596eb2 | Iustin Pop | fh, fname = tempfile.mkstemp(prefix="ganeti-test", suffix=".tmp") |
110 | 51596eb2 | Iustin Pop | os.close(fh) |
111 | 51596eb2 | Iustin Pop | self._temp_files.append(fname)
|
112 | 51596eb2 | Iustin Pop | return fname |