~launchpad-pqm/launchpad/devel

8687.15.19 by Karl Fogel
Add the copyright header block to __init__.py files under lib/canonical/
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
2976.10.92 by Stuart Bishop
Fix logging system test isolation
3
4
"""Testing helpers"""
5
6
__metaclass__ = type
7
3691.57.47 by Stuart Bishop
Rename canonical.testing.layers.* to canonical.testing.*Layer
8
__all__ = [
6789.5.4 by Jonathan Lange
Add a layer that doesn't offer the librarian.
9
    'BaseLayer',
10
    'DatabaseFunctionalLayer',
11
    'DatabaseLayer',
12
    'ExperimentalLaunchpadZopelessLayer',
13
    'FunctionalLayer',
14
    'LaunchpadFunctionalLayer',
15
    'LaunchpadLayer',
16
    'LaunchpadScriptLayer',
17
    'LaunchpadZopelessLayer',
18
    'LibrarianLayer',
19
    'PageTestLayer',
3691.57.47 by Stuart Bishop
Rename canonical.testing.layers.* to canonical.testing.*Layer
20
    'reset_logging',
6789.14.1 by Michael Hudson
cherry pick addition of TwistedAppServerLayer from the move-codehosting thread
21
    'TwistedAppServerLayer',
6789.5.4 by Jonathan Lange
Add a layer that doesn't offer the librarian.
22
    'TwistedLaunchpadZopelessLayer',
23
    'TwistedLayer',
6789.14.2 by Michael Hudson
cherry pick jml's appserver layer refactoring and addition of ZopelessAppServerLayer
24
    'ZopelessAppServerLayer',
6789.5.4 by Jonathan Lange
Add a layer that doesn't offer the librarian.
25
    'ZopelessLayer',
3691.57.47 by Stuart Bishop
Rename canonical.testing.layers.* to canonical.testing.*Layer
26
    ]
3691.57.1 by Stuart Bishop
Improved Layers for test suite
27
2976.10.92 by Stuart Bishop
Fix logging system test isolation
28
import logging
29
9893.6.28 by Stuart Bishop
Reset logging between tests to Launchpad default state, not Z3 default state
30
import lp_sitecustomize
31
8922.4.5 by Jonathan Lange
Make sure reset_logging restores the log level too.
32
2976.10.92 by Stuart Bishop
Fix logging system test isolation
33
def reset_logging():
34
    """Reset the logging system back to defaults
4264.3.6 by James Henstridge
some small cleanups from Elliot's review
35
2976.10.92 by Stuart Bishop
Fix logging system test isolation
36
    Currently, defaults means 'the way the Z3 testrunner sets it up'
9893.6.28 by Stuart Bishop
Reset logging between tests to Launchpad default state, not Z3 default state
37
    plus customizations made in lp_sitecustomize
2976.10.92 by Stuart Bishop
Fix logging system test isolation
38
    """
39
    # Remove all handlers from non-root loggers, and remove the loggers too.
40
    loggerDict = logging.Logger.manager.loggerDict
41
    for name, logger in list(loggerDict.items()):
5521.3.7 by Guilherme Salgado
Fix a few tests with Francis help
42
        if name == 'pagetests-access':
43
            # Don't reset the hit logger used by the test infrastructure.
44
            continue
2976.10.92 by Stuart Bishop
Fix logging system test isolation
45
        if not isinstance(logger, logging.PlaceHolder):
46
            for handler in list(logger.handlers):
47
                logger.removeHandler(handler)
48
        del loggerDict[name]
49
50
    # Remove all handlers from the root logger
51
    root = logging.getLogger('')
52
    for handler in root.handlers:
53
        root.removeHandler(handler)
54
8922.4.5 by Jonathan Lange
Make sure reset_logging restores the log level too.
55
    # Set the root logger's log level back to the default level: WARNING.
56
    root.setLevel(logging.WARNING)
57
2976.10.119 by Stuart Bishop
Reset logging harder
58
    # Clean out the guts of the logging module. We don't want handlers that
59
    # have already been closed hanging around for the atexit handler to barf
60
    # on, for example.
61
    del logging._handlerList[:]
62
    logging._handlers.clear()
63
2976.10.92 by Stuart Bishop
Fix logging system test isolation
64
    # Reset the setup
8697.27.23 by Gary Poster
use new zope.testing 3.8.1 snapshot with changes I made for supporting Launchpad
65
    from zope.testing.testrunner.runner import Runner
66
    from zope.testing.testrunner.logsupport import Logging
67
    Logging(Runner()).global_setup()
9893.6.28 by Stuart Bishop
Reset logging between tests to Launchpad default state, not Z3 default state
68
    lp_sitecustomize.customize_logger()
8922.4.5 by Jonathan Lange
Make sure reset_logging restores the log level too.
69
11705.1.1 by Ian Booth
Initial coding - remove hard coded http ports
70
8697.11.2 by Stuart Bishop
Add test harness for doctest codec doctests
71
# This import registers the 'doctest' Unicode codec.
8697.11.1 by Stuart Bishop
'doctest' codec to encode Unicode strings to a more human readable representation for doctests
72
import canonical.testing.doctestcodec
4330.4.30 by Jonathan Lange
Merge RF, resolving conflicts
73
3691.57.47 by Stuart Bishop
Rename canonical.testing.layers.* to canonical.testing.*Layer
74
# Imported here to avoid circular import issues
6278.1.1 by Barry Warsaw
appserver layer
75
# pylint: disable-msg=W0401
6278.1.13 by Barry Warsaw
fix imports
76
from canonical.testing.layers import *
6278.1.1 by Barry Warsaw
appserver layer
77
from canonical.testing.layers import __all__ as layers_all
78
__all__.extend(layers_all)