Skip to content

Commit 17fb8c3

Browse files
NodeApp was moved to own py-file (refactoring)
1 parent d6be913 commit 17fb8c3

File tree

3 files changed

+173
-166
lines changed

3 files changed

+173
-166
lines changed

testgres/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@
3333
ProcessType, \
3434
DumpFormat
3535

36-
from .node import PostgresNode, NodeApp
36+
from .node import PostgresNode
3737
from .node import PortManager
38+
from .node_app import NodeApp
3839

3940
from .utils import \
4041
reserve_port, \
@@ -62,8 +63,9 @@
6263
"NodeConnection", "DatabaseError", "InternalError", "ProgrammingError", "OperationalError",
6364
"TestgresException", "ExecUtilException", "QueryException", "TimeoutException", "CatchUpException", "StartNodeException", "InitNodeException", "BackupException", "InvalidOperationException",
6465
"XLogMethod", "IsolationLevel", "NodeStatus", "ProcessType", "DumpFormat",
65-
"PostgresNode", "NodeApp",
66-
"PortManager",
66+
NodeApp.__name__,
67+
PostgresNode.__name__,
68+
PortManager.__name__,
6769
"reserve_port", "release_port", "bound_ports", "get_bin_path", "get_pg_config", "get_pg_version",
6870
"First", "Any",
6971
"OsOperations", "LocalOperations", "RemoteOperations", "ConnectionParams"

testgres/node.py

Lines changed: 0 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import signal
88
import subprocess
99
import threading
10-
import tempfile
11-
import platform
1210
from queue import Queue
1311

1412
import time
@@ -2352,164 +2350,3 @@ def delect_port_conflict(log_reader: PostgresNodeLogReader) -> bool:
23522350
return True
23532351

