~launchpad-pqm/launchpad/devel

3944.1.1 by Francis J. Lacoste
Use system version python2.4 for scripts.
1
#!/usr/bin/python2.4
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
2
##############################################################################
3
#
4
# Copyright (c) 2004 Zope Corporation and Contributors.
5
# All Rights Reserved.
6
#
7
# This software is subject to the provisions of the Zope Public License,
8
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
9
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
10
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
11
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
12
# FOR A PARTICULAR PURPOSE.
13
#
14
##############################################################################
15
"""Test script
16
17
$Id: test.py 25177 2004-06-02 13:17:31Z jim $
18
"""
5925.1.1 by Curtis Hovey
Added a mechanism to select the correct conf to load. The switch
19
import sys, os, time, logging, warnings, re
809 by Canonical.com Patch Queue Manager
Nuke some arch-tags
20
5796.2.1 by Maris Fogels
Fixed the Unix process session handling so that test.py can be run from the Emacs compile buffer.
21
22
if os.getsid(0) == os.getsid(os.getppid()):
23
    # We need to become the process group leader so test_on_merge.py
24
    # can reap its children.
25
    #
26
    # Note that if setpgrp() is used to move a process from one
27
    # process group to another (as is done by some shells when
28
    # creating pipelines), then both process groups must be part of
29
    # the same session.
30
    os.setpgrp()
2083 by Canonical.com Patch Queue Manager
[r=jamesh] testrunner improvements (?)
31
32
# Make tests run in a timezone no launchpad developers live in.
33
# Our tests need to run in any timezone.
34
# (No longer actually required, as PQM does this)
35
os.environ['TZ'] = 'Asia/Calcutta'
36
time.tzset()
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
37
6523.1.2 by James Henstridge
* Refactor passing of environment variables to the app server, and make
38
# Enable Storm's C extensions
39
os.environ['STORM_CEXTENSIONS'] = '1'
40
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
41
here = os.path.dirname(os.path.realpath(__file__))
1806 by Canonical.com Patch Queue Manager
Always use sys.path.insert instead of sys.path.append to give precedence for launchpad libs that are inside our tree. r=stub
42
sys.path.insert(0, os.path.join(here, 'lib'))
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
43
6667.1.2 by Barry Warsaw
Lint cleanups
44
# Mailman lives in a subdirectory.
45
mailman_dir = os.path.join(here, 'lib', 'mailman')
46
if os.path.isdir(mailman_dir):
47
    sys.path.insert(0, mailman_dir)
48
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
49
# Set PYTHONPATH environment variable for spawned processes
50
os.environ['PYTHONPATH'] = ':'.join(sys.path)
51
4385.1.2 by Stuart Bishop
Add profiling information on test suite infrastructure
52
# Set a flag if this is the main testrunner process
53
if len(sys.argv) > 1 and sys.argv[1] == '--resume-layer':
54
    main_process = False
55
else:
56
    main_process = True
57
2097 by Canonical.com Patch Queue Manager
[trivial] more import fascist improvements
58
# Install the import fascist import hook and atexit handler.
59
import importfascist
60
importfascist.install_import_fascist()
1968 by Canonical.com Patch Queue Manager
DatabaseCodeImportFascist: warn about database imports in browser code after running tests. [r=SteveA]
61
2103 by Canonical.com Patch Queue Manager
[trivial] improved warning output for tests, and fixed a bunch of ambiguous use of select results warnings. some xxx comments left in person code. one untested codepath in hct backend.
62
# Install the warning handler hook and atexit handler.
63
import warninghandler
64
warninghandler.install_warning_handler()
65
6697.3.2 by Francis J. Lacoste
Refactored AppServerLayer to only start up the web app server using runlaunchpad.py.
66
# Ensure that atexit handlers are executed on TERM.
67
import signal
68
def exit_with_atexit_handlers(*ignored):
69
    sys.exit(-1 * signal.SIGTERM)
