Revision 51596eb2 test/testutils.py

b/test/testutils.py
22 22
"""Utilities for unit testing"""
23 23

  
24 24
import os
25
import tempfile
25 26
import unittest
26 27

  
27 28
from ganeti import utils
28 29

  
29 30

  
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

  
31 48
  def assertFileContent(self, file_name, expected_content):
32 49
    """Checks the content of a file is what we expect.
33 50

  
......
67 84
    """
68 85

  
69 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

Also available in: Unified diff