Statistics
| Branch: | Tag: | Revision:

root / test / testutils.py @ 7d20c647

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 3f991867 Michael Hanselmann
def GetSourceDir():
33 3f991867 Michael Hanselmann
  return os.environ.get("TOP_SRCDIR", ".")
34 3f991867 Michael Hanselmann
35 3f991867 Michael Hanselmann
36 c9c4f19e Michael Hanselmann
class GanetiTestCase(unittest.TestCase):
37 51596eb2 Iustin Pop
  """Helper class for unittesting.
38 51596eb2 Iustin Pop

39 51596eb2 Iustin Pop
  This class defines a few utility functions that help in building
40 51596eb2 Iustin Pop
  unittests. Child classes must call the parent setup and cleanup.
41 51596eb2 Iustin Pop

42 51596eb2 Iustin Pop
  """
43 51596eb2 Iustin Pop
  def setUp(self):
44 51596eb2 Iustin Pop
    self._temp_files = []
45 51596eb2 Iustin Pop
46 51596eb2 Iustin Pop
  def tearDown(self):
47 51596eb2 Iustin Pop
    while self._temp_files:
48 51596eb2 Iustin Pop
      try:
49 51596eb2 Iustin Pop
        utils.RemoveFile(self._temp_files.pop())
50 51596eb2 Iustin Pop
      except EnvironmentError, err:
51 51596eb2 Iustin Pop
        pass
52 51596eb2 Iustin Pop
53 149a5439 Iustin Pop
  def assertFileContent(self, file_name, expected_content):
54 9b977740 Guido Trotter
    """Checks that the content of a file is what we expect.
55 149a5439 Iustin Pop

56 149a5439 Iustin Pop
    @type file_name: str
57 149a5439 Iustin Pop
    @param file_name: the file whose contents we should check
58 149a5439 Iustin Pop
    @type expected_content: str
59 149a5439 Iustin Pop
    @param expected_content: the content we expect
60 149a5439 Iustin Pop

61 149a5439 Iustin Pop
    """
62 149a5439 Iustin Pop
    actual_content = utils.ReadFile(file_name)
63 149a5439 Iustin Pop
    self.assertEqual(actual_content, expected_content)
64 149a5439 Iustin Pop
65 9b977740 Guido Trotter
  def assertFileMode(self, file_name, expected_mode):
66 9b977740 Guido Trotter
    """Checks that the mode of a file is what we expect.
67 9b977740 Guido Trotter

68 9b977740 Guido Trotter
    @type file_name: str
69 9b977740 Guido Trotter
    @param file_name: the file whose contents we should check
70 9b977740 Guido Trotter
    @type expected_mode: int
71 9b977740 Guido Trotter
    @param expected_mode: the mode we expect
72 9b977740 Guido Trotter

73 9b977740 Guido Trotter
    """
74 9b977740 Guido Trotter
    st = os.stat(file_name)
75 9b977740 Guido Trotter
    actual_mode = stat.S_IMODE(st.st_mode)
76 9b977740 Guido Trotter
    self.assertEqual(actual_mode, expected_mode)
77 9b977740 Guido Trotter
78 149a5439 Iustin Pop
  @staticmethod
79 149a5439 Iustin Pop
  def _TestDataFilename(name):
80 149a5439 Iustin Pop
    """Returns the filename of a given test data file.
81 149a5439 Iustin Pop

82 149a5439 Iustin Pop
    @type name: str
83 149a5439 Iustin Pop
    @param name: the 'base' of the file name, as present in
84 149a5439 Iustin Pop
        the test/data directory
85 149a5439 Iustin Pop
    @rtype: str
86 149a5439 Iustin Pop
    @return: the full path to the filename, such that it can
87 149a5439 Iustin Pop
        be used in 'make distcheck' rules
88 c9c4f19e Michael Hanselmann

89 c9c4f19e Michael Hanselmann
    """
90 3f991867 Michael Hanselmann
    return "%s/test/data/%s" % (GetSourceDir(), name)
91 149a5439 Iustin Pop
92 149a5439 Iustin Pop
  @classmethod
93 149a5439 Iustin Pop
  def _ReadTestData(cls, name):
94 149a5439 Iustin Pop
    """Returns the contents of a test data file.
95 149a5439 Iustin Pop

96 149a5439 Iustin Pop
    This is just a very simple wrapper over utils.ReadFile with the
97 149a5439 Iustin Pop
    proper test file name.
98 149a5439 Iustin Pop

99 149a5439 Iustin Pop
    """
100 149a5439 Iustin Pop
101 149a5439 Iustin Pop
    return utils.ReadFile(cls._TestDataFilename(name))
102 51596eb2 Iustin Pop
103 51596eb2 Iustin Pop
  def _CreateTempFile(self):
104 51596eb2 Iustin Pop
    """Creates a temporary file and adds it to the internal cleanup list.
105 51596eb2 Iustin Pop

106 51596eb2 Iustin Pop
    This method simplifies the creation and cleanup of temporary files
107 51596eb2 Iustin Pop
    during tests.
108 51596eb2 Iustin Pop

109 51596eb2 Iustin Pop
    """
110 51596eb2 Iustin Pop
    fh, fname = tempfile.mkstemp(prefix="ganeti-test", suffix=".tmp")
111 51596eb2 Iustin Pop
    os.close(fh)
112 51596eb2 Iustin Pop
    self._temp_files.append(fname)
113 51596eb2 Iustin Pop
    return fname