Revision 9a6813ac test/ganeti.utils.log_unittest.py

b/test/ganeti.utils.log_unittest.py
25 25
import unittest
26 26
import logging
27 27
import tempfile
28
import shutil
28 29

  
29 30
from ganeti import constants
30 31
from ganeti import errors
......
133 134
      raise Exception
134 135

  
135 136

  
137
class TestSetupLogging(unittest.TestCase):
138
  def setUp(self):
139
    self.tmpdir = tempfile.mkdtemp()
140

  
141
  def tearDown(self):
142
    shutil.rmtree(self.tmpdir)
143

  
144
  def testSimple(self):
145
    logfile = utils.PathJoin(self.tmpdir, "basic.log")
146
    logger = logging.Logger("TestLogger")
147
    self.assertTrue(callable(utils.SetupLogging(logfile, "test",
148
                                                console_logging=False,
149
                                                syslog=constants.SYSLOG_NO,
150
                                                stderr_logging=False,
151
                                                multithreaded=False,
152
                                                root_logger=logger)))
153
    self.assertEqual(utils.ReadFile(logfile), "")
154
    logger.error("This is a test")
155

  
156
    # Ensure SetupLogging used custom logger
157
    logging.error("This message should not show up in the test log file")
158

  
159
    self.assertTrue(utils.ReadFile(logfile).endswith("This is a test\n"))
160

  
161
  def testReopen(self):
162
    logfile = utils.PathJoin(self.tmpdir, "reopen.log")
163
    logfile2 = utils.PathJoin(self.tmpdir, "reopen.log.OLD")
164
    logger = logging.Logger("TestLogger")
165
    reopen_fn = utils.SetupLogging(logfile, "test",
166
                                   console_logging=False,
167
                                   syslog=constants.SYSLOG_NO,
168
                                   stderr_logging=False,
169
                                   multithreaded=False,
170
                                   root_logger=logger)
171
    self.assertTrue(callable(reopen_fn))
172

  
173
    self.assertEqual(utils.ReadFile(logfile), "")
174
    logger.error("This is a test")
175
    self.assertTrue(utils.ReadFile(logfile).endswith("This is a test\n"))
176

  
177
    os.rename(logfile, logfile2)
178
    assert not os.path.exists(logfile)
179

  
180
    # Notify logger to reopen on the next message
181
    reopen_fn()
182
    assert not os.path.exists(logfile)
183

  
184
    # Provoke actual reopen
185
    logger.error("First message")
186

  
187
    self.assertTrue(utils.ReadFile(logfile).endswith("First message\n"))
188
    self.assertTrue(utils.ReadFile(logfile2).endswith("This is a test\n"))
189

  
190

  
136 191
if __name__ == "__main__":
137 192
  testutils.GanetiTestProgram()

Also available in: Unified diff