Fix burnin problems when using http checks
[ganeti-local] / test / testutils.py
1 #
2 #
3
4 # Copyright (C) 2006, 2007, 2008 Google Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
20
21
22 """Utilities for unit testing"""
23
24 import os
25 import tempfile
26 import unittest
27
28 from ganeti import utils
29
30
31 class GanetiTestCase(unittest.TestCase):
32   """Helper class for unittesting.
33
34   This class defines a few utility functions that help in building
35   unittests. Child classes must call the parent setup and cleanup.
36
37   """
38   def setUp(self):
39     self._temp_files = []
40
41   def tearDown(self):
42     while self._temp_files:
43       try:
44         utils.RemoveFile(self._temp_files.pop())
45       except EnvironmentError, err:
46         pass
47
48   def assertFileContent(self, file_name, expected_content):
49     """Checks the content of a file is what we expect.
50
51     @type file_name: str
52     @param file_name: the file whose contents we should check
53     @type expected_content: str
54     @param expected_content: the content we expect
55
56     """
57     actual_content = utils.ReadFile(file_name)
58     self.assertEqual(actual_content, expected_content)
59
60   @staticmethod
61   def _TestDataFilename(name):
62     """Returns the filename of a given test data file.
63
64     @type name: str
65     @param name: the 'base' of the file name, as present in
66         the test/data directory
67     @rtype: str
68     @return: the full path to the filename, such that it can
69         be used in 'make distcheck' rules
70
71     """
72     prefix = os.environ.get("srcdir", "")
73     if prefix:
74       prefix = prefix + "/test/"
75     return "%sdata/%s" % (prefix, name)
76
77   @classmethod
78   def _ReadTestData(cls, name):
79     """Returns the contents of a test data file.
80
81     This is just a very simple wrapper over utils.ReadFile with the
82     proper test file name.
83
84     """
85
86     return utils.ReadFile(cls._TestDataFilename(name))
87
88   def _CreateTempFile(self):
89     """Creates a temporary file and adds it to the internal cleanup list.
90
91     This method simplifies the creation and cleanup of temporary files
92     during tests.
93
94     """
95     fh, fname = tempfile.mkstemp(prefix="ganeti-test", suffix=".tmp")
96     os.close(fh)
97     self._temp_files.append(fname)
98     return fname