1 # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
1 # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
3 # |
3 # |
4 # This file is part of CubicWeb. |
4 # This file is part of CubicWeb. |
5 # |
5 # |
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
291 class TestDataBaseHandler(object): |
291 class TestDataBaseHandler(object): |
292 DRIVER = None |
292 DRIVER = None |
293 db_cache = {} |
293 db_cache = {} |
294 explored_glob = set() |
294 explored_glob = set() |
295 |
295 |
296 def __init__(self, config): |
296 def __init__(self, config, init_config=None): |
297 self.config = config |
297 self.config = config |
|
298 self.init_config = init_config |
298 self._repo = None |
299 self._repo = None |
299 # pure consistency check |
300 # pure consistency check |
300 assert self.system_source['db-driver'] == self.DRIVER |
301 assert self.system_source['db-driver'] == self.DRIVER |
301 |
302 |
302 def _ensure_test_backup_db_dir(self): |
303 def _ensure_test_backup_db_dir(self): |
380 (turn the current repo object "on" if there is one or recreate one) |
381 (turn the current repo object "on" if there is one or recreate one) |
381 if startup is True, server startup server hooks will be called if needed |
382 if startup is True, server startup server hooks will be called if needed |
382 """ |
383 """ |
383 if self._repo is None: |
384 if self._repo is None: |
384 self._repo = self._new_repo(self.config) |
385 self._repo = self._new_repo(self.config) |
|
386 # config has now been bootstrapped, call init_config if specified |
|
387 if self.init_config is not None: |
|
388 self.init_config(self.config) |
385 repo = self._repo |
389 repo = self._repo |
386 repo.turn_repo_on() |
390 repo.turn_repo_on() |
387 if startup and not repo._has_started: |
391 if startup and not repo._has_started: |
388 repo.hm.call_hooks('server_startup', repo=repo) |
392 repo.hm.call_hooks('server_startup', repo=repo) |
389 repo._has_started = True |
393 repo._has_started = True |
534 @classmethod |
538 @classmethod |
535 def killall(cls): |
539 def killall(cls): |
536 for datadir in cls.__CTL: |
540 for datadir in cls.__CTL: |
537 subprocess.call(['pg_ctl', 'stop', '-D', datadir, '-m', 'fast']) |
541 subprocess.call(['pg_ctl', 'stop', '-D', datadir, '-m', 'fast']) |
538 |
542 |
539 def __init__(self, config): |
543 def __init__(self, *args, **kwargs): |
540 super(PostgresTestDataBaseHandler, self).__init__(config) |
544 super(PostgresTestDataBaseHandler, self).__init__(*args, **kwargs) |
541 datadir = join(self.config.apphome, 'pgdb') |
545 datadir = join(self.config.apphome, 'pgdb') |
542 if not exists(datadir): |
546 if not exists(datadir): |
543 subprocess.check_call(['initdb', '-D', datadir, '-E', 'utf-8', '--locale=C']) |
547 subprocess.check_call(['initdb', '-D', datadir, '-E', 'utf-8', '--locale=C']) |
544 port = self.system_source['db-port'] |
548 port = self.system_source['db-port'] |
545 directory = self.system_source['db-host'] |
549 directory = self.system_source['db-host'] |
603 self.helper.create_language(templcursor, extlang) |
607 self.helper.create_language(templcursor, extlang) |
604 cnx.commit() |
608 cnx.commit() |
605 finally: |
609 finally: |
606 templcursor.close() |
610 templcursor.close() |
607 cnx.close() |
611 cnx.close() |
608 init_repository(self.config, interactive=False) |
612 init_repository(self.config, interactive=False, |
|
613 init_config=self.init_config) |
609 except BaseException: |
614 except BaseException: |
610 if self.dbcnx is not None: |
615 if self.dbcnx is not None: |
611 self.dbcnx.rollback() |
616 self.dbcnx.rollback() |
612 sys.stderr.write('building %s failed\n' % self.dbname) |
617 sys.stderr.write('building %s failed\n' % self.dbname) |
613 #self._drop(self.dbname) |
618 #self._drop(self.dbname) |
679 |
684 |
680 def init_test_database(self): |
685 def init_test_database(self): |
681 """initialize a fresh sqlserver databse used for testing purpose""" |
686 """initialize a fresh sqlserver databse used for testing purpose""" |
682 if self.config.init_repository: |
687 if self.config.init_repository: |
683 from cubicweb.server import init_repository |
688 from cubicweb.server import init_repository |
684 init_repository(self.config, interactive=False, drop=True) |
689 init_repository(self.config, interactive=False, drop=True, |
|
690 init_config=self.init_config) |
685 |
691 |
686 ### sqlite test database handling ############################################## |
692 ### sqlite test database handling ############################################## |
687 |
693 |
688 class SQLiteTestDataBaseHandler(TestDataBaseHandler): |
694 class SQLiteTestDataBaseHandler(TestDataBaseHandler): |
689 DRIVER = 'sqlite' |
695 DRIVER = 'sqlite' |
756 def init_test_database(self): |
762 def init_test_database(self): |
757 """initialize a fresh sqlite databse used for testing purpose""" |
763 """initialize a fresh sqlite databse used for testing purpose""" |
758 # initialize the database |
764 # initialize the database |
759 from cubicweb.server import init_repository |
765 from cubicweb.server import init_repository |
760 self._cleanup_database(self.absolute_dbfile()) |
766 self._cleanup_database(self.absolute_dbfile()) |
761 init_repository(self.config, interactive=False) |
767 init_repository(self.config, interactive=False, |
|
768 init_config=self.init_config) |
762 |
769 |
763 import atexit |
770 import atexit |
764 atexit.register(SQLiteTestDataBaseHandler._cleanup_all_tmpdb) |
771 atexit.register(SQLiteTestDataBaseHandler._cleanup_all_tmpdb) |
765 atexit.register(PostgresTestDataBaseHandler.killall) |
772 atexit.register(PostgresTestDataBaseHandler.killall) |
766 |
773 |
850 |
857 |
851 |
858 |
852 # XXX a class method on Test ? |
859 # XXX a class method on Test ? |
853 |
860 |
854 _CONFIG = None |
861 _CONFIG = None |
855 def get_test_db_handler(config): |
862 def get_test_db_handler(config, init_config=None): |
856 global _CONFIG |
863 global _CONFIG |
857 if _CONFIG is not None and config is not _CONFIG: |
864 if _CONFIG is not None and config is not _CONFIG: |
858 from logilab.common.modutils import cleanup_sys_modules |
865 from logilab.common.modutils import cleanup_sys_modules |
859 # cleanup all dynamically loaded modules and everything in the instance |
866 # cleanup all dynamically loaded modules and everything in the instance |
860 # directory |
867 # directory |
871 return handler |
878 return handler |
872 driver = config.system_source_config['db-driver'] |
879 driver = config.system_source_config['db-driver'] |
873 key = (driver, config) |
880 key = (driver, config) |
874 handlerkls = HANDLERS.get(driver, None) |
881 handlerkls = HANDLERS.get(driver, None) |
875 if handlerkls is not None: |
882 if handlerkls is not None: |
876 handler = handlerkls(config) |
883 handler = handlerkls(config, init_config) |
877 if config.skip_db_create_and_restore: |
884 if config.skip_db_create_and_restore: |
878 handler = NoCreateDropDatabaseHandler(handler) |
885 handler = NoCreateDropDatabaseHandler(handler) |
879 HCACHE.set(config, handler) |
886 HCACHE.set(config, handler) |
880 return handler |
887 return handler |
881 else: |
888 else: |