File: pqxx-fulltest

package info (click to toggle)
libpqxx 6.4.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 3,992 kB
  • sloc: cpp: 12,634; sh: 4,426; xml: 1,104; makefile: 309; python: 294; ansic: 3
file content (351 lines) | stat: -rwxr-xr-x 8,104 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#! /bin/sh -e

# Full test suite.  Run libpqxx tests against a rich sampling of available
# compilers, libpq versions, and backend versions.

TESTDIR="$HOME/tmp/libpqxxtest"
TMPDIR="$TESTDIR/tmp"
OPTIMIZING_COMPILERS="g++ clang++"

POSTGRES_VERSION_PATTERN="^v[91][0-9A-Za-z_.-]\+"
POSTGRES_FTP_SERVER="${PG_FTPSERVER:-ftp.postgresql.org}"
POSTGRES_FTP_DIR="${PG_FTPDIR:-pub/source}"

if which nproc >/dev/null
then
        PROCESSORS=$(nproc)
else
        PROCESSORS=2
fi

CONCURRENCY_LEVEL="${CONCURRENCY_LEVEL:-${PROCESSORS}}"

mkdir -p "$TMPDIR"

log_message() {
	echo "\n"
	date --iso-8601=second
	echo "*** $1 ***"
	echo
}


check_for_compilers() {
	local POTENTIAL_COMPILER 
	for POTENTIAL_COMPILER in $1
	do
		which "$POTENTIAL_COMPILER"
	done
}


get_tarball() {
	# Print full path of libpqxx tarball in current directory.
	echo "$(pwd)/libpqxx-$(cat VERSION).tar.gz"
}

build_tarball() {
	# Produce a lipqxx tarball.
	log_message "Creating tarball"
	rm -f -- "$(get_tarball)"
	env CXXFLAGS= ./autogen.sh
	make dist -j$CONCURRENCY_LEVEL
}


unpack_tarball() {
	# Unpack libpqxx tarball, set it up for building.
	tar xf "$1"
	echo "$(pwd)/$(ls -c -d -v libpqxx-* | tail -n1)"
}


find_backend_dir()  {
	local FBD_OLD_PWD
	FBD_OLD_PWD="$(pwd)"
	cd -- "$TESTDIR/pg/$1"
	pwd
	cd -- "$FBD_OLD_PWD"
}


locate_pidfile() {
	echo "$(find_backend_dir "$1")/postgres.pid"
}


start_backend() {
	# Start up a PostgreSQL backend.
	local ARG_BACKEND BACKEND_DIR SB_OLD_PWD
	log_message "Starting backend $1"
	ARG_BACKEND="$1"
	BACKEND_DIR="$(find_backend_dir "$ARG_BACKEND")"
	SB_OLD_PWD="$(pwd)"
	cd "$TESTDIR/pg"
	if test -f "$BACKEND_DIR/PQXX_SKIP"
	then
		echo "\n\nSkipping backend $ARG_BACKEND.\n"
		false
	else
		# Use postmaster, not postgres; "postgres" didn't support the
		# -h or -k options until version 8.2, and we need both.
		env PATH="$BACKEND_DIR/bin:$PATH:/sbin" \
			LD_LIBRARY_PATH="$BACKEND_DIR/lib" \
			PGDATA="$BACKEND_DIR/data" \
		start-stop-daemon -b -m -p "$(locate_pidfile "$ARG_BACKEND")" \
			--oknodo -S -x "$BACKEND_DIR/bin/postmaster" -- \
				-F -h '' -k "$BACKEND_DIR" \
				>"$BACKEND_DIR/backend.log" 2>&1
	fi
	cd "$SB_OLD_PWD"
}


stop_backend() {
	local BACKEND_PIDFILE
	log_message "Stopping backend $1"
	BACKEND_PIDFILE="$(locate_pidfile "$1")"
	if PATH="$PATH:/sbin" start-stop-daemon -p "$BACKEND_PIDFILE" -K
	then
		rm -- "$BACKEND_PIDFILE"
	fi
}


