devtools/livetest.py
branchstable
changeset 4556 43c14e0e8972
parent 4252 6c4f109c2b03
child 5421 8167de96c523
equal deleted inserted replaced
4555:8968c50818db 4556:43c14e0e8972
     4 :copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
     4 :copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
     6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
     7 """
     7 """
     8 
     8 
       
     9 import os
     9 import socket
    10 import socket
    10 import logging
    11 import logging
    11 from os.path import join, dirname, exists
    12 from os.path import join, dirname, normpath, abspath
    12 from StringIO import StringIO
    13 from StringIO import StringIO
    13 
    14 
    14 #from twisted.application import service, strports
    15 #from twisted.application import service, strports
    15 # from twisted.internet import reactor, task
    16 # from twisted.internet import reactor, task
    16 from twisted.web2 import channel
    17 from twisted.web2 import channel
    19 from twisted.internet import reactor
    20 from twisted.internet import reactor
    20 from twisted.internet.error import CannotListenError
    21 from twisted.internet.error import CannotListenError
    21 
    22 
    22 from logilab.common.testlib import TestCase
    23 from logilab.common.testlib import TestCase
    23 
    24 
    24 import cubicweb.web
       
    25 from cubicweb.dbapi import in_memory_cnx
    25 from cubicweb.dbapi import in_memory_cnx
    26 from cubicweb.etwist.server import CubicWebRootResource
    26 from cubicweb.etwist.server import CubicWebRootResource
    27 from cubicweb.devtools import LivetestConfiguration, init_test_database
    27 from cubicweb.devtools import BaseApptestConfiguration, init_test_database
    28 
    28 
    29 
    29 
    30 
    30 
    31 def get_starturl(port=7777, login=None, passwd=None):
    31 def get_starturl(port=7777, login=None, passwd=None):
    32     if login:
    32     if login:
    48         # Otherwise we use this single resource
    48         # Otherwise we use this single resource
    49         return self, ()
    49         return self, ()
    50 
    50 
    51 
    51 
    52 
    52 
       
    53 class LivetestConfiguration(BaseApptestConfiguration):
       
    54     init_repository = False
       
    55 
       
    56     def __init__(self, cube=None, sourcefile=None, pyro_name=None,
       
    57                  log_threshold=logging.CRITICAL):
       
    58         BaseApptestConfiguration.__init__(self, cube, log_threshold=log_threshold)
       
    59         self.appid = pyro_name or cube
       
    60         # don't change this, else some symlink problems may arise in some
       
    61         # environment (e.g. mine (syt) ;o)
       
    62         # XXX I'm afraid this test will prevent to run test from a production
       
    63         # environment
       
    64         self._sources = None
       
    65         # instance cube test
       
    66         if cube is not None:
       
    67             self.apphome = self.cube_dir(cube)
       
    68         elif 'web' in os.getcwd().split(os.sep):
       
    69             # web test
       
    70             self.apphome = join(normpath(join(dirname(__file__), '..')), 'web')
       
    71         else:
       
    72             # cube test
       
    73             self.apphome = abspath('..')
       
    74         self.sourcefile = sourcefile
       
    75         self.global_set_option('realm', '')
       
    76         self.use_pyro = pyro_name is not None
       
    77 
       
    78     def pyro_enabled(self):
       
    79         if self.use_pyro:
       
    80             return True
       
    81         else:
       
    82             return False
       
    83 
       
    84 
       
    85 
    53 def make_site(cube, options=None):
    86 def make_site(cube, options=None):
    54     from cubicweb.etwist import twconfig # trigger configuration registration
    87     from cubicweb.etwist import twconfig # trigger configuration registration
    55     sourcefile = options.sourcefile
    88     config = LivetestConfiguration(cube, options.sourcefile,
    56     config = LivetestConfiguration(cube, sourcefile,
       
    57                                    pyro_name=options.pyro_name,
    89                                    pyro_name=options.pyro_name,
    58                                    log_threshold=logging.DEBUG)
    90                                    log_threshold=logging.DEBUG)
    59     source = config.sources()['system']
    91     init_test_database(config=config)
    60     init_test_database(driver=source['db-driver'], config=config)
       
    61     # if '-n' in sys.argv: # debug mode
    92     # if '-n' in sys.argv: # debug mode
    62     cubicweb = LivetestResource(config, debug=True)
    93     cubicweb = LivetestResource(config, debug=True)
    63     toplevel = cubicweb
    94     toplevel = cubicweb
    64     website = server.Site(toplevel)
    95     website = server.Site(toplevel)
    65     cube_dir = config.cube_dir(cube)
    96     cube_dir = config.cube_dir(cube)
       
    97     source = config.sources()['system']
    66     for port in xrange(7777, 7798):
    98     for port in xrange(7777, 7798):
    67         try:
    99         try:
    68             reactor.listenTCP(port, channel.HTTPFactory(website))
   100             reactor.listenTCP(port, channel.HTTPFactory(website))
    69             saveconf(cube_dir, port, source['db-user'], source['db-password'])
   101             saveconf(cube_dir, port, source['db-user'], source['db-password'])
    70             break
   102             break
    71         except CannotListenError, exc:
   103         except CannotListenError:
    72             print "port %s already in use, I will try another one" % port
   104             print "port %s already in use, I will try another one" % port
    73     else:
   105     else:
    74         raise
   106         raise
    75     cubicweb.base_url = get_starturl(port=port)
   107     cubicweb.base_url = get_starturl(port=port)
    76     print "you can go here : %s" % cubicweb.base_url
   108     print "you can go here : %s" % cubicweb.base_url
   117         self.twill_output = StringIO()
   149         self.twill_output = StringIO()
   118         hijack_twill_output(self.twill_output)
   150         hijack_twill_output(self.twill_output)
   119         # build a config, and get a connection
   151         # build a config, and get a connection
   120         self.config = LivetestConfiguration(self.cube, self.sourcefile)
   152         self.config = LivetestConfiguration(self.cube, self.sourcefile)
   121         _, user, passwd, _ = loadconf()
   153         _, user, passwd, _ = loadconf()
   122         self.repo, self.cnx = in_memory_cnx(self.config, user, passwd)
   154         self.repo, self.cnx = in_memory_cnx(self.config, user, password=passwd)
   123         self.setup_db(self.cnx)
   155         self.setup_db(self.cnx)
   124 
   156 
   125     def tearDown(self):
   157     def tearDown(self):
   126         self.teardown_db(self.cnx)
   158         self.teardown_db(self.cnx)
   127 
   159