author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 15 Feb 2010 15:11:02 +0100 | |
branch | stable |
changeset 4571 | 794ac358dc36 |
parent 4252 | 6c4f109c2b03 |
child 5421 | 8167de96c523 |
permissions | -rw-r--r-- |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
1 |
"""provide utilies for web (live) unit testing |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
2 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
3 |
:organization: Logilab |
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
4 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
7 |
""" |
0 | 8 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
9 |
import os |
0 | 10 |
import socket |
11 |
import logging |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
12 |
from os.path import join, dirname, normpath, abspath |
0 | 13 |
from StringIO import StringIO |
14 |
||
15 |
#from twisted.application import service, strports |
|
16 |
# from twisted.internet import reactor, task |
|
17 |
from twisted.web2 import channel |
|
18 |
from twisted.web2 import server |
|
19 |
from twisted.web2 import static |
|
20 |
from twisted.internet import reactor |
|
21 |
from twisted.internet.error import CannotListenError |
|
22 |
||
23 |
from logilab.common.testlib import TestCase |
|
24 |
||
25 |
from cubicweb.dbapi import in_memory_cnx |
|
26 |
from cubicweb.etwist.server import CubicWebRootResource |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
27 |
from cubicweb.devtools import BaseApptestConfiguration, init_test_database |
0 | 28 |
|
29 |
||
30 |
||
31 |
def get_starturl(port=7777, login=None, passwd=None): |
|
32 |
if login: |
|
33 |
return 'http://%s:%s/view?login=%s&password=%s' % (socket.gethostname(), port, login, passwd) |
|
34 |
else: |
|
35 |
return 'http://%s:%s/' % (socket.gethostname(), port) |
|
36 |
||
37 |
||
38 |
class LivetestResource(CubicWebRootResource): |
|
39 |
"""redefines main resource to search for data files in several directories""" |
|
40 |
||
41 |
def locateChild(self, request, segments): |
|
42 |
"""Indicate which resource to use to process down the URL's path""" |
|
43 |
if len(segments) and segments[0] == 'data': |
|
44 |
# Anything in data/ is treated as static files |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
45 |
datadir = self.config.locate_resource(segments[1]) |
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
46 |
if datadir: |
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
47 |
return static.File(str(datadir), segments[1:]) |
0 | 48 |
# Otherwise we use this single resource |
49 |
return self, () |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
50 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
51 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
52 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
53 |
class LivetestConfiguration(BaseApptestConfiguration): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
54 |
init_repository = False |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
55 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
56 |
def __init__(self, cube=None, sourcefile=None, pyro_name=None, |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
57 |
log_threshold=logging.CRITICAL): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
58 |
BaseApptestConfiguration.__init__(self, cube, log_threshold=log_threshold) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
59 |
self.appid = pyro_name or cube |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
60 |
# don't change this, else some symlink problems may arise in some |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
61 |
# environment (e.g. mine (syt) ;o) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
62 |
# XXX I'm afraid this test will prevent to run test from a production |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
63 |
# environment |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
64 |
self._sources = None |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
65 |
# instance cube test |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
66 |
if cube is not None: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
67 |
self.apphome = self.cube_dir(cube) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
68 |
elif 'web' in os.getcwd().split(os.sep): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
69 |
# web test |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
70 |
self.apphome = join(normpath(join(dirname(__file__), '..')), 'web') |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
71 |
else: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
72 |
# cube test |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
73 |
self.apphome = abspath('..') |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
74 |
self.sourcefile = sourcefile |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
75 |
self.global_set_option('realm', '') |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
76 |
self.use_pyro = pyro_name is not None |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
77 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
78 |
def pyro_enabled(self): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
79 |
if self.use_pyro: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
80 |
return True |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
81 |
else: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
82 |
return False |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
83 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
84 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
85 |
|
0 | 86 |
def make_site(cube, options=None): |
87 |
from cubicweb.etwist import twconfig # trigger configuration registration |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
88 |
config = LivetestConfiguration(cube, options.sourcefile, |
0 | 89 |
pyro_name=options.pyro_name, |
90 |
log_threshold=logging.DEBUG) |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
91 |
init_test_database(config=config) |
0 | 92 |
# if '-n' in sys.argv: # debug mode |
93 |
cubicweb = LivetestResource(config, debug=True) |
|
94 |
toplevel = cubicweb |
|
95 |
website = server.Site(toplevel) |
|
96 |
cube_dir = config.cube_dir(cube) |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
97 |
source = config.sources()['system'] |
0 | 98 |
for port in xrange(7777, 7798): |
99 |
try: |
|
100 |
reactor.listenTCP(port, channel.HTTPFactory(website)) |
|
101 |
saveconf(cube_dir, port, source['db-user'], source['db-password']) |
|
102 |
break |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
103 |
except CannotListenError: |
0 | 104 |
print "port %s already in use, I will try another one" % port |
105 |
else: |
|
106 |
raise |
|
107 |
cubicweb.base_url = get_starturl(port=port) |
|
108 |
print "you can go here : %s" % cubicweb.base_url |
|
109 |
||
110 |
def runserver(): |
|
111 |
reactor.run() |
|
112 |
||
113 |
def saveconf(templhome, port, user, passwd): |
|
114 |
import pickle |
|
115 |
conffile = file(join(templhome, 'test', 'livetest.conf'), 'w') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
116 |
|
0 | 117 |
pickle.dump((port, user, passwd, get_starturl(port, user, passwd)), |
118 |
conffile) |
|
119 |
conffile.close() |
|
120 |
||
121 |
||
122 |
def loadconf(filename='livetest.conf'): |
|
123 |
import pickle |
|
124 |
return pickle.load(file(filename)) |
|
125 |
||
126 |
||
127 |
def execute_scenario(filename, **kwargs): |
|
128 |
"""based on twill.parse.execute_file, but inserts cubicweb extensions""" |
|
129 |
from twill.parse import _execute_script |
|
130 |
stream = StringIO('extend_with cubicweb.devtools.cubicwebtwill\n' + file(filename).read()) |
|
131 |
kwargs['source'] = filename |
|
132 |
_execute_script(stream, **kwargs) |
|
133 |
||
134 |
||
135 |
def hijack_twill_output(new_output): |
|
136 |
from twill import commands as twc |
|
137 |
from twill import browser as twb |
|
138 |
twc.OUT = new_output |
|
139 |
twb.OUT = new_output |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
140 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
141 |
|
0 | 142 |
class LiveTestCase(TestCase): |
143 |
||
144 |
sourcefile = None |
|
145 |
cube = '' |
|
146 |
def setUp(self): |
|
147 |
assert self.cube, "You must specify a cube in your testcase" |
|
148 |
# twill can be quite verbose ... |
|
149 |
self.twill_output = StringIO() |
|
150 |
hijack_twill_output(self.twill_output) |
|
151 |
# build a config, and get a connection |
|
152 |
self.config = LivetestConfiguration(self.cube, self.sourcefile) |
|
153 |
_, user, passwd, _ = loadconf() |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2773
diff
changeset
|
154 |
self.repo, self.cnx = in_memory_cnx(self.config, user, password=passwd) |
0 | 155 |
self.setup_db(self.cnx) |
156 |
||
157 |
def tearDown(self): |
|
158 |
self.teardown_db(self.cnx) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
159 |
|
0 | 160 |
|
161 |
def setup_db(self, cnx): |
|
162 |
"""override setup_db() to setup your environment""" |
|
163 |
||
164 |
def teardown_db(self, cnx): |
|
165 |
"""override teardown_db() to clean up your environment""" |
|
166 |
||
167 |
def get_loggedurl(self): |
|
168 |
port, user, passwd, logged_url = loadconf() |
|
169 |
return logged_url |
|
170 |
||
171 |
def get_anonurl(self): |
|
172 |
port, _, _, _ = loadconf() |
|
173 |
return 'http://%s:%s/view?login=anon&password=anon' % ( |
|
174 |
socket.gethostname(), port) |
|
175 |
||
176 |
# convenience |
|
177 |
execute_scenario = staticmethod(execute_scenario) |
|
178 |
||
179 |
||
180 |
if __name__ == '__main__': |
|
181 |
runserver() |