Skip to content

Commit 56ebe67

Browse files
committed
postgresql-pre-start hook example and test - run 'initdb' from 'assemble', and bake the datadir into image - install hook which extracts the tarball when data is not initialized Fixes: sclorg#220
1 parent 6d49b8b commit 56ebe67

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#! /usr/bin/bash -x
2+
3+
# fail early
4+
set -e
5+
6+
# source the convenience tooling
7+
source "${CONTAINER_SCRIPTS_PATH}/common.sh"
8+
9+
# set $PGDATA variable
10+
set_pgdata
11+
12+
# assert uninitialized data
13+
test ! -f "$PGDATA/postgresql.conf"
14+
15+
# empty config file is needed after 'initialize_database' call
16+
touch "$POSTGRESQL_CONFIG_FILE"
17+
18+
initialize_database
19+
20+
# start local PostgreSQL server (wait with '-w')
21+
pg_ctl -w start -o "-h ''"
22+
23+
# load all sql files
24+
shopt -s nullglob
25+
for file in /tmp/src/init/*.sql; do
26+
psql -f "$file"
27+
done
28+
29+
pg_ctl stop
30+
31+
# dump the data into $PWD (in-image storage)
32+
tar caf data.tar.xz -C "$PGDATA" .
33+
rm -rf "$PGDATA"
34+
35+
# install pre-start hook
36+
cp -r /tmp/src/postgresql-pre-start .

examples/s2i-dump-data/init/init.sql

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CREATE TABLE test (sth TEXT);
2+
INSERT INTO test VALUES ('hello world');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
if test ! -f "$PGDATA/postgresql.conf"; then
2+
tar xf "$APP_DATA"/src/data.tar.xz -C "$PGDATA"
3+
fi

test/run_test

+20
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ run_master_restart_test
2626
run_doc_test
2727
run_s2i_test
2828
run_test_cfg_hook
29+
run_s2i_bake_data_test
2930
"
3031

3132
test $# -eq 1 -a "${1-}" == --list && echo "$TEST_LIST" && exit 0
@@ -707,6 +708,25 @@ run_test_cfg_hook()
707708
assert_runtime_option "$name-3" shared_buffers 111MB
708709
}
709710

711+
run_s2i_bake_data_test ()
712+
{
713+
local s2i_image_name="$IMAGE_NAME-bake_$(ct_random_string)"
714+
s2i build "file://$test_dir/../examples/s2i-dump-data" "${IMAGE_NAME}" "$s2i_image_name"
715+
ct_path_append images_to_clean "$s2i_image_name"
716+
717+
local container_name=bake-data-test
718+
719+
DOCKER_ARGS="-e POSTGRESQL_ADMIN_PASSWORD=password" \
720+
IMAGE_NAME="$s2i_image_name" create_container "$container_name"
721+
722+
wait_ready "$container_name"
723+
724+
test "hello world" == "$(docker exec "$(get_cid "$container_name")" \
725+
bash -c "psql -tA -c 'SELECT * FROM test;'")"
726+
727+
docker stop "$(get_cid "$container_name")"
728+
}
729+
710730
function run_all_tests() {
711731
for test_case in $TEST_LIST; do
712732
: "Running test $test_case"

0 commit comments

Comments
 (0)