root / test / testutils.py @ f34901f8
History | View | Annotate | Download (2 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 | c9c4f19e | Michael Hanselmann | import unittest |
26 | c9c4f19e | Michael Hanselmann | |
27 | 149a5439 | Iustin Pop | from ganeti import utils |
28 | 149a5439 | Iustin Pop | |
29 | c9c4f19e | Michael Hanselmann | |
30 | c9c4f19e | Michael Hanselmann | class GanetiTestCase(unittest.TestCase): |
31 | 149a5439 | Iustin Pop | def assertFileContent(self, file_name, expected_content): |
32 | 149a5439 | Iustin Pop | """Checks the content of a file is what we expect.
|
33 | 149a5439 | Iustin Pop |
|
34 | 149a5439 | Iustin Pop | @type file_name: str
|
35 | 149a5439 | Iustin Pop | @param file_name: the file whose contents we should check
|
36 | 149a5439 | Iustin Pop | @type expected_content: str
|
37 | 149a5439 | Iustin Pop | @param expected_content: the content we expect
|
38 | 149a5439 | Iustin Pop |
|
39 | 149a5439 | Iustin Pop | """
|
40 | 149a5439 | Iustin Pop | actual_content = utils.ReadFile(file_name) |
41 | 149a5439 | Iustin Pop | self.assertEqual(actual_content, expected_content)
|
42 | 149a5439 | Iustin Pop | |
43 | 149a5439 | Iustin Pop | @staticmethod
|
44 | 149a5439 | Iustin Pop | def _TestDataFilename(name): |
45 | 149a5439 | Iustin Pop | """Returns the filename of a given test data file.
|
46 | 149a5439 | Iustin Pop |
|
47 | 149a5439 | Iustin Pop | @type name: str
|
48 | 149a5439 | Iustin Pop | @param name: the 'base' of the file name, as present in
|
49 | 149a5439 | Iustin Pop | the test/data directory
|
50 | 149a5439 | Iustin Pop | @rtype: str
|
51 | 149a5439 | Iustin Pop | @return: the full path to the filename, such that it can
|
52 | 149a5439 | Iustin Pop | be used in 'make distcheck' rules
|
53 | c9c4f19e | Michael Hanselmann |
|
54 | c9c4f19e | Michael Hanselmann | """
|
55 | 149a5439 | Iustin Pop | prefix = os.environ.get("srcdir", "") |
56 | 149a5439 | Iustin Pop | if prefix:
|
57 | 149a5439 | Iustin Pop | prefix = prefix + "/test/"
|
58 | 149a5439 | Iustin Pop | return "%sdata/%s" % (prefix, name) |
59 | 149a5439 | Iustin Pop | |
60 | 149a5439 | Iustin Pop | @classmethod
|
61 | 149a5439 | Iustin Pop | def _ReadTestData(cls, name): |
62 | 149a5439 | Iustin Pop | """Returns the contents of a test data file.
|
63 | 149a5439 | Iustin Pop |
|
64 | 149a5439 | Iustin Pop | This is just a very simple wrapper over utils.ReadFile with the
|
65 | 149a5439 | Iustin Pop | proper test file name.
|
66 | 149a5439 | Iustin Pop |
|
67 | 149a5439 | Iustin Pop | """
|
68 | 149a5439 | Iustin Pop | |
69 | 149a5439 | Iustin Pop | return utils.ReadFile(cls._TestDataFilename(name)) |