author | Denis Laxalde <denis.laxalde@logilab.fr> |
Tue, 21 Jun 2016 16:21:07 +0200 | |
changeset 11299 | f94f5e81fb9d |
parent 11093 | f28a5c19d4ed |
permissions | -rw-r--r-- |
11030
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
11029
diff
changeset
|
1 |
# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
6315
8ca3ee849bee
[test] fix broken tests and minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6281
diff
changeset
|
18 |
"""Test tools for cubicweb""" |
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10588
diff
changeset
|
19 |
from __future__ import print_function |
0 | 20 |
|
21 |
__docformat__ = "restructuredtext en" |
|
22 |
||
23 |
import os |
|
5754
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
24 |
import sys |
9886
1b26289217a3
[devtools] improve error message when postgresql tools are missing
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
9864
diff
changeset
|
25 |
import errno |
0 | 26 |
import logging |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
27 |
import shutil |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
28 |
import glob |
9498
a76ac18f09c4
[devtools] make PostgresTestDataBaseHandler start its own postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents:
9460
diff
changeset
|
29 |
import subprocess |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
30 |
import warnings |
9337
70f6b183085a
[devtools] write db config through a temporary file
Julien Cristau <julien.cristau@logilab.fr>
parents:
9235
diff
changeset
|
31 |
import tempfile |
9498
a76ac18f09c4
[devtools] make PostgresTestDataBaseHandler start its own postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents:
9460
diff
changeset
|
32 |
import getpass |
11035 | 33 |
from hashlib import sha1 # pylint: disable=E0611 |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
937
diff
changeset
|
34 |
from datetime import timedelta |
11035 | 35 |
from os.path import abspath, join, exists, split, isabs, isdir |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
36 |
from functools import partial |
0 | 37 |
|
10622
3cc6154b94a3
[py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10602
diff
changeset
|
38 |
from six import text_type |
10602
4845012cfc8e
[py3k] import 'pickle' using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10595
diff
changeset
|
39 |
from six.moves import cPickle as pickle |
4845012cfc8e
[py3k] import 'pickle' using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10595
diff
changeset
|
40 |
|
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4436
diff
changeset
|
41 |
from logilab.common.date import strptime |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
42 |
from logilab.common.decorators import cached, clear_cache |
7301
93e96700e0c0
[configuration] exit with proper message when sources file is unreadable (you usually started cw while logged with a wrong user). Closes #1631238
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7240
diff
changeset
|
43 |
|
8930
6a02be304486
remove unused import
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8694
diff
changeset
|
44 |
from cubicweb import ExecutionError, BadConnectionId |
6a02be304486
remove unused import
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8694
diff
changeset
|
45 |
from cubicweb import schema, cwconfig |
0 | 46 |
from cubicweb.server.serverconfig import ServerConfiguration |
9252
01bca75ee8bd
[devtools,etwist] rename TwistedConfiguration to WebConfigurationBase (follows #2919310)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9235
diff
changeset
|
47 |
from cubicweb.etwist.twconfig import WebConfigurationBase |
0 | 48 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
49 |
cwconfig.CubicWebConfiguration.cls_adjust_sys_path() |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
50 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
51 |
# db auto-population configuration ############################################# |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
52 |
|
6377
3bb415310d4f
[schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6315
diff
changeset
|
53 |
SYSTEM_ENTITIES = (schema.SCHEMA_TYPES |
3bb415310d4f
[schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6315
diff
changeset
|
54 |
| schema.INTERNAL_TYPES |
3bb415310d4f
[schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6315
diff
changeset
|
55 |
| schema.WORKFLOW_TYPES |
3bb415310d4f
[schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6315
diff
changeset
|
56 |
| set(('CWGroup', 'CWUser',)) |
3bb415310d4f
[schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6315
diff
changeset
|
57 |
) |
3bb415310d4f
[schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6315
diff
changeset
|
58 |
SYSTEM_RELATIONS = (schema.META_RTYPES |
3bb415310d4f
[schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6315
diff
changeset
|
59 |
| schema.WORKFLOW_RTYPES |
3bb415310d4f
[schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6315
diff
changeset
|
60 |
| schema.WORKFLOW_DEF_RTYPES |
3bb415310d4f
[schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6315
diff
changeset
|
61 |
| schema.SYSTEM_RTYPES |
3bb415310d4f
[schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6315
diff
changeset
|
62 |
| schema.SCHEMA_TYPES |
3bb415310d4f
[schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6315
diff
changeset
|
63 |
| set(('primary_email', # deducted from other relations |
3bb415310d4f
[schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6315
diff
changeset
|
64 |
)) |
3bb415310d4f
[schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6315
diff
changeset
|
65 |
) |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
66 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
67 |
# content validation configuration ############################################# |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
68 |
|
0 | 69 |
# validators are used to validate (XML, DTD, whatever) view's content |
70 |
# validators availables are : |
|
71 |
# 'dtd' : validates XML + declared DTD |
|
72 |
# 'xml' : guarantees XML is well formed |
|
73 |
# None : do not try to validate anything |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
74 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
75 |
# {'vid': validator} |
0 | 76 |
VIEW_VALIDATORS = {} |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
77 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
78 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
79 |
# cubicweb test configuration ################################################## |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
80 |
|
0 | 81 |
BASE_URL = 'http://testing.fr/cubicweb/' |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
82 |
|
0 | 83 |
DEFAULT_SOURCES = {'system': {'adapter' : 'native', |
84 |
'db-encoding' : 'UTF-8', #'ISO-8859-1', |
|
85 |
'db-user' : u'admin', |
|
86 |
'db-password' : 'gingkow', |
|
87 |
'db-name' : 'tmpdb', |
|
88 |
'db-driver' : 'sqlite', |
|
89 |
'db-host' : None, |
|
90 |
}, |
|
91 |
'admin' : {'login': u'admin', |
|
92 |
'password': u'gingkow', |
|
93 |
}, |
|
94 |
} |
|
9498
a76ac18f09c4
[devtools] make PostgresTestDataBaseHandler start its own postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents:
9460
diff
changeset
|
95 |
DEFAULT_PSQL_SOURCES = DEFAULT_SOURCES.copy() |
a76ac18f09c4
[devtools] make PostgresTestDataBaseHandler start its own postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents:
9460
diff
changeset
|
96 |
DEFAULT_PSQL_SOURCES['system'] = DEFAULT_SOURCES['system'].copy() |
a76ac18f09c4
[devtools] make PostgresTestDataBaseHandler start its own postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents:
9460
diff
changeset
|
97 |
DEFAULT_PSQL_SOURCES['system']['db-driver'] = 'postgres' |
10622
3cc6154b94a3
[py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10602
diff
changeset
|
98 |
DEFAULT_PSQL_SOURCES['system']['db-user'] = text_type(getpass.getuser()) |
9498
a76ac18f09c4
[devtools] make PostgresTestDataBaseHandler start its own postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents:
9460
diff
changeset
|
99 |
DEFAULT_PSQL_SOURCES['system']['db-password'] = None |
0 | 100 |
|
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
101 |
def turn_repo_off(repo): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
102 |
""" Idea: this is less costly than a full re-creation of the repo object. |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
103 |
off: |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
104 |
* session are closed, |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7380
diff
changeset
|
105 |
* cnxsets are closed |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
106 |
* system source is shutdown |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
107 |
""" |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
108 |
if not repo._needs_refresh: |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
109 |
for sessionid in list(repo._sessions): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
110 |
warnings.warn('%s Open session found while turning repository off' |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
111 |
%sessionid, RuntimeWarning) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
112 |
try: |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
113 |
repo.close(sessionid) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
114 |
except BadConnectionId: #this is strange ? thread issue ? |
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10588
diff
changeset
|
115 |
print('XXX unknown session', sessionid) |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7380
diff
changeset
|
116 |
for cnxset in repo.cnxsets: |
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7380
diff
changeset
|
117 |
cnxset.close(True) |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
118 |
repo.system_source.shutdown() |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
119 |
repo._needs_refresh = True |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
120 |
repo._has_started = False |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
121 |
|
9564
e2d5b0712974
[test] when the config change, we should cleanup sys.path and sys.modules. Closes #3634164
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9337
diff
changeset
|
122 |
|
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
123 |
def turn_repo_on(repo): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
124 |
"""Idea: this is less costly than a full re-creation of the repo object. |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
125 |
on: |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7380
diff
changeset
|
126 |
* cnxsets are connected |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
127 |
* cache are cleared |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
128 |
""" |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
129 |
if repo._needs_refresh: |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7380
diff
changeset
|
130 |
for cnxset in repo.cnxsets: |
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7380
diff
changeset
|
131 |
cnxset.reconnect() |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
132 |
repo._type_source_cache = {} |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
133 |
repo._extid_cache = {} |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
134 |
repo.querier._rql_cache = {} |
9456
a79e88aad555
[multi-sources-removal] Kill repo.sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9402
diff
changeset
|
135 |
repo.system_source.reset_caches() |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
136 |
repo._needs_refresh = False |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
137 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
138 |
|
0 | 139 |
class TestServerConfiguration(ServerConfiguration): |
140 |
mode = 'test' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1978
diff
changeset
|
141 |
read_instance_schema = False |
0 | 142 |
init_repository = True |
7239
078cfa8a5187
fix server/test/unittest_checkintegrity (failing since 70538ea2532d)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7220
diff
changeset
|
143 |
skip_db_create_and_restore = False |
9498
a76ac18f09c4
[devtools] make PostgresTestDataBaseHandler start its own postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents:
9460
diff
changeset
|
144 |
default_sources = DEFAULT_SOURCES |
a76ac18f09c4
[devtools] make PostgresTestDataBaseHandler start its own postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents:
9460
diff
changeset
|
145 |
|
7065
6a6ea9966931
[testlib] simplify code by using a class attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7056
diff
changeset
|
146 |
def __init__(self, appid='data', apphome=None, log_threshold=logging.CRITICAL+10): |
6585
9af22f2c0c4c
[test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6441
diff
changeset
|
147 |
# must be set before calling parent __init__ |
9af22f2c0c4c
[test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6441
diff
changeset
|
148 |
if apphome is None: |
6593
0fd8792c9c8a
[testlib] self.appid not yet defined
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6585
diff
changeset
|
149 |
if exists(appid): |
0fd8792c9c8a
[testlib] self.appid not yet defined
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6585
diff
changeset
|
150 |
apphome = abspath(appid) |
6585
9af22f2c0c4c
[test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6441
diff
changeset
|
151 |
else: # cube test |
9af22f2c0c4c
[test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6441
diff
changeset
|
152 |
apphome = abspath('..') |
9af22f2c0c4c
[test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6441
diff
changeset
|
153 |
self._apphome = apphome |
11017
3dfed980071c
[devtools] Use super() in TestServerConfiguration.__init__()
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10960
diff
changeset
|
154 |
super(TestServerConfiguration, self).__init__(appid) |
0 | 155 |
self.init_log(log_threshold, force=True) |
156 |
# need this, usually triggered by cubicweb-ctl |
|
157 |
self.load_cwctl_plugins() |
|
158 |
||
7071
db7608cb32bc
[devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7065
diff
changeset
|
159 |
# By default anonymous login are allow but some test need to deny of to |
db7608cb32bc
[devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7065
diff
changeset
|
160 |
# change the default user. Set it to None to prevent anonymous login. |
db7608cb32bc
[devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7065
diff
changeset
|
161 |
anonymous_credential = ('anon', 'anon') |
db7608cb32bc
[devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7065
diff
changeset
|
162 |
|
db7608cb32bc
[devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7065
diff
changeset
|
163 |
def anonymous_user(self): |
db7608cb32bc
[devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7065
diff
changeset
|
164 |
if not self.anonymous_credential: |
db7608cb32bc
[devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7065
diff
changeset
|
165 |
return None, None |
db7608cb32bc
[devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7065
diff
changeset
|
166 |
return self.anonymous_credential |
db7608cb32bc
[devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7065
diff
changeset
|
167 |
|
10888
fbbb028d0a75
[devtools] Fix sqlite unicode warning
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10819
diff
changeset
|
168 |
def set_anonymous_allowed(self, allowed, anonuser=u'anon'): |
7071
db7608cb32bc
[devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7065
diff
changeset
|
169 |
if allowed: |
db7608cb32bc
[devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7065
diff
changeset
|
170 |
self.anonymous_credential = (anonuser, anonuser) |
db7608cb32bc
[devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7065
diff
changeset
|
171 |
else: |
db7608cb32bc
[devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7065
diff
changeset
|
172 |
self.anonymous_credential = None |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
173 |
|
0 | 174 |
@property |
175 |
def apphome(self): |
|
6585
9af22f2c0c4c
[test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6441
diff
changeset
|
176 |
return self._apphome |
0 | 177 |
appdatahome = apphome |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
178 |
|
10474
1dcc52f5e340
[cwctl] allow overriding config settings from the command line (closes #5557656)
David Douard <david.douard@logilab.fr>
parents:
10472
diff
changeset
|
179 |
def load_configuration(self, **kw): |
1dcc52f5e340
[cwctl] allow overriding config settings from the command line (closes #5557656)
David Douard <david.douard@logilab.fr>
parents:
10472
diff
changeset
|
180 |
super(TestServerConfiguration, self).load_configuration(**kw) |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4761
diff
changeset
|
181 |
# no undo support in tests |
8306
4da49700b06a
[config, undo] Fix undo-support option migration
Anthony Truchet <anthony.truchet@logilab.fr>
parents:
8265
diff
changeset
|
182 |
self.global_set_option('undo-enabled', 'n') |
3435
84036ad2c82d
add anon user for TestServerConfiguration, no only AppTestConfiguration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
183 |
|
0 | 184 |
def main_config_file(self): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1978
diff
changeset
|
185 |
"""return instance's control configuration file""" |
0 | 186 |
return join(self.apphome, '%s.conf' % self.name) |
187 |
||
188 |
def bootstrap_cubes(self): |
|
189 |
try: |
|
190 |
super(TestServerConfiguration, self).bootstrap_cubes() |
|
191 |
except IOError: |
|
192 |
# no cubes |
|
193 |
self.init_cubes( () ) |
|
194 |
||
195 |
sourcefile = None |
|
196 |
def sources_file(self): |
|
197 |
"""define in subclasses self.sourcefile if necessary""" |
|
198 |
if self.sourcefile: |
|
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10588
diff
changeset
|
199 |
print('Reading sources from', self.sourcefile) |
0 | 200 |
sourcefile = self.sourcefile |
201 |
if not isabs(sourcefile): |
|
202 |
sourcefile = join(self.apphome, sourcefile) |
|
203 |
else: |
|
204 |
sourcefile = super(TestServerConfiguration, self).sources_file() |
|
205 |
return sourcefile |
|
206 |
||
9460
a2a0bc984863
[config] cleanup/refactor server sources file values handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9456
diff
changeset
|
207 |
def read_sources_file(self): |
0 | 208 |
"""By default, we run tests with the sqlite DB backend. One may use its |
209 |
own configuration by just creating a 'sources' file in the test |
|
9498
a76ac18f09c4
[devtools] make PostgresTestDataBaseHandler start its own postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents:
9460
diff
changeset
|
210 |
directory from which tests are launched or by specifying an alternative |
0 | 211 |
sources file using self.sourcefile. |
212 |
""" |
|
7301
93e96700e0c0
[configuration] exit with proper message when sources file is unreadable (you usually started cw while logged with a wrong user). Closes #1631238
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7240
diff
changeset
|
213 |
try: |
9460
a2a0bc984863
[config] cleanup/refactor server sources file values handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9456
diff
changeset
|
214 |
sources = super(TestServerConfiguration, self).read_sources_file() |
7301
93e96700e0c0
[configuration] exit with proper message when sources file is unreadable (you usually started cw while logged with a wrong user). Closes #1631238
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7240
diff
changeset
|
215 |
except ExecutionError: |
93e96700e0c0
[configuration] exit with proper message when sources file is unreadable (you usually started cw while logged with a wrong user). Closes #1631238
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7240
diff
changeset
|
216 |
sources = {} |
0 | 217 |
if not sources: |
9498
a76ac18f09c4
[devtools] make PostgresTestDataBaseHandler start its own postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents:
9460
diff
changeset
|
218 |
sources = self.default_sources |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6425
diff
changeset
|
219 |
if 'admin' not in sources: |
9498
a76ac18f09c4
[devtools] make PostgresTestDataBaseHandler start its own postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents:
9460
diff
changeset
|
220 |
sources['admin'] = self.default_sources['admin'] |
0 | 221 |
return sources |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
222 |
|
6441
adbce42ff1b5
TestServerConfig needs default_base_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6424
diff
changeset
|
223 |
# web config methods needed here for cases when we use this config as a web |
adbce42ff1b5
TestServerConfig needs default_base_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6424
diff
changeset
|
224 |
# config |
adbce42ff1b5
TestServerConfig needs default_base_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6424
diff
changeset
|
225 |
|
adbce42ff1b5
TestServerConfig needs default_base_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6424
diff
changeset
|
226 |
def default_base_url(self): |
adbce42ff1b5
TestServerConfig needs default_base_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6424
diff
changeset
|
227 |
return BASE_URL |
adbce42ff1b5
TestServerConfig needs default_base_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6424
diff
changeset
|
228 |
|
0 | 229 |
|
9252
01bca75ee8bd
[devtools,etwist] rename TwistedConfiguration to WebConfigurationBase (follows #2919310)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9235
diff
changeset
|
230 |
class BaseApptestConfiguration(TestServerConfiguration, WebConfigurationBase): |
6843
b70a26ca271c
[devtools] set configuration name to all-in-one so test/data/all-in-one.conf file will be loaded as one may expected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6750
diff
changeset
|
231 |
name = 'all-in-one' # so it search for all-in-one.conf, not repository.conf |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
232 |
options = cwconfig.merge_options(TestServerConfiguration.options |
9252
01bca75ee8bd
[devtools,etwist] rename TwistedConfiguration to WebConfigurationBase (follows #2919310)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9235
diff
changeset
|
233 |
+ WebConfigurationBase.options) |
01bca75ee8bd
[devtools,etwist] rename TwistedConfiguration to WebConfigurationBase (follows #2919310)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9235
diff
changeset
|
234 |
cubicweb_appobject_path = TestServerConfiguration.cubicweb_appobject_path | WebConfigurationBase.cubicweb_appobject_path |
01bca75ee8bd
[devtools,etwist] rename TwistedConfiguration to WebConfigurationBase (follows #2919310)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9235
diff
changeset
|
235 |
cube_appobject_path = TestServerConfiguration.cube_appobject_path | WebConfigurationBase.cube_appobject_path |
0 | 236 |
|
237 |
def available_languages(self, *args): |
|
6749
48f468f33704
[config, i18n] Create default translation mechanism for all supported languages.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
6729
diff
changeset
|
238 |
return self.cw_languages() |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
239 |
|
0 | 240 |
|
6424
f443a2b8a5c7
[devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6315
diff
changeset
|
241 |
# XXX merge with BaseApptestConfiguration ? |
0 | 242 |
class ApptestConfiguration(BaseApptestConfiguration): |
7214
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
243 |
# `skip_db_create_and_restore` controls wether or not the test database |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
244 |
# should be created / backuped / restored. If set to True, those |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
245 |
# steps are completely skipped, the database is used as is and is |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
246 |
# considered initialized |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
247 |
skip_db_create_and_restore = False |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
248 |
|
6585
9af22f2c0c4c
[test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6441
diff
changeset
|
249 |
def __init__(self, appid, apphome=None, |
10231
fdcc500002ef
[devtools] don't hide warnings and errors during tests
Julien Cristau <julien.cristau@logilab.fr>
parents:
10175
diff
changeset
|
250 |
log_threshold=logging.WARNING, sourcefile=None): |
6585
9af22f2c0c4c
[test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6441
diff
changeset
|
251 |
BaseApptestConfiguration.__init__(self, appid, apphome, |
9af22f2c0c4c
[test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6441
diff
changeset
|
252 |
log_threshold=log_threshold) |
0 | 253 |
self.init_repository = sourcefile is None |
254 |
self.sourcefile = sourcefile |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
255 |
|
6424
f443a2b8a5c7
[devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6315
diff
changeset
|
256 |
|
9498
a76ac18f09c4
[devtools] make PostgresTestDataBaseHandler start its own postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents:
9460
diff
changeset
|
257 |
class PostgresApptestConfiguration(ApptestConfiguration): |
a76ac18f09c4
[devtools] make PostgresTestDataBaseHandler start its own postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents:
9460
diff
changeset
|
258 |
default_sources = DEFAULT_PSQL_SOURCES |
a76ac18f09c4
[devtools] make PostgresTestDataBaseHandler start its own postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents:
9460
diff
changeset
|
259 |
|
a76ac18f09c4
[devtools] make PostgresTestDataBaseHandler start its own postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents:
9460
diff
changeset
|
260 |
|
6164
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
261 |
class RealDatabaseConfiguration(ApptestConfiguration): |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
262 |
"""configuration class for tests to run on a real database. |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
263 |
|
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
264 |
The intialization is done by specifying a source file path. |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
265 |
|
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
266 |
Important note: init_test_database / reset_test_database steps are |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
267 |
skipped. It's thus up to the test developer to implement setUp/tearDown |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
268 |
accordingly. |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
269 |
|
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
270 |
Example usage:: |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
271 |
|
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
272 |
class MyTests(CubicWebTC): |
7856
51a3fb272bf3
[skeleton] fix new class configuration in realdbtest
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
7815
diff
changeset
|
273 |
_config = RealDatabaseConfiguration('myapp', |
51a3fb272bf3
[skeleton] fix new class configuration in realdbtest
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
7815
diff
changeset
|
274 |
sourcefile='/path/to/sources') |
51a3fb272bf3
[skeleton] fix new class configuration in realdbtest
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
7815
diff
changeset
|
275 |
|
6164
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
276 |
def test_something(self): |
9864
f60a80592224
[webtests] finish to give all self.view(....) a req=req parameter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9790
diff
changeset
|
277 |
with self.admin_access.web_request() as req: |
f60a80592224
[webtests] finish to give all self.view(....) a req=req parameter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9790
diff
changeset
|
278 |
rset = req.execute('Any X WHERE X is CWUser') |
f60a80592224
[webtests] finish to give all self.view(....) a req=req parameter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9790
diff
changeset
|
279 |
self.view('foaf', rset, req=req) |
6164
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
280 |
|
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
281 |
""" |
7214
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
282 |
skip_db_create_and_restore = True |
6164
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
283 |
read_instance_schema = True # read schema from database |
0 | 284 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
285 |
# test database handling ####################################################### |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
286 |
|
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
287 |
DEFAULT_EMPTY_DB_ID = '__default_empty_db__' |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
288 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
289 |
class TestDataBaseHandler(object): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
290 |
DRIVER = None |
10064
91a8f355c082
[devtools] Avoid db_cache collisions and mis-loading
Christophe de Vienne <christophe@unlish.com>
parents:
10063
diff
changeset
|
291 |
|
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
292 |
db_cache = {} |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
293 |
explored_glob = set() |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
294 |
|
9790
0872ac2a1db0
[testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9789
diff
changeset
|
295 |
def __init__(self, config, init_config=None): |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
296 |
self.config = config |
9790
0872ac2a1db0
[testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9789
diff
changeset
|
297 |
self.init_config = init_config |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
298 |
self._repo = None |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
299 |
# pure consistency check |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
300 |
assert self.system_source['db-driver'] == self.DRIVER |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
301 |
|
10472
7dd84b69c7d4
[devtools] avoid warning about source['global-db-name']
Julien Cristau <julien.cristau@logilab.fr>
parents:
10440
diff
changeset
|
302 |
# some handlers want to store info here, avoid a warning |
7dd84b69c7d4
[devtools] avoid warning about source['global-db-name']
Julien Cristau <julien.cristau@logilab.fr>
parents:
10440
diff
changeset
|
303 |
from cubicweb.server.sources.native import NativeSQLSource |
7dd84b69c7d4
[devtools] avoid warning about source['global-db-name']
Julien Cristau <julien.cristau@logilab.fr>
parents:
10440
diff
changeset
|
304 |
NativeSQLSource.options += ( |
7dd84b69c7d4
[devtools] avoid warning about source['global-db-name']
Julien Cristau <julien.cristau@logilab.fr>
parents:
10440
diff
changeset
|
305 |
('global-db-name', |
7dd84b69c7d4
[devtools] avoid warning about source['global-db-name']
Julien Cristau <julien.cristau@logilab.fr>
parents:
10440
diff
changeset
|
306 |
{'type': 'string', 'help': 'for internal use only' |
7dd84b69c7d4
[devtools] avoid warning about source['global-db-name']
Julien Cristau <julien.cristau@logilab.fr>
parents:
10440
diff
changeset
|
307 |
}), |
7dd84b69c7d4
[devtools] avoid warning about source['global-db-name']
Julien Cristau <julien.cristau@logilab.fr>
parents:
10440
diff
changeset
|
308 |
) |
7dd84b69c7d4
[devtools] avoid warning about source['global-db-name']
Julien Cristau <julien.cristau@logilab.fr>
parents:
10440
diff
changeset
|
309 |
|
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
310 |
def _ensure_test_backup_db_dir(self): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
311 |
"""Return path of directory for database backup. |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
312 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
313 |
The function create it if necessary""" |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
314 |
backupdir = join(self.config.apphome, 'database') |
9235
3eb8d65824f5
[devtools] fix race when creating backupdir
Julien Cristau <julien.cristau@logilab.fr>
parents:
8930
diff
changeset
|
315 |
try: |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
316 |
os.makedirs(backupdir) |
9235
3eb8d65824f5
[devtools] fix race when creating backupdir
Julien Cristau <julien.cristau@logilab.fr>
parents:
8930
diff
changeset
|
317 |
except: |
3eb8d65824f5
[devtools] fix race when creating backupdir
Julien Cristau <julien.cristau@logilab.fr>
parents:
8930
diff
changeset
|
318 |
if not isdir(backupdir): |
3eb8d65824f5
[devtools] fix race when creating backupdir
Julien Cristau <julien.cristau@logilab.fr>
parents:
8930
diff
changeset
|
319 |
raise |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
320 |
return backupdir |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
321 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
322 |
def config_path(self, db_id): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
323 |
"""Path for config backup of a given database id""" |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
324 |
return self.absolute_backup_file(db_id, 'config') |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
325 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
326 |
def absolute_backup_file(self, db_id, suffix): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
327 |
"""Path for config backup of a given database id""" |
7217
4b860903d8c7
[testlib] fix bug in backup file name generation: it may erroneously replace '-' by '_' in database **parent directories**. This should fix random error when test are run under apycot.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7214
diff
changeset
|
328 |
# in case db name is an absolute path, we don't want to replace anything |
4b860903d8c7
[testlib] fix bug in backup file name generation: it may erroneously replace '-' by '_' in database **parent directories**. This should fix random error when test are run under apycot.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7214
diff
changeset
|
329 |
# in parent directories |
4b860903d8c7
[testlib] fix bug in backup file name generation: it may erroneously replace '-' by '_' in database **parent directories**. This should fix random error when test are run under apycot.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7214
diff
changeset
|
330 |
directory, basename = split(self.dbname) |
4b860903d8c7
[testlib] fix bug in backup file name generation: it may erroneously replace '-' by '_' in database **parent directories**. This should fix random error when test are run under apycot.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7214
diff
changeset
|
331 |
dbname = basename.replace('-', '_') |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
332 |
assert '.' not in db_id |
7217
4b860903d8c7
[testlib] fix bug in backup file name generation: it may erroneously replace '-' by '_' in database **parent directories**. This should fix random error when test are run under apycot.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7214
diff
changeset
|
333 |
filename = join(directory, '%s-%s.%s' % (dbname, db_id, suffix)) |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
334 |
return join(self._ensure_test_backup_db_dir(), filename) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
335 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
336 |
def db_cache_key(self, db_id, dbname=None): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
337 |
"""Build a database cache key for a db_id with the current config |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
338 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
339 |
This key is meant to be used in the cls.db_cache mapping""" |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
340 |
if dbname is None: |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
341 |
dbname = self.dbname |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
342 |
dbname = os.path.basename(dbname) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
343 |
dbname = dbname.replace('-', '_') |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
344 |
return (self.config.apphome, dbname, db_id) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
345 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
346 |
def backup_database(self, db_id): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
347 |
"""Store the content of the current database as <db_id> |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
348 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
349 |
The config used are also stored.""" |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
350 |
backup_data = self._backup_database(db_id) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
351 |
config_path = self.config_path(db_id) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
352 |
# XXX we dump a dict of the config |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
353 |
# This is an experimental to help config dependant setup (like BFSS) to |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
354 |
# be propertly restored |
9337
70f6b183085a
[devtools] write db config through a temporary file
Julien Cristau <julien.cristau@logilab.fr>
parents:
9235
diff
changeset
|
355 |
with tempfile.NamedTemporaryFile(dir=os.path.dirname(config_path), delete=False) as conf_file: |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
356 |
conf_file.write(pickle.dumps(dict(self.config))) |
9337
70f6b183085a
[devtools] write db config through a temporary file
Julien Cristau <julien.cristau@logilab.fr>
parents:
9235
diff
changeset
|
357 |
os.rename(conf_file.name, config_path) |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
358 |
self.db_cache[self.db_cache_key(db_id)] = (backup_data, config_path) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
359 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
360 |
def _backup_database(self, db_id): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
361 |
"""Actual backup the current database. |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
362 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
363 |
return a value to be stored in db_cache to allow restoration""" |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
364 |
raise NotImplementedError() |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
365 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
366 |
def restore_database(self, db_id): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
367 |
"""Restore a database. |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
368 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
369 |
takes as argument value stored in db_cache by self._backup_database""" |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
370 |
# XXX set a clearer error message ??? |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
371 |
backup_coordinates, config_path = self.db_cache[self.db_cache_key(db_id)] |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
372 |
# reload the config used to create the database. |
10819
5b5864483c4d
[devtools] fix ResourceWarning: unclosed file tmpdb-__default_empty_db__.config
Julien Cristau <julien.cristau@logilab.fr>
parents:
10796
diff
changeset
|
373 |
with open(config_path, 'rb') as f: |
5b5864483c4d
[devtools] fix ResourceWarning: unclosed file tmpdb-__default_empty_db__.config
Julien Cristau <julien.cristau@logilab.fr>
parents:
10796
diff
changeset
|
374 |
config = pickle.load(f) |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
375 |
# shutdown repo before changing database content |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
376 |
if self._repo is not None: |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
377 |
self._repo.turn_repo_off() |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
378 |
self._restore_database(backup_coordinates, config) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
379 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
380 |
def _restore_database(self, backup_coordinates, config): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
381 |
"""Actual restore of the current database. |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
382 |
|
8543
b7c9443d8625
minor cleanups/typos
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8306
diff
changeset
|
383 |
Use the value stored in db_cache as input """ |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
384 |
raise NotImplementedError() |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
385 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
386 |
def get_repo(self, startup=False): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
387 |
""" return Repository object on the current database. |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
388 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
389 |
(turn the current repo object "on" if there is one or recreate one) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
390 |
if startup is True, server startup server hooks will be called if needed |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
391 |
""" |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
392 |
if self._repo is None: |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
393 |
self._repo = self._new_repo(self.config) |
9790
0872ac2a1db0
[testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9789
diff
changeset
|
394 |
# config has now been bootstrapped, call init_config if specified |
0872ac2a1db0
[testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9789
diff
changeset
|
395 |
if self.init_config is not None: |
0872ac2a1db0
[testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9789
diff
changeset
|
396 |
self.init_config(self.config) |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
397 |
repo = self._repo |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
398 |
repo.turn_repo_on() |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
399 |
if startup and not repo._has_started: |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
400 |
repo.hm.call_hooks('server_startup', repo=repo) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
401 |
repo._has_started = True |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
402 |
return repo |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
403 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
404 |
def _new_repo(self, config): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
405 |
"""Factory method to create a new Repository Instance""" |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
406 |
config._cubes = None |
10568
3914388b2d0f
[repoapi, cwconfig] give a convenience method to get a repository from a config
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10510
diff
changeset
|
407 |
repo = config.repository() |
11093
f28a5c19d4ed
[devtools] fix config.repository signature
Julien Cristau <julien.cristau@logilab.fr>
parents:
11035
diff
changeset
|
408 |
config.repository = lambda vreg=None: repo |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
409 |
# extending Repository class |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
410 |
repo._has_started = False |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
411 |
repo._needs_refresh = False |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
412 |
repo.turn_repo_on = partial(turn_repo_on, repo) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
413 |
repo.turn_repo_off = partial(turn_repo_off, repo) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
414 |
return repo |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
415 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
416 |
def get_cnx(self): |
7222
fcb8932082a5
[testlib] refactor create_user and grant_permission to make them usable from pre_setup_database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7217
diff
changeset
|
417 |
"""return Connection object on the current repository""" |
9576
5d4f662f5e31
[devtools] make get_repo_and_cnx return a repoapi ClientConnection
Julien Cristau <julien.cristau@logilab.fr>
parents:
9519
diff
changeset
|
418 |
from cubicweb.repoapi import connect |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
419 |
repo = self.get_repo() |
9460
a2a0bc984863
[config] cleanup/refactor server sources file values handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9456
diff
changeset
|
420 |
sources = self.config.read_sources_file() |
10622
3cc6154b94a3
[py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10602
diff
changeset
|
421 |
login = text_type(sources['admin']['login']) |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
422 |
password = sources['admin']['password'] or 'xxx' |
9576
5d4f662f5e31
[devtools] make get_repo_and_cnx return a repoapi ClientConnection
Julien Cristau <julien.cristau@logilab.fr>
parents:
9519
diff
changeset
|
423 |
cnx = connect(repo, login, password=password) |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
424 |
return cnx |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
425 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
426 |
def get_repo_and_cnx(self, db_id=DEFAULT_EMPTY_DB_ID): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
427 |
"""Reset database with the current db_id and return (repo, cnx) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
428 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
429 |
A database *MUST* have been build with the current <db_id> prior to |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
430 |
call this method. See the ``build_db_cache`` method. The returned |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
431 |
repository have it's startup hooks called and the connection is |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
432 |
establised as admin.""" |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
433 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
434 |
self.restore_database(db_id) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
435 |
repo = self.get_repo(startup=True) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
436 |
cnx = self.get_cnx() |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
437 |
return repo, cnx |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
438 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
439 |
@property |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
440 |
def system_source(self): |
9460
a2a0bc984863
[config] cleanup/refactor server sources file values handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9456
diff
changeset
|
441 |
return self.config.system_source_config |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
442 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
443 |
@property |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
444 |
def dbname(self): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
445 |
return self.system_source['db-name'] |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
446 |
|
7220
eb0f5f46138f
[testlib] fix typos / NameErrors in devtools/__init__.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7217
diff
changeset
|
447 |
def init_test_database(self): |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
448 |
"""actual initialisation of the database""" |
7220
eb0f5f46138f
[testlib] fix typos / NameErrors in devtools/__init__.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7217
diff
changeset
|
449 |
raise ValueError('no initialization function for driver %r' % self.DRIVER) |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
450 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
451 |
def has_cache(self, db_id): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
452 |
"""Check if a given database id exist in cb cache for the current config""" |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
453 |
cache_glob = self.absolute_backup_file('*', '*') |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
454 |
if cache_glob not in self.explored_glob: |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
455 |
self.discover_cached_db() |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
456 |
return self.db_cache_key(db_id) in self.db_cache |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
457 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
458 |
def discover_cached_db(self): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
459 |
"""Search available db_if for the current config""" |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
460 |
cache_glob = self.absolute_backup_file('*', '*') |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
461 |
directory = os.path.dirname(cache_glob) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
462 |
entries={} |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
463 |
candidates = glob.glob(cache_glob) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
464 |
for filepath in candidates: |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
465 |
data = os.path.basename(filepath) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
466 |
# database backup are in the forms are <dbname>-<db_id>.<backtype> |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
467 |
dbname, data = data.split('-', 1) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
468 |
db_id, filetype = data.split('.', 1) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
469 |
entries.setdefault((dbname, db_id), {})[filetype] = filepath |
10662
10942ed172de
[py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10651
diff
changeset
|
470 |
for (dbname, db_id), entry in entries.items(): |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
471 |
# apply necessary transformation from the driver |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
472 |
value = self.process_cache_entry(directory, dbname, db_id, entry) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
473 |
assert 'config' in entry |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
474 |
if value is not None: # None value means "not handled by this driver |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
475 |
# XXX Ignored value are shadowed to other Handler if cache are common. |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
476 |
key = self.db_cache_key(db_id, dbname=dbname) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
477 |
self.db_cache[key] = value, entry['config'] |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
478 |
self.explored_glob.add(cache_glob) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
479 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
480 |
def process_cache_entry(self, directory, dbname, db_id, entry): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
481 |
"""Transforms potential cache entry to proper backup coordinate |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
482 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
483 |
entry argument is a "filetype" -> "filepath" mapping |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
484 |
Return None if an entry should be ignored.""" |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
485 |
return None |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
486 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
487 |
def build_db_cache(self, test_db_id=DEFAULT_EMPTY_DB_ID, pre_setup_func=None): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
488 |
"""Build Database cache for ``test_db_id`` if a cache doesn't exist |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
489 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
490 |
if ``test_db_id is DEFAULT_EMPTY_DB_ID`` self.init_test_database is |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
491 |
called. otherwise, DEFAULT_EMPTY_DB_ID is build/restored and |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
492 |
``pre_setup_func`` to setup the database. |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
493 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
494 |
This function backup any database it build""" |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
495 |
if self.has_cache(test_db_id): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
496 |
return #test_db_id, 'already in cache' |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
497 |
if test_db_id is DEFAULT_EMPTY_DB_ID: |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
498 |
self.init_test_database() |
6164
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
499 |
else: |
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10588
diff
changeset
|
500 |
print('Building %s for database %s' % (test_db_id, self.dbname)) |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
501 |
self.build_db_cache(DEFAULT_EMPTY_DB_ID) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
502 |
self.restore_database(DEFAULT_EMPTY_DB_ID) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
503 |
repo = self.get_repo(startup=True) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
504 |
cnx = self.get_cnx() |
9788
47d356ee14d2
[devtools] pre_setup_database should take a connection object
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9679
diff
changeset
|
505 |
with cnx: |
10354
635cfac73d28
[repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10331
diff
changeset
|
506 |
pre_setup_func(cnx, self.config) |
9788
47d356ee14d2
[devtools] pre_setup_database should take a connection object
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9679
diff
changeset
|
507 |
cnx.commit() |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
508 |
self.backup_database(test_db_id) |
0 | 509 |
|
7214
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
510 |
|
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
511 |
class NoCreateDropDatabaseHandler(TestDataBaseHandler): |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
512 |
"""This handler is used if config.skip_db_create_and_restore is True |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
513 |
|
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
514 |
This is typically the case with RealDBConfig. In that case, |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
515 |
we explicitely want to skip init / backup / restore phases. |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
516 |
|
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
517 |
This handler redefines the three corresponding methods and delegates |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
518 |
to original handler for any other method / attribute |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
519 |
""" |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
520 |
|
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
521 |
def __init__(self, base_handler): |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
522 |
self.base_handler = base_handler |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
523 |
|
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
524 |
# override init / backup / restore methods |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
525 |
def init_test_database(self): |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
526 |
pass |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
527 |
|
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
528 |
def backup_database(self, db_id): |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
529 |
pass |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
530 |
|
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
531 |
def restore_database(self, db_id): |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
532 |
pass |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
533 |
|
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
534 |
# delegate to original handler in all other cases |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
535 |
def __getattr__(self, attrname): |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
536 |
return getattr(self.base_handler, attrname) |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
537 |
|
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
538 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
539 |
### postgres test database handling ############################################ |
0 | 540 |
|
10439
45e18b4a7466
[devtools] change the way we start/stop postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents:
10438
diff
changeset
|
541 |
def startpgcluster(pyfile): |
10440
eecb7bbb6795
[devtools] Put pg sockets in /tmp
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10439
diff
changeset
|
542 |
"""Start a postgresql cluster next to pyfile""" |
11029
c9d12d1d3081
[testlib] put postgres test database in test/data/database directory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
11017
diff
changeset
|
543 |
datadir = join(os.path.dirname(pyfile), 'data', 'database', |
10439
45e18b4a7466
[devtools] change the way we start/stop postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents:
10438
diff
changeset
|
544 |
'pgdb-%s' % os.path.splitext(os.path.basename(pyfile))[0]) |
10437
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
545 |
if not exists(datadir): |
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
546 |
try: |
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
547 |
subprocess.check_call(['initdb', '-D', datadir, '-E', 'utf-8', '--locale=C']) |
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
548 |
|
10588
fdaa0e4b7eaf
[py3k] except as
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10568
diff
changeset
|
549 |
except OSError as err: |
10437
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
550 |
if err.errno == errno.ENOENT: |
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
551 |
raise OSError('"initdb" could not be found. ' |
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
552 |
'You should add the postgresql bin folder to your PATH ' |
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
553 |
'(/usr/lib/postgresql/9.1/bin for example).') |
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
554 |
raise |
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
555 |
datadir = os.path.abspath(datadir) |
10440
eecb7bbb6795
[devtools] Put pg sockets in /tmp
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10439
diff
changeset
|
556 |
pgport = '5432' |
10437
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
557 |
env = os.environ.copy() |
10440
eecb7bbb6795
[devtools] Put pg sockets in /tmp
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10439
diff
changeset
|
558 |
sockdir = tempfile.mkdtemp(prefix='cwpg') |
eecb7bbb6795
[devtools] Put pg sockets in /tmp
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10439
diff
changeset
|
559 |
DEFAULT_PSQL_SOURCES['system']['db-host'] = sockdir |
eecb7bbb6795
[devtools] Put pg sockets in /tmp
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10439
diff
changeset
|
560 |
DEFAULT_PSQL_SOURCES['system']['db-port'] = pgport |
eecb7bbb6795
[devtools] Put pg sockets in /tmp
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10439
diff
changeset
|
561 |
options = '-h "" -k %s -p %s' % (sockdir, pgport) |
10437
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
562 |
options += ' -c fsync=off -c full_page_writes=off' |
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
563 |
options += ' -c synchronous_commit=off' |
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
564 |
try: |
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
565 |
subprocess.check_call(['pg_ctl', 'start', '-w', '-D', datadir, |
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
566 |
'-o', options], |
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
567 |
env=env) |
10588
fdaa0e4b7eaf
[py3k] except as
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10568
diff
changeset
|
568 |
except OSError as err: |
10796
26a36c2a5fbd
[devtools] delete postgresql socket directory after killing the server
Julien Cristau <julien.cristau@logilab.fr>
parents:
10671
diff
changeset
|
569 |
try: |
26a36c2a5fbd
[devtools] delete postgresql socket directory after killing the server
Julien Cristau <julien.cristau@logilab.fr>
parents:
10671
diff
changeset
|
570 |
os.rmdir(sockdir) |
26a36c2a5fbd
[devtools] delete postgresql socket directory after killing the server
Julien Cristau <julien.cristau@logilab.fr>
parents:
10671
diff
changeset
|
571 |
except OSError: |
26a36c2a5fbd
[devtools] delete postgresql socket directory after killing the server
Julien Cristau <julien.cristau@logilab.fr>
parents:
10671
diff
changeset
|
572 |
pass |
10437
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
573 |
if err.errno == errno.ENOENT: |
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
574 |
raise OSError('"pg_ctl" could not be found. ' |
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
575 |
'You should add the postgresql bin folder to your PATH ' |
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
576 |
'(/usr/lib/postgresql/9.1/bin for example).') |
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
577 |
raise |
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
578 |
|
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
579 |
|
10439
45e18b4a7466
[devtools] change the way we start/stop postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents:
10438
diff
changeset
|
580 |
def stoppgcluster(pyfile): |
45e18b4a7466
[devtools] change the way we start/stop postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents:
10438
diff
changeset
|
581 |
"""Kill the postgresql cluster running next to pyfile""" |
11029
c9d12d1d3081
[testlib] put postgres test database in test/data/database directory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
11017
diff
changeset
|
582 |
datadir = join(os.path.dirname(pyfile), 'data', 'database', |
10439
45e18b4a7466
[devtools] change the way we start/stop postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents:
10438
diff
changeset
|
583 |
'pgdb-%s' % os.path.splitext(os.path.basename(pyfile))[0]) |
10437
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
584 |
subprocess.call(['pg_ctl', 'stop', '-D', datadir, '-m', 'fast']) |
10796
26a36c2a5fbd
[devtools] delete postgresql socket directory after killing the server
Julien Cristau <julien.cristau@logilab.fr>
parents:
10671
diff
changeset
|
585 |
try: |
26a36c2a5fbd
[devtools] delete postgresql socket directory after killing the server
Julien Cristau <julien.cristau@logilab.fr>
parents:
10671
diff
changeset
|
586 |
os.rmdir(DEFAULT_PSQL_SOURCES['system']['db-host']) |
26a36c2a5fbd
[devtools] delete postgresql socket directory after killing the server
Julien Cristau <julien.cristau@logilab.fr>
parents:
10671
diff
changeset
|
587 |
except OSError: |
26a36c2a5fbd
[devtools] delete postgresql socket directory after killing the server
Julien Cristau <julien.cristau@logilab.fr>
parents:
10671
diff
changeset
|
588 |
pass |
10437
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
589 |
|
9cddd711419c
[devtools] extract functions to start/stop a postgresql cluster
Julien Cristau <julien.cristau@logilab.fr>
parents:
10354
diff
changeset
|
590 |
|
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
591 |
class PostgresTestDataBaseHandler(TestDataBaseHandler): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
592 |
DRIVER = 'postgres' |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
593 |
|
10064
91a8f355c082
[devtools] Avoid db_cache collisions and mis-loading
Christophe de Vienne <christophe@unlish.com>
parents:
10063
diff
changeset
|
594 |
# Separate db_cache for PG databases, to avoid collisions with sqlite dbs |
91a8f355c082
[devtools] Avoid db_cache collisions and mis-loading
Christophe de Vienne <christophe@unlish.com>
parents:
10063
diff
changeset
|
595 |
db_cache = {} |
91a8f355c082
[devtools] Avoid db_cache collisions and mis-loading
Christophe de Vienne <christophe@unlish.com>
parents:
10063
diff
changeset
|
596 |
explored_glob = set() |
91a8f355c082
[devtools] Avoid db_cache collisions and mis-loading
Christophe de Vienne <christophe@unlish.com>
parents:
10063
diff
changeset
|
597 |
|
9498
a76ac18f09c4
[devtools] make PostgresTestDataBaseHandler start its own postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents:
9460
diff
changeset
|
598 |
__CTL = set() |
a76ac18f09c4
[devtools] make PostgresTestDataBaseHandler start its own postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents:
9460
diff
changeset
|
599 |
|
9790
0872ac2a1db0
[testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9789
diff
changeset
|
600 |
def __init__(self, *args, **kwargs): |
0872ac2a1db0
[testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9789
diff
changeset
|
601 |
super(PostgresTestDataBaseHandler, self).__init__(*args, **kwargs) |
10438
f27a489994e8
[devtools] make test db name for postgresql process-specific
Julien Cristau <julien.cristau@logilab.fr>
parents:
10437
diff
changeset
|
602 |
if 'global-db-name' not in self.system_source: |
f27a489994e8
[devtools] make test db name for postgresql process-specific
Julien Cristau <julien.cristau@logilab.fr>
parents:
10437
diff
changeset
|
603 |
self.system_source['global-db-name'] = self.system_source['db-name'] |
f27a489994e8
[devtools] make test db name for postgresql process-specific
Julien Cristau <julien.cristau@logilab.fr>
parents:
10437
diff
changeset
|
604 |
self.system_source['db-name'] = self.system_source['db-name'] + str(os.getpid()) |
9498
a76ac18f09c4
[devtools] make PostgresTestDataBaseHandler start its own postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents:
9460
diff
changeset
|
605 |
|
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
606 |
@property |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
607 |
@cached |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
608 |
def helper(self): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
609 |
from logilab.database import get_db_helper |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
610 |
return get_db_helper('postgres') |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
611 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
612 |
@property |
10438
f27a489994e8
[devtools] make test db name for postgresql process-specific
Julien Cristau <julien.cristau@logilab.fr>
parents:
10437
diff
changeset
|
613 |
def dbname(self): |
f27a489994e8
[devtools] make test db name for postgresql process-specific
Julien Cristau <julien.cristau@logilab.fr>
parents:
10437
diff
changeset
|
614 |
return self.system_source['global-db-name'] |
f27a489994e8
[devtools] make test db name for postgresql process-specific
Julien Cristau <julien.cristau@logilab.fr>
parents:
10437
diff
changeset
|
615 |
|
f27a489994e8
[devtools] make test db name for postgresql process-specific
Julien Cristau <julien.cristau@logilab.fr>
parents:
10437
diff
changeset
|
616 |
@property |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
617 |
def dbcnx(self): |
7520
b7456b454334
[devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7506
diff
changeset
|
618 |
try: |
b7456b454334
[devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7506
diff
changeset
|
619 |
return self._cnx |
b7456b454334
[devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7506
diff
changeset
|
620 |
except AttributeError: |
b7456b454334
[devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7506
diff
changeset
|
621 |
from cubicweb.server.serverctl import _db_sys_cnx |
b7456b454334
[devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7506
diff
changeset
|
622 |
try: |
b7456b454334
[devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7506
diff
changeset
|
623 |
self._cnx = _db_sys_cnx( |
b7456b454334
[devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7506
diff
changeset
|
624 |
self.system_source, 'CREATE DATABASE and / or USER', |
b7456b454334
[devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7506
diff
changeset
|
625 |
interactive=False) |
b7456b454334
[devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7506
diff
changeset
|
626 |
return self._cnx |
b7456b454334
[devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7506
diff
changeset
|
627 |
except Exception: |
b7456b454334
[devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7506
diff
changeset
|
628 |
self._cnx = None |
b7456b454334
[devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7506
diff
changeset
|
629 |
raise |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
630 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
631 |
@property |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
632 |
@cached |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
633 |
def cursor(self): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
634 |
return self.dbcnx.cursor() |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
635 |
|
7214
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
636 |
def process_cache_entry(self, directory, dbname, db_id, entry): |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
637 |
backup_name = self._backup_name(db_id) |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
638 |
if backup_name in self.helper.list_databases(self.cursor): |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
639 |
return backup_name |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
640 |
return None |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
641 |
|
10644
c43e5dc41f8b
[devtools] add has_cache for postgres (closes #5739624)
Julien Cristau <julien.cristau@logilab.fr>
parents:
10510
diff
changeset
|
642 |
def has_cache(self, db_id): |
c43e5dc41f8b
[devtools] add has_cache for postgres (closes #5739624)
Julien Cristau <julien.cristau@logilab.fr>
parents:
10510
diff
changeset
|
643 |
backup_name = self._backup_name(db_id) |
c43e5dc41f8b
[devtools] add has_cache for postgres (closes #5739624)
Julien Cristau <julien.cristau@logilab.fr>
parents:
10510
diff
changeset
|
644 |
return (super(PostgresTestDataBaseHandler, self).has_cache(db_id) |
c43e5dc41f8b
[devtools] add has_cache for postgres (closes #5739624)
Julien Cristau <julien.cristau@logilab.fr>
parents:
10510
diff
changeset
|
645 |
and backup_name in self.helper.list_databases(self.cursor)) |
c43e5dc41f8b
[devtools] add has_cache for postgres (closes #5739624)
Julien Cristau <julien.cristau@logilab.fr>
parents:
10510
diff
changeset
|
646 |
|
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
647 |
def init_test_database(self): |
7214
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
648 |
"""initialize a fresh postgresql database used for testing purpose""" |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
649 |
from cubicweb.server import init_repository |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
650 |
from cubicweb.server.serverctl import system_source_cnx, createdb |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
651 |
# connect on the dbms system base to create our base |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
652 |
try: |
10438
f27a489994e8
[devtools] make test db name for postgresql process-specific
Julien Cristau <julien.cristau@logilab.fr>
parents:
10437
diff
changeset
|
653 |
self._drop(self.system_source['db-name']) |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
654 |
createdb(self.helper, self.system_source, self.dbcnx, self.cursor) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
655 |
self.dbcnx.commit() |
7112
bb27cc300040
[db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7095
diff
changeset
|
656 |
cnx = system_source_cnx(self.system_source, special_privs='LANGUAGE C', |
bb27cc300040
[db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7095
diff
changeset
|
657 |
interactive=False) |
5754
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
658 |
templcursor = cnx.cursor() |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
659 |
try: |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
660 |
# XXX factorize with db-create code |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
661 |
self.helper.init_fti_extensions(templcursor) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
662 |
# install plpythonu/plpgsql language if not installed by the cube |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
663 |
langs = sys.platform == 'win32' and ('plpgsql',) or ('plpythonu', 'plpgsql') |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
664 |
for extlang in langs: |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
665 |
self.helper.create_language(templcursor, extlang) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
666 |
cnx.commit() |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
667 |
finally: |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
668 |
templcursor.close() |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
669 |
cnx.close() |
9790
0872ac2a1db0
[testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9789
diff
changeset
|
670 |
init_repository(self.config, interactive=False, |
0872ac2a1db0
[testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9789
diff
changeset
|
671 |
init_config=self.init_config) |
7815
2a164a9cf81c
[exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7765
diff
changeset
|
672 |
except BaseException: |
7520
b7456b454334
[devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7506
diff
changeset
|
673 |
if self.dbcnx is not None: |
b7456b454334
[devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7506
diff
changeset
|
674 |
self.dbcnx.rollback() |
7896
4c954e1e73ef
[lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7879
diff
changeset
|
675 |
sys.stderr.write('building %s failed\n' % self.dbname) |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
676 |
#self._drop(self.dbname) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
677 |
raise |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
678 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
679 |
def helper_clear_cache(self): |
7520
b7456b454334
[devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7506
diff
changeset
|
680 |
if self.dbcnx is not None: |
b7456b454334
[devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7506
diff
changeset
|
681 |
self.dbcnx.commit() |
b7456b454334
[devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7506
diff
changeset
|
682 |
self.dbcnx.close() |
b7456b454334
[devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7506
diff
changeset
|
683 |
del self._cnx |
b7456b454334
[devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7506
diff
changeset
|
684 |
clear_cache(self, 'cursor') |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
685 |
clear_cache(self, 'helper') |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
686 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
687 |
def __del__(self): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
688 |
self.helper_clear_cache() |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
689 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
690 |
@property |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
691 |
def _config_id(self): |
10671
e08102f45237
[py3k] bytes required for hashlib.{md5,sha1}()
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10662
diff
changeset
|
692 |
return sha1(self.config.apphome.encode('utf-8')).hexdigest()[:10] |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
693 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
694 |
def _backup_name(self, db_id): # merge me with parent |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
695 |
backup_name = '_'.join(('cache', self._config_id, self.dbname, db_id)) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
696 |
return backup_name.lower() |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
697 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
698 |
def _drop(self, db_name): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
699 |
if db_name in self.helper.list_databases(self.cursor): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
700 |
self.cursor.execute('DROP DATABASE %s' % db_name) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
701 |
self.dbcnx.commit() |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
702 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
703 |
def _backup_database(self, db_id): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
704 |
"""Actual backup the current database. |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
705 |
|
7506
c8cda2aca92e
[testlib, realdb] we should close connections prior to create a new db using current db as template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7240
diff
changeset
|
706 |
return a value to be stored in db_cache to allow restoration |
c8cda2aca92e
[testlib, realdb] we should close connections prior to create a new db using current db as template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7240
diff
changeset
|
707 |
""" |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
708 |
from cubicweb.server.serverctl import createdb |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
709 |
orig_name = self.system_source['db-name'] |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
710 |
try: |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
711 |
backup_name = self._backup_name(db_id) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
712 |
self._drop(backup_name) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
713 |
self.system_source['db-name'] = backup_name |
10063
335fa90d5ef4
[devtools][pg] Remove a wrong assertion in _backup_database
Christophe de Vienne <christophe@unlish.com>
parents:
10038
diff
changeset
|
714 |
if self._repo: |
335fa90d5ef4
[devtools][pg] Remove a wrong assertion in _backup_database
Christophe de Vienne <christophe@unlish.com>
parents:
10038
diff
changeset
|
715 |
self._repo.turn_repo_off() |
335fa90d5ef4
[devtools][pg] Remove a wrong assertion in _backup_database
Christophe de Vienne <christophe@unlish.com>
parents:
10038
diff
changeset
|
716 |
try: |
335fa90d5ef4
[devtools][pg] Remove a wrong assertion in _backup_database
Christophe de Vienne <christophe@unlish.com>
parents:
10038
diff
changeset
|
717 |
createdb(self.helper, self.system_source, self.dbcnx, self.cursor, template=orig_name) |
335fa90d5ef4
[devtools][pg] Remove a wrong assertion in _backup_database
Christophe de Vienne <christophe@unlish.com>
parents:
10038
diff
changeset
|
718 |
self.dbcnx.commit() |
335fa90d5ef4
[devtools][pg] Remove a wrong assertion in _backup_database
Christophe de Vienne <christophe@unlish.com>
parents:
10038
diff
changeset
|
719 |
finally: |
335fa90d5ef4
[devtools][pg] Remove a wrong assertion in _backup_database
Christophe de Vienne <christophe@unlish.com>
parents:
10038
diff
changeset
|
720 |
if self._repo: |
335fa90d5ef4
[devtools][pg] Remove a wrong assertion in _backup_database
Christophe de Vienne <christophe@unlish.com>
parents:
10038
diff
changeset
|
721 |
self._repo.turn_repo_on() |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
722 |
return backup_name |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
723 |
finally: |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
724 |
self.system_source['db-name'] = orig_name |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
725 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
726 |
def _restore_database(self, backup_coordinates, config): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
727 |
from cubicweb.server.serverctl import createdb |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
728 |
"""Actual restore of the current database. |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
729 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
730 |
Use the value tostored in db_cache as input """ |
10438
f27a489994e8
[devtools] make test db name for postgresql process-specific
Julien Cristau <julien.cristau@logilab.fr>
parents:
10437
diff
changeset
|
731 |
self._drop(self.system_source['db-name']) |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
732 |
createdb(self.helper, self.system_source, self.dbcnx, self.cursor, |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
733 |
template=backup_coordinates) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
734 |
self.dbcnx.commit() |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
735 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
736 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
737 |
|
5754
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
738 |
### sqlserver2005 test database handling ####################################### |
5166
1e96d01728c4
enable live test against sqlserver 2005 database
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5043
diff
changeset
|
739 |
|
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
740 |
class SQLServerTestDataBaseHandler(TestDataBaseHandler): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
741 |
DRIVER = 'sqlserver' |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
742 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
743 |
# XXX complete me |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
744 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
745 |
def init_test_database(self): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
746 |
"""initialize a fresh sqlserver databse used for testing purpose""" |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
747 |
if self.config.init_repository: |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
748 |
from cubicweb.server import init_repository |
9790
0872ac2a1db0
[testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9789
diff
changeset
|
749 |
init_repository(self.config, interactive=False, drop=True, |
0872ac2a1db0
[testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9789
diff
changeset
|
750 |
init_config=self.init_config) |
5212
a545eb9add6f
[testlib] fix dummy bug in reset_test_database(): s/if/elif
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5210
diff
changeset
|
751 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
752 |
### sqlite test database handling ############################################## |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
753 |
|
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
754 |
class SQLiteTestDataBaseHandler(TestDataBaseHandler): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
755 |
DRIVER = 'sqlite' |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
756 |
|
7722
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
757 |
__TMPDB = set() |
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
758 |
|
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
759 |
@classmethod |
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
760 |
def _cleanup_all_tmpdb(cls): |
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
761 |
for dbpath in cls.__TMPDB: |
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
762 |
cls._cleanup_database(dbpath) |
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
763 |
|
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
764 |
|
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
765 |
|
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
766 |
def __init__(self, *args, **kwargs): |
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
767 |
super(SQLiteTestDataBaseHandler, self).__init__(*args, **kwargs) |
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
768 |
# use a dedicated base for each process. |
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
769 |
if 'global-db-name' not in self.system_source: |
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
770 |
self.system_source['global-db-name'] = self.system_source['db-name'] |
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
771 |
process_db = self.system_source['db-name'] + str(os.getpid()) |
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
772 |
self.system_source['db-name'] = process_db |
7765
6f6253e44bdd
devtools: Fix cleanup of temporary database (closes #1908656)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7722
diff
changeset
|
773 |
process_db = self.absolute_dbfile() # update db-name to absolute path |
6f6253e44bdd
devtools: Fix cleanup of temporary database (closes #1908656)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7722
diff
changeset
|
774 |
self.__TMPDB.add(process_db) |
7722
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
775 |
|
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
776 |
@staticmethod |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
777 |
def _cleanup_database(dbfile): |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
778 |
try: |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
779 |
os.remove(dbfile) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
780 |
os.remove('%s-journal' % dbfile) |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
781 |
except OSError: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
782 |
pass |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
783 |
|
7722
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
784 |
@property |
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
785 |
def dbname(self): |
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
786 |
return self.system_source['global-db-name'] |
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
787 |
|
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
788 |
def absolute_dbfile(self): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
789 |
"""absolute path of current database file""" |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
790 |
dbfile = join(self._ensure_test_backup_db_dir(), |
9460
a2a0bc984863
[config] cleanup/refactor server sources file values handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9456
diff
changeset
|
791 |
self.system_source['db-name']) |
a2a0bc984863
[config] cleanup/refactor server sources file values handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9456
diff
changeset
|
792 |
self.system_source['db-name'] = dbfile |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
793 |
return dbfile |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
794 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
795 |
def process_cache_entry(self, directory, dbname, db_id, entry): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
796 |
return entry.get('sqlite') |
0 | 797 |
|
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
798 |
def _backup_database(self, db_id=DEFAULT_EMPTY_DB_ID): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
799 |
# XXX remove database file if it exists ??? |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
800 |
dbfile = self.absolute_dbfile() |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
801 |
backup_file = self.absolute_backup_file(db_id, 'sqlite') |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
802 |
shutil.copy(dbfile, backup_file) |
10510
51321946da37
Spelling fixes in comments and docstrings
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10474
diff
changeset
|
803 |
# Useful to debug WHO writes a database |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
804 |
# backup_stack = self.absolute_backup_file(db_id, '.stack') |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
805 |
#with open(backup_stack, 'w') as backup_stack_file: |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
806 |
# import traceback |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
807 |
# traceback.print_stack(file=backup_stack_file) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
808 |
return backup_file |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
809 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
810 |
def _restore_database(self, backup_coordinates, _config): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
811 |
# remove database file if it exists ? |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
812 |
dbfile = self.absolute_dbfile() |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
813 |
self._cleanup_database(dbfile) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
814 |
shutil.copy(backup_coordinates, dbfile) |
8543
b7c9443d8625
minor cleanups/typos
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8306
diff
changeset
|
815 |
self.get_repo() |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
816 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
817 |
def init_test_database(self): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
818 |
"""initialize a fresh sqlite databse used for testing purpose""" |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
819 |
# initialize the database |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
820 |
from cubicweb.server import init_repository |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
821 |
self._cleanup_database(self.absolute_dbfile()) |
9790
0872ac2a1db0
[testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9789
diff
changeset
|
822 |
init_repository(self.config, interactive=False, |
0872ac2a1db0
[testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9789
diff
changeset
|
823 |
init_config=self.init_config) |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
824 |
|
7722
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
825 |
import atexit |
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
826 |
atexit.register(SQLiteTestDataBaseHandler._cleanup_all_tmpdb) |
fb231d62adda
[devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7536
diff
changeset
|
827 |
|
0 | 828 |
|
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
829 |
HANDLERS = {} |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
830 |
|
7090
d9e6e79e023a
[testlib] require explicite overwriting of TestDataBaseHandler.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7078
diff
changeset
|
831 |
def register_handler(handlerkls, overwrite=False): |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
832 |
assert handlerkls is not None |
7090
d9e6e79e023a
[testlib] require explicite overwriting of TestDataBaseHandler.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7078
diff
changeset
|
833 |
if overwrite or handlerkls.DRIVER not in HANDLERS: |
d9e6e79e023a
[testlib] require explicite overwriting of TestDataBaseHandler.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7078
diff
changeset
|
834 |
HANDLERS[handlerkls.DRIVER] = handlerkls |
d9e6e79e023a
[testlib] require explicite overwriting of TestDataBaseHandler.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7078
diff
changeset
|
835 |
else: |
7093
dccc22c60473
fix stupid typo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7090
diff
changeset
|
836 |
msg = "%s: Handler already exists use overwrite if it's intended\n"\ |
7090
d9e6e79e023a
[testlib] require explicite overwriting of TestDataBaseHandler.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7078
diff
changeset
|
837 |
"(existing handler class is %r)" |
d9e6e79e023a
[testlib] require explicite overwriting of TestDataBaseHandler.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7078
diff
changeset
|
838 |
raise ValueError(msg % (handlerkls.DRIVER, HANDLERS[handlerkls.DRIVER])) |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
839 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
840 |
register_handler(PostgresTestDataBaseHandler) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
841 |
register_handler(SQLiteTestDataBaseHandler) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
842 |
register_handler(SQLServerTestDataBaseHandler) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
843 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
844 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
845 |
class HCache(object): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
846 |
"""Handler cache object: store database handler for a given configuration. |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
847 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
848 |
We only keep one repo in cache to prevent too much objects to stay alive |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
849 |
(database handler holds a reference to a repository). As at the moment a new |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
850 |
handler is created for each TestCase class and all test methods are executed |
10903
da30851f9706
spelling: *aly → *ally
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10888
diff
changeset
|
851 |
sequentially whithin this class, there should not have more cache miss that |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
852 |
if we had a wider cache as once a Handler stop being used it won't be used |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
853 |
again. |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
854 |
""" |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
855 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
856 |
def __init__(self): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
857 |
self.config = None |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
858 |
self.handler = None |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
859 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
860 |
def get(self, config): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
861 |
if config is self.config: |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
862 |
return self.handler |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
863 |
else: |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
864 |
return None |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
865 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
866 |
def set(self, config, handler): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
867 |
self.config = config |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
868 |
self.handler = handler |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
869 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
870 |
HCACHE = HCache() |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
871 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
872 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
873 |
# XXX a class method on Test ? |
9564
e2d5b0712974
[test] when the config change, we should cleanup sys.path and sys.modules. Closes #3634164
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9337
diff
changeset
|
874 |
|
e2d5b0712974
[test] when the config change, we should cleanup sys.path and sys.modules. Closes #3634164
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9337
diff
changeset
|
875 |
_CONFIG = None |
9790
0872ac2a1db0
[testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9789
diff
changeset
|
876 |
def get_test_db_handler(config, init_config=None): |
9564
e2d5b0712974
[test] when the config change, we should cleanup sys.path and sys.modules. Closes #3634164
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9337
diff
changeset
|
877 |
global _CONFIG |
e2d5b0712974
[test] when the config change, we should cleanup sys.path and sys.modules. Closes #3634164
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9337
diff
changeset
|
878 |
if _CONFIG is not None and config is not _CONFIG: |
e2d5b0712974
[test] when the config change, we should cleanup sys.path and sys.modules. Closes #3634164
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9337
diff
changeset
|
879 |
from logilab.common.modutils import cleanup_sys_modules |
e2d5b0712974
[test] when the config change, we should cleanup sys.path and sys.modules. Closes #3634164
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9337
diff
changeset
|
880 |
# cleanup all dynamically loaded modules and everything in the instance |
e2d5b0712974
[test] when the config change, we should cleanup sys.path and sys.modules. Closes #3634164
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9337
diff
changeset
|
881 |
# directory |
e2d5b0712974
[test] when the config change, we should cleanup sys.path and sys.modules. Closes #3634164
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9337
diff
changeset
|
882 |
apphome = _CONFIG.apphome |
e2d5b0712974
[test] when the config change, we should cleanup sys.path and sys.modules. Closes #3634164
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9337
diff
changeset
|
883 |
if apphome: # may be unset in tests |
e2d5b0712974
[test] when the config change, we should cleanup sys.path and sys.modules. Closes #3634164
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9337
diff
changeset
|
884 |
cleanup_sys_modules([apphome]) |
e2d5b0712974
[test] when the config change, we should cleanup sys.path and sys.modules. Closes #3634164
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9337
diff
changeset
|
885 |
# also cleanup sys.path |
e2d5b0712974
[test] when the config change, we should cleanup sys.path and sys.modules. Closes #3634164
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9337
diff
changeset
|
886 |
if apphome in sys.path: |
e2d5b0712974
[test] when the config change, we should cleanup sys.path and sys.modules. Closes #3634164
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9337
diff
changeset
|
887 |
sys.path.remove(apphome) |
e2d5b0712974
[test] when the config change, we should cleanup sys.path and sys.modules. Closes #3634164
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9337
diff
changeset
|
888 |
_CONFIG = config |
e2d5b0712974
[test] when the config change, we should cleanup sys.path and sys.modules. Closes #3634164
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9337
diff
changeset
|
889 |
config.adjust_sys_path() |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
890 |
handler = HCACHE.get(config) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
891 |
if handler is not None: |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
892 |
return handler |
9460
a2a0bc984863
[config] cleanup/refactor server sources file values handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9456
diff
changeset
|
893 |
driver = config.system_source_config['db-driver'] |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
894 |
key = (driver, config) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
895 |
handlerkls = HANDLERS.get(driver, None) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
896 |
if handlerkls is not None: |
9790
0872ac2a1db0
[testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9789
diff
changeset
|
897 |
handler = handlerkls(config, init_config) |
7214
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
898 |
if config.skip_db_create_and_restore: |
70538ea2532d
[testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7129
diff
changeset
|
899 |
handler = NoCreateDropDatabaseHandler(handler) |
7078
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
900 |
HCACHE.set(config, handler) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
901 |
return handler |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
902 |
else: |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
903 |
raise ValueError('no initialization function for driver %r' % driver) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
904 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
905 |
### compatibility layer ############################################## |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
906 |
from logilab.common.deprecation import deprecated |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
907 |
|
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
908 |
@deprecated("please use the new DatabaseHandler mecanism") |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
909 |
def init_test_database(config=None, configdir='data', apphome=None): |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
910 |
"""init a test database for a specific driver""" |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
911 |
if config is None: |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
912 |
config = TestServerConfiguration(apphome=apphome) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
913 |
handler = get_test_db_handler(config) |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
914 |
handler.build_db_cache() |
bad26a22fe29
[test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7071
diff
changeset
|
915 |
return handler.get_repo_and_cnx() |