Skip to content

Commit 8f95cba

Browse files
committed
Update tests
1 parent 2c81de9 commit 8f95cba

File tree

9 files changed

+164
-76
lines changed

9 files changed

+164
-76
lines changed

tests/00-smoke-test.bats

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load test_functions.bash
2+
load standard_setup.bash
23

34
@test "smoke test - select 1" {
45
run docker run --rm \

tests/create-user-db.bats

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load test_functions.bash
2+
load standard_setup.bash
23

34
@test "create-user-db" {
45
run docker run --rm \
@@ -19,9 +20,3 @@ load test_functions.bash
1920
# check the output of the second last line
2021
[[ "${lines[-2]}" = " myuser" ]]
2122
}
22-
23-
# echo "===> Test create-user-db command"
24-
# docker run -d --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=password postgres:$POSTGRES_VERSION > /dev/null
25-
# sleep 5
26-
# docker run -t -i --name $TEST_NAME --link postgres $TEST_CONTAINER create-user-db foo foopass
27-
# cleanup postgres $TEST_NAME

tests/dind-runner.sh

Lines changed: 0 additions & 27 deletions
This file was deleted.

tests/drop-user-db.bats

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
1-
# echo "===> Test delete-user-db command"
2-
# docker run -d --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=password postgres:$POSTGRES_VERSION > /dev/null
3-
# sleep 5
4-
# docker run -t -i --name ${TEST_NAME}-create --link postgres $TEST_CONTAINER create-user-db foo
5-
# docker run -t -i --name ${TEST_NAME}-delete --link postgres $TEST_CONTAINER delete-user-db foo
6-
# cleanup postgres ${TEST_NAME}-create ${TEST_NAME}-delete
1+
load test_functions.bash
2+
load standard_setup.bash
3+
4+
@test "drop-user-db" {
5+
# create the user
6+
run docker run --rm \
7+
-e DATABASE_HOST=${postgres_container_ip} \
8+
-e DATABASE_USERNAME=postgres \
9+
-e DATABASE_PASSWORD=password \
10+
panubo/postgres-toolbox create-user-db myuser myuserpassword
11+
diag "${output}"
12+
[[ "${status}" -eq 0 ]]
13+
14+
# drop the user
15+
run docker run --rm \
16+
-e DATABASE_HOST=${postgres_container_ip} \
17+
-e DATABASE_USERNAME=postgres \
18+
-e DATABASE_PASSWORD=password \
19+
panubo/postgres-toolbox drop-user-db --drop-database myuser
20+
diag "${output}"
21+
[[ "${status}" -eq 0 ]]
22+
23+
# check the user and db do not exist
24+
run docker run --rm \
25+
-e DATABASE_HOST=${postgres_container_ip} \
26+
-e DATABASE_USERNAME=postgres \
27+
-e DATABASE_PASSWORD=password \
28+
panubo/postgres-toolbox psql -- -c 'SELECT count(*) FROM pg_database WHERE datname='"'myuser'"';'
29+
diag "${output}"
30+
[[ "${lines[-2]}" -eq "0" ]]
31+
}

tests/load.bats

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,78 @@
1-
# echo "===> Test load command"
2-
# docker run -d --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=password postgres:$POSTGRES_VERSION > /dev/null
3-
# sleep 5
4-
# docker run -t -i --name ${TEST_NAME}-create1 --link postgres $TEST_CONTAINER create-user-db foo
5-
# docker run -t -i --name ${TEST_NAME}-save --link postgres -e DUMP_DIR="/srv" -v /srv:/srv $TEST_CONTAINER save
6-
# docker run -t -i --name ${TEST_NAME}-delete --link postgres $TEST_CONTAINER delete-user-db foo
7-
# docker run -t -i --name ${TEST_NAME}-create2 --link postgres $TEST_CONTAINER create-user-db foo
8-
# docker run -t -i --name ${TEST_NAME}-load --link postgres -e DUMP_DIR="/srv" -v /srv:/srv $TEST_CONTAINER load foo
9-
# cleanup postgres ${TEST_NAME}-create1 ${TEST_NAME}-save ${TEST_NAME}-delete ${TEST_NAME}-load ${TEST_NAME}-create2
1+
load test_functions.bash
2+
3+
# Generate a sql dump for testing load function
4+
generate_testing_dump() {
5+
postgres_container="$(docker run -d -e POSTGRES_PASSWORD=password postgres:${POSTGRES_TARGET_VERSION})"
6+
postgres_container_ip="$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' ${postgres_container})"
7+
docker run --rm -e DATABASE_HOST=${postgres_container_ip} \
8+
-e DATABASE_USERNAME=postgres \
9+
-e DATABASE_PASSWORD=password \
10+
${TOOLBOX_IMAGE} bash -c '. /panubo-functions.sh; wait_postgres ${DATABASE_HOST}'
11+
12+
docker run --rm \
13+
-e DATABASE_HOST=${postgres_container_ip} \
14+
-e DATABASE_USERNAME=postgres \
15+
-e DATABASE_PASSWORD=password \
16+
"${TOOLBOX_IMAGE}" create-user-db mydb
17+
18+
docker exec "${postgres_container}" pgbench -U postgres -i -s 5 mydb
19+
20+
# The dump will be saved to this volume and used later
21+
export working_volume="$(docker volume create)"
22+
23+
docker run --rm \
24+
-e DATABASE_HOST=${postgres_container_ip} \
25+
-e DATABASE_USERNAME=postgres \
26+
-e DATABASE_PASSWORD=password \
27+
-v "${working_volume}:/db-dumps" \
28+
"${TOOLBOX_IMAGE}" save --format custom /db-dumps
29+
30+
docker rm -f "${postgres_container}"
31+
}
32+
33+
setup_file() {
34+
generate_testing_dump
35+
36+
export postgres_container="$(docker run -d -e POSTGRES_PASSWORD=password postgres:${POSTGRES_TARGET_VERSION})"
37+
export postgres_container_ip="$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' ${postgres_container})"
38+
docker run --rm -e DATABASE_HOST=${postgres_container_ip} \
39+
-e DATABASE_USERNAME=postgres \
40+
-e DATABASE_PASSWORD=password \
41+
${TOOLBOX_IMAGE} bash -c '. /panubo-functions.sh; wait_postgres ${DATABASE_HOST}'
42+
43+
docker run --rm \
44+
-e DATABASE_HOST=${postgres_container_ip} \
45+
-e DATABASE_USERNAME=postgres \
46+
-e DATABASE_PASSWORD=password \
47+
"${TOOLBOX_IMAGE}" create-user-db mydb
48+
}
49+
50+
teardown_file() {
51+
# teardown runs after each test
52+
docker rm -f "${postgres_container}"
53+
docker volume rm "${working_volume}"
54+
}
55+
56+
@test "load from disk" {
57+
run docker run --rm \
58+
-e DATABASE_HOST=${postgres_container_ip} \
59+
-e DATABASE_USERNAME=postgres \
60+
-e DATABASE_PASSWORD=password \
61+
-v "${working_volume}:/db-dumps" \
62+
"${TOOLBOX_IMAGE}" bash -c 'echo "*:5432:*:${DATABASE_USERNAME}:${DATABASE_PASSWORD}" > ${HOME}/.pgpass; \
63+
chmod 600 ${HOME}/.pgpass; \
64+
gunzip < /db-dumps/*/mydb.dump.gz | pg_restore --host "${DATABASE_HOST}" --username "${DATABASE_USERNAME}" --role mydb --dbname=mydb --no-acl --no-owner'
65+
diag "${output}"
66+
[[ "${status}" -eq 0 ]]
67+
68+
run docker run --rm \
69+
-e DATABASE_HOST=${postgres_container_ip} \
70+
-e DATABASE_USERNAME=postgres \
71+
-e DATABASE_PASSWORD=password \
72+
"${TOOLBOX_IMAGE}" psql -- -c 'SELECT pg_database_size('"'mydb'"');'
73+
diag "${output}"
74+
[[ "${status}" -eq 0 ]]
75+
76+
# we expect the dump to be larger than 80MiB
77+
[[ "${lines[-2]}" -gt "83886080" ]]
78+
}

tests/save.bats

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
1-
# echo "===> Test save command"
2-
# docker run -d --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=password postgres:$POSTGRES_VERSION > /dev/null
3-
# sleep 5
4-
# docker run -t -i --name ${TEST_NAME}-create --link postgres $TEST_CONTAINER create-user-db foo
5-
# docker run -t -i --name ${TEST_NAME}-save --link postgres -e DUMP_DIR="/srv" $TEST_CONTAINER save
6-
# cleanup postgres ${TEST_NAME}-create ${TEST_NAME}-save
1+
load test_functions.bash
2+
3+
setup_file() {
4+
export postgres_container="$(docker run -d -e POSTGRES_PASSWORD=password postgres:${POSTGRES_TARGET_VERSION})"
5+
export postgres_container_ip="$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' ${postgres_container})"
6+
docker run --rm -e DATABASE_HOST=${postgres_container_ip} \
7+
-e DATABASE_USERNAME=postgres \
8+
-e DATABASE_PASSWORD=password \
9+
${TOOLBOX_IMAGE} bash -c '. /panubo-functions.sh; wait_postgres ${DATABASE_HOST}'
10+
11+
docker run --rm \
12+
-e DATABASE_HOST=${postgres_container_ip} \
13+
-e DATABASE_USERNAME=postgres \
14+
-e DATABASE_PASSWORD=password \
15+
"${TOOLBOX_IMAGE}" create-user-db mydb
16+
17+
docker exec "${postgres_container}" pgbench -U postgres -i -s 5 mydb
18+
19+
export working_volume="$(docker volume create)"
20+
}
21+
22+
teardown_file() {
23+
# teardown runs after each test
24+
docker rm -f "${postgres_container}"
25+
docker volume rm "${working_volume}"
26+
}
27+
28+
@test "save to disk" {
29+
run docker run --rm \
30+
-e DATABASE_HOST=${postgres_container_ip} \
31+
-e DATABASE_USERNAME=postgres \
32+
-e DATABASE_PASSWORD=password \
33+
-v "${working_volume}:/db-dumps" \
34+
"${TOOLBOX_IMAGE}" save /db-dumps
35+
diag "${output}"
36+
[[ "${status}" -eq 0 ]]
37+
}

tests/standard_setup.bash

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
setup() {
2+
# setup runs before each test
3+
# Important: we aren't exposing port etc so running tests in parallel works
4+
postgres_container="$(docker run -d -e POSTGRES_PASSWORD=password postgres:${POSTGRES_TARGET_VERSION})"
5+
postgres_container_ip="$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' ${postgres_container})"
6+
docker run --rm -e DATABASE_HOST=${postgres_container_ip} \
7+
-e DATABASE_USERNAME=postgres \
8+
-e DATABASE_PASSWORD=password \
9+
${TOOLBOX_IMAGE} bash -c '. /panubo-functions.sh; wait_postgres ${DATABASE_HOST}'
10+
}
11+
12+
teardown() {
13+
# teardown runs after each test
14+
docker rm -f "${postgres_container}"
15+
}

tests/test_functions.bash

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,3 @@ POSTGRES_TARGET_VERSION="12.7"
44
diag() {
55
echo "$@" | sed -e 's/^/# /' >&3 ;
66
}
7-
8-
setup() {
9-
# setup runs before each test
10-
# Important: we aren't exposing port etc so running tests in parallel works
11-
postgres_container="$(docker run -d -e POSTGRES_PASSWORD=password postgres:${POSTGRES_TARGET_VERSION})"
12-
postgres_container_ip="$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' ${postgres_container})"
13-
docker run --rm -e DATABASE_HOST=${postgres_container_ip} \
14-
-e DATABASE_USERNAME=postgres \
15-
-e DATABASE_PASSWORD=password \
16-
${TOOLBOX_IMAGE} bash -c '. /panubo-functions.sh; wait_postgres ${DATABASE_HOST}'
17-
}
18-
19-
teardown() {
20-
# teardown runs after each test
21-
docker rm -f "${postgres_container}"
22-
}

tests/vaccumdb.bats

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load test_functions.bash
2+
load standard_setup.bash
23

34
@test "vacuum" {
45
run docker run --rm \
@@ -9,9 +10,3 @@ load test_functions.bash
910
diag "${output}"
1011
[[ "${status}" -eq 0 ]]
1112
}
12-
13-
# echo "===> Test vacuum command"
14-
# docker run -d --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=password postgres:$POSTGRES_VERSION > /dev/null
15-
# sleep 5
16-
# docker run -t -i --name $TEST_NAME --link postgres $TEST_CONTAINER vacuum
17-
# cleanup postgres $TEST_NAME

0 commit comments

Comments
 (0)