augment_postgres_config() {
	# Tweak configuration for backend $1.
	local BACKEND_CONFIG
	BACKEND_CONFIG="$1/data/postgresql.conf"
	if ! grep -- '^fsync = false' "$BACKEND_CONFIG"
	then
		log_message "Disabling fsync in $BACKEND_CONFIG"
		cat <<EOF >>"$BACKEND_CONFIG"
# Don't bother syncing to disk.
fsync = false
#unix_socket_directory = "$BACKEND_DIR"
EOF
	fi
}


build_postgres() {
	log_message "Building postgres for installation to $1"
	./configure --without-readline --prefix="$1"
	make -j$CONCURRENCY_LEVEL
}


create_database() {
	local PQXX_USER BACKEND_DIR
	PQXX_USER="$(whoami)"
	log_message "(Re-)creating database $PQXX_USER in $1"
	BACKEND_DIR="$(find_backend_dir "$1")"
	env PGHOST="$BACKEND_DIR" \
		LD_LIBRARY_PATH="$BACKEND_DIR/lib" \
		PATH="$BACKEND_DIR/bin:$PATH" \
	createdb "$PQXX_USER" || /bin/true
}

install_postgres() {
	# Download, build, and install a postgres version.
	local ARG_VERSION_NAME STRIPPED_VERSION BASE_NAME PG_TARBALL \
		DOWNLOAD_PATH PG_URL INSTALL_DIR IP_OLD_PWD
	ARG_VERSION_NAME="$1"
	STRIPPED_VERSION="$(echo "$ARG_VERSION_NAME" | sed -e 's/^v//')"
	BASE_NAME="postgresql-$STRIPPED_VERSION"
	PG_TARBALL="$BASE_NAME.tar.bz2"
	DOWNLOAD_PATH="$ARG_VERSION_NAME/$PG_TARBALL"
	PG_URL="ftp://$POSTGRES_FTP_SERVER/$POSTGRES_FTP_DIR/$DOWNLOAD_PATH"
	INSTALL_DIR="$TESTDIR/pg/$ARG_VERSION_NAME"
	IP_OLD_PWD="$(pwd)"
	log_message "Downloading $PG_TARBALL"

	if wget -q "$PG_URL"
	then
		tar xf "$PG_TARBALL"
		rm -f -- "$PG_TARBALL"
		cd -- "$BASE_NAME"
		if build_postgres "$INSTALL_DIR"
		then
			make install
			env PATH="$INSTALL_DIR/bin:$PATH" \
				LD_LIBRARY_PATH="$INSTALL_DIR/lib" \
				PGDATA="$INSTALL_DIR/data" \
			initdb >/dev/null
			augment_postgres_config "$INSTALL_DIR"
		fi
		cd ..
		rm -rf -- "./$BASE_NAME"
		cd -- "$IP_OLD_PWD"
	fi
}


find_upstream_postgreses() {
	lftp "$POSTGRES_FTP_SERVER" -e "cls -1 -B $POSTGRES_FTP_DIR ; exit" |
		grep -o "$POSTGRES_VERSION_PATTERN"
}


find_installed_postgreses() {
	local FIP_OLD_PWD SKIP
	FIP_OLD_PWD="$(pwd)"
	cd -- "$TESTDIR/pg"
	SKIP="$(find v* -maxdepth 1 -name PQXX_SKIP | sed -e 's|/.*||')"
	ls -1 | grep -o "$POSTGRES_VERSION_PATTERN" | grep -vFx "$SKIP"
	cd -- "$FIP_OLD_PWD"
}


stop_all_backends() {
	local SAB_BACKENDS STOPPABLE_BACKEND 
	SAB_BACKENDS=$(find_installed_postgreses)
	for STOPPABLE_BACKEND in $SAB_BACKENDS
	do
		stop_backend $STOPPABLE_BACKEND
	done
}


prepare_postgreses() {
	local PP_OLD_PWD DOWNLOADABLE AVAILABLE_BACKENDS PREPARE_BACKEND 
	PP_OLD_PWD="$(pwd)"
	for DOWNLOADABLE in $(find_upstream_postgreses)
	do
		if ! test -d "$TESTDIR/pg/$DOWNLOADABLE"
		then
			cd -- "$TESTDIR/tmp"
			install_postgres "$DOWNLOADABLE"
			cd -- "$PP_OLD_PWD"
		fi
	done

	stop_all_backends
	sleep 5
	AVAILABLE_BACKENDS=$(find_backends)
	for PREPARE_BACKEND in $AVAILABLE_BACKENDS
	do
		start_backend "$PREPARE_BACKEND"
		sleep 2
	done
	sleep 5
	for PREPARE_BACKEND in $AVAILABLE_BACKENDS
	do
		create_database "$PREPARE_BACKEND" || /bin/true
	done
}


