Skip to content

Commit 49a0154

Browse files
praiskuppkubatrh
authored andcommitted
tests: make sure that s2i test tests something
Previously the test have not tested that the backup user was created and can login. Also implement cleanup hook for the s2i image, which now has randomly generated name. Implemented "create tempfile" method with garbage collector.
1 parent 7d84aae commit 49a0154

File tree

2 files changed

+42
-26
lines changed

2 files changed

+42
-26
lines changed

Diff for: test/run_test

+34-22
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ test -n "${OS-}" || false 'make sure $OS is defined'
3535
CIDFILE_DIR=$(mktemp --suffix=postgresql_test_cidfiles -d)
3636

3737
volumes_to_clean=
38-
s2i_test_app_dir="$(mktemp -d)"
38+
images_to_clean=
39+
files_to_clean=
3940
test_dir="$(readlink -f "$(dirname "$0")")"
4041

4142
function cleanup() {
@@ -56,8 +57,8 @@ function cleanup() {
5657
rmdir $CIDFILE_DIR
5758

5859
ct_path_foreach "$volumes_to_clean" cleanup_volume_dir
59-
60-
test -d "$s2i_test_app_dir" && rm -rf "$s2i_test_app_dir"
60+
ct_path_foreach "$images_to_clean" cleanup_image
61+
ct_path_foreach "$files_to_clean" rm
6162
}
6263
trap cleanup EXIT
6364

@@ -74,6 +75,11 @@ cleanup_volume_dir ()
7475
rmdir "$1"
7576
}
7677

78+
cleanup_image ()
79+
{
80+
docker rmi -f "$1"
81+
}
82+
7783
function get_cid() {
7884
local id="$1" ; shift || return 1
7985
echo $(cat "$CIDFILE_DIR/$id")
@@ -150,6 +156,14 @@ create_volume_dir ()
150156
}
151157

152158

159+
create_temp_file ()
160+
{
161+
temp_file=`mktemp --tmpdir pg-testfile.XXXXX`
162+
setfacl -m u:26:rw- "$temp_file"
163+
ct_path_append files_to_clean "$temp_file"
164+
}
165+
166+
153167
function assert_login_access() {
154168
local PGUSER=$1 ; shift
155169
local PASS=$1 ; shift
@@ -563,9 +577,9 @@ run_doc_test() {
563577
echo
564578
}
565579

566-
_s2i_test_image() {
567-
local container_name="$1"
568-
local mount_opts="$2"
580+
test_the_app_image () {
581+
local container_name=$1
582+
local mount_opts=$2
569583
echo " Testing s2i app image with invalid configuration"
570584
assert_container_creation_fails -e POSTGRESQL_PASSWORD=pass -e POSTGRESQL_DATABASE=db
571585
echo " Testing s2i app image with correct configuration"
@@ -580,36 +594,34 @@ _s2i_test_image() {
580594
${mount_opts}
581595
" create_container "$container_name"
582596

583-
584-
# need to set these because `postgresql_cmd` relies on global variables
585-
PGUSER=user
586-
PASS=password
587-
588597
# need this to wait for the container to start up
589-
CONTAINER_IP=$(get_container_ip "${container_name}")
590-
test_connection "$container_name"
598+
PGUSER=user PASS=password test_connection "$container_name"
599+
PGUSER=backuser PASS=pass DB=backup test_connection "$container_name"
591600

592601
docker stop "$(get_cid $container_name)" >/dev/null
593602
}
594603

595604
run_s2i_test() {
605+
local temp_file
596606
echo " Testing s2i usage"
597607
s2i usage --pull-policy=never "${IMAGE_NAME}" &>/dev/null
598608

599609
echo " Testing s2i build"
600-
s2i build "file://${test_dir}/test-app" "${IMAGE_NAME}" "${IMAGE_NAME}-testapp"
601-
local image_name_backup="${IMAGE_NAME}"
602-
export IMAGE_NAME="${IMAGE_NAME}-testapp"
603610

604-
local container_name=s2i_config_build
605-
_s2i_test_image "s2i_config_build" ""
611+
local s2i_image_name=$IMAGE_NAME-testapp_$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 10 | head -n 1)
612+
ct_path_append images_to_clean "$s2i_image_name"
606613

607-
# return back original value for IMAGE_NAME
608-
export IMAGE_NAME="${image_name_backup}"
614+
s2i build "file://${test_dir}/test-app" "${IMAGE_NAME}" "$s2i_image_name"
615+
IMAGE_NAME=$s2i_image_name test_the_app_image s2i_config_build ""
609616

610617
echo " Testing s2i mount"
611-
cp -Lr "${test_dir}/test-app" "${s2i_test_app_dir}/"
612-
_s2i_test_image "_s2i_test_mount" "-v ${s2i_test_app_dir}/test-app:/opt/app-root/src/:z"
618+
create_temp_file
619+
cat "$test_dir"/test-app/postgresql-init/backup_user.sh >> "$temp_file"
620+
621+
# Test against original image, not the s2i one. But even if so, we expect
622+
# user mouns the directory under "s2i" direcetory $APP_DATA/src.
623+
local mount_point=/opt/app-root/src/postgresql-init/add_backup_user.sh
624+
test_the_app_image _s2i_test_mount "-v ${temp_file}:$mount_point:z,ro"
613625
echo " Success!"
614626
}
615627

Diff for: test/test-app/postgresql-init/backup_user.sh

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
[[ -v POSTGRESQL_BACKUP_USER && -v POSTGRESQL_BACKUP_PASSWORD ]] || usage "You have to set all variables for user for doing backup: POSTGRESQL_BACKUP_USER, POSTGRESQL_BACKUP_PASSWORD"
44

5-
# create backup user
6-
7-
psql -c "CREATE USER $POSTGRESQL_BACKUP_USER SUPERUSER password '$POSTGRESQL_BACKUP_PASSWORD';"
8-
psql -c "ALTER USER $POSTGRESQL_BACKUP_USER set default_transaction_read_only = on;"
5+
# create backup user with 'backup' database
6+
psql --variable=user="$POSTGRESQL_BACKUP_USER" \
7+
--variable=password="$POSTGRESQL_BACKUP_PASSWORD" \
8+
<<<"
9+
CREATE USER :user SUPERUSER password :'password';
10+
CREATE DATABASE backup OWNER = :user;
11+
ALTER USER :user set default_transaction_read_only = on;
12+
"

0 commit comments

Comments
 (0)