Skip to content

Commit cefc1ae

Browse files
praiskuppkubatrh
authored andcommitted
simplify working with temporary volume directories
Instead of special-case the master-slave tests by (a) stopping the container manually, (b) removing cidfile and (c) remove data directory, from now the volume cleanup is "just" planned to be executed during regular container cleanup in 'cleanup()'. Related: container-common-scripts#37
1 parent 24a39b1 commit cefc1ae

File tree

6 files changed

+30
-28
lines changed

6 files changed

+30
-28
lines changed

Diff for: common

Submodule common updated 1 file

Diff for: test/test_lib renamed to test/pg-test-lib.sh

File renamed without changes.

Diff for: test/run_migration_test

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#! /bin/bash
22

33
set -e
4-
. test/test_lib
4+
. test/pg-test-lib.sh
55
ADMIN_PASSWORD=redhat
66

77
test $# -eq 2 || error "two args expected: $0 FROM TO"

Diff for: test/run_test

+26-25
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
set -exo nounset
1010
shopt -s nullglob
1111

12-
. test/test_lib
12+
# library from container-common-scripts
13+
. test/test-lib.sh
14+
15+
# local library
16+
. test/pg-test-lib.sh
1317

1418
TEST_LIST="\
1519
run_upgrade_test
@@ -29,6 +33,7 @@ test -n "${OS-}" || false 'make sure $OS is defined'
2933

3034
CIDFILE_DIR=$(mktemp --suffix=postgresql_test_cidfiles -d)
3135

36+
volumes_to_clean=
3237

3338
function cleanup() {
3439
for cidfile in $CIDFILE_DIR/* ; do
@@ -46,18 +51,22 @@ function cleanup() {
4651
echo "Done."
4752
done
4853
rmdir $CIDFILE_DIR
54+
55+
ct_path_foreach "$volumes_to_clean" cleanup_volume_dir
4956
}
5057
trap cleanup EXIT
5158

52-
function cleanup_volume() {
53-
local volume_dir="$1"; shift
54-
local cid="$1"; shift
55-
local volume_args="-v ${volume_dir}:/var/lib/pgsql/data:Z"
56-
57-
docker stop $cid
58-
local rm_cmd='/bin/rm -rf /var/lib/pgsql/data/userdata'
59-
docker run $volume_args --rm $IMAGE_NAME /bin/sh -c "$rm_cmd"
60-
rmdir ${volume_dir}
59+
cleanup_volume_dir ()
60+
{
61+
test ! -d "$1" && : "WARN: cleaned $1 for some reason" && return 0
62+
# When we run this test script as non-root (we should?), the PostgreSQL server
63+
# within container is still run under 'postgres' user. It means that, taking
64+
# into account 0077 umask of PostgreSQL server, we are unable to remove files
65+
# created by server. That's why we need to let docker escalate the privileges
66+
# again.
67+
local datadir=/var/lib/pgsql/data
68+
docker run -v "$1:$datadir:Z" --rm "$IMAGE_NAME" /bin/sh -c "/bin/rm -rf $datadir/userdata"
69+
rmdir "$1"
6170
}
6271

6372
function get_cid() {
@@ -127,13 +136,15 @@ function create_container() {
127136
echo "Created container $(cat $cidfile)"
128137
}
129138

130-
function create_volume() {
131-
local volume_dir
139+
140+
create_volume_dir ()
141+
{
132142
volume_dir=`mktemp -d --tmpdir pg-testdata.XXXXX`
133143
setfacl -m u:26:-wx "$volume_dir"
134-
echo "$volume_dir"
144+
ct_path_append volumes_to_clean "$volume_dir"
135145
}
136146

147+
137148
function assert_login_access() {
138149
local PGUSER=$1 ; shift
139150
local PASS=$1 ; shift
@@ -385,7 +396,7 @@ function run_master_restart_test() {
385396
local master_ip=
386397
local slave_cids=
387398

388-
local volume_dir=$(create_volume)
399+
create_volume_dir
389400
local master_args="-v ${volume_dir}:/var/lib/pgsql/data:Z"
390401

391402
# Setup the cluster
@@ -409,9 +420,6 @@ function run_master_restart_test() {
409420

410421
# Check if the replication works
411422
table_name="t1" test_value_replication
412-
413-
# Cleanup the volume
414-
cleanup_volume $volume_dir $(cat $cidfile)
415423
}
416424

417425
function run_replication_test() {
@@ -445,7 +453,7 @@ function run_change_password_test() {
445453
local password='password'
446454
local admin_password='adminPassword'
447455

448-
local volume_dir=$(create_volume)
456+
create_volume_dir
449457
local volume_options="-v ${volume_dir}:/var/lib/pgsql/data:Z"
450458

451459
DOCKER_ARGS="
@@ -495,13 +503,6 @@ $volume_options
495503
assert_login_access 'postgres' "NEW_${admin_password}" true
496504
assert_login_access 'postgres' ${admin_password} false
497505

498-
# When we run this test script as non-root (we should?), the PostgreSQL server
499-
# within container is still run under 'postgres' user. It means that, taking
500-
# into account 0077 umask of PostgreSQL server, we are unable to remove files
501-
# created by server. That's why we need to let docker escalate the privileges
502-
# again.
503-
cleanup_volume $volume_dir $(get_cid ${name})
504-
505506
echo " Success!"
506507
}
507508

Diff for: test/run_upgrade_test

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -e
44

5-
. test/test_lib
5+
. test/pg-test-lib.sh
66

77
ADMIN_PASSWORD=redhat
88

Diff for: test/test-lib.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../common/test-lib.sh

0 commit comments

Comments
 (0)