Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 650a7df

Browse files
committed
refactoring, fix tests, fix moron comments
1 parent 1f74dde commit 650a7df

File tree

4 files changed

+51
-52
lines changed

4 files changed

+51
-52
lines changed

run_tests.sh

+10-10
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@
55
set -eux
66

77
if [ "$PYTHON" == "python2" ]; then
8-
virtualenv="virtualenv --python=/usr/bin/python2"
9-
pip=pip2
8+
VIRTUALENV="virtualenv --python=/usr/bin/python2"
9+
PIP=pip2
1010
else
11-
virtualenv="virtualenv --python=/usr/bin/python3"
12-
pip=pip3
11+
VIRTUALENV="virtualenv --python=/usr/bin/python3"
12+
PIP=pip3
1313
fi
1414

1515
# prepare environment
16-
cd ..
17-
$virtualenv env
16+
VENV_PATH=/tmp/testgres_venv
17+
rm -rf $VENV_PATH
18+
$VIRTUALENV $VENV_PATH
1819
export VIRTUAL_ENV_DISABLE_PROMPT=1
19-
source env/bin/activate
20-
cd -
20+
source $VENV_PATH/bin/activate
2121

2222
# install utilities
23-
$pip install coverage flake8
23+
$PIP install coverage flake8
2424

2525
# install testgres' dependencies
2626
export PYTHONPATH=$(pwd)
27-
$pip install .
27+
$PIP install .
2828

2929
# test code quality
3030
flake8 .

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ description-file = README.md
33

44
[flake8]
55
ignore = E501, F401, F403, F841
6-
exclude = .git,__pycache__
6+
exclude = .git,__pycache__,env,venv

testgres/testgres.py

+31-31
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ def spawn_primary(self, name, destroy=True, use_logging=False):
363363
Create a primary node from a backup.
364364
365365
Args:
366-
name: name for a new node (str).
366+
name: name for a new node.
367367
destroy: should we convert this backup into a node?
368368
use_logging: enable python logging.
369369
@@ -392,7 +392,7 @@ def spawn_replica(self, name, destroy=True, use_logging=False):
392392
Create a replica of the original node from a backup.
393393
394394
Args:
395-
name: name for a new node (str).
395+
name: name for a new node.
396396
destroy: should we convert this backup into a node?
397397
use_logging: enable python logging.
398398
@@ -814,10 +814,10 @@ def psql(self, dbname, query=None, filename=None, username=None):
814814
Execute a query using psql.
815815
816816
Args:
817-
dbname: database name to connect to (str).
818-
query: query to be executed (str).
819-
filename: file with a query (str).
820-
username: database user name (str).
817+
dbname: database name to connect to.
818+
query: query to be executed.
819+
filename: file with a query.
820+
username: database user name.
821821
822822
Returns:
823823
A tuple of (code, stdout, stderr).
@@ -857,9 +857,9 @@ def safe_psql(self, dbname, query, username=None):
857857
Execute a query using psql.
858858
859859
Args:
860-
dbname: database name to connect to (str).
861-
query: query to be executed (str).
862-
username: database user name (str).
860+
dbname: database name to connect to.
861+
query: query to be executed.
862+
username: database user name.
863863
864864
Returns:
865865
psql's output as str.
@@ -875,8 +875,8 @@ def dump(self, dbname, filename=None):
875875
Dump database using pg_dump.
876876
877877
Args:
878-
dbname: database name to connect to (str).
879-
filename: output file (str).
878+
dbname: database name to connect to.
879+
filename: output file.
880880
881881
Returns:
882882
Path to file containing dump.
@@ -900,8 +900,8 @@ def restore(self, dbname, filename, username=None):
900900
Restore database from pg_dump's file.
901901
902902
Args:
903-
dbname: database name to connect to (str).
904-
filename: database dump taken by pg_dump (str).
903+
dbname: database name to connect to.
904+
filename: database dump taken by pg_dump.
905905
"""
906906

907907
self.psql(dbname=dbname, filename=filename, username=username)
@@ -919,9 +919,9 @@ def poll_query_until(self,
919919
Run a query once a second until it returs 'expected'.
920920
921921
Args:
922-
dbname: database name to connect to (str).
923-
query: query to be executed (str).
924-
username: database user name (str).
922+
dbname: database name to connect to.
923+
query: query to be executed.
924+
username: database user name.
925925
max_attempts: how many times should we try?
926926
sleep_time: how long should we sleep after a failure?
927927
expected: what should be returned to break the cycle?
@@ -970,9 +970,9 @@ def execute(self, dbname, query, username=None, commit=True):
970970
Execute a query and return all rows as list.
971971
972972
Args:
973-
dbname: database name to connect to (str).
974-
query: query to be executed (str).
975-
username: database user name (str).
973+
dbname: database name to connect to.
974+
query: query to be executed.
975+
username: database user name.
976976
commit: should we commit this query?
977977
978978
Returns:
@@ -990,7 +990,7 @@ def backup(self, username=None, xlog_method=DEFAULT_XLOG_METHOD):
990990
Perform pg_basebackup.
991991
992992
Args:
993-
username: database user name (str).
993+
username: database user name.
994994
xlog_method: a method for collecting the logs ('fetch' | 'stream').
995995
996996
Returns:
@@ -1008,8 +1008,8 @@ def replicate(self, name, username=None,
10081008
Create a replica of this node.
10091009
10101010
Args:
1011-
name: replica's name (str).
1012-
username: database user name (str).
1011+
name: replica's name.
1012+
username: database user name.
10131013
xlog_method: a method for collecting the logs ('fetch' | 'stream').
10141014
use_logging: enable python logging.
10151015
"""
@@ -1048,7 +1048,7 @@ def pgbench_init(self, dbname='postgres', scale=1, options=[]):
10481048
Prepare database for pgbench (create tables etc).
10491049
10501050
Args:
1051-
dbname: database name to connect to (str).
1051+
dbname: database name to connect to.
10521052
scale: report this scale factor in output (int).
10531053
options: additional options for pgbench (list).
10541054
@@ -1071,7 +1071,7 @@ def pgbench(self, dbname='postgres', stdout=None, stderr=None, options=[]):
10711071
Spawn a pgbench process.
10721072
10731073
Args:
1074-
dbname: database name to connect to (str).
1074+
dbname: database name to connect to.
10751075
stdout: stdout file to be used by Popen.
10761076
stderr: stderr file to be used by Popen.
10771077
options: additional options for pgbench (list).
@@ -1091,8 +1091,8 @@ def connect(self, dbname='postgres', username=None):
10911091
Connect to a database.
10921092
10931093
Args:
1094-
dbname: database name to connect to (str).
1095-
username: database user name (str).
1094+
dbname: database name to connect to.
1095+
username: database user name.
10961096
10971097
Returns:
10981098
An instance of NodeConnection.
@@ -1152,9 +1152,9 @@ def _execute_utility(util, args, logfile, write_to_pipe=True):
11521152
Execute utility (pg_ctl, pg_dump etc) using get_bin_path().
11531153
11541154
Args:
1155-
util: utility to be executed (str).
1155+
util: utility to be executed.
11561156
args: arguments for utility (list).
1157-
logfile: stores stdout and stderr (str).
1157+
logfile: path to file to store stdout and stderr.
11581158
write_to_pipe: do we care about stdout?
11591159
11601160
Returns:
@@ -1174,7 +1174,7 @@ def _execute_utility(util, args, logfile, write_to_pipe=True):
11741174

