Skip to content

Commit 882582c

Browse files
committed
small fixes
1 parent 933c7dd commit 882582c

File tree

5 files changed

+46
-29
lines changed

5 files changed

+46
-29
lines changed

Dockerfile.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ WORKDIR /pg/testgres
1818
RUN chown -R postgres:postgres /pg
1919

2020
USER postgres
21-
ENTRYPOINT PYTHON=${PYTHON} /run.sh
21+
ENTRYPOINT PYTHON_VERSION=${PYTHON_VERSION} /run.sh

README.md

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,31 @@ python my_tests.py
3939

4040
### Logging
4141

42-
By default, `cleanup()` removes all temporary files (DB files, logs etc) that were created by testgres' API methods. If you'd like to keep logs, execute `configure_testgres(node_cleanup_full=False)` before running any tests.
42+
By default, `cleanup()` removes all temporary files (DB files, logs etc) that were created by testgres' API methods.
43+
If you'd like to keep logs, execute `configure_testgres(node_cleanup_full=False)` before running any tests.
4344

44-
> Note: context managers (aka `with`) call `cleanup()` automatically.
45+
> Note: context managers (aka `with`) call `stop()` and `cleanup()` automatically.
4546
46-
Nodes support python logging system, so if you have configured logging
47-
in your tests, you can use it to redirect postgres logs to yours.
48-
49-
To do that, just use `use_logging` argument:
47+
testgres supports [python logging](https://docs.python.org/3.6/library/logging.html),
48+
which means that you can aggregate logs from several nodes into one file:
5049

5150
```python
52-
node = testgres.get_new_node('master', use_logging=True)
53-
```
51+
import io
52+
import logging
53+
54+
# write everything to /tmp/testgres.log
55+
logfile = io.open('/tmp/testgres.log', 'w')
56+
logger = logging.getLogger('testgres')
57+
logger.FileHandler(logfile)
5458

55-
You can find working configuration example for logging in `tests/test_simple.py`.
59+
# create two different nodes with logging
60+
node1 = testgres.get_new_node('node1', use_logging=True).init().start()
61+
node2 = testgres.get_new_node('node2', use_logging=True).init().start()
62+
63+
# execute a few queries
64+
node1.execute('postgres', 'select 1')
65+
node2.execute('postgres', 'select 2')
66+
```
5667

5768

5869
### Examples
@@ -81,13 +92,16 @@ or
8192
with testgres.get_new_node('master', '/path/to/DB') as node:
8293
```
8394

84-
where `master` is a node's name, not a DB's name. Name matters if you're testing something like replication. Function `get_new_node()` only creates directory structure in specified directory (or somewhere in '/tmp' if we did not specify base directory) for cluster. After that, we have to initialize the PostgreSQL cluster:
95+
where `master` is a node's name, not a DB's name. Name matters if you're testing something like replication.
96+
Function `get_new_node()` only creates directory structure in specified directory (or somewhere in '/tmp' if
97+
we did not specify base directory) for cluster. After that, we have to initialize the PostgreSQL cluster:
8598

8699
```python
87100
node.init()
88101
```
89102

90-
This function runs `initdb` command and adds some basic configuration to `postgresql.conf` and `pg_hba.conf` files. Function `init()` accepts optional parameter `allows_streaming` which configures cluster for streaming replication (default is `False`).
103+
This function runs `initdb` command and adds some basic configuration to `postgresql.conf` and `pg_hba.conf` files.
104+
Function `init()` accepts optional parameter `allows_streaming` which configures cluster for streaming replication (default is `False`).
91105
Now we are ready to start:
92106

93107
```python
@@ -126,29 +140,33 @@ It's quite easy to create a backup and start a new replica:
126140
with testgres.get_new_node('master') as master:
127141
master.init().start()
128142
with master.backup() as backup:
143+
# create and start a new replica
129144
replica = backup.spawn_replica('replica').start()
130-
replica.catchup() # catch up with master
145+
146+
# catch up with master node
147+
replica.catchup()
148+
149+
# execute a dummy query
131150
print(replica.execute('postgres', 'select 1'))
132151
```
133152

134-
> Note: you could take a look at [`pg_pathman`](https://github.com/postgrespro/pg_pathman) to get an idea of `testgres`' capabilities.
135-
136153
### Benchmarks
137154

138155
`testgres` also can help you to make benchmarks using `pgbench` from postgres installation:
139156

140157
```python
141158
with testgres.get_new_node('master') as master:
142-
# start new node
159+
# start a new node
143160
master.init().start()
144161

145-
# initialize database for TPC-B
162+
# initialize default database for TPC-B
146163
master.pgbench_run(options=['-i'])
147164

148165
# run benchmark for 20 seconds and show results
149166
print(master.pgbench_run(options=['-T', '20']))
150167
```
151168

169+
152170
## Authors
153171

154172
[Ildar Musin](https://github.com/zilder) <i.musin(at)postgrespro.ru> Postgres Professional Ltd., Russia

run_tests.sh

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@
44

55
set -eux
66

7-
if [ "$PYTHON" == "python2" ]; then
8-
VIRTUALENV="virtualenv --python=/usr/bin/python2"
9-
PIP=pip2
10-
else
11-
VIRTUALENV="virtualenv --python=/usr/bin/python3"
12-
PIP=pip3
13-
fi
147

8+
# choose python version
9+
echo python version is $PYTHON_VERSION
10+
VIRTUALENV="virtualenv --python=/usr/bin/python$PYTHON_VERSION"
11+
PIP="pip$PYTHON_VERSION"
1512

16-
echo check that pg_config exists
13+
# fail early
14+
echo check that pg_config is in PATH
1715
command -v pg_config
1816

19-
2017
# prepare environment
2118
VENV_PATH=/tmp/testgres_venv
2219
rm -rf $VENV_PATH

testgres/node.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -876,5 +876,7 @@ def connect(self, dbname='postgres', username=None):
876876
An instance of NodeConnection.
877877
"""
878878

879-
return NodeConnection(
880-
parent_node=self, dbname=dbname, username=username)
879+
return NodeConnection(parent_node=self,
880+
host=self.host,
881+
dbname=dbname,
882+
username=username)

tests/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export PG_BIN=/path/to/pg/bin
2222
```bash
2323
# Set path to PostgreSQL and python version
2424
export PATH=$PATH:/path/to/pg/bin
25-
export PYTHON=3 # (or 2)
25+
export PYTHON_VERSION=3 # or 2
2626

2727
# Run tests
2828
./run_tests.sh

0 commit comments

Comments
 (0)