23542352
return False
2355-
2356-
2357-
class NodeApp:
2358-
2359-
def __init__(self, test_path=None, nodes_to_cleanup=None, os_ops=None):
2360-
assert os_ops is None or isinstance(os_ops, OsOperations)
2361-
2362-
if os_ops is None:
2363-
os_ops = LocalOperations.get_single_instance()
2364-
2365-
assert isinstance(os_ops, OsOperations)
2366-
2367-
if test_path:
2368-
if os.path.isabs(test_path):
2369-
self.test_path = test_path
2370-
else:
2371-
self.test_path = os_ops.build_path(os_ops.cwd(), test_path)
2372-
else:
2373-
self.test_path = os_ops.cwd()
2374-
self.nodes_to_cleanup = nodes_to_cleanup if nodes_to_cleanup else []
2375-
self.os_ops = os_ops
2376-
2377-
def make_empty(
2378-
self,
2379-
base_dir=None,
2380-
port=None,
2381-
bin_dir=None):
2382-
real_base_dir = self.os_ops.build_path(self.test_path, base_dir)
2383-
self.os_ops.rmdirs(real_base_dir, ignore_errors=True)
2384-
self.os_ops.makedirs(real_base_dir)
2385-
2386-
node = PostgresNode(base_dir=real_base_dir, port=port, bin_dir=bin_dir)
2387-
self.nodes_to_cleanup.append(node)
2388-
2389-
return node
2390-
2391-
def make_simple(
2392-
self,
2393-
base_dir=None,
2394-
port=None,
2395-
set_replication=False,
2396-
ptrack_enable=False,
2397-
initdb_params=[],
2398-
pg_options={},
2399-
checksum=True,
2400-
bin_dir=None):
2401-
assert type(pg_options) == dict # noqa: E721
2402-
2403-
if checksum and '--data-checksums' not in initdb_params:
2404-
initdb_params.append('--data-checksums')
2405-
node = self.make_empty(base_dir, port, bin_dir=bin_dir)
2406-
node.init(
2407-
initdb_params=initdb_params, allow_streaming=set_replication)
2408-
2409-
# set major version
2410-
pg_version_file = self.os_ops.read(self.os_ops.build_path(node.data_dir, 'PG_VERSION'))
2411-
node.major_version_str = str(pg_version_file.rstrip())
2412-
node.major_version = float(node.major_version_str)
2413-
2414-
# Set default parameters
2415-
options = {
2416-
'max_connections': 100,
2417-
'shared_buffers': '10MB',
2418-
'fsync': 'off',
2419-
'wal_level': 'logical',
2420-
'hot_standby': 'off',
2421-
'log_line_prefix': '%t [%p]: [%l-1] ',
2422-
'log_statement': 'none',
2423-
'log_duration': 'on',
2424-
'log_min_duration_statement': 0,
2425-
'log_connections': 'on',
2426-
'log_disconnections': 'on',
2427-
'restart_after_crash': 'off',
2428-
'autovacuum': 'off',
2429-
# unix_socket_directories will be defined later
2430-
}
2431-
2432-
# Allow replication in pg_hba.conf
2433-
if set_replication:
2434-
options['max_wal_senders'] = 10
2435-
2436-
if ptrack_enable:
2437-
options['ptrack.map_size'] = '1'
2438-
options['shared_preload_libraries'] = 'ptrack'
2439-
2440-
if node.major_version >= 13:
2441-
options['wal_keep_size'] = '200MB'
2442-
else:
2443-
options['wal_keep_segments'] = '12'
2444-
2445-
# Apply given parameters
2446-
for option_name, option_value in iteritems(pg_options):
2447-
options[option_name] = option_value
2448-
2449-
# Define delayed propertyes
2450-
if not ("unix_socket_directories" in options.keys()):
2451-
options["unix_socket_directories"] = __class__._gettempdir_for_socket()
2452-
2453-
# Set config values
2454-
node.set_auto_conf(options)
2455-
2456-
# kludge for testgres
2457-
# https://github.com/postgrespro/testgres/issues/54
2458-
# for PG >= 13 remove 'wal_keep_segments' parameter
2459-
if node.major_version >= 13:
2460-
node.set_auto_conf({}, 'postgresql.conf', ['wal_keep_segments'])
2461-
2462-
return node
2463-
2464-
@staticmethod
2465-
def _gettempdir_for_socket():
2466-
platform_system_name = platform.system().lower()
2467-
2468-
if platform_system_name == "windows":
2469-
return __class__._gettempdir()
2470-
2471-
#
2472-
# [2025-02-17] Hot fix.
2473-
#
2474-
# Let's use hard coded path as Postgres likes.
2475-
#
2476-
# pg_config_manual.h:
2477-
#
2478-
# #ifndef WIN32
2479-
# #define DEFAULT_PGSOCKET_DIR "/tmp"
2480-
# #else
2481-
# #define DEFAULT_PGSOCKET_DIR ""
2482-
# #endif
2483-
#
2484-
# On the altlinux-10 tempfile.gettempdir() may return
2485-
# the path to "private" temp directiry - "/temp/.private/<username>/"
2486-
#
2487-
# But Postgres want to find a socket file in "/tmp" (see above).
2488-
#
2489-
2490-
return "/tmp"
2491-
2492-
@staticmethod
2493-
def _gettempdir():
2494-
v = tempfile.gettempdir()
2495-
2496-
#
2497-
# Paranoid checks
2498-
#
2499-
if type(v) != str: # noqa: E721
2500-
__class__._raise_bugcheck("tempfile.gettempdir returned a value with type {0}.".format(type(v).__name__))
2501-
2502-
if v == "":
2503-
__class__._raise_bugcheck("tempfile.gettempdir returned an empty string.")
2504-
2505-
if not os.path.exists(v):
2506-
__class__._raise_bugcheck("tempfile.gettempdir returned a not exist path [{0}].".format(v))
2507-
2508-
# OK
2509-
return v
2510-
2511-
@staticmethod
2512-
def _raise_bugcheck(msg):
2513-
assert type(msg) == str # noqa: E721
2514-
assert msg != ""
2515-
raise Exception("[BUG CHECK] " + msg)