find_historic_compilers() {
	local COMPILERS
	COMPILERS="$(find /usr/bin /usr/local/bin -name g++-[0-9].[0-9])"
	# TODO: Any way we can skip the one that g++ links to?
	echo ${COMPILERS:-c++}
}


test_against_compilers() {
	local COMPILER 
	log_message "Testing against compilers: $1"
	for COMPILER in $1
	do
		log_message "Testing against $COMPILER"
		env CXX=${COMPILER} ./configure \
			--enable-shared \
			--enable-maintainer-mode \
			--disable-documentation
		make check -j$CONCURRENCY_LEVEL || exit 1
	done
}


find_frontends() {
	# Find frontends to test against.
	find_installed_postgreses
}


find_backends() {
	# Find backends to test against.
	# Just sample the oldest & newest available major versions.
	find_installed_postgreses | head -n1
	find_installed_postgreses | tail -n1
}


test_against_postgreses() {
	local ARG_FRONTENDS ARG_BACKENDS TEST_FRONTEND TEST_FRONTEND_DIR \
		EXTENDED_PATH LIB_PATH TEST_BACKEND
	ARG_FRONTENDS=$1
	ARG_BACKENDS=$2
	for TEST_FRONTEND in $ARG_FRONTENDS
	do
		log_message "Building against frontend $TEST_FRONTEND"
		TEST_FRONTEND_DIR="$(find_backend_dir "$TEST_FRONTEND")"
		EXTENDED_PATH="$TEST_FRONTEND_DIR/bin:$PATH"
		LIB_PATH="$TEST_FRONTEND_DIR/lib"

		env PATH="$EXTENDED_PATH" LD_LIBRARY_PATH="$LIB_PATH" \
		./configure \
			--enable-shared \
			--enable-maintainer-mode \
			--disable-documentation

		env PATH="$EXTENDED_PATH" LD_LIBRARY_PATH="$LIB_PATH" \
		make -j$CONCURRENCY_LEVEL

		for TEST_BACKEND in $ARG_BACKENDS
		do
			log_message "Testing against backend $TEST_BACKEND"
			TEST_BACKEND_DIR="$(find_backend_dir "$TEST_BACKEND")"
			env PGHOST="$TEST_BACKEND_DIR" \
				PATH="$EXTENDED_PATH" \
				LD_LIBRARY_PATH="$LIB_PATH" \
			make check -j$CONCURRENCY_LEVEL || exit 1
		done
	done
}


main() {
	local PQXX_TARBALL CURRENT_POSTGRES CURRENT_POSTGRES_DIR CURRENT_PATH \
		CURRENT_LIB_PATH
	build_tarball
	PQXX_TARBALL="$(get_tarball)"
	cd -- "$TESTDIR"
	rm -rf build
	mv -- "$(unpack_tarball "$PQXX_TARBALL")" build

	mkdir -p pg
	prepare_postgreses

	CURRENT_POSTGRES="$(find_backends | tail -n 1)"
	CURRENT_POSTGRES_DIR="$(find_backend_dir "$CURRENT_POSTGRES")"
	CURRENT_PATH="$CURRENT_POSTGRES_DIR/bin:$PATH"
	CURRENT_LIB_PATH="$CURRENT_POSTGRES_DIR/lib"

	cd -- "$TESTDIR/build"

	PGHOST="$CURRENT_POSTGRES_DIR"
	export PGHOST
	LD_LIBRARY_PATH="$CURRENT_POSTGRES_DIR/lib"
	export LD_LIBRARY_PATH
	PATH="$CURRENT_POSTGRES_DIR/bin:$PATH"
	export PATH

	test_against_compilers "$(check_for_compilers "$OPTIMIZING_COMPILERS")"

	CXXFLAGS=
	export CXXFLAGS

	test_against_compilers "$(find_historic_compilers)"

	test_against_postgreses "$(find_frontends)" "$(find_backends)"

	stop_all_backends
}


log_message "Starting full libpqxx test"
main
log_message "Done"