def devpipostgresql_postgresql(): tmpdir = py.path.local( tempfile.mkdtemp(prefix='test-', suffix='-devpi-postgresql')) try: cap = py.io.StdCaptureFD() cap.startall() subprocess.check_call(['initdb', tmpdir.strpath]) cap.reset() with tmpdir.join('postgresql.conf').open('w+b') as f: f.write(b"\n".join([ b"fsync = off", b"full_page_writes = off", b"synchronous_commit = off", b"unix_socket_directories = '" + tmpdir.strpath.encode('ascii') + b"'"])) host = 'localhost' port = get_open_port(host) cap = py.io.StdCaptureFD() cap.startall() p = subprocess.Popen([ 'postgres', '-D', tmpdir.strpath, '-h', host, '-p', str(port)]) wait_for_port(host, port) cap.reset() try: cap = py.io.StdCaptureFD() cap.startall() subprocess.check_call([ 'createdb', '-h', host, '-p', str(port), 'devpi']) cap.reset() settings = dict(host=host, port=port, user=getpass.getuser()) main.Storage( tmpdir, notify_on_commit=False, cache_size=10000, settings=settings) yield settings for conn, db, ts in Storage._connections: try: conn.close() except AttributeError: pass # use a copy of the set, as it might be changed in another thread for db in set(Storage._dbs_created): try: subprocess.check_call([ 'dropdb', '-h', Storage.host, '-p', str(Storage.port), db]) except subprocess.CalledProcessError: pass finally: p.terminate() p.wait() finally: tmpdir.remove(ignore_errors=True)
def postgresql(): tmpdir = py.path.local( tempfile.mkdtemp(prefix='test-', suffix='-devpi-postgresql')) try: subprocess.check_call(['initdb', tmpdir.strpath]) with tmpdir.join('postgresql.conf').open('w+b') as f: f.write(b"\n".join([ b"fsync = off", b"full_page_writes = off", b"synchronous_commit = off" ])) host = 'localhost' port = conftest.get_open_port(host) p = subprocess.Popen( ['postgres', '-D', tmpdir.strpath, '-h', host, '-p', str(port)]) conftest.wait_for_port(host, port) try: subprocess.check_call( ['createdb', '-h', host, '-p', str(port), 'devpi']) settings = dict(host=host, port=port) main.Storage(tmpdir, notify_on_commit=False, cache_size=10000, settings=settings) yield settings for conn, db, ts in Storage._connections: try: conn.close() except AttributeError: pass for db in Storage._dbs_created: try: subprocess.check_call([ 'dropdb', '-h', Storage.host, '-p', str(Storage.port), db ]) except subprocess.CalledProcessError: pass finally: p.terminate() p.wait() finally: tmpdir.remove(ignore_errors=True)
def devpipostgresql_postgresql(request): tmpdir = py.path.local( tempfile.mkdtemp(prefix='test-', suffix='-devpi-postgresql')) try: cap = py.io.StdCaptureFD() cap.startall() subprocess.check_call(['initdb', tmpdir.strpath]) cap.reset() postgresql_conf_lines = [ "fsync = off", "full_page_writes = off", "synchronous_commit = off", "unix_socket_directories = '{}'".format(tmpdir.strpath) ] 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) server_cert = ca.cert_for_host(host) if not sys.platform.startswith("win"): # Postgres requires restrictive permissions on private key. os.chmod(server_cert, 0o600) postgresql_conf_lines.extend([ "ssl = on", "ssl_cert_file = '{}'".format(server_cert), "ssl_key_file = '{}'".format(server_cert), "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: 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: f.write("\n".join(postgresql_conf_lines)) port = get_open_port(host) cap = py.io.StdCaptureFD() cap.startall() p = subprocess.Popen( ['postgres', '-D', tmpdir.strpath, '-h', host, '-p', str(port)]) wait_for_port(host, port) cap.reset() try: cap = py.io.StdCaptureFD() cap.startall() subprocess.check_call( ['createdb', '-h', host, '-p', str(port), 'devpi']) cap.reset() user = getpass.getuser() settings = dict(host=host, port=port, user=user) if pg_ssl: # 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_certfile'] = client_cert main.Storage(tmpdir, notify_on_commit=False, cache_size=10000, settings=settings) yield settings for conn, db, ts in Storage._connections: try: conn.close() except AttributeError: pass # use a copy of the set, as it might be changed in another thread for db in set(Storage._dbs_created): try: subprocess.check_call([ 'dropdb', '-h', Storage.host, '-p', str(Storage.port), db ]) except subprocess.CalledProcessError: pass finally: p.terminate() p.wait() finally: tmpdir.remove(ignore_errors=True)