70
signal.signal(signal.SIGTERM, exit_with_atexit_handlers)
71
2976.10.126 by Stuart Bishop
Ensure ZCML overrides are generated
72
# Ensure overrides are generated
73
from configs import generate_overrides
74
generate_overrides()
2103 by Canonical.com Patch Queue Manager
[trivial] improved warning output for tests, and fixed a bunch of ambiguous use of select results warnings. some xxx comments left in person code. one untested codepath in hct backend.
75
5925.1.1 by Curtis Hovey
Added a mechanism to select the correct conf to load. The switch
76
# Tell canonical.config to use the testrunner config instance.
1628 by Canonical.com Patch Queue Manager
Configuration, Librian and Librarian test harness work
77
from canonical.config import config
5925.1.1 by Curtis Hovey
Added a mechanism to select the correct conf to load. The switch
78
config.setInstance('testrunner')
1628 by Canonical.com Patch Queue Manager
Configuration, Librian and Librarian test harness work
79
3498.5.2 by Bjorn Tillenius
clarify a comment.
80
# Remove this module's directory from path, so that zope.testbrowser
81
# can import pystone from test:
3498.4.1 by Bjorn Tillenius
patch test.py to remove '.' from the python path, to allow testbrowser import pystone from test.
82
sys.path[:] = [p for p in sys.path if os.path.abspath(p) != here]
83
84
1210 by Canonical.com Patch Queue Manager
Refactor and document test harnesses
85
# Turn on psycopg debugging wrapper
86
#import canonical.database.debug
87
#canonical.database.debug.install()
88
2770.1.30 by Guilherme Salgado
Clean up the http_proxy env var before running tests, as we don't want the requests we make to localhost to be proxied
89
# Unset the http_proxy environment variable, because we're going to make
90
# requests to localhost and we don't wand this to be proxied.
91
try:
92
    os.environ.pop('http_proxy')
93
except KeyError:
94
    pass
95
2976.10.30 by Stuart Bishop
filter some test suite warnings
96
# Silence spurious warnings. Note that this does not propagate to subprocesses
97
# so this is not always as easy as it seems. Warnings caused by our code that
98
# need to be silenced should have an accomponied Bug reference.
2976.10.19 by Stuart Bishop
Update test.py for Zope3.2
99
#
100
warnings.filterwarnings(
101
        'ignore', 'PyCrypto', RuntimeWarning, 'twisted[.]conch[.]ssh'
102
        )
103
warnings.filterwarnings(
6435.1.10 by Michael Hudson
remove importd config -- not sure this was ever used, btw!
104
        'ignore', 'twisted.python.plugin', DeprecationWarning,
2976.10.30 by Stuart Bishop
filter some test suite warnings
105
        )
106
warnings.filterwarnings(
2976.10.116 by Stuart Bishop
Fix and update tests
107
        'ignore', 'bzrlib.*was deprecated', DeprecationWarning
6061.3.1 by Curtis Hovey
Made the minimum changes to to code and ZCML to make python2.4 -Wi::DeprecationWarning utilities/shhh.py make -C sourcecode build PYTHON=python2.4 -Wi::DeprecationWarning \
108
)
109
2976.10.61 by Stuart Bishop
Update warning filters
110
# This warning will be triggered if the beforeTraversal hook fails. We
111
# want to ensure it is not raised as an error, as this will mask the real
112
# problem.
113
warnings.filterwarnings(
114
        'always',
115
        re.escape('clear_request_started() called outside of a request'),
116
        UserWarning
117
        )
