author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 15 Sep 2009 14:47:39 +0200 | |
branch | 3.5 |
changeset 3225 | 464c5b9c6fa0 |
parent 1977 | 606923dff11b |
child 2773 | b2530e3e0afb |
child 4212 | ab6573088b4a |
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 |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
4 |
:copyright: 2001-2009 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 |
|
9 |
import socket |
|
10 |
import logging |
|
11 |
from os.path import join, dirname, exists |
|
12 |
from StringIO import StringIO |
|
13 |
||
14 |
#from twisted.application import service, strports |
|
15 |
# from twisted.internet import reactor, task |
|
16 |
from twisted.web2 import channel |
|
17 |
from twisted.web2 import server |
|
18 |
from twisted.web2 import static |
|
19 |
from twisted.internet import reactor |
|
20 |
from twisted.internet.error import CannotListenError |
|
21 |
||
22 |
from logilab.common.testlib import TestCase |
|
23 |
||
24 |
import cubicweb.web |
|
25 |
from cubicweb.dbapi import in_memory_cnx |
|
26 |
from cubicweb.etwist.server import CubicWebRootResource |
|
27 |
from cubicweb.devtools import LivetestConfiguration, init_test_database |
|
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 |
|
0 | 53 |
def make_site(cube, options=None): |
54 |
from cubicweb.etwist import twconfig # trigger configuration registration |
|
55 |
sourcefile = options.sourcefile |
|
56 |
config = LivetestConfiguration(cube, sourcefile, |
|
57 |
pyro_name=options.pyro_name, |
|
58 |
log_threshold=logging.DEBUG) |
|
59 |
source = config.sources()['system'] |
|
60 |
init_test_database(driver=source['db-driver'], config=config) |
|
61 |
# if '-n' in sys.argv: # debug mode |
|
62 |
cubicweb = LivetestResource(config, debug=True) |
|
63 |
toplevel = cubicweb |
|
64 |
website = server.Site(toplevel) |
|
65 |
cube_dir = config.cube_dir(cube) |
|
66 |
for port in xrange(7777, 7798): |
|
67 |
try: |
|
68 |
reactor.listenTCP(port, channel.HTTPFactory(website)) |
|
69 |
saveconf(cube_dir, port, source['db-user'], source['db-password']) |
|
70 |
break |
|
71 |
except CannotListenError, exc: |
|
72 |
print "port %s already in use, I will try another one" % port |
|
73 |
else: |
|
74 |
raise |
|
75 |
cubicweb.base_url = get_starturl(port=port) |
|
76 |
print "you can go here : %s" % cubicweb.base_url |
|
77 |
||
78 |
def runserver(): |
|
79 |
reactor.run() |
|
80 |
||
81 |
def saveconf(templhome, port, user, passwd): |
|
82 |
import pickle |
|
83 |
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
|
84 |
|
0 | 85 |
pickle.dump((port, user, passwd, get_starturl(port, user, passwd)), |
86 |
conffile) |
|
87 |
conffile.close() |
|
88 |
||
89 |
||
90 |
def loadconf(filename='livetest.conf'): |
|
91 |
import pickle |
|
92 |
return pickle.load(file(filename)) |
|
93 |
||
94 |
||
95 |
def execute_scenario(filename, **kwargs): |
|
96 |
"""based on twill.parse.execute_file, but inserts cubicweb extensions""" |
|
97 |
from twill.parse import _execute_script |
|
98 |
stream = StringIO('extend_with cubicweb.devtools.cubicwebtwill\n' + file(filename).read()) |
|
99 |
kwargs['source'] = filename |
|
100 |
_execute_script(stream, **kwargs) |
|
101 |
||
102 |
||
103 |
def hijack_twill_output(new_output): |
|
104 |
from twill import commands as twc |
|
105 |
from twill import browser as twb |
|
106 |
twc.OUT = new_output |
|
107 |
twb.OUT = new_output |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
108 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
109 |
|
0 | 110 |
class LiveTestCase(TestCase): |
111 |
||
112 |
sourcefile = None |
|
113 |
cube = '' |
|
114 |
def setUp(self): |
|
115 |
assert self.cube, "You must specify a cube in your testcase" |
|
116 |
# twill can be quite verbose ... |
|
117 |
self.twill_output = StringIO() |
|
118 |
hijack_twill_output(self.twill_output) |
|
119 |
# build a config, and get a connection |
|
120 |
self.config = LivetestConfiguration(self.cube, self.sourcefile) |
|
121 |
_, user, passwd, _ = loadconf() |
|
122 |
self.repo, self.cnx = in_memory_cnx(self.config, user, passwd) |
|
123 |
self.setup_db(self.cnx) |
|
124 |
||
125 |
def tearDown(self): |
|
126 |
self.teardown_db(self.cnx) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
127 |
|
0 | 128 |
|
129 |
def setup_db(self, cnx): |
|
130 |
"""override setup_db() to setup your environment""" |
|
131 |
||
132 |
def teardown_db(self, cnx): |
|
133 |
"""override teardown_db() to clean up your environment""" |
|
134 |
||
135 |
def get_loggedurl(self): |
|
136 |
port, user, passwd, logged_url = loadconf() |
|
137 |
return logged_url |
|
138 |
||
139 |
def get_anonurl(self): |
|
140 |
port, _, _, _ = loadconf() |
|
141 |
return 'http://%s:%s/view?login=anon&password=anon' % ( |
|
142 |
socket.gethostname(), port) |
|
143 |
||
144 |
# convenience |
|
145 |
execute_scenario = staticmethod(execute_scenario) |
|
146 |
||
147 |
||
148 |
if __name__ == '__main__': |
|
149 |
runserver() |