Skip to content

Commit 7c4e14f

Browse files
committed
Add method for deeper configuration of testgres
1 parent 30af983 commit 7c4e14f

File tree

3 files changed

+53
-18
lines changed

3 files changed

+53
-18
lines changed

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ tags
55
Dockerfile
66
.coverage
77
env
8+
/build
9+
coverage.xml

‎testgres/testgres.py

+36-17
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@
7575
DEFAULT_XLOG_METHOD = "fetch"
7676

7777

78+
class TestgresConfig:
79+
cache_pg_config = True
80+
cache_initdb = True
81+
82+
7883
class TestgresException(Exception):
7984
"""
8085
Base exception
@@ -1081,7 +1086,7 @@ def call_initdb(_data_dir):
10811086
raise InitNodeException(str(e))
10821087

10831088
# Call initdb if we have custom params
1084-
if initdb_params:
1089+
if initdb_params or not TestgresConfig.cache_initdb:
10851090
call_initdb(data_dir)
10861091
# Else we can use cached dir
10871092
else:
@@ -1190,25 +1195,31 @@ def get_pg_config():
11901195

11911196
global pg_config_data
11921197

1193-
if not pg_config_data:
1194-
pg_config_cmd = os.environ.get("PG_CONFIG") or "pg_config"
1198+
if TestgresConfig.cache_pg_config and pg_config_data:
1199+
return pg_config_data
1200+
1201+
data = {}
1202+
pg_config_cmd = os.environ.get("PG_CONFIG") or "pg_config"
1203+
out = six.StringIO(subprocess.check_output([pg_config_cmd],
1204+
universal_newlines=True))
1205+
for line in out:
1206+
if line and "=" in line:
1207+
key, value = line.split("=", 1)
1208+
data[key.strip()] = value.strip()
11951209

1196-
out = six.StringIO(subprocess.check_output([pg_config_cmd],
1197-
universal_newlines=True))
1198-
for line in out:
1199-
if line and "=" in line:
1200-
key, value = line.split("=", 1)
1201-
pg_config_data[key.strip()] = value.strip()
1210+
# Fetch version of PostgreSQL and save it as VERSION_NUM
1211+
version = data["VERSION"]
1212+
version = version.split(" ")[-1] \
1213+
.partition('devel')[0] \
1214+
.partition('beta')[0] \
1215+
.partition('rc')[0]
1216+
data["VERSION_NUM"] = version
12021217

1203-
# Fetch version of PostgreSQL and save it as VERSION_NUM
1204-
version = pg_config_data["VERSION"]
1205-
version = version.split(" ")[-1] \
1206-
.partition('devel')[0] \
1207-
.partition('beta')[0] \
1208-
.partition('rc')[0]
1209-
pg_config_data["VERSION_NUM"] = version
1218+
if TestgresConfig.cache_pg_config:
1219+
pg_config_data.clear()
1220+
pg_config_data.update(data)
12101221

1211-
return pg_config_data
1222+
return data
12121223

12131224

12141225
def get_new_node(name, base_dir=None, use_logging=False):
@@ -1225,3 +1236,11 @@ def get_new_node(name, base_dir=None, use_logging=False):
12251236
"""
12261237

12271238
return PostgresNode(name=name, base_dir=base_dir, use_logging=use_logging)
1239+
1240+
1241+
def configure_testgres(**options):
1242+
'''
1243+
Configure testgres. Look for TestgresConfig to check what can be changed.
1244+
'''
1245+
for key, option in options.items():
1246+
setattr(TestgresConfig, key, option)

‎tests/test_simple.py

100644100755
+15-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
import tempfile
77
import logging.config
88
import subprocess
9+
import testgres
910

1011
from distutils.version import LooseVersion
1112

1213
from testgres import InitNodeException, \
1314
StartNodeException, ExecUtilException, \
1415
BackupException, QueryException, CatchUpException
1516

16-
from testgres import get_new_node, get_pg_config
17+
from testgres import get_new_node, get_pg_config, configure_testgres
1718
from testgres import bound_ports
1819
from testgres import NodeStatus
1920

@@ -414,6 +415,19 @@ def test_version_management(self):
414415
self.assertTrue(b > c)
415416
self.assertTrue(a > c)
416417

418+
def test_configure(self):
419+
# set global if it wasn't set
420+
pg_config = get_pg_config()
421+
configure_testgres(cache_initdb=True, cache_pg_config=True)
422+
423+
# check that is the same instance
424+
self.assertEqual(id(get_pg_config()), id(testgres.pg_config_data))
425+
configure_testgres(cache_initdb=True, cache_pg_config=False)
426+
self.assertNotEqual(id(get_pg_config()), id(testgres.pg_config_data))
427+
428+
# return to the base state
429+
configure_testgres(cache_initdb=True, cache_pg_config=True)
430+
417431

418432
if __name__ == '__main__':
419433
unittest.main()

0 commit comments

Comments
 (0)