├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── pyembedpg.py ├── setup.cfg ├── setup.py ├── tests ├── test_pyembedpg.py └── test_pyembedpg_with_context.py └── tox.ini /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | 47 | # Translations 48 | *.mo 49 | *.pot 50 | 51 | # Django stuff: 52 | *.log 53 | 54 | # Sphinx documentation 55 | docs/_build/ 56 | 57 | # PyBuilder 58 | target/ 59 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: xenial 2 | language: python 3 | python: 4 | - 3.4 5 | - 3.5 6 | - 3.6 7 | - 3.7 8 | before_install: pip install --upgrade setuptools 9 | install: pip install tox tox-travis coveralls 10 | before_script: if [[ $TRAVIS_PYTHON_VERSION == '3.7' ]]; then tox -e flake8; fi 11 | script: tox -r 12 | after_success: coveralls 13 | sudo: false 14 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | # Version 0.0.4 4 | 5 | * Install extensions while unpack (@kimkimkeren) [#3] 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | pyembedpg 2 | ========= 3 | 4 | [![pypi](http://img.shields.io/pypi/v/pyembedpg.png)](https://pypi.python.org/pypi/pyembedpg) 5 | [![Supported Python Versions](https://img.shields.io/pypi/pyversions/Pyembedpg.svg)](https://pypi.python.org/pypi/pyembedpg/) 6 | [![pypi downloads](http://img.shields.io/pypi/dm/pyembedpg.png)](https://pypi.python.org/pypi/pyembedpg) 7 | [![Build Status](https://travis-ci.org/Simulmedia/pyembedpg.svg)](https://travis-ci.org/Simulmedia/pyembedpg) 8 | [![License](https://img.shields.io/pypi/l/Pyembedpg.svg)](https://pypi.python.org/pypi/pyembedpg/) 9 | [![Codacy Badge](https://www.codacy.com/project/badge/391726fcad274a24b1427abf5fa10380)](https://www.codacy.com/app/francois-dangngoc/pyembedpg) 10 | [![Coverage Status](https://coveralls.io/repos/Simulmedia/pyembedpg/badge.svg?branch=master&service=github)](https://coveralls.io/github/Simulmedia/pyembedpg?branch=master) 11 | 12 | Provide a platform neutral way to run Postgres in integration tests. More info at: http://www.simulmedia.com/resources/blog/pyembedpg-simple-way-use-postgres-python-integration-tests/ 13 | 14 | ### Why 15 | 16 | - Running tests in parallel on different instances of Postgres 17 | - Testing on different versions can be useful (e.g., prod has another version of Postgres installed) 18 | - Easier to install a particular version than manually 19 | - Dropping databases and users all the time is painful (and cleanup might not be done properly everytime) 20 | 21 | ### Features 22 | 23 | It downloads the specified version of Postgres automatically from https://ftp.postgresql.org/pub/source/, 24 | build it and cache it in `.pyembedpg` so subsequent uses won't download it. 25 | You can start the Postgres server on any port in a context (using `with`) that will be shutdown when you exit the context. 26 | 27 | 28 | ### How to use it 29 | 30 | You can install pyembedpg with pip: 31 | 32 | pip install pyembedpg 33 | 34 | You can start a new postgres server as follows: 35 | ```python 36 | pg = PyEmbedPg('9.4.0') 37 | # Start the database and download it and build it if it hasn't been done so already 38 | with pg.start(15432) as db: 39 | # do your thing 40 | ``` 41 | or: 42 | ```python 43 | pg = PyEmbedPg('9.4.0') 44 | # Start the database and download it and build it if it hasn't been done so already 45 | db = pg.start(15432) 46 | try: 47 | # do your thing 48 | finally: 49 | db.shutdown() 50 | ``` 51 | 52 | If you don't specify the version (e.g., `PyEmbedPg()`), it will use the latest version found in `.pyembedpg` or on the FTP server if `.pyembedpg` is empty. 53 | 54 | If you want to use the current Postgres version installed on the server you can do `PyEmbedPg('local')`. 55 | 56 | You can use it as follows: 57 | ```python 58 | pg = PyEmbedPg('9.4.0') 59 | with pg.start(15432) as postgres: 60 | postgres.create_user('scott', 'tiger') 61 | postgres.create_database('testdb', 'scott') 62 | 63 | # Postgres is initialized, now run some queries 64 | with psycopg2.connect(database='postgres', user='scott', password='tiger', port=postgres.running_port) as conn: 65 | with conn.cursor() as cursor: 66 | cursor.execute('CREATE TABLE employee(name VARCHAR(32), age INT)') 67 | cursor.execute("INSERT INTO employee VALUES ('John', 32)") 68 | cursor.execute("INSERT INTO employee VALUES ('Mary', 22)") 69 | cursor.execute('SELECT * FROM employee ORDER BY age') 70 | assert cursor.fetchall() == [('Mary', 22), ('John', 32)] 71 | ``` 72 | 73 | or in unit tests: 74 | ```python 75 | import unittest 76 | import psycopg2 77 | from pyembedpg import PyEmbedPg 78 | 79 | 80 | class TestPyEmbedPgWithContext(unittest.TestCase): 81 | def setUp(self): 82 | self.postgres = PyEmbedPg('9.4.0').start(15432) 83 | 84 | def test_simple_run(self): 85 | self.postgres.create_user('scott', 'tiger') 86 | self.postgres.create_database('testdb', 'scott') 87 | 88 | # Postgres is initialized, now run some queries 89 | with psycopg2.connect(database='postgres', user='scott', password='tiger', port=self.postgres.running_port) as conn: 90 | with conn.cursor() as cursor: 91 | cursor.execute('CREATE TABLE employee(name VARCHAR(32), age INT)') 92 | cursor.execute("INSERT INTO employee VALUES ('John', 32)") 93 | cursor.execute("INSERT INTO employee VALUES ('Mary', 22)") 94 | cursor.execute('SELECT * FROM employee ORDER BY age') 95 | assert cursor.fetchall() == [('Mary', 22), ('John', 32)] 96 | 97 | def tearDown(self): 98 | self.postgres.shutdown() 99 | ``` 100 | 101 | You can also specify a list of ports to use (e.g., `PyEmbedPg('9.4.0').start(range(15432, 15440))`). The first available port will be used. This can be useful when you run multiple tests in parallel. 102 | 103 | ### Troubleshooting 104 | 105 | #### Error while building postgres 106 | 107 | Check that gcc and readline (e.g., libreadline-dev on Debian, readline on MacOS) are installed. 108 | 109 | ### Alternatives 110 | Alternatively, there is another project [testing.postgres](https://github.com/tk0miya/testing.postgresql) that allows to use the current Postgres application 111 | installed on the server and initialize a new instance. 112 | 113 | ### Contributors 114 | 115 | * Lukmanul Hakim [@kimkimkeren](https://github.com/kimkimkeren) 116 | -------------------------------------------------------------------------------- /pyembedpg.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Simulmedia, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | import logging 18 | import os 19 | import re 20 | import shutil 21 | import socket 22 | import tarfile 23 | import tempfile 24 | import time 25 | from contextlib import closing 26 | from distutils import spawn 27 | from os.path import expanduser 28 | from subprocess import Popen 29 | 30 | import psycopg2 31 | import requests 32 | from psycopg2._psycopg import OperationalError 33 | from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT 34 | 35 | logger = logging.getLogger('pyembedpg') 36 | 37 | 38 | class PyEmbedPg(object): 39 | 40 | DOWNLOAD_BASE_URL = 'http://ftp.postgresql.org/pub/source' 41 | DOWNLOAD_URL = DOWNLOAD_BASE_URL + '/v{version}/postgresql-{version}.tar.bz2' 42 | LOCAL_VERSION = 'local' 43 | 44 | CACHE_DIRECTORY = '.pyembedpg' 45 | 46 | def __init__(self, version=None): 47 | """ 48 | Initialize a new Postgres object 49 | :param version: version to use. If it is not set, use the latest version in .pyembedpg directory. If not present 50 | use the latest version remotely. Use 'local' to use the local postgres version installed on the machine 51 | :return: 52 | """ 53 | home_dir = expanduser("~") 54 | self._cache_dir = os.path.join(home_dir, PyEmbedPg.CACHE_DIRECTORY) 55 | 56 | # if version is not specified, check local last version otherwise get last remote version 57 | self.version = version 58 | if not self.version: 59 | self.version = self.get_latest_local_version() 60 | if not self.version: 61 | self.version = self.get_latest_remote_version() 62 | 63 | if version == PyEmbedPg.LOCAL_VERSION: 64 | full_path = spawn.find_executable('postgres') 65 | if not full_path: 66 | raise PyEmbedPgException('Cannot find postgres executable. Make sure it is in your path') 67 | self._version_path = os.path.dirname(full_path) 68 | else: 69 | self._version_path = os.path.join(self._cache_dir, self.version) 70 | 71 | def get_latest_local_version(self): 72 | """ 73 | Return the latest version installed in the cache 74 | :return: latest version installed locally in the cache and None if there is nothing downloaded 75 | """ 76 | 77 | if not os.path.exists(self._cache_dir): 78 | return None 79 | 80 | tags = os.listdir(self._cache_dir) 81 | # we want to sort based on numbers so: 82 | # v3.0.0-QA6 83 | # v3.0.0-QA15 84 | # v3.0.0-QA2 85 | # are sorted according to the numbers so no lexigraphically 86 | revs_to_tag = [(re.split(r"[^\d]+", tag), tag) for tag in tags] 87 | return max(revs_to_tag)[1] 88 | 89 | def get_latest_remote_version(self): 90 | """ 91 | Return the latest version on the Postgres FTP server 92 | :return: latest version installed locally on the Postgres FTP server 93 | """ 94 | response = requests.get(PyEmbedPg.DOWNLOAD_BASE_URL) 95 | last_version_match = list(re.finditer('>v(?P[^<]+)<', response.content.decode()))[-1] 96 | return last_version_match.group('version') 97 | 98 | def check_version_present(self): 99 | """ 100 | Check if the version is present in the cache 101 | :return: True if the version has already been downloaded and build, False otherwise 102 | """ 103 | return os.path.exists(self._version_path) 104 | 105 | def download_and_unpack(self): 106 | # if the version we want to download already exists, do not do anything 107 | if self.check_version_present(): 108 | logger.debug('Version {version} already present in cache'.format(version=self.version)) 109 | return 110 | 111 | url = PyEmbedPg.DOWNLOAD_URL.format(version=self.version) 112 | response = requests.get(url, stream=True) 113 | 114 | if not response.ok: 115 | raise PyEmbedPgException('Cannot download file {url}. Error: {error}'.format(url=url, error=response.content)) 116 | 117 | with tempfile.NamedTemporaryFile() as fd: 118 | logger.debug('Downloading {url}'.format(url=url)) 119 | for block in response.iter_content(chunk_size=4096): 120 | fd.write(block) 121 | fd.flush() 122 | # Unpack the file into temporary dir 123 | temp_dir = tempfile.mkdtemp() 124 | source_dir = os.path.join(temp_dir, 'postgresql-{version}'.format(version=self.version)) 125 | try: 126 | # Can't use with context directly because of python 2.6 127 | with closing(tarfile.open(fd.name)) as tar: 128 | tar.extractall(temp_dir) 129 | os.system( 130 | 'sh -c "cd {path} && ./configure --prefix={target_dir} && make install && cd contrib && make install"'.format( 131 | path=source_dir, 132 | target_dir=self._version_path) 133 | ) 134 | finally: 135 | shutil.rmtree(temp_dir, ignore_errors=True) 136 | 137 | def start(self, port=5432): 138 | """ 139 | Start a new Postgres server on the specified port 140 | :param port: port to connect to, can be an int or a list of ports 141 | :return: 142 | """ 143 | if not self.check_version_present(): 144 | self.download_and_unpack() 145 | 146 | bin_dir = os.path.join(self._version_path, 'bin') 147 | ports = [port] if isinstance(port, int) else port 148 | return DatabaseRunner(bin_dir, ports) 149 | 150 | 151 | class DatabaseRunner(object): 152 | ADMIN_USER = 'root' 153 | TIMEOUT = 10 154 | 155 | def __init__(self, bin_dir, ports): 156 | self._ports = ports 157 | self._postgres_cmd = os.path.join(bin_dir, 'postgres') 158 | 159 | # init db 160 | init_db = os.path.join(bin_dir, 'initdb') 161 | self._temp_dir = tempfile.mkdtemp() 162 | command = init_db + ' -D ' + self._temp_dir + ' -U ' + DatabaseRunner.ADMIN_USER 163 | logger.debug('Running command: {command}'.format(command=command)) 164 | os.system(command) 165 | 166 | # overwrite pg_hba.conf to only allow local access with password authentication 167 | with open(os.path.join(self._temp_dir, 'pg_hba.conf'), 'w') as fd: 168 | fd.write( 169 | '# TYPE DATABASE USER ADDRESS METHOD\n' 170 | '# "local" is for Unix domain socket connections only\n' 171 | 'local all {admin} trust\n' 172 | 'local all all md5\n' 173 | 'host all {admin} 127.0.0.1/32 trust\n' 174 | 'host all all 127.0.0.1/32 md5\n' 175 | '# IPv6 local connections:\n' 176 | 'host all {admin} ::1/128 trust\n' 177 | 'host all all ::1/128 md5\n'.format(admin=DatabaseRunner.ADMIN_USER) 178 | ) 179 | 180 | def can_connect(port): 181 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 182 | return sock.connect_ex(('127.0.0.1', port)) != 0 183 | 184 | self.running_port = next((port for port in ports if can_connect(port)), None) 185 | if self.running_port is None: 186 | raise PyEmbedPgException('Cannot run postgres on any of these ports [{ports}]'.format(ports=', '.join((str(p) for p in ports)))) 187 | 188 | self.proc = Popen([self._postgres_cmd, '-D', self._temp_dir, '-p', str(self.running_port)]) 189 | logger.debug('Postgres started on port {port}...'.format(port=self.running_port)) 190 | 191 | # Loop until the server is started 192 | logger.debug('Waiting for Postgres to start...') 193 | start = time.time() 194 | while time.time() - start < DatabaseRunner.TIMEOUT: 195 | try: 196 | with psycopg2.connect(database='postgres', user=DatabaseRunner.ADMIN_USER, host='localhost', port=self.running_port): 197 | break 198 | except OperationalError: 199 | pass 200 | time.sleep(0.1) 201 | else: 202 | raise PyEmbedPgException('Cannot start postgres after {timeout} seconds'.format(timeout=DatabaseRunner.TIMEOUT)) 203 | 204 | def __enter__(self): 205 | return self 206 | 207 | def __exit__(self, exc_type, exc_val, exc_tb): 208 | self.shutdown() 209 | 210 | def create_user(self, username, password): 211 | """Create a user 212 | :param username: 213 | :type username: basestring 214 | :param password: 215 | :type password: basestring 216 | """ 217 | with psycopg2.connect(database='postgres', user=DatabaseRunner.ADMIN_USER, host='localhost', port=self.running_port) as conn: 218 | with conn.cursor() as cursor: 219 | cursor.execute("CREATE USER {username} WITH ENCRYPTED PASSWORD '{password}'".format(username=username, password=password)) 220 | 221 | def create_database(self, name, owner=None): 222 | """Create a new database 223 | :param name: database name 224 | :type name: basestring 225 | :param owner: username of the owner or None if unspecified 226 | :type owner: basestring 227 | """ 228 | with psycopg2.connect(database='postgres', user=DatabaseRunner.ADMIN_USER, host='localhost', port=self.running_port) as conn: 229 | conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) 230 | with conn.cursor() as cursor: 231 | sql = 'CREATE DATABASE {name} ' + ('WITH OWNER {owner}' if owner else '') 232 | cursor.execute(sql.format(name=name, owner=owner)) 233 | 234 | def shutdown(self): 235 | """ 236 | Shutdown postgres and remove the data directory 237 | """ 238 | # stop pg 239 | try: 240 | logger.debug('Killing postgres on port {port}'.format(port=self.running_port)) 241 | self.proc.kill() 242 | os.waitpid(self.proc.pid, 0) 243 | finally: 244 | logger.debug('Removing postgres data dir on {dir}'.format(dir=self._temp_dir)) 245 | # remove data directory 246 | shutil.rmtree(self._temp_dir, ignore_errors=True) 247 | 248 | 249 | class PyEmbedPgException(Exception): 250 | pass 251 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [flake8] 2 | ignore = 3 | max-line-length = 160 4 | statistics = True 5 | count = True 6 | 7 | [build_sphinx] 8 | source-dir = docs/source 9 | build-dir = docs/_build 10 | all_files = 1 11 | 12 | [upload_sphinx] 13 | upload-dir = docs/_build/html 14 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # 4 | # Copyright 2015 Simulmedia, Inc. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | import sys 20 | 21 | from setuptools import setup 22 | from setuptools.command.test import test as TestCommand 23 | 24 | 25 | class PyTestCommand(TestCommand): 26 | user_options = [('pytest-args=', 'a', "Arguments to pass to py.test")] 27 | 28 | def initialize_options(self): 29 | TestCommand.initialize_options(self) 30 | self.pytest_args = [] 31 | 32 | def finalize_options(self): 33 | TestCommand.finalize_options(self) 34 | self.test_args = [] 35 | self.test_suite = True 36 | 37 | def run_tests(self): 38 | import pytest 39 | errno = pytest.main(self.pytest_args) 40 | sys.exit(errno) 41 | 42 | 43 | setup( 44 | name='pyembedpg', 45 | version='0.0.5', 46 | description='Run embedded version of Postgres', 47 | long_description='Run embedded version of Postgres', 48 | keywords='postgres, python, tests', 49 | license='Apache License 2.0', 50 | author='Simulmedia', 51 | author_email='francois@simulmedia.com', 52 | url='http://github.com/simulmedia/pyembedpg/', 53 | packages=[''], 54 | classifiers=[ 55 | 'License :: OSI Approved :: Apache Software License', 56 | 'Topic :: Software Development :: Libraries :: Python Modules', 57 | 'Programming Language :: Python', 58 | 'Programming Language :: Python :: 3', 59 | 'Programming Language :: Python :: 3.3', 60 | 'Programming Language :: Python :: 3.4', 61 | 'Programming Language :: Python :: 3.5', 62 | 'Programming Language :: Python :: 3.6', 63 | 'Programming Language :: Python :: 3.7', 64 | ], 65 | install_requires=[ 66 | 'requests', 67 | 'psycopg2' 68 | ], 69 | tests_require=[ 70 | 'pytest' 71 | ], 72 | test_suite='tests', 73 | cmdclass={ 74 | 'test': PyTestCommand 75 | } 76 | ) 77 | -------------------------------------------------------------------------------- /tests/test_pyembedpg.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Simulmedia, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | import unittest 18 | 19 | import psycopg2 20 | 21 | from pyembedpg import PyEmbedPg 22 | 23 | 24 | class TestPyEmbedPg(unittest.TestCase): 25 | def setUp(self): 26 | self.port = 15433 27 | self.embedpg = PyEmbedPg('9.4.0') 28 | self.postgres = self.embedpg.start(self.port) 29 | self.postgres.create_user('scott', 'tiger') 30 | self.postgres.create_database('testdb', 'scott') 31 | 32 | def test_simple_run(self): 33 | # Postgres is initialized, now run some queries 34 | with psycopg2.connect(database='postgres', user='scott', password='tiger', host='localhost', port=self.port) as conn: 35 | with conn.cursor() as cursor: 36 | cursor.execute('CREATE TABLE employee(name VARCHAR(32), age INT)') 37 | cursor.execute("INSERT INTO employee VALUES ('John', 32)") 38 | cursor.execute("INSERT INTO employee VALUES ('Mary', 22)") 39 | cursor.execute('SELECT * FROM employee ORDER BY age') 40 | assert cursor.fetchall() == [('Mary', 22), ('John', 32)] 41 | 42 | # Test that the version is installed locally 43 | assert self.embedpg.get_latest_local_version() is not None 44 | 45 | def tearDown(self): 46 | self.postgres.shutdown() 47 | -------------------------------------------------------------------------------- /tests/test_pyembedpg_with_context.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Simulmedia, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | import re 18 | 19 | import psycopg2 20 | 21 | from pyembedpg import PyEmbedPg 22 | 23 | 24 | class TestPyEmbedPgWithContext(object): 25 | def setup(self): 26 | self.port = 15433 27 | 28 | def test_get_remote_version(self): 29 | pg = PyEmbedPg('test') 30 | last_version = pg.get_latest_remote_version() 31 | # can be 9.5.alpha1 32 | assert re.match(r'\d+\.[\w\.]+', last_version) 33 | 34 | def test_simple_run(self): 35 | pg = PyEmbedPg('9.4.0') 36 | with pg.start(self.port) as postgres: 37 | postgres.create_user('scott', 'tiger') 38 | postgres.create_database('testdb', 'scott') 39 | 40 | # Postgres is initialized, now run some queries 41 | with psycopg2.connect(database='postgres', user='scott', password='tiger', host='localhost', port=self.port) as conn: 42 | with conn.cursor() as cursor: 43 | cursor.execute('CREATE TABLE employee(name VARCHAR(32), age INT)') 44 | cursor.execute("INSERT INTO employee VALUES ('John', 32)") 45 | cursor.execute("INSERT INTO employee VALUES ('Mary', 22)") 46 | cursor.execute('SELECT * FROM employee ORDER BY age') 47 | assert cursor.fetchall() == [('Mary', 22), ('John', 32)] 48 | 49 | # Test that the version is installed locally 50 | assert pg.get_latest_local_version() is not None 51 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist = flake8, py{33,34,35,36,37} 3 | 4 | [testenv] 5 | passenv = TRAVIS TRAVIS_JOB_ID TRAVIS_BRANCH 6 | deps = 7 | pytest 8 | coveralls 9 | commands = 10 | coverage run --include=pyembedpg.py setup.py test 11 | coverage report -m 12 | coveralls 13 | 14 | [testenv:flake8] 15 | basepython = python 16 | deps = flake8 17 | commands = flake8 setup.py pyembedpg.py tests 18 | --------------------------------------------------------------------------------