118
2976.10.30 by Stuart Bishop
filter some test suite warnings
119
# Any warnings not explicitly silenced are errors
120
warnings.filterwarnings('error', append=True)
121
1210 by Canonical.com Patch Queue Manager
Refactor and document test harnesses
122
1224 by Canonical.com Patch Queue Manager
Test isolation enforcement
123
from canonical.ftests import pgsql
1617 by Canonical.com Patch Queue Manager
Refactor PostgreSQL harnesses to avoid bug found by ddaa. Add more tests
124
# If this is removed, make sure canonical.ftests.pgsql is updated
125
# because the test harness there relies on the Connection wrapper being
126
# installed.
1224 by Canonical.com Patch Queue Manager
Test isolation enforcement
127
pgsql.installFakeConnect()
128
2976.10.19 by Stuart Bishop
Update test.py for Zope3.2
129
from zope.testing import testrunner
130
131
defaults = [
132
    # Find tests in the tests and ftests directories
133
    '--tests-pattern=^f?tests$',
134
    '--test-path=%s' % os.path.join(here, 'lib'),
135
    '--package=canonical',
136
    ]
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
137
6233.2.1 by Celso Providelo
Monkey-patch os.listdir to return randomised results (random.shuffle) in our test environment. Also fixes the two tests relying on unstable ordering.
138
# Monkey-patch os.listdir to randomise the results
139
original_listdir = os.listdir
140
141
import random
142
143
def listdir(path):
144
    """Randomise the results of os.listdir.
145
146
    It uses random.suffle to randomise os.listdir results, this way tests
147
    relying on unstable ordering will have a higher chance to fail in the
148
    development environment.
149
    """
150
    directory_contents = original_listdir(path)
151
    random.shuffle(directory_contents)
152
    return directory_contents
153
154
os.listdir = listdir
155
156
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
157
if __name__ == '__main__':
5925.1.1 by Curtis Hovey
Added a mechanism to select the correct conf to load. The switch
158
    # Extract arguments so we can see them too. We need to strip
159
    # --resume-layer and --default stuff if found as get_options can't
160
    # handle it.
4385.1.10 by Stuart Bishop
Review updates
161
    if len(sys.argv) > 1 and sys.argv[1] == '--resume-layer':
162
        args = list(sys.argv)
163
        args.pop(1) # --resume-layer
164
        args.pop(1) # The layer name
6061.14.13 by Francis J. Lacoste
--resume-layer takes an additional argument.
165
        args.pop(1) # The resume number
4385.1.10 by Stuart Bishop
Review updates
166
        while len(args) > 1 and args[1] == '--default':
167
            args.pop(1) # --default
168
            args.pop(1) # The default value
169
        args.insert(0, sys.argv[0])
170
    else:
171
        args = sys.argv
172
    options = testrunner.get_options(args=args, defaults=defaults)
4385.1.5 by Stuart Bishop
Docstrings and comments
173
4385.1.7 by Stuart Bishop
Don't want profiling overhead if profiling not requested
174
    # Turn on Layer profiling if requested.
175
    from canonical.testing import profiled
4385.1.10 by Stuart Bishop
Review updates
176
    if options.verbose >= 3 and main_process:
4385.1.11 by Stuart Bishop
Review feedback
177
        profiled.setup_profiling()
4385.1.7 by Stuart Bishop
Don't want profiling overhead if profiling not requested
178
5642.2.1 by Elliot Murphy
Set the working directory for the test runner script.
179
    # The working directory change is just so that the test script
180
    # can be invoked from places other than the root of the source
181
    # tree. This is very useful for IDE integration, so an IDE can
182
    # e.g. run the test that you are currently editing.
183
    try:
184
        there = os.getcwd()
185
        os.chdir(here)
186
        result = testrunner.run(defaults)
187
    finally:
188
        os.chdir(there)
2976.10.19 by Stuart Bishop
Update test.py for Zope3.2
189
    # Cribbed from sourcecode/zope/test.py - avoid spurious error during exit.
190
    logging.disable(999999999)
4385.1.2 by Stuart Bishop
Add profiling information on test suite infrastructure
191
4385.1.7 by Stuart Bishop
Don't want profiling overhead if profiling not requested
192
    # Print Layer profiling report if requested.
4385.1.5 by Stuart Bishop
Docstrings and comments
193
    if main_process and options.verbose >= 3:
4385.1.7 by Stuart Bishop
Don't want profiling overhead if profiling not requested
194
        profiled.report_profile_stats()
2976.10.19 by Stuart Bishop
Update test.py for Zope3.2
195
    sys.exit(result)