1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
#!/usr/bin/env python2.3
##############################################################################
#
# Copyright (c) 2004 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Test script
$Id: test.py 25177 2004-06-02 13:17:31Z jim $
"""
import sys, os, psycopg
here = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, os.path.join(here, 'lib'))
# Set PYTHONPATH environment variable for spawned processes
os.environ['PYTHONPATH'] = ':'.join(sys.path)
# Import fascist. We set this up early to try to intercept as many imports as
# possible.
import __builtin__
import atexit
original_import = __builtin__.__import__
database_root = 'canonical.launchpad.database'
browser_root = 'canonical.launchpad.browser'
naughty_imports = set()
class JackbootError(ImportError):
"""Import Fascist says you can't make this import."""
def import_fascist(name, globals={}, locals={}, fromlist=[]):
import_into = globals.get('__name__', '')
if name.startswith(database_root) and import_into.startswith(browser_root):
# We'll eventually disallow these imports altogether. For now we just
# warn about it.
naughty_imports.add((name, import_into))
#raise JackbootError("ImportFascist says you cannot import %s into %s"
# % (name, import_into))
return original_import(name, globals, locals, fromlist)
__builtin__.__import__ = import_fascist
def report_naughty_imports():
if naughty_imports:
print
print '** %d import policy violations **' % len(naughty_imports)
current_name = None
for name, import_into in sorted(naughty_imports):
if name != current_name:
print "You should not import %s into:" % name
current_name = name
print " %s" % import_into
atexit.register(report_naughty_imports)
# Tell canonical.config to use the test config file, not launchpad.conf
from canonical.config import config
config.setDefaultSection('testrunner')
# Turn on psycopg debugging wrapper
#import canonical.database.debug
#canonical.database.debug.install()
# Silence spurious warnings or turn them into errors
import warnings
# Our Z3 is still using whrandom
warnings.filterwarnings(
"ignore",
"the whrandom module is deprecated; please use the random module"
)
# Some stuff got deprecated in 2.4 that we can clean up
warnings.filterwarnings(
"error", category=DeprecationWarning, module="email"
)
from canonical.ftests import pgsql
# If this is removed, make sure canonical.ftests.pgsql is updated
# because the test harness there relies on the Connection wrapper being
# installed.
pgsql.installFakeConnect()
# This is a terrible hack to divorce the FunctionalTestSetup from
# its assumptions about the ZODB.
from zope.app.tests.functional import FunctionalTestSetup
FunctionalTestSetup.__init__ = lambda *x: None
# Install our own test runner to to pre/post sanity checks
import zope.app.tests.test
from canonical.database.sqlbase import SQLBase, ZopelessTransactionManager
class LaunchpadTestRunner(zope.app.tests.test.ImmediateTestRunner):
def precheck(self, test):
pass
def postcheck(self, test):
'''Tests run at the conclusion of every top level test suite'''
# Confirm Zopeless teardown has been called if necessary
assert ZopelessTransactionManager._installed is None, \
'Test used Zopeless but failed to tearDown correctly'
# Confirm all database connections have been dropped
assert len(pgsql.PgTestSetup.connections) == 0, \
'Not all PostgreSQL connections closed'
# Disabled this check - we now optimize by only dropping the
# db if necessary
#
#con = psycopg.connect('dbname=template1')
#try:
# cur = con.cursor()
# cur.execute("""
# SELECT count(*) FROM pg_database
# WHERE datname='launchpad_ftest'
# """)
# r = cur.fetchone()[0]
# assert r == 0, 'launchpad_ftest database not dropped'
#finally:
# con.close()
def run(self, test):
self.precheck(test)
rv = super(LaunchpadTestRunner, self).run(test)
self.postcheck(test)
return rv
zope.app.tests.test.ImmediateTestRunner = LaunchpadTestRunner
if __name__ == '__main__':
zope.app.tests.test.process_args()
|