~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/services/twistedsupport/tests/test_loggingsupport.py

  • Committer: Robert Collins
  • Date: 2011-10-16 19:13:13 UTC
  • mfrom: (14104.5.9 use-oops-twisted)
  • mto: This revision was merged to the branch mainline in revision 14213.
  • Revision ID: robertc@robertcollins.net-20111016191313-g3fpjgr2fg1yyjeu
Merge use-oops-twisted to resolve conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
from canonical.launchpad.webapp.errorlog import globalErrorUtility
28
28
from lp.services.twistedsupport.loggingsupport import (
29
29
    LaunchpadLogFile,
30
 
    OOPSLoggingObserver,
31
30
    )
32
31
from lp.services.twistedsupport.tests.test_processmonitor import (
33
32
    makeFailure,
39
38
UTC = pytz.utc
40
39
 
41
40
 
42
 
class LoggingSupportTests(TestCase):
43
 
 
44
 
    run_tests_with = AsynchronousDeferredRunTest
45
 
 
46
 
    def setUp(self):
47
 
        super(LoggingSupportTests, self).setUp()
48
 
        self.temp_dir = tempfile.mkdtemp()
49
 
        self.addCleanup(shutil.rmtree, self.temp_dir)
50
 
        config.push('testing', dedent("""
51
 
            [error_reports]
52
 
            oops_prefix: O
53
 
            error_dir: %s
54
 
            """ % self.temp_dir))
55
 
        globalErrorUtility.configure()
56
 
        self.log_stream = StringIO.StringIO()
57
 
        self.logger = logging.getLogger('test')
58
 
        self.logger.setLevel(logging.DEBUG)
59
 
        self.logger.addHandler(logging.StreamHandler(self.log_stream))
60
 
        self.observer = OOPSLoggingObserver('test')
61
 
 
62
 
    def tearDown(self):
63
 
        config.pop('testing')
64
 
        globalErrorUtility.configure()
65
 
        super(LoggingSupportTests, self).tearDown()
66
 
 
67
 
    def assertLogMatches(self, pattern):
68
 
        """Assert that the messages logged by self.logger matches a regexp."""
69
 
        log_text = self.log_stream.getvalue()
70
 
        self.failUnless(re.match(pattern, log_text, re.M))
71
 
 
72
 
    @suppress_stderr
73
 
    def test_oops_reporting(self):
74
 
        # Calling log.err should result in an OOPS being logged.
75
 
        log.addObserver(self.observer.emit)
76
 
        self.addCleanup(log.removeObserver, self.observer.emit)
77
 
        error_time = datetime.datetime.now(UTC)
78
 
        fail = makeFailure(RuntimeError)
79
 
        log.err(fail, error_time=error_time)
80
 
        flush_logged_errors(RuntimeError)
81
 
        oops = self.oopses[-1]
82
 
        self.assertEqual(oops['type'], 'RuntimeError')
83
 
        self.assertLogMatches('^Logged OOPS id.*')
84
 
 
85
 
 
86
41
class TestLaunchpadLogFile(TestCase):
87
42
 
88
43
    def setUp(self):