testgres/node_app.py

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
from .node import OsOperations
2+
from .node import LocalOperations
3+
from .node import PostgresNode
4+
5+
import os
6+
import platform
7+
import tempfile
8+
9+
10+
class NodeApp:
11+
12+
def __init__(self, test_path=None, nodes_to_cleanup=None, os_ops=None):
13+
assert os_ops is None or isinstance(os_ops, OsOperations)
14+
15+
if os_ops is None:
16+
os_ops = LocalOperations.get_single_instance()
17+
18+
assert isinstance(os_ops, OsOperations)
19+
20+
if test_path:
21+
if os.path.isabs(test_path):
22+
self.test_path = test_path
23+
else:
24+
self.test_path = os_ops.build_path(os_ops.cwd(), test_path)
25+
else:
26+
self.test_path = os_ops.cwd()
27+
self.nodes_to_cleanup = nodes_to_cleanup if nodes_to_cleanup else []
28+
self.os_ops = os_ops
29+
30+
def make_empty(
31+
self,
32+
base_dir=None,
33+
port=None,
34+
bin_dir=None):
35+
real_base_dir = self.os_ops.build_path(self.test_path, base_dir)
36+
self.os_ops.rmdirs(real_base_dir, ignore_errors=True)
37+
self.os_ops.makedirs(real_base_dir)
38+
39+
node = PostgresNode(base_dir=real_base_dir, port=port, bin_dir=bin_dir)
40+
self.nodes_to_cleanup.append(node)
41+
42+
return node
43+
44+
def make_simple(
45+
self,
46+
base_dir=None,
47+
port=None,
48+
set_replication=False,
49+
ptrack_enable=False,
50+
initdb_params=[],
51+
pg_options={},
52+
checksum=True,
53+
bin_dir=None):
54+
assert type(pg_options) == dict # noqa: E721
55+
56+
if checksum and '--data-checksums' not in initdb_params:
57+
initdb_params.append('--data-checksums')
58+
node = self.make_empty(base_dir, port, bin_dir=bin_dir)
59+
node.init(
60+
initdb_params=initdb_params, allow_streaming=set_replication)
61+
62+
# set major version
63+
pg_version_file = self.os_ops.read(self.os_ops.build_path(node.data_dir, 'PG_VERSION'))
64+
node.major_version_str = str(pg_version_file.rstrip())
65+
node.major_version = float(node.major_version_str)
66+
67+
# Set default parameters
68+
options = {
69+
'max_connections': 100,
70+
'shared_buffers': '10MB',
71+
'fsync': 'off',
72+
'wal_level': 'logical',
73+
'hot_standby': 'off',
74+
'log_line_prefix': '%t [%p]: [%l-1] ',
75+
'log_statement': 'none',
76+
'log_duration': 'on',
77+
'log_min_duration_statement': 0,
78+
'log_connections': 'on',
79+
'log_disconnections': 'on',
80+
'restart_after_crash': 'off',
81+
'autovacuum': 'off',
82+
# unix_socket_directories will be defined later
83+
}
84+
85+
# Allow replication in pg_hba.conf
86+
if set_replication:
87+
options['max_wal_senders'] = 10
88+
89+
if ptrack_enable:
90+
options['ptrack.map_size'] = '1'
91+
options['shared_preload_libraries'] = 'ptrack'
92+
93+
if node.major_version >= 13:
94+
options['wal_keep_size'] = '200MB'
95+
else:
96+
options['wal_keep_segments'] = '12'
97+
98+
# Apply given parameters
99+
for option_name, option_value in pg_options.items():
100+
options[option_name] = option_value
101+
102+
# Define delayed propertyes
103+
if not ("unix_socket_directories" in options.keys()):
104+
options["unix_socket_directories"] = __class__._gettempdir_for_socket()
105+
106+
# Set config values
107+
node.set_auto_conf(options)
108+
109+
# kludge for testgres
110+
# https://github.com/postgrespro/testgres/issues/54
111+
# for PG >= 13 remove 'wal_keep_segments' parameter
112+
if node.major_version >= 13:
113+
node.set_auto_conf({}, 'postgresql.conf', ['wal_keep_segments'])
114+
115+
return node
116+
117+
@staticmethod
118+
def _gettempdir_for_socket():
119+
platform_system_name = platform.system().lower()
120+
121+
if platform_system_name == "windows":
122+
return __class__._gettempdir()
123+
124+
#
125+
# [2025-02-17] Hot fix.
126+
#
127+
# Let's use hard coded path as Postgres likes.
128+
#
129+
# pg_config_manual.h:
130+
#
131+
# #ifndef WIN32
132+
# #define DEFAULT_PGSOCKET_DIR "/tmp"
133+
# #else
134+
# #define DEFAULT_PGSOCKET_DIR ""
135+
# #endif
136+
#
137+
# On the altlinux-10 tempfile.gettempdir() may return
138+
# the path to "private" temp directiry - "/temp/.private/<username>/"
139+
#
140+
# But Postgres want to find a socket file in "/tmp" (see above).
141+
#
142+
143+
return "/tmp"
144+
145+
@staticmethod
146+
def _gettempdir():
147+
v = tempfile.gettempdir()
148+
149+
#
150+
# Paranoid checks
151+
#
152+
if type(v) != str: # noqa: E721
153+
__class__._raise_bugcheck("tempfile.gettempdir returned a value with type {0}.".format(type(v).__name__))
154+
155+
if v == "":
156+
__class__._raise_bugcheck("tempfile.gettempdir returned an empty string.")
157+
158+
if not os.path.exists(v):
159+
__class__._raise_bugcheck("tempfile.gettempdir returned a not exist path [{0}].".format(v))
160+
161+
# OK
162+
return v
163+
164+
@staticmethod
165+
def _raise_bugcheck(msg):
166+
assert type(msg) == str # noqa: E721
167+
assert msg != ""
168+
raise Exception("[BUG CHECK] " + msg)

0 commit comments

Comments
 (0)