Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Switch python tests to testgres #16

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ env:
- PG_VERSION=10
- PG_VERSION=9.6 LEVEL=hardcore
- PG_VERSION=9.6
- PG_VERSION=10 LEVEL=stress

matrix:
allow_failures:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ FROM postgres:${PG_VERSION}-alpine
RUN apk add --no-cache \
openssl curl \
perl perl-ipc-run \
make musl-dev gcc bison flex coreutils \
make musl-dev gcc bison flex coreutils git\
zlib-dev libedit-dev \
clang clang-analyzer linux-headers \
python2 python2-dev py2-virtualenv;
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ DATA_built = $(EXTENSION)--$(EXTVERSION).sql
PGFILEDESC = "pg_query_state - facility to track progress of plan execution"

EXTRA_CLEAN = ./isolation_output $(EXTENSION)--$(EXTVERSION).sql \
Dockerfile ./tests/*.pyc
Dockerfile ./tests/*.pyc ./tmp_stress

ifdef USE_PGXS
PG_CONFIG ?= pg_config
Expand Down
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,15 @@ Done!
## Tests
Tests using parallel sessions using python 2.7 script:
```
export PG_CONFIG=/path/to/pg_config
python tests/pg_qs_test_runner.py [OPTION]...
```
*prerequisite packages*:
* `psycopg2` version 2.6 or later
* `testgres` version 1.8.2 or later
* `PyYAML` version 3.11 or later

*options*:
* *- -host* --- postgres server host, default value is *localhost*
* *- -port* --- postgres server port, default value is *5432*
* *- -database* --- database name, default value is *postgres*
* *- -user* --- user name, default value is *postgres*
* *- -password* --- user's password, default value is empty
* *--stress* --- run stress test using tpc-ds benchmark

## Function pg\_query\_state
```plpgsql
Expand Down
14 changes: 11 additions & 3 deletions run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# * scan-build
# * hardcore
# * nightmare
# * stress
#

set -ux
Expand Down Expand Up @@ -143,10 +144,17 @@ if [ -f regression.diffs ]; then cat regression.diffs; fi

# run python tests
set +x -e
virtualenv /tmp/env && source /tmp/env/bin/activate &&
pip install PyYAML && pip install psycopg2
virtualenv /tmp/env && source /tmp/env/bin/activate
pip install PyYAML
pip install psycopg2
pip install testgres
pip install progressbar2
set -e #exit virtualenv with error code
python tests/pg_qs_test_runner.py --port $PGPORT
if [ "$LEVEL" = "stress" ]; then
python tests/pg_qs_test_runner.py --stress
else
python tests/pg_qs_test_runner.py
fi
deactivate
set -x

Expand Down
69 changes: 25 additions & 44 deletions tests/pg_qs_test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@
'''

import argparse
import psycopg2
import sys
from test_cases import *

class PasswordPromptAction(argparse.Action):
def __call__(self, parser, args, values, option_string=None):
password = getpass.getpass()
setattr(args, self.dest, password)
import testgres

class SetupException(Exception): pass
class TeardownException(Exception): pass

setup_cmd = [
'drop extension if exists pg_query_state cascade',
Expand All @@ -25,17 +19,11 @@ class TeardownException(Exception): pass
'create table foo(c1 integer, c2 text)',
'create table bar(c1 integer, c2 boolean)',
'insert into foo select i, md5(random()::text) from generate_series(1, 1000000) as i',
'insert into bar select i, i%2=1 from generate_series(1, 500000) as i',
'insert into bar select i, i%%2=1 from generate_series(1, 500000) as i',
'analyze foo',
'analyze bar',
]

teardown_cmd = [
'drop table foo cascade',
'drop table bar cascade',
'drop extension pg_query_state cascade',
]

tests = [
test_deadlock,
test_simple_query,
Expand All @@ -50,55 +38,48 @@ class TeardownException(Exception): pass
test_insert_on_conflict,
]

def setup(con):
def setup(node):
''' Creates pg_query_state extension, creates tables for tests, fills it with data '''
print 'setting up...'
conn = testgres.connection.NodeConnection(node)
try:
cur = con.cursor()
for cmd in setup_cmd:
cur.execute(cmd)
con.commit()
cur.close()
conn.execute(cmd)
conn.commit()
conn.close()
except Exception, e:
raise SetupException('Setup failed: %s' % e)
print 'done!'

def teardown(con):
''' Drops table and extension '''
print 'tearing down...'
try:
cur = con.cursor()
for cmd in teardown_cmd:
cur.execute(cmd)
con.commit()
cur.close()
except Exception, e:
raise TeardownException('Teardown failed: %s' % e)
print 'done!'

def main(config):
def main(args):
''' Main test function '''
con = psycopg2.connect(**config)
setup(con)
node = testgres.get_new_node()
node.init()
node.append_conf("shared_preload_libraries='pg_query_state'\n")
node.start()
setup(node)

for i, test in enumerate(tests):
if test.__doc__:
descr = test.__doc__
else:
descr = 'test case %d' % (i+1)
print ("%s..." % descr),; sys.stdout.flush()
test(config)
test(node)
print 'ok!'

teardown(con)
con.close()
if args.stress:
print 'Start stress test'
stress_test(node)
print 'Start stress finished successfully'
print 'stop'

node.stop()
node.cleanup()

if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Query state of running backends tests')
parser.add_argument('--host', default='localhost', help='postgres server host')
parser.add_argument('--port', type=int, default=5432, help='postgres server port')
parser.add_argument('--user', dest='user', default='postgres', help='user name')
parser.add_argument('--database', dest='database', default='postgres', help='database name')
parser.add_argument('--password', dest='password', nargs=0, action=PasswordPromptAction, default='')
parser.add_argument('--stress', help='run stress test using tpc-ds benchmark',
action="store_true")
args = parser.parse_args()
main(args.__dict__)
main(args)
19 changes: 19 additions & 0 deletions tests/prepare_stress.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh
mkdir -p tmp_stress
cd tmp_stress
rm -rf ./*
git clone https://github.com/gregrahn/tpcds-kit.git
cd tpcds-kit/tools
make -s

#Generate data
./dsdgen -FORCE -VERBOSE
mkdir tables -p
#Prepare data
for i in `ls *.dat`; do
echo "Prepare file " $i
sed 's/|$//' $i > tables/$i
done
#Generate queries
./dsqgen -DIRECTORY ../query_templates -INPUT ../query_templates/templates.lst \
-VERBOSE Y -QUALIFY Y -DIALECT netezza
Loading