11751175
# get result
11761176
out, _ = process.communicate()
1177-
out = '' if not out else out.decode('utf-8')
1177+
out = '' if not out else six.text_type(out)
11781178

11791179
# write new log entry if possible
11801180
try:
@@ -1296,8 +1296,8 @@ def get_new_node(name, base_dir=None, use_logging=False):
12961296
Create a new node (select port automatically).
12971297
12981298
Args:
1299-
name: node's name (str).
1300-
base_dir: path to node's data directory (str).
1299+
name: node's name.
1300+
base_dir: path to node's data directory.
13011301
use_logging: should we use custom logger?
13021302
13031303
Returns:

tests/test_simple.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,6 @@ def test_poll_query_until(self):
336336
self.assertTrue(got_exception)
337337

338338
def test_logging(self):
339-
regex = re.compile('.+?LOG:.*')
340339
logfile = tempfile.NamedTemporaryFile('w', delete=True)
341340

342341
log_conf = {
@@ -362,17 +361,17 @@ def test_logging(self):
362361

363362
logging.config.dictConfig(log_conf)
364363

365-
with get_new_node('master', use_logging=True) as node0, \
366-
get_new_node('slave1', use_logging=True) as node1, \
367-
get_new_node('slave2', use_logging=True) as node2:
364+
with get_new_node('master', use_logging=True) as master:
365+
master.init().start()
368366

369-
node0.init().start()
370-
node1.init().start()
371-
node2.init().start()
367+
import time
368+
time.sleep(0.5)
372369

370+
# check that master's port is found
373371
with open(logfile.name, 'r') as log:
374-
for line in log:
375-
self.assertTrue(regex.match(line))
372+
lines = log.readlines()
373+
port = str(master.port)
374+
self.assertTrue(any(port in s for s in lines))
376375

377376
def test_pgbench(self):
378377
with get_new_node('node') as node:
@@ -403,7 +402,7 @@ def test_pg_ctl(self):
403402
node.init().start()
404403

405404
status = node.pg_ctl(['status'])
406-
self.assertTrue("server is running" in status)
405+
self.assertTrue('PID' in status)
407406

408407
def test_ports_management(self):
409408
# check that no ports have been bound yet

0 commit comments

Comments
 (0)