def postgresql(): """Provides a clean temporary PostgreSQL server/database. If the environment variable `INDICO_TEST_DATABASE_URI` is set, this fixture will do nothing and simply return the connection string from that variable """ # Use existing database if 'INDICO_TEST_DATABASE_URI' in os.environ: yield os.environ['INDICO_TEST_DATABASE_URI'] return db_name = 'test' # Ensure we have initdb and a recent enough postgres version try: version_output = subprocess.check_output(['initdb', '--version']) except Exception as e: pytest.skip('Could not retrieve PostgreSQL version: {}'.format(e)) pg_version = map( int, re.match(r'initdb \(PostgreSQL\) ((?:\d+\.?)+)', version_output).group(1).split('.')) if pg_version[0] < 9 or (pg_version[0] == 9 and pg_version[1] < 2): pytest.skip('PostgreSQL version is too old: {}'.format(version_output)) # Prepare server instance and a test database temp_dir = tempfile.mkdtemp(prefix='indicotestpg.') postgres_args = '-h "" -k "{}"'.format(temp_dir) try: silent_check_call(['initdb', '--encoding', 'UTF8', temp_dir]) silent_check_call( ['pg_ctl', '-D', temp_dir, '-w', '-o', postgres_args, 'start']) silent_check_call(['createdb', '-h', temp_dir, db_name]) except Exception as e: shutil.rmtree(temp_dir) pytest.skip('Could not init/start PostgreSQL: {}'.format(e)) yield 'postgresql:///{}?host={}'.format(db_name, temp_dir) try: silent_check_call( ['pg_ctl', '-D', temp_dir, '-m', 'immediate', 'stop']) except Exception as e: # If it failed for any reason, try killing it try: with open(os.path.join(temp_dir, 'postmaster.pid')) as f: pid = int(f.readline().strip()) os.kill(pid, signal.SIGKILL) pytest.skip( 'Could not stop postgresql; killed it instead: {}'.format(e)) except Exception as e: pytest.skip('Could not stop/kill postgresql: {}'.format(e)) finally: shutil.rmtree(temp_dir)
def postgresql(): """Provides a clean temporary PostgreSQL server/database. If the environment variable `INDICO_TEST_DATABASE_URI` is set, this fixture will do nothing and simply return the connection string from that variable """ # Use existing database if 'INDICO_TEST_DATABASE_URI' in os.environ: yield os.environ['INDICO_TEST_DATABASE_URI'] return db_name = 'test' # Ensure we have initdb and a recent enough postgres version try: version_output = subprocess.check_output(['initdb', '--version']) except Exception as e: pytest.skip('Could not retrieve PostgreSQL version: {}'.format(e)) pg_version = map(int, re.match(r'initdb \(PostgreSQL\) ((?:\d+\.?)+)', version_output).group(1).split('.')) if pg_version[0] < 9 or (pg_version[0] == 9 and pg_version[1] < 2): pytest.skip('PostgreSQL version is too old: {}'.format(version_output)) # Prepare server instance and a test database temp_dir = tempfile.mkdtemp(prefix='indicotestpg.') postgres_args = '-h "" -k "{}"'.format(temp_dir) try: silent_check_call(['initdb', '--encoding', 'UTF8', temp_dir]) silent_check_call(['pg_ctl', '-D', temp_dir, '-w', '-o', postgres_args, 'start']) silent_check_call(['createdb', '-h', temp_dir, db_name]) except Exception as e: shutil.rmtree(temp_dir) pytest.skip('Could not init/start PostgreSQL: {}'.format(e)) yield 'postgresql:///{}?host={}'.format(db_name, temp_dir) try: silent_check_call(['pg_ctl', '-D', temp_dir, '-m', 'immediate', 'stop']) except Exception as e: # If it failed for any reason, try killing it try: with open(os.path.join(temp_dir, 'postmaster.pid')) as f: pid = int(f.readline().strip()) os.kill(pid, signal.SIGKILL) pytest.skip('Could not stop postgresql; killed it instead: {}'.format(e)) except Exception as e: pytest.skip('Could not stop/kill postgresql: {}'.format(e)) finally: shutil.rmtree(temp_dir)