devtools/__init__.py
changeset 9498 a76ac18f09c4
parent 9460 a2a0bc984863
child 9519 32662203e334
equal deleted inserted replaced
9497:7310738fafe5 9498:a76ac18f09c4
    23 import sys
    23 import sys
    24 import logging
    24 import logging
    25 import shutil
    25 import shutil
    26 import pickle
    26 import pickle
    27 import glob
    27 import glob
       
    28 import random
       
    29 import subprocess
    28 import warnings
    30 import warnings
    29 import tempfile
    31 import tempfile
       
    32 import getpass
    30 from hashlib import sha1 # pylint: disable=E0611
    33 from hashlib import sha1 # pylint: disable=E0611
    31 from datetime import timedelta
    34 from datetime import timedelta
    32 from os.path import (abspath, join, exists, split, isabs, isdir)
    35 from os.path import (abspath, join, exists, split, isabs, isdir)
    33 from functools import partial
    36 from functools import partial
    34 
    37 
    84                               },
    87                               },
    85                    'admin' : {'login': u'admin',
    88                    'admin' : {'login': u'admin',
    86                               'password': u'gingkow',
    89                               'password': u'gingkow',
    87                               },
    90                               },
    88                    }
    91                    }
       
    92 DEFAULT_PSQL_SOURCES = DEFAULT_SOURCES.copy()
       
    93 DEFAULT_PSQL_SOURCES['system'] = DEFAULT_SOURCES['system'].copy()
       
    94 DEFAULT_PSQL_SOURCES['system']['db-driver'] = 'postgres'
       
    95 DEFAULT_PSQL_SOURCES['system']['db-host'] = '/tmp'
       
    96 DEFAULT_PSQL_SOURCES['system']['db-port'] = str(random.randrange(5432, 2**16))
       
    97 DEFAULT_PSQL_SOURCES['system']['db-user'] = unicode(getpass.getuser())
       
    98 DEFAULT_PSQL_SOURCES['system']['db-password'] = None
    89 
    99 
    90 def turn_repo_off(repo):
   100 def turn_repo_off(repo):
    91     """ Idea: this is less costly than a full re-creation of the repo object.
   101     """ Idea: this is less costly than a full re-creation of the repo object.
    92     off:
   102     off:
    93     * session are closed,
   103     * session are closed,
   127 class TestServerConfiguration(ServerConfiguration):
   137 class TestServerConfiguration(ServerConfiguration):
   128     mode = 'test'
   138     mode = 'test'
   129     read_instance_schema = False
   139     read_instance_schema = False
   130     init_repository = True
   140     init_repository = True
   131     skip_db_create_and_restore = False
   141     skip_db_create_and_restore = False
       
   142     default_sources = DEFAULT_SOURCES
       
   143 
   132     def __init__(self, appid='data', apphome=None, log_threshold=logging.CRITICAL+10):
   144     def __init__(self, appid='data', apphome=None, log_threshold=logging.CRITICAL+10):
   133         # must be set before calling parent __init__
   145         # must be set before calling parent __init__
   134         if apphome is None:
   146         if apphome is None:
   135             if exists(appid):
   147             if exists(appid):
   136                 apphome = abspath(appid)
   148                 apphome = abspath(appid)
   191         return sourcefile
   203         return sourcefile
   192 
   204 
   193     def read_sources_file(self):
   205     def read_sources_file(self):
   194         """By default, we run tests with the sqlite DB backend.  One may use its
   206         """By default, we run tests with the sqlite DB backend.  One may use its
   195         own configuration by just creating a 'sources' file in the test
   207         own configuration by just creating a 'sources' file in the test
   196         directory from wich tests are launched or by specifying an alternative
   208         directory from which tests are launched or by specifying an alternative
   197         sources file using self.sourcefile.
   209         sources file using self.sourcefile.
   198         """
   210         """
   199         try:
   211         try:
   200             sources = super(TestServerConfiguration, self).read_sources_file()
   212             sources = super(TestServerConfiguration, self).read_sources_file()
   201         except ExecutionError:
   213         except ExecutionError:
   202             sources = {}
   214             sources = {}
   203         if not sources:
   215         if not sources:
   204             sources = DEFAULT_SOURCES
   216             sources = self.default_sources
   205         if 'admin' not in sources:
   217         if 'admin' not in sources:
   206             sources['admin'] = DEFAULT_SOURCES['admin']
   218             sources['admin'] = self.default_sources['admin']
   207         return sources
   219         return sources
   208 
   220 
   209     # web config methods needed here for cases when we use this config as a web
   221     # web config methods needed here for cases when we use this config as a web
   210     # config
   222     # config
   211 
   223 
   240                  log_threshold=logging.CRITICAL, sourcefile=None):
   252                  log_threshold=logging.CRITICAL, sourcefile=None):
   241         BaseApptestConfiguration.__init__(self, appid, apphome,
   253         BaseApptestConfiguration.__init__(self, appid, apphome,
   242                                           log_threshold=log_threshold)
   254                                           log_threshold=log_threshold)
   243         self.init_repository = sourcefile is None
   255         self.init_repository = sourcefile is None
   244         self.sourcefile = sourcefile
   256         self.sourcefile = sourcefile
       
   257 
       
   258 
       
   259 class PostgresApptestConfiguration(ApptestConfiguration):
       
   260     default_sources = DEFAULT_PSQL_SOURCES
   245 
   261 
   246 
   262 
   247 class RealDatabaseConfiguration(ApptestConfiguration):
   263 class RealDatabaseConfiguration(ApptestConfiguration):
   248     """configuration class for tests to run on a real database.
   264     """configuration class for tests to run on a real database.
   249 
   265 
   515 
   531 
   516 ### postgres test database handling ############################################
   532 ### postgres test database handling ############################################
   517 
   533 
   518 class PostgresTestDataBaseHandler(TestDataBaseHandler):
   534 class PostgresTestDataBaseHandler(TestDataBaseHandler):
   519     DRIVER = 'postgres'
   535     DRIVER = 'postgres'
       
   536 
       
   537     __CTL = set()
       
   538 
       
   539     @classmethod
       
   540     def killall(cls):
       
   541         for datadir in cls.__CTL:
       
   542             subprocess.call(['pg_ctl', 'stop', '-D', datadir, '-m', 'fast'])
       
   543 
       
   544     def __init__(self, config):
       
   545         super(PostgresTestDataBaseHandler, self).__init__(config)
       
   546         datadir = join(self.config.apphome, 'pgdb')
       
   547         if not exists(datadir):
       
   548             subprocess.check_call(['initdb', '-D', datadir, '-E', 'utf-8'])
       
   549         port = self.system_source['db-port']
       
   550         subprocess.check_call(['pg_ctl', 'start', '-w', '-D', datadir, '-o', '-h "" -k /tmp -p %s' % port])
       
   551         self.__CTL.add(datadir)
   520 
   552 
   521     @property
   553     @property
   522     @cached
   554     @cached
   523     def helper(self):
   555     def helper(self):
   524         from logilab.database import get_db_helper
   556         from logilab.database import get_db_helper
   728         self._cleanup_database(self.absolute_dbfile())
   760         self._cleanup_database(self.absolute_dbfile())
   729         init_repository(self.config, interactive=False)
   761         init_repository(self.config, interactive=False)
   730 
   762 
   731 import atexit
   763 import atexit
   732 atexit.register(SQLiteTestDataBaseHandler._cleanup_all_tmpdb)
   764 atexit.register(SQLiteTestDataBaseHandler._cleanup_all_tmpdb)
       
   765 atexit.register(PostgresTestDataBaseHandler.killall)
   733 
   766 
   734 
   767 
   735 def install_sqlite_patch(querier):
   768 def install_sqlite_patch(querier):
   736     """This patch hotfixes the following sqlite bug :
   769     """This patch hotfixes the following sqlite bug :
   737        - http://www.sqlite.org/cvstrac/tktview?tn=1327,33
   770        - http://www.sqlite.org/cvstrac/tktview?tn=1327,33