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