1
# Copyright (C) 2005-2011, 2016 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
# "weren't nothing promised to you. do i look like i got a promise face?"
19
"""Tests for trace library"""
33
from ..sixish import (
36
from . import features, TestCaseInTempDir, TestCase
38
mutter, mutter_callsite, report_exception,
39
set_verbosity_level, get_verbosity_level, is_quiet, is_verbose, be_quiet,
42
_rollover_trace_maybe,
47
def _format_exception():
48
"""Format an exception as it would normally be displayed to the user"""
50
report_exception(sys.exc_info(), buf)
54
class TestTrace(TestCase):
56
def test_format_sys_exception(self):
57
# Test handling of an internal/unexpected error that probably
58
# indicates a bug in brz. The details of the message may vary
59
# depending on whether apport is available or not. See test_crash for
62
raise NotImplementedError("time travel")
63
except NotImplementedError:
65
err = _format_exception()
66
self.assertEqualDiff(err.splitlines()[0],
67
'brz: ERROR: exceptions.NotImplementedError: time travel')
68
self.assertContainsRe(err,
69
'Bazaar has encountered an internal error.')
71
def test_format_interrupt_exception(self):
73
raise KeyboardInterrupt()
74
except KeyboardInterrupt:
75
# XXX: Some risk that a *real* keyboard interrupt won't be seen
77
msg = _format_exception()
78
self.assertTrue(len(msg) > 0)
79
self.assertEqualDiff(msg, 'brz: interrupted\n')
81
def test_format_memory_error(self):
86
msg = _format_exception()
88
"brz: out of memory\nUse -Dmem_dump to dump memory to a file.\n")
90
def test_format_mem_dump(self):
91
self.requireFeature(features.meliae)
92
debug.debug_flags.add('mem_dump')
97
msg = _format_exception()
98
self.assertStartsWith(msg,
99
"brz: out of memory\nMemory dumped to ")
101
def test_format_os_error(self):
103
os.rmdir('nosuchfile22222')
106
msg = _format_exception()
107
# Linux seems to give "No such file" but Windows gives "The system
108
# cannot find the file specified".
109
self.assertEqual('brz: ERROR: %s\n' % (e_str,), msg)
111
def test_format_io_error(self):
113
file('nosuchfile22222')
116
msg = _format_exception()
117
# Even though Windows and Linux differ for 'os.rmdir', they both give
118
# 'No such file' for open()
119
# However it now gets translated so we can not test for a specific message
120
self.assertContainsRe(msg,
121
r'^brz: ERROR: \[Errno .*\] .*nosuchfile')
123
def test_format_pywintypes_error(self):
124
self.requireFeature(features.pywintypes)
125
import pywintypes, win32file
127
win32file.RemoveDirectory('nosuchfile22222')
128
except pywintypes.error:
130
msg = _format_exception()
131
# GZ 2010-05-03: Formatting for pywintypes.error is basic, a 3-tuple
132
# with errno, function name, and locale error message
133
self.assertContainsRe(msg,
134
r"^brz: ERROR: \(2, 'RemoveDirectory[AW]?', .*\)")
136
def test_format_sockets_error(self):
139
sock = socket.socket()
140
sock.send("This should fail.")
143
msg = _format_exception()
145
self.assertNotContainsRe(msg,
146
r"Traceback (most recent call last):")
148
def test_format_unicode_error(self):
150
raise errors.BzrCommandError(u'argument foo\xb5 does not exist')
151
except errors.BzrCommandError:
153
msg = _format_exception()
155
def test_format_exception(self):
156
"""Short formatting of brz exceptions"""
158
raise errors.NotBranchError('wibble')
159
except errors.NotBranchError:
161
msg = _format_exception()
162
self.assertTrue(len(msg) > 0)
163
self.assertEqualDiff(msg, 'brz: ERROR: Not a branch: \"wibble\".\n')
165
def test_report_external_import_error(self):
166
"""Short friendly message for missing system modules."""
168
import ImaginaryModule
169
except ImportError as e:
172
self.fail("somehow succeeded in importing %r" % ImaginaryModule)
173
msg = _format_exception()
174
self.assertEqual(msg,
175
'brz: ERROR: No module named ImaginaryModule\n'
176
'You may need to install this Python library separately.\n')
178
def test_report_import_syntax_error(self):
180
raise ImportError("syntax error")
181
except ImportError as e:
183
msg = _format_exception()
184
self.assertContainsRe(msg,
185
r'Bazaar has encountered an internal error')
187
def test_trace_unicode(self):
188
"""Write Unicode to trace log"""
189
self.log(u'the unicode character for benzene is \N{BENZENE RING}')
191
self.assertContainsRe(log, "the unicode character for benzene is")
193
def test_trace_argument_unicode(self):
194
"""Write a Unicode argument to the trace log"""
195
mutter(u'the unicode character for benzene is %s', u'\N{BENZENE RING}')
197
self.assertContainsRe(log, 'the unicode character')
199
def test_trace_argument_utf8(self):
200
"""Write a Unicode argument to the trace log"""
201
mutter(u'the unicode character for benzene is %s',
202
u'\N{BENZENE RING}'.encode('utf-8'))
204
self.assertContainsRe(log, 'the unicode character')
206
def test_report_broken_pipe(self):
208
raise IOError(errno.EPIPE, 'broken pipe foofofo')
210
msg = _format_exception()
211
self.assertEqual(msg, "brz: broken pipe\n")
213
self.fail("expected error not raised")
215
def assertLogStartsWith(self, log, string):
216
"""Like assertStartsWith, but skips the log timestamp."""
217
self.assertContainsRe(log,
218
'^\\d+\\.\\d+ ' + re.escape(string))
220
def test_mutter_callsite_1(self):
221
"""mutter_callsite can capture 1 level of stack frame."""
222
mutter_callsite(1, "foo %s", "a string")
224
# begin with the message
225
self.assertLogStartsWith(log, 'foo a string\nCalled from:\n')
226
# should show two frame: this frame and the one above
227
self.assertContainsRe(log,
228
'test_trace\\.py", line \\d+, in test_mutter_callsite_1\n')
229
# this frame should be the final one
230
self.assertEndsWith(log, ' "a string")\n')
232
def test_mutter_callsite_2(self):
233
"""mutter_callsite can capture 2 levels of stack frame."""
234
mutter_callsite(2, "foo %s", "a string")
236
# begin with the message
237
self.assertLogStartsWith(log, 'foo a string\nCalled from:\n')
238
# should show two frame: this frame and the one above
239
self.assertContainsRe(log,
240
'test_trace.py", line \d+, in test_mutter_callsite_2\n')
241
# this frame should be the final one
242
self.assertEndsWith(log, ' "a string")\n')
244
def test_mutter_never_fails(self):
245
# Even if the decode/encode stage fails, mutter should not
247
# This test checks that mutter doesn't fail; the current behaviour
248
# is that it doesn't fail *and writes non-utf8*.
249
mutter(u'Writing a greek mu (\xb5) works in a unicode string')
250
mutter('But fails in an ascii string \xb5')
251
mutter('and in an ascii argument: %s', '\xb5')
253
self.assertContainsRe(log, 'Writing a greek mu')
254
self.assertContainsRe(log, "But fails in an ascii string")
255
# However, the log content object does unicode replacement on reading
256
# to let it get unicode back where good data has been written. So we
257
# have to do a replaceent here as well.
258
self.assertContainsRe(log, "ascii argument: \xb5".decode('utf8',
261
def test_show_error(self):
263
show_error(u'error2 \xb5 blah')
264
show_error('arg: %s', 'blah')
265
show_error('arg2: %(key)s', {'key':'stuff'})
267
raise Exception("oops")
269
show_error('kwarg', exc_info=True)
271
self.assertContainsRe(log, 'error1')
272
self.assertContainsRe(log, u'error2 \xb5 blah')
273
self.assertContainsRe(log, 'arg: blah')
274
self.assertContainsRe(log, 'arg2: stuff')
275
self.assertContainsRe(log, 'kwarg')
276
self.assertContainsRe(log, 'Traceback \\(most recent call last\\):')
277
self.assertContainsRe(log, 'File ".*test_trace.py", line .*, in test_show_error')
278
self.assertContainsRe(log, 'raise Exception\\("oops"\\)')
279
self.assertContainsRe(log, 'Exception: oops')
281
def test_push_log_file(self):
282
"""Can push and pop log file, and this catches mutter messages.
284
This is primarily for use in the test framework.
286
tmp1 = tempfile.NamedTemporaryFile()
287
tmp2 = tempfile.NamedTemporaryFile()
289
memento1 = push_log_file(tmp1)
290
mutter("comment to file1")
292
memento2 = push_log_file(tmp2)
294
mutter("comment to file2")
296
pop_log_file(memento2)
297
mutter("again to file1")
299
pop_log_file(memento1)
300
# the files were opened in binary mode, so should have exactly
301
# these bytes. and removing the file as the log target should
302
# have caused them to be flushed out. need to match using regexps
303
# as there's a timestamp at the front.
305
self.assertContainsRe(tmp1.read(),
306
r"\d+\.\d+ comment to file1\n\d+\.\d+ again to file1\n")
308
self.assertContainsRe(tmp2.read(),
309
r"\d+\.\d+ comment to file2\n")
314
def test__open_brz_log_uses_stderr_for_failures(self):
315
# If _open_brz_log cannot open the file, then we should write the
316
# warning to stderr. Since this is normally happening before logging is
318
self.overrideAttr(sys, 'stderr', BytesIO())
319
# Set the log file to something that cannot exist
320
self.overrideEnv('BRZ_LOG', '/no-such-dir/brz.log')
321
self.overrideAttr(trace, '_brz_log_filename')
322
logf = trace._open_brz_log()
323
self.assertIs(None, logf)
324
self.assertContainsRe(
325
sys.stderr.getvalue(),
326
"failed to open trace file: .* '/no-such-dir/brz.log'$")
329
class TestVerbosityLevel(TestCase):
331
def test_verbosity_level(self):
332
set_verbosity_level(1)
333
self.assertEqual(1, get_verbosity_level())
334
self.assertTrue(is_verbose())
335
self.assertFalse(is_quiet())
336
set_verbosity_level(-1)
337
self.assertEqual(-1, get_verbosity_level())
338
self.assertFalse(is_verbose())
339
self.assertTrue(is_quiet())
340
set_verbosity_level(0)
341
self.assertEqual(0, get_verbosity_level())
342
self.assertFalse(is_verbose())
343
self.assertFalse(is_quiet())
345
def test_be_quiet(self):
346
# Confirm the old API still works
348
self.assertEqual(-1, get_verbosity_level())
350
self.assertEqual(0, get_verbosity_level())
353
class TestLogging(TestCase):
354
"""Check logging functionality robustly records information"""
358
self.assertEqual(" INFO Noted\n", self.get_log())
360
def test_warning(self):
361
trace.warning("Warned")
362
self.assertEqual(" WARNING Warned\n", self.get_log())
365
logging.getLogger("brz").error("Errored")
366
self.assertEqual(" ERROR Errored\n", self.get_log())
368
def test_log_sub(self):
369
logging.getLogger("brz.test_log_sub").debug("Whispered")
370
self.assertEqual(" DEBUG Whispered\n", self.get_log())
372
def test_log_unicode_msg(self):
373
logging.getLogger("brz").debug(u"\xa7")
374
self.assertEqual(u" DEBUG \xa7\n", self.get_log())
376
def test_log_unicode_arg(self):
377
logging.getLogger("brz").debug("%s", u"\xa7")
378
self.assertEqual(u" DEBUG \xa7\n", self.get_log())
380
def test_log_utf8_msg(self):
381
logging.getLogger("brz").debug("\xc2\xa7")
382
self.assertEqual(u" DEBUG \xa7\n", self.get_log())
384
def test_log_utf8_arg(self):
385
logging.getLogger("brz").debug("%s", "\xc2\xa7")
386
self.assertEqual(u" DEBUG \xa7\n", self.get_log())
388
def test_log_bytes_msg(self):
389
logging.getLogger("brz").debug("\xa7")
391
self.assertContainsString(log, "UnicodeDecodeError: ")
392
self.assertContainsString(log,
393
"Logging record unformattable: '\\xa7' % ()\n")
395
def test_log_bytes_arg(self):
396
logging.getLogger("brz").debug("%s", "\xa7")
398
self.assertContainsString(log, "UnicodeDecodeError: ")
399
self.assertContainsString(log,
400
"Logging record unformattable: '%s' % ('\\xa7',)\n")
402
def test_log_mixed_strings(self):
403
logging.getLogger("brz").debug(u"%s", "\xa7")
405
self.assertContainsString(log, "UnicodeDecodeError: ")
406
self.assertContainsString(log,
407
"Logging record unformattable: u'%s' % ('\\xa7',)\n")
409
def test_log_repr_broken(self):
410
class BadRepr(object):
412
raise ValueError("Broken object")
413
logging.getLogger("brz").debug("%s", BadRepr())
415
self.assertContainsRe(log, "ValueError: Broken object\n")
416
self.assertContainsRe(log, "Logging record unformattable: '%s' % .*\n")
419
class TestBzrLog(TestCaseInTempDir):
421
def test_log_rollover(self):
422
temp_log_name = 'test-log'
423
trace_file = open(temp_log_name, 'at')
424
trace_file.writelines(['test_log_rollover padding\n'] * 200000)
426
_rollover_trace_maybe(temp_log_name)
427
# should have been rolled over
428
self.assertFalse(os.access(temp_log_name, os.R_OK))
431
class TestTraceConfiguration(TestCaseInTempDir):
433
def test_default_config(self):
434
config = trace.DefaultConfig()
435
self.overrideAttr(trace, "_brz_log_filename", None)
436
trace._brz_log_filename = None
437
expected_filename = trace._get_brz_log_filename()
438
self.assertEqual(None, trace._brz_log_filename)
441
# Should have entered and setup a default filename.
442
self.assertEqual(expected_filename, trace._brz_log_filename)
444
config.__exit__(None, None, None)
445
# Should have exited and cleaned up.
446
self.assertEqual(None, trace._brz_log_filename)