[devtools] change the way we start/stop postgresql
authorJulien Cristau <julien.cristau@logilab.fr>
Mon, 18 May 2015 16:44:49 +0200
changeset 10439 45e18b4a7466
parent 10438 f27a489994e8
child 10440 eecb7bbb6795
[devtools] change the way we start/stop postgresql Instead of having the test db handler start a cluster on demand, use the test module's setUp/tearDown callbacks to do it. This allows to have one data directory (and thus cluster) per test module, allowing different test modules to run in parallel, each using its own database cluster whose path is based on the test module.
devtools/__init__.py
server/test/unittest_postgres.py
--- a/devtools/__init__.py	Mon May 18 16:36:26 2015 +0200
+++ b/devtools/__init__.py	Mon May 18 16:44:49 2015 +0200
@@ -527,8 +527,10 @@
 
 ### postgres test database handling ############################################
 
-def _startpgcluster(datadir):
-    """Start a postgresql cluster using datadir and a random port number"""
+def startpgcluster(pyfile):
+    """Start a postgresql cluster next to pyfile and using a random port number"""
+    datadir = join(os.path.dirname(pyfile), 'data',
+                   'pgdb-%s' % os.path.splitext(os.path.basename(pyfile))[0])
     if not exists(datadir):
         try:
             subprocess.check_call(['initdb', '-D', datadir, '-E', 'utf-8', '--locale=C'])
@@ -559,8 +561,10 @@
         raise
 
 
-def _stoppgcluster(datadir):
-    """Kill the postgresql cluster running in datadir"""
+def stoppgcluster(pyfile):
+    """Kill the postgresql cluster running next to pyfile"""
+    datadir = join(os.path.dirname(pyfile), 'data',
+                   'pgdb-%s' % os.path.splitext(os.path.basename(pyfile))[0])
     subprocess.call(['pg_ctl', 'stop', '-D', datadir, '-m', 'fast'])
 
 
@@ -573,18 +577,8 @@
 
     __CTL = set()
 
-    @classmethod
-    def killall(cls):
-        for datadir in cls.__CTL:
-            _stoppgcluster(datadir)
-
     def __init__(self, *args, **kwargs):
         super(PostgresTestDataBaseHandler, self).__init__(*args, **kwargs)
-        datadir = realpath(join(self.config.apphome, 'pgdb'))
-        if datadir in self.__CTL:
-            return
-        _startpgcluster(datadir)
-        self.__CTL.add(datadir)
         if 'global-db-name' not in self.system_source:
             self.system_source['global-db-name'] = self.system_source['db-name']
             self.system_source['db-name'] = self.system_source['db-name'] + str(os.getpid())
@@ -810,7 +804,6 @@
 
 import atexit
 atexit.register(SQLiteTestDataBaseHandler._cleanup_all_tmpdb)
-atexit.register(PostgresTestDataBaseHandler.killall)
 
 
 def install_sqlite_patch(querier):
--- a/server/test/unittest_postgres.py	Mon May 18 16:36:26 2015 +0200
+++ b/server/test/unittest_postgres.py	Mon May 18 16:44:49 2015 +0200
@@ -1,4 +1,4 @@
-# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of CubicWeb.
@@ -16,18 +16,28 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
 
+import os.path as osp
 from datetime import datetime
 from threading import Thread
 
 from logilab.common.testlib import SkipTest
 
-from cubicweb.devtools import PostgresApptestConfiguration
+from cubicweb.devtools import PostgresApptestConfiguration, startpgcluster, stoppgcluster
 from cubicweb.devtools.testlib import CubicWebTC
 from cubicweb.predicates import is_instance
 from cubicweb.entities.adapters import IFTIndexableAdapter
 
 from unittest_querier import FixedOffset
 
+
+def setUpModule():
+    startpgcluster(__file__)
+
+
+def tearDownModule():
+    stoppgcluster(__file__)
+
+
 class PostgresFTITC(CubicWebTC):
     configcls = PostgresApptestConfiguration