~launchpad-pqm/launchpad/devel

11300.2.26 by Stuart Bishop
Test unhandled exception handling by LaunchpadScript
1
#!/usr/bin/python -S
2
# Copyright 2010 Canonical Ltd.  This software is licensed under the
3
# GNU Affero General Public License version 3 (see the file LICENSE).
4
5
"""Cronscript that raises an unhandled exception."""
6
7
__metaclass__ = type
8
__all__ = []
9
10
import _pythonpath
11
12
from lp.services.scripts.base import LaunchpadCronScript
13
11300.2.27 by Stuart Bishop
Fix log level bug and test WARN and above generate OOPS reports
14
from canonical.launchpad.webapp.errorlog import globalErrorUtility
15
11300.2.26 by Stuart Bishop
Test unhandled exception handling by LaunchpadScript
16
17
class CrashScript(LaunchpadCronScript):
18
11300.2.30 by Stuart Bishop
Cope with no previous oops reports in test run
19
    def last_oops_id(self):
20
        return getattr(globalErrorUtility.getLastOopsReport(), 'id', None)
21
11300.2.26 by Stuart Bishop
Test unhandled exception handling by LaunchpadScript
22
    def main(self):
11300.2.30 by Stuart Bishop
Cope with no previous oops reports in test run
23
        initial_oops = self.last_oops_id()
11300.2.29 by Stuart Bishop
Confirm lower loglevels do not generate OOPS reports
24
11300.2.27 by Stuart Bishop
Fix log level bug and test WARN and above generate OOPS reports
25
        self.logger.debug("This is debug level")
11300.2.29 by Stuart Bishop
Confirm lower loglevels do not generate OOPS reports
26
        # Debug messages do not generate an OOPS.
11300.2.30 by Stuart Bishop
Cope with no previous oops reports in test run
27
        assert self.last_oops_id() == initial_oops
11300.2.29 by Stuart Bishop
Confirm lower loglevels do not generate OOPS reports
28
11300.2.26 by Stuart Bishop
Test unhandled exception handling by LaunchpadScript
29
        self.logger.warn("This is a warning")
11300.2.30 by Stuart Bishop
Cope with no previous oops reports in test run
30
        first_oops = self.last_oops_id()
11300.2.27 by Stuart Bishop
Fix log level bug and test WARN and above generate OOPS reports
31
        if first_oops != initial_oops:
32
            self.logger.info("New OOPS detected")
33
34
        self.logger.critical("This is critical")
11300.2.30 by Stuart Bishop
Cope with no previous oops reports in test run
35
        second_oops = self.last_oops_id()
11300.2.27 by Stuart Bishop
Fix log level bug and test WARN and above generate OOPS reports
36
        if second_oops != first_oops:
37
            self.logger.info("New OOPS detected")
38
11300.2.26 by Stuart Bishop
Test unhandled exception handling by LaunchpadScript
39
        raise NotImplementedError("Whoops")
40
41
42
if __name__ == "__main__":
43
    script = CrashScript("crash")
44
    script.lock_and_run()