-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathexecutor.py
More file actions
307 lines (272 loc) · 11.3 KB
/
executor.py
File metadata and controls
307 lines (272 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# Copyright (C) 2016-2020 by Clearcode <http://clearcode.cc>
# and associates (see AUTHORS).
# This file is part of pytest-postgresql.
# pytest-postgresql is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# pytest-postgresql is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public License
# along with pytest-postgresql. If not, see <http://www.gnu.org/licenses/>.
"""PostgreSQL executor crafter around pg_ctl."""
import logging
import os
import os.path
import platform
import re
import shutil
import subprocess
import tempfile
import time
from typing import Any, Optional, TypeVar
from mirakuru import TCPExecutor
from mirakuru.exceptions import ProcessFinishedWithError
from packaging.version import parse
from pytest_postgresql.exceptions import ExecutableMissingException, PostgreSQLUnsupported
logger = logging.getLogger(__name__)
_LOCALE = "C.UTF-8"
if platform.system() == "Darwin":
# Darwin does not have C.UTF-8, but en_US.UTF-8 is always available
_LOCALE = "en_US.UTF-8"
elif platform.system() == "Windows":
# Windows doesn't support C.UTF-8 or en_US.UTF-8, use plain "C" locale
_LOCALE = "C"
T = TypeVar("T", bound="PostgreSQLExecutor")
class PostgreSQLExecutor(TCPExecutor):
"""PostgreSQL executor running on pg_ctl.
Based over an `pg_ctl program
<http://www.postgresql.org/docs/current/static/app-pg-ctl.html>`_
"""
# Unix command template - uses single quotes for PostgreSQL config value quoting
# which protects paths with spaces in unix_socket_directories.
# On Unix, mirakuru uses shlex.split() with shell=False, so single quotes
# inside double-quoted strings are preserved and passed to PostgreSQL's config parser.
UNIX_PROC_START_COMMAND = (
'{executable} start -D "{datadir}" '
"-o \"-F -p {port} -c log_destination='stderr' "
"-c logging_collector=off "
"-c unix_socket_directories='{unixsocketdir}'{postgres_options}\" "
'-l "{logfile}" {startparams}'
)
# Windows command template - no single quotes (cmd.exe treats them as literals,
# not delimiters) and unix_socket_directories is omitted entirely since PostgreSQL
# ignores it on Windows. On Windows, mirakuru forces shell=True so the command
# goes through cmd.exe.
WINDOWS_PROC_START_COMMAND = (
'{executable} start -D "{datadir}" '
'-o "-F -p {port} -c log_destination=stderr '
'-c logging_collector=off{postgres_options}" '
'-l "{logfile}" {startparams}'
)
VERSION_RE = re.compile(r".* (?P<version>\d+(?:\.\d+)?)")
MIN_SUPPORTED_VERSION = parse("14")
def __init__(
self,
executable: str,
host: str,
port: int,
datadir: str,
unixsocketdir: str,
logfile: str,
startparams: str,
dbname: str,
shell: bool = False,
timeout: Optional[int] = 60,
sleep: float = 0.1,
user: str = "postgres",
password: str = "",
options: str = "",
postgres_options: str = "",
):
"""Initialize PostgreSQLExecutor executor.
:param executable: pg_ctl location
:param host: host under which process is accessible
:param port: port under which process is accessible
:param datadir: path to postgresql datadir
:param unixsocketdir: path to socket directory
:param logfile: path to logfile for postgresql
:param startparams: additional start parameters
:param shell: see `subprocess.Popen`
:param timeout: time to wait for process to start or stop.
if None, wait indefinitely.
:param sleep: how often to check for start/stop condition
:param user: postgresql's username used to manage
and access PostgreSQL
:param password: optional password for the user
:param dbname: database name (might not yet exist)
:param options:
:param postgres_options: extra arguments to `postgres start`
"""
self._directory_initialised = False
self.executable = executable
self.user = user
self.password = password
self.dbname = dbname
self.options = options
self.datadir = datadir
self.unixsocketdir = unixsocketdir
self.logfile = logfile
self.startparams = startparams
self.postgres_options = postgres_options
if platform.system() == "Windows":
command_template = self.WINDOWS_PROC_START_COMMAND
else:
command_template = self.UNIX_PROC_START_COMMAND
command = command_template.format(
executable=self.executable,
datadir=self.datadir,
port=port,
unixsocketdir=self.unixsocketdir,
logfile=self.logfile,
startparams=self.startparams,
postgres_options=f" {self.postgres_options}" if self.postgres_options else "",
)
super().__init__(
command,
host,
port,
shell=shell,
timeout=timeout,
sleep=sleep,
envvars={
"LC_ALL": _LOCALE,
"LC_CTYPE": _LOCALE,
"LANG": _LOCALE,
},
)
@property
def template_dbname(self) -> str:
"""Return the template database name."""
return f"{self.dbname}_tmpl"
def start(self: T) -> T:
"""Add check for postgresql version before starting process."""
if self.version < self.MIN_SUPPORTED_VERSION:
raise PostgreSQLUnsupported(
f"Your version of PostgreSQL is not supported. "
f"Consider updating to PostgreSQL {self.MIN_SUPPORTED_VERSION} at least. "
f"The currently installed version of PostgreSQL: {self.version}."
)
self.init_directory()
return super().start()
def clean_directory(self) -> None:
"""Remove directory created for postgresql run."""
if os.path.isdir(self.datadir):
shutil.rmtree(self.datadir)
self._directory_initialised = False
def init_directory(self) -> None:
"""Initialize postgresql data directory.
See `Initialize postgresql data directory
<www.postgresql.org/docs/9.5/static/app-initdb.html>`_
"""
# only make sure it's removed if it's handled by this exact process
if self._directory_initialised:
return
# remove old one if exists first.
self.clean_directory()
init_directory = [self.executable, "initdb", "--pgdata", self.datadir]
options = ["--username=%s" % self.user]
if self.password:
with tempfile.NamedTemporaryFile() as password_file:
options += ["--auth=password", "--pwfile=%s" % password_file.name]
if hasattr(self.password, "encode"):
password = self.password.encode("utf-8")
else:
password = self.password # type: ignore[assignment]
password_file.write(password)
password_file.flush()
init_directory += ["-o", " ".join(options)]
# Passing envvars to command to avoid weird MacOs error.
subprocess.check_output(init_directory, env=self.envvars)
else:
options += ["--auth=trust"]
init_directory += ["-o", " ".join(options)]
# Passing envvars to command to avoid weird MacOs error.
subprocess.check_output(init_directory, env=self.envvars)
self._directory_initialised = True
def wait_for_postgres(self) -> None:
"""Wait for postgresql being started."""
if "-w" not in self.startparams:
return
# wait until server is running
while 1:
if self.running():
break
time.sleep(1)
@property
def version(self) -> Any:
"""Detect postgresql version."""
try:
version_string = subprocess.check_output([self.executable, "--version"]).decode("utf-8")
except FileNotFoundError as ex:
raise ExecutableMissingException(
f"Could not find {self.executable}. Is PostgreSQL server installed? "
f"Alternatively pg_config installed might be from different "
f"version that postgresql-server."
) from ex
matches = self.VERSION_RE.search(version_string)
assert matches is not None
return parse(matches.groupdict()["version"])
def running(self) -> bool:
"""Check if server is running."""
if not os.path.exists(self.datadir):
return False
status_code = subprocess.getstatusoutput(f'{self.executable} status -D "{self.datadir}"')[0]
return status_code == 0
def _windows_terminate_process(self, _sig: Optional[int] = None) -> None:
"""Terminate process on Windows.
:param _sig: Signal parameter (unused on Windows but included for consistency)
"""
if self.process is None:
return
try:
# On Windows, try to terminate gracefully first
self.process.terminate()
# Give it a chance to terminate gracefully
try:
self.process.wait(timeout=5)
except subprocess.TimeoutExpired:
# If it doesn't terminate gracefully, force kill
self.process.kill()
try:
self.process.wait(timeout=10)
except subprocess.TimeoutExpired:
logger.warning(
"Process %s could not be cleaned up after kill() and may be a zombie process",
self.process.pid if self.process else "unknown",
)
except (OSError, AttributeError) as e:
# Process might already be dead or other issues
logger.debug(
"Exception during Windows process termination: %s: %s",
type(e).__name__,
e,
)
def stop(self: T, sig: Optional[int] = None, exp_sig: Optional[int] = None) -> T:
"""Issue a stop request to executable."""
subprocess.check_output(
[self.executable, "stop", "-D", self.datadir, "-m", "f"],
)
try:
if platform.system() == "Windows":
self._windows_terminate_process(sig)
else:
super().stop(sig, exp_sig)
except ProcessFinishedWithError:
# Finished, leftovers ought to be cleaned afterwards anyway
pass
except AttributeError:
# Fallback for edge cases where os.killpg doesn't exist (e.g., Windows)
if not hasattr(os, "killpg"):
self._windows_terminate_process(sig)
else:
raise
return self
def __del__(self) -> None:
"""Make sure the directories are properly removed at the end."""
try:
super().__del__()
finally:
self.clean_directory()