Skip to content

Commit 6fc0811

Browse files
committed
ci: add testing workflow
1 parent 4504c61 commit 6fc0811

File tree

7 files changed

+174
-72
lines changed

7 files changed

+174
-72
lines changed

.github/workflows/testing.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Run tests
2+
on: [push, pull_request]
3+
defaults:
4+
run:
5+
shell: bash
6+
7+
jobs:
8+
test:
9+
runs-on: ubuntu-latest
10+
strategy:
11+
fail-fast: false
12+
matrix:
13+
pgversion: ['17', '16', '15', '14', '13', '12', '11', '10', '9.6']
14+
steps:
15+
- name: Setup repository
16+
uses: actions/checkout@v4
17+
- name: Setup NodeJS
18+
uses: actions/setup-node@v4
19+
with:
20+
node-version: '20.x'
21+
cache: 'npm'
22+
- name: Setup environment
23+
run: |
24+
sudo apt-get install -y build-essential wget gdb lldb libpq5
25+
npm install --save-dev
26+
- name: Build extension
27+
run: npm run compile
28+
- name: Setup sources
29+
run: ./src/test/setup.sh -j 4 --pg-version=${{ matrix.pgversion }}
30+
- name: Run tests
31+
run: ./src/test/test.sh --no-gui --pg-versions=${{ matrix.pgversion }}
32+
- name: Upload artifacts
33+
uses: actions/upload-artifact@v4
34+
if: failure()
35+
with:
36+
name: postgresql-logs-${{ matrix.pgversion }}
37+
retention-days: 3
38+
path: |
39+
pgsrc/${{ matrix.pgversion }}/data/postgresql.log
40+
src/test/log

src/test/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Example usage:
2828
./src/test/setup.sh --pg-version=17.4
2929
```
3030

31-
After that, source code, binaries and database will be installed in `./pgsrc`, `./pgsrc/build` and `./pgsrc/data` accordingly (starting from extension directory root).
31+
After that, source code, binaries and database will be installed in `./pgsrc/VERSION`, `./pgsrc/VERSION/build` and `./pgsrc/VERSION/data` accordingly (starting from extension directory root), where `VERSION` - is a major version of PostgreSQL.
3232

3333
To run tests use `./src/test/test.sh` script:
3434

src/test/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ source "$ENV_FILE"
5757
if [ "$RUN_DB" ]; then
5858
# Not 0 exit code can mean DB already running.
5959
# For tests this is valid
60-
pg_ctl start -o '-k ""' -l ./postgresql.log -w || true
60+
pg_ctl start -l "$PGDATA/postgresql.log" -w || true
6161
fi
6262

6363
if [ "$STOP_DB" ]; then

src/test/runTests.ts

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,40 @@ import { downloadAndUnzipVSCode,
55
resolveCliArgsFromVSCodeExecutablePath,
66
runTests } from '@vscode/test-electron';
77

8-
function getDebuggerExtensionId() {
9-
const debuggerType = process.env.PGHH_DEBUGGER;
10-
if (!debuggerType || debuggerType === 'cppdbg') {
8+
function getDebuggerExtensionId(debuggerType: string) {
9+
if (debuggerType === 'cppdbg') {
1110
return 'ms-vscode.cpptools';
12-
} else if (debuggerType == 'lldb') {
13-
return 'vadimcn.vscode-lldb';
1411
} else {
15-
throw new Error(`Unknown debugger type: ${debuggerType}`);
12+
return 'vadimcn.vscode-lldb';
1613
}
1714
}
1815

16+
function getPgsrcDir(extPath: string, version: string) {
17+
return path.join(extPath, 'pgsrc', version);
18+
}
19+
20+
function getTestEnv() {
21+
const pgVersion = process.env.PGHH_PG_VERSION;
22+
const debuggerType = process.env.PGHH_DEBUGGER ?? 'cppdbg';
23+
const vscodeVersion = process.env.PGHH_VSCODE_VERSION ?? 'stable';
24+
25+
if (!pgVersion) {
26+
throw new Error('PGHH_PG_VERSION env variable is not set');
27+
}
28+
29+
if (!debuggerType) {
30+
throw new Error('PGHH_DEBUGGER env variable is not set');
31+
}
32+
33+
if (!['cppdbg', 'lldb'].includes(debuggerType)) {
34+
throw new Error(`Debugger ${debuggerType} is not supported`);
35+
}
36+
37+
return {pgVersion, debuggerType, vscodeVersion};
38+
}
39+
1940
async function main() {
20-
let error = false;
21-
/*
41+
/*
2242
* The folder containing the Extension Manifest package.json.
2343
* Passed to `--extensionDevelopmentPath`
2444
*/
@@ -28,28 +48,29 @@ async function main() {
2848
* Passed to --extensionTestsPath
2949
*/
3050
const extensionTestsPath = path.resolve(__dirname, './suite/index');
51+
52+
const testEnv = getTestEnv();
53+
3154
/*
3255
* The path to source code of PostgreSQL we are testing now
3356
*/
34-
const pgsrcDir = path.resolve(extensionDevelopmentPath, './pgsrc');
57+
const pgsrcDir = getPgsrcDir(extensionDevelopmentPath, testEnv.pgVersion);
58+
const vscodeExecutablePath = await downloadAndUnzipVSCode(testEnv.vscodeVersion);
59+
const [cliPath, ...args] = resolveCliArgsFromVSCodeExecutablePath(vscodeExecutablePath);
3560

61+
/* Install required debugger extension */
62+
const dbgExtId = getDebuggerExtensionId(testEnv.debuggerType);
63+
cp.spawnSync(cliPath, [...args, '--install-extension', dbgExtId],
64+
{ encoding: 'utf-8', stdio: 'inherit'});
65+
66+
/* Run PostgreSQL */
67+
cp.spawnSync('/bin/bash', ['./run.sh', '--run'], {
68+
cwd: pgsrcDir,
69+
stdio: 'inherit',
70+
encoding: 'utf-8',
71+
});
72+
3673
try {
37-
const vscodeVersion = process.env.PGHH_VSCODE_VERSION ?? 'stable';
38-
const vscodeExecutablePath = await downloadAndUnzipVSCode(vscodeVersion);
39-
const [cliPath, ...args] = resolveCliArgsFromVSCodeExecutablePath(vscodeExecutablePath);
40-
41-
/* Install required debugger extension */
42-
const dbgExtId = getDebuggerExtensionId();
43-
cp.spawnSync(cliPath, [...args, '--install-extension', dbgExtId],
44-
{ encoding: 'utf-8', stdio: 'inherit'});
45-
46-
/* Run PostgreSQL */
47-
cp.spawnSync('/bin/bash', ['./run.sh', '--run'], {
48-
cwd: pgsrcDir,
49-
stdio: 'inherit',
50-
encoding: 'utf-8',
51-
});
52-
5374
/* Start tests */
5475
await runTests({
5576
extensionDevelopmentPath,
@@ -62,21 +83,19 @@ async function main() {
6283
'--enable-proposed-api', dbgExtId,
6384
],
6485
});
65-
} catch (err) {
66-
console.error(err);
67-
console.error('Failed to run tests');
68-
error = true;
69-
}
70-
71-
cp.spawnSync('/bin/bash', ['./run.sh', '--stop'], {
72-
cwd: pgsrcDir,
73-
stdio: 'inherit',
74-
encoding: 'utf-8',
75-
});
76-
77-
if (error) {
78-
process.exit(1);
86+
} finally {
87+
cp.spawnSync('/bin/bash', ['./run.sh', '--stop'], {
88+
cwd: pgsrcDir,
89+
stdio: 'inherit',
90+
encoding: 'utf-8',
91+
});
7992
}
8093
}
8194

82-
main();
95+
try {
96+
main();
97+
} catch (err) {
98+
console.error(err);
99+
console.error('Failed to run tests');
100+
process.exit(1);
101+
}

src/test/setup.sh

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ function print_help {
44
cat <<EOM
55
Setup environment for PostgreSQL Hacker Helper extension testing.
66
This script downloads specified PostgreSQL version (source code), applies patch, runs build and setups database (initdb + initial schema).
7-
Source code is installed to EXT_SRC/pgsrc, where EXT_SRC - root directory of extension.
7+
Source code is installed to EXT_SRC/pgsrc/PG_VERSION, where EXT_SRC - root directory of extension and PG_VERSION - major PostgreSQL version.
88
99
Usage: $0 --pg-version=17
1010
1111
Options:
1212
-h, --help Print this help message
1313
--pg-version Major version of PostgreSQL to install.
1414
--threads Number of threads to use during build
15+
--force Remove old installation if it exists.
1516
1617
Supported PG versions from 17 to 9.6 inclusive.
1718
@@ -21,17 +22,21 @@ Example:
2122
EOM
2223
}
2324

24-
ARG_PG_VERSION=""
25+
MAJOR_PG_VERSION=""
2526
THREADS="1"
27+
FORCE=""
2628
while [ "$1" ]; do
2729
ARG="$1"
2830
case "$ARG" in
2931
--pg-version=*)
30-
ARG_PG_VERSION="${ARG#*=}"
32+
MAJOR_PG_VERSION="${ARG#*=}"
3133
;;
3234
--threads=*)
3335
THREADS="${ARG#*=}"
3436
;;
37+
--force)
38+
FORCE="1"
39+
;;
3540
-j)
3641
shift
3742
THREADS="$1"
@@ -51,13 +56,13 @@ done
5156
# Exit on error
5257
set -e -o pipefail
5358

54-
if [[ -z "$ARG_PG_VERSION" ]]; then
59+
if [[ -z "$MAJOR_PG_VERSION" ]]; then
5560
echo "--pg-version is not set - specify PostgreSQL version"
5661
exit 1
5762
fi
5863

5964
PG_VERSION=""
60-
case "$ARG_PG_VERSION" in
65+
case "$MAJOR_PG_VERSION" in
6166
'17')
6267
PG_VERSION='17.4'
6368
;;
@@ -86,7 +91,7 @@ case "$ARG_PG_VERSION" in
8691
PG_VERSION='9.6.24'
8792
;;
8893
*)
89-
echo "Version $ARG_PG_VERSION is not supported"
94+
echo "Version $MAJOR_PG_VERSION is not supported"
9095
echo "Supported version from 17 to 9.6 inclusive"
9196
exit 1
9297
;;
@@ -96,28 +101,37 @@ esac
96101
cd "$(dirname ${BASH_SOURCE[0]:-$0})/../.."
97102
EXT_ROOT_DIR="$PWD"
98103

99-
CFLAGS="-O0 -g $CFLAGS"
100-
CPPFLAGS="-O0 -g $CPPFLAGS"
101104
PATCH_FILE="$EXT_ROOT_DIR/src/test/patches/pg${PG_VERSION}.patch"
102105
PG_SRC_DOWNLOAD_URL="https://ftp.postgresql.org/pub/source/v${PG_VERSION}/postgresql-${PG_VERSION}.tar.gz"
103-
CACHEDIR="$PWD/src/test/cache"
106+
CACHEDIR="$EXT_ROOT_DIR/src/test/cache"
104107
TARFILE="$CACHEDIR/postgresql-${PG_VERSION}.tar.gz"
105-
SRC_PATH="$EXT_ROOT_DIR/pgsrc"
106-
INSTALL_PATH="$EXT_ROOT_DIR/pgsrc/build"
107-
LOGDIR="$PWD/src/test/log"
108+
SRC_PATH="$EXT_ROOT_DIR/pgsrc/$MAJOR_PG_VERSION"
109+
INSTALL_PATH="$SRC_PATH/build"
110+
LOGDIR="$EXT_ROOT_DIR/src/test/log"
108111
LOGFILE="$LOGDIR/setup_$(date +%Y%m%d%H%M).log"
112+
113+
set -e -o pipefail
109114
mkdir -p "$LOGDIR"
110115

111-
# Download PostgreSQL source code into it's directory and apply patch
112116
{
113-
set -e -o pipefail
114-
rm -rf "$SRC_PATH"
117+
118+
if [[ -d "$SRC_PATH" ]]; then
119+
if [[ -z "$FORCE" ]]; then
120+
echo "Installation already exists. Use --force to remove old src dir"
121+
exit 0
122+
else
123+
rm -rf "$SRC_PATH"
124+
fi
125+
fi
126+
127+
128+
# Download PostgreSQL source code into it's directory and apply patch
115129
mkdir -p "$SRC_PATH"
116130
mkdir -p "$CACHEDIR"
117131
if [[ ! -f "$TARFILE" ]]; then
118-
wget "$PG_SRC_DOWNLOAD_URL" -O "$TARFILE"
132+
wget -q "$PG_SRC_DOWNLOAD_URL" -O "$TARFILE"
119133
fi
120-
tar -xvzf "$TARFILE" -C "$SRC_PATH" --strip-components=1
134+
tar -xvzf "$TARFILE" -C "$SRC_PATH" --strip-components=1 1>/dev/null
121135
cd "$SRC_PATH"
122136
patch -p1 -i "$PATCH_FILE"
123137

@@ -127,15 +141,29 @@ patch -p1 -i "$PATCH_FILE"
127141
--enable-debug \
128142
--enable-cassert \
129143
--without-openssl \
130-
CFLAGS="$CFLAGS" \
131-
CPPFLAGS="$CPPFLAGS"
144+
--without-readline \
145+
--without-python \
146+
--without-tcl \
147+
--without-pam \
148+
--without-selinux \
149+
--without-icu \
150+
--without-ldap \
151+
--without-libxml \
152+
--without-libxslt \
153+
--without-bonjour \
154+
--without-lz4 \
155+
--without-zstd \
156+
--without-llvm \
157+
--without-zlib \
158+
CFLAGS="-O0 -g3 $CFLAGS" \
159+
CPPFLAGS="-O0 -g3 $CPPFLAGS"
132160

133161
# Setup special file with
134162
ENV_PATH="${PWD}/env.sh"
135163
cat <<EOF >"$ENV_PATH"
136164
export PGINSTDIR="$INSTALL_PATH"
137165
export PGDATA="$SRC_PATH/data"
138-
export PGHOST="localhost"
166+
export PGHOST="$SRC_PATH/data"
139167
export PGPORT="5432"
140168
export PGUSER="postgres"
141169
export PGDATABASE="postgres"
@@ -153,7 +181,14 @@ make install-world-bin
153181

154182
# Create database and setup schema
155183
initdb -U "$PGUSER"
156-
pg_ctl start -l ./postgresql.log -o '-k ""' -w
184+
cat <<EOF >>"$PGDATA/postgresql.conf"
185+
unix_socket_directories='$PGDATA'
186+
listen_addresses=''
187+
port=5432
188+
log_min_messages=DEBUG1
189+
EOF
190+
191+
pg_ctl start -l "$PGDATA/postgresql.log" -w
157192
psql -c "CREATE TABLE t1(x int, y int);"
158193
psql -c "CREATE TABLE t2(x int, y int);"
159194
pg_ctl stop -w

0 commit comments

Comments
 (0)