Skip to content

Commit

Permalink
Remove usage of py.path from devpi-postgresql. Refs devpi#930
Browse files Browse the repository at this point in the history
  • Loading branch information
fschulze authored and markmcclain committed Jan 8, 2024
1 parent a6e86a8 commit 2f76615
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions postgresql/pytest_devpi_postgresql/__init__.py
@@ -1,12 +1,13 @@
from contextlib import closing
from devpi_postgresql import main
from pathlib import Path
from pluggy import HookimplMarker
from certauth.certauth import CertificateAuthority
from shutil import rmtree
from typing import Set
import sys
import os
import getpass
import py
import pytest
import socket
import subprocess
Expand Down Expand Up @@ -45,24 +46,25 @@ def pytest_addoption(parser):

@pytest.fixture(scope="session")
def devpipostgresql_postgresql(request):
tmpdir = py.path.local(
tmpdir = Path(
tempfile.mkdtemp(prefix='test-', suffix='-devpi-postgresql'))
tmpdir_path = str(tmpdir)
try:
subprocess.check_call(['initdb', tmpdir.strpath])
subprocess.check_call(['initdb', tmpdir_path])

postgresql_conf_lines = [
"fsync = off",
"full_page_writes = off",
"synchronous_commit = off",
"unix_socket_directories = '{}'".format(tmpdir.strpath)]
"unix_socket_directories = '{}'".format(tmpdir_path)]

pg_ssl = request.config.option.backend_postgresql_ssl
host = 'localhost'

if pg_ssl:
# Make certificate authority and server certificate
ca = CertificateAuthority('Test CA', tmpdir.join('ca.pem').strpath,
cert_cache=tmpdir.strpath)
ca = CertificateAuthority('Test CA', str(tmpdir / 'ca.pem'),
cert_cache=tmpdir_path)
server_cert = ca.cert_for_host(host)
if not sys.platform.startswith("win"):
# Postgres requires restrictive permissions on private key.
Expand All @@ -75,20 +77,20 @@ def devpipostgresql_postgresql(request):
"ssl_ca_file = 'ca.pem'"])

# Require SSL connections to be authenticated by client certificates.
with tmpdir.join('pg_hba.conf').open('w', encoding='ascii') as f:
with tmpdir.joinpath('pg_hba.conf').open('w', encoding='ascii') as f:
f.write("\n".join([
# "local" is for Unix domain socket connections only
'local all all trust',
# IPv4 local connections:
'hostssl all all 127.0.0.1/32 cert',
'host all all 127.0.0.1/32 trust']))

with tmpdir.join('postgresql.conf').open('w+', encoding='ascii') as f:
with tmpdir.joinpath('postgresql.conf').open('w+', encoding='ascii') as f:
f.write("\n".join(postgresql_conf_lines))

port = get_open_port(host)
p = subprocess.Popen([
'postgres', '-D', tmpdir.strpath, '-h', host, '-p', str(port)])
'postgres', '-D', tmpdir_path, '-h', host, '-p', str(port)])
wait_for_port(host, port)
try:
subprocess.check_call([
Expand All @@ -101,11 +103,11 @@ def devpipostgresql_postgresql(request):
# Make client certificate for user and authenticate with it.
client_cert = ca.cert_for_host(user)
settings['ssl_check_hostname'] = 'yes'
settings['ssl_ca_certs'] = tmpdir.join('ca.pem').strpath
settings['ssl_ca_certs'] = str(tmpdir / 'ca.pem')
settings['ssl_certfile'] = client_cert

main.Storage(
tmpdir, notify_on_commit=False,
tmpdir_path, notify_on_commit=False,
cache_size=10000, settings=settings)
yield settings
for conn, db, ts in Storage._connections:
Expand All @@ -124,7 +126,7 @@ def devpipostgresql_postgresql(request):
p.terminate()
p.wait()
finally:
tmpdir.remove(ignore_errors=True)
rmtree(tmpdir_path)


class Storage(main.Storage):
Expand All @@ -135,7 +137,7 @@ class Storage(main.Storage):
def _get_test_db(cls, basedir):
import hashlib
db = hashlib.md5(
basedir.strpath.encode('ascii', errors='ignore')).hexdigest()
str(basedir).encode('ascii', errors='ignore')).hexdigest()
if db not in cls._dbs_created:
subprocess.call([
'createdb', '-h', cls.host, '-p', str(cls.port),
Expand Down

0 comments on commit 2f76615

Please sign in to comment.