1+ #! /usr/bin/env bash
2+
3+ #
4+ # Copyright (c) 2018, Postgres Professional
5+ #
6+ # supported levels:
7+ # * standard
8+ # * scan-build
9+ # * hardcore
10+ # * nightmare
11+ #
12+
13+ set -ux
14+ status=0
15+
16+ # global exports
17+ export PGPORT=55435
18+ export VIRTUAL_ENV_DISABLE_PROMPT=1
19+
20+
21+ set -e
22+
23+ CUSTOM_PG_BIN=$PWD /pg_bin
24+ CUSTOM_PG_SRC=$PWD /postgresql
25+
26+ # here PG_VERSION is provided by postgres:X-alpine docker image
27+ curl " https://ftp.postgresql.org/pub/source/v$PG_VERSION /postgresql-$PG_VERSION .tar.bz2" -o postgresql.tar.bz2
28+ echo " $PG_SHA256 *postgresql.tar.bz2" | sha256sum -c -
29+
30+ mkdir $CUSTOM_PG_SRC
31+
32+ tar \
33+ --extract \
34+ --file postgresql.tar.bz2 \
35+ --directory $CUSTOM_PG_SRC \
36+ --strip-components 1
37+
38+ PQS_DIR=$( pwd)
39+ cd $CUSTOM_PG_SRC
40+
41+ # apply patches
42+ if [ " $( printf ' %s\n' " 10" " $PG_VERSION " | sort -V | head -n1) " = " $PG_VERSION " ]; then
43+ # patch version 9.6
44+ patch -p1 < $PQS_DIR /patches/custom_signals_${PG_VERSION% .* } .patch
45+ patch -p1 < $PQS_DIR /patches/runtime_explain.patch;
46+ elif [ " $( printf ' %s\n' " 11" " $PG_VERSION " | sort -V | head -n1) " = " $PG_VERSION " ]; then
47+ # patch version 10
48+ patch -p1 < $PQS_DIR /patches/custom_signals_${PG_VERSION% .* } .0.patch
49+ patch -p1 < $PQS_DIR /patches/runtime_explain.patch;
50+ else
51+ # patch version 11 and newer
52+ patch -p1 < $PQS_DIR /patches/custom_signals_${PG_VERSION% .* } .0.patch
53+ patch -p1 < $PQS_DIR /patches/runtime_explain_${PG_VERSION% .* } .0.patch;
54+ fi
55+
56+ # build and install PostgreSQL
57+ if [ " $LEVEL " = " hardcore" ] || \
58+ [ " $LEVEL " = " nightmare" ]; then
59+ # enable Valgrind support
60+ sed -i.bak " s/\/* #define USE_VALGRIND *\//#define USE_VALGRIND/g" src/include/pg_config_manual.h
61+
62+ # enable additional options
63+ ./configure \
64+ CFLAGS=' -Og -ggdb3 -fno-omit-frame-pointer' \
65+ --enable-cassert \
66+ --prefix=$CUSTOM_PG_BIN \
67+ --quiet
68+ else
69+ ./configure \
70+ --prefix=$CUSTOM_PG_BIN \
71+ --quiet
72+ fi
73+ time make -s -j$( nproc) && make -s install
74+
75+ # override default PostgreSQL instance
76+ export PATH=$CUSTOM_PG_BIN /bin:$PATH
77+ export LD_LIBRARY_PATH=$CUSTOM_PG_BIN /lib
78+
79+ # show pg_config path (just in case)
80+ which pg_config
81+
82+ cd -
83+
84+ set +e
85+
86+ # show pg_config just in case
87+ pg_config
88+
89+ # perform code checks if asked to
90+ if [ " $LEVEL " = " scan-build" ] || \
91+ [ " $LEVEL " = " hardcore" ] || \
92+ [ " $LEVEL " = " nightmare" ]; then
93+
94+ # perform static analyzis
95+ scan-build --status-bugs make USE_PGXS=1 || status=$?
96+
97+ # something's wrong, exit now!
98+ if [ $status -ne 0 ]; then exit 1; fi
99+
100+ fi
101+
102+ # don't forget to "make clean"
103+ make USE_PGXS=1 clean
104+
105+ # build and install extension (using PG_CPPFLAGS and SHLIB_LINK for gcov)
106+ make USE_PGXS=1 PG_CPPFLAGS=" -coverage" SHLIB_LINK=" -coverage"
107+ make USE_PGXS=1 install
108+
109+ # initialize database
110+ initdb -D $PGDATA
111+
112+ # change PG's config
113+ echo " port = $PGPORT " >> $PGDATA /postgresql.conf
114+ cat test.conf >> $PGDATA /postgresql.conf
115+
116+ # restart cluster 'test'
117+ if [ " $LEVEL " = " nightmare" ]; then
118+ ls $CUSTOM_PG_BIN /bin
119+
120+ valgrind \
121+ --tool=memcheck \
122+ --leak-check=no \
123+ --time-stamp=yes \
124+ --track-origins=yes \
125+ --trace-children=yes \
126+ --gen-suppressions=all \
127+ --suppressions=$CUSTOM_PG_SRC /src/tools/valgrind.supp \
128+ --log-file=/tmp/valgrind-%p.log \
129+ pg_ctl start -l /tmp/postgres.log -w || status=$?
130+ else
131+ pg_ctl start -l /tmp/postgres.log -w || status=$?
132+ fi
133+
134+ # something's wrong, exit now!
135+ if [ $status -ne 0 ]; then cat /tmp/postgres.log; exit 1; fi
136+
137+ # run regression tests
138+ export PG_REGRESS_DIFF_OPTS=" -w -U3" # for alpine's diff (BusyBox)
139+ make USE_PGXS=1 installcheck || status=$?
140+
141+ # show diff if it exists
142+ if [ -f regression.diffs ]; then cat regression.diffs; fi
143+
144+ # run python tests
145+ set +x
146+ virtualenv /tmp/env && source /tmp/env/bin/activate &&
147+ pip install PyYAML && pip install psycopg2 &&
148+ python tests/pg_qs_test_runner.py --port $PGPORT # --database db --user zloj
149+ deactivate
150+ set -x
151+
152+ # show Valgrind logs if necessary
153+ if [ " $LEVEL " = " nightmare" ]; then
154+ for f in $( find /tmp -name valgrind-* .log) ; do
155+ if grep -q ' Command: [^ ]*/postgres' $f && grep -q ' ERROR SUMMARY: [1-9]' $f ; then
156+ echo " ========= Contents of $f "
157+ cat $f
158+ status=1
159+ fi
160+ done
161+ fi
162+
163+ # something's wrong, exit now!
164+ if [ $status -ne 0 ]; then exit 1; fi
165+
166+ # generate *.gcov files
167+ gcov * .c * .h
168+
169+ set +ux
170+
171+ # send coverage stats to Codecov
172+ bash <( curl -s https://codecov.io/bash)
0 commit comments