author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 12 Jul 2010 10:36:28 +0200 | |
branch | stable |
changeset 5954 | 987086484876 |
parent 5424 | 8ecbcbff9777 |
child 5907 | 5eba1248e12b |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
18 |
"""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
|
19 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
20 |
""" |
0 | 21 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
22 |
import os |
0 | 23 |
import socket |
24 |
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
|
25 |
from os.path import join, dirname, normpath, abspath |
0 | 26 |
from StringIO import StringIO |
27 |
||
28 |
#from twisted.application import service, strports |
|
29 |
# from twisted.internet import reactor, task |
|
30 |
from twisted.web2 import channel |
|
31 |
from twisted.web2 import server |
|
32 |
from twisted.web2 import static |
|
33 |
from twisted.internet import reactor |
|
34 |
from twisted.internet.error import CannotListenError |
|
35 |
||
36 |
from logilab.common.testlib import TestCase |
|
37 |
||
38 |
from cubicweb.dbapi import in_memory_cnx |
|
39 |
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
|
40 |
from cubicweb.devtools import BaseApptestConfiguration, init_test_database |
0 | 41 |
|
42 |
||
43 |
||
44 |
def get_starturl(port=7777, login=None, passwd=None): |
|
45 |
if login: |
|
46 |
return 'http://%s:%s/view?login=%s&password=%s' % (socket.gethostname(), port, login, passwd) |
|
47 |
else: |
|
48 |
return 'http://%s:%s/' % (socket.gethostname(), port) |
|
49 |
||
50 |
||
51 |
class LivetestResource(CubicWebRootResource): |
|
52 |
"""redefines main resource to search for data files in several directories""" |
|
53 |
||
54 |
def locateChild(self, request, segments): |
|
55 |
"""Indicate which resource to use to process down the URL's path""" |
|
56 |
if len(segments) and segments[0] == 'data': |
|
57 |
# 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
|
58 |
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
|
59 |
if datadir: |
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
60 |
return static.File(str(datadir), segments[1:]) |
0 | 61 |
# Otherwise we use this single resource |
62 |
return self, () |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
63 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
64 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
65 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
66 |
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
|
67 |
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
|
68 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
69 |
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
|
70 |
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
|
71 |
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
|
72 |
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
|
73 |
# 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
|
74 |
# 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
|
75 |
# 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
|
76 |
# environment |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
77 |
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
|
78 |
# 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
|
79 |
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
|
80 |
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
|
81 |
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
|
82 |
# 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
|
83 |
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
|
84 |
else: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
85 |
# 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
|
86 |
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
|
87 |
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
|
88 |
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
|
89 |
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
|
90 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
91 |
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
|
92 |
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
|
93 |
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
|
94 |
else: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
95 |
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
|
96 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
97 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
98 |
|
0 | 99 |
def make_site(cube, options=None): |
100 |
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
|
101 |
config = LivetestConfiguration(cube, options.sourcefile, |
0 | 102 |
pyro_name=options.pyro_name, |
103 |
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
|
104 |
init_test_database(config=config) |
0 | 105 |
# if '-n' in sys.argv: # debug mode |
106 |
cubicweb = LivetestResource(config, debug=True) |
|
107 |
toplevel = cubicweb |
|
108 |
website = server.Site(toplevel) |
|
109 |
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
|
110 |
source = config.sources()['system'] |
0 | 111 |
for port in xrange(7777, 7798): |
112 |
try: |
|
113 |
reactor.listenTCP(port, channel.HTTPFactory(website)) |
|
114 |
saveconf(cube_dir, port, source['db-user'], source['db-password']) |
|
115 |
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
|
116 |
except CannotListenError: |
0 | 117 |
print "port %s already in use, I will try another one" % port |
118 |
else: |
|
119 |
raise |
|
120 |
cubicweb.base_url = get_starturl(port=port) |
|
121 |
print "you can go here : %s" % cubicweb.base_url |
|
122 |
||
123 |
def runserver(): |
|
124 |
reactor.run() |
|
125 |
||
126 |
def saveconf(templhome, port, user, passwd): |
|
127 |
import pickle |
|
128 |
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
|
129 |
|
0 | 130 |
pickle.dump((port, user, passwd, get_starturl(port, user, passwd)), |
131 |
conffile) |
|
132 |
conffile.close() |
|
133 |
||
134 |
||
135 |
def loadconf(filename='livetest.conf'): |
|
136 |
import pickle |
|
137 |
return pickle.load(file(filename)) |
|
138 |
||
139 |
||
140 |
def execute_scenario(filename, **kwargs): |
|
141 |
"""based on twill.parse.execute_file, but inserts cubicweb extensions""" |
|
142 |
from twill.parse import _execute_script |
|
143 |
stream = StringIO('extend_with cubicweb.devtools.cubicwebtwill\n' + file(filename).read()) |
|
144 |
kwargs['source'] = filename |
|
145 |
_execute_script(stream, **kwargs) |
|
146 |
||
147 |
||
148 |
def hijack_twill_output(new_output): |
|
149 |
from twill import commands as twc |
|
150 |
from twill import browser as twb |
|
151 |
twc.OUT = new_output |
|
152 |
twb.OUT = new_output |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
153 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
154 |
|
0 | 155 |
class LiveTestCase(TestCase): |
156 |
||
157 |
sourcefile = None |
|
158 |
cube = '' |
|
159 |
def setUp(self): |
|
160 |
assert self.cube, "You must specify a cube in your testcase" |
|
161 |
# twill can be quite verbose ... |
|
162 |
self.twill_output = StringIO() |
|
163 |
hijack_twill_output(self.twill_output) |
|
164 |
# build a config, and get a connection |
|
165 |
self.config = LivetestConfiguration(self.cube, self.sourcefile) |
|
166 |
_, 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
|
167 |
self.repo, self.cnx = in_memory_cnx(self.config, user, password=passwd) |
0 | 168 |
self.setup_db(self.cnx) |
169 |
||
170 |
def tearDown(self): |
|
171 |
self.teardown_db(self.cnx) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
172 |
|
0 | 173 |
|
174 |
def setup_db(self, cnx): |
|
175 |
"""override setup_db() to setup your environment""" |
|
176 |
||
177 |
def teardown_db(self, cnx): |
|
178 |
"""override teardown_db() to clean up your environment""" |
|
179 |
||
180 |
def get_loggedurl(self): |
|
181 |
port, user, passwd, logged_url = loadconf() |
|
182 |
return logged_url |
|
183 |
||
184 |
def get_anonurl(self): |
|
185 |
port, _, _, _ = loadconf() |
|
186 |
return 'http://%s:%s/view?login=anon&password=anon' % ( |
|
187 |
socket.gethostname(), port) |
|
188 |
||
189 |
# convenience |
|
190 |
execute_scenario = staticmethod(execute_scenario) |
|
191 |
||
192 |
||
193 |
if __name__ == '__main__': |
|
194 |
runserver() |