Skip to content

Commit 5630e84

Browse files
committed
Make tests more verbose
1 parent c9bbfae commit 5630e84

6 files changed

Lines changed: 124 additions & 46 deletions

File tree

tests/.lib.sh

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
set -u
5+
set -o pipefail
6+
7+
8+
###
9+
### Run
10+
###
11+
function run() {
12+
local cmd="${1}"
13+
local to_stdout=0
14+
15+
# Output to stdout instead?
16+
if [ "${#}" -eq "2" ]; then
17+
to_stdout="${2}"
18+
fi
19+
20+
local red="\033[0;31m"
21+
local green="\033[0;32m"
22+
local yellow="\033[0;33m"
23+
local reset="\033[0m"
24+
25+
if [ "${to_stdout}" -eq "0" ]; then
26+
printf "${yellow}[%s] ${red}%s \$ ${green}${cmd}${reset}\n" "$(hostname)" "$(whoami)" >&2
27+
else
28+
printf "${yellow}[%s] ${red}%s \$ ${green}${cmd}${reset}\n" "$(hostname)" "$(whoami)"
29+
fi
30+
31+
if sh -c "${cmd}"; then
32+
if [ "${to_stdout}" -eq "0" ]; then
33+
printf "${green}[%s]${reset}\n" "OK" >&2
34+
else
35+
printf "${green}[%s]${reset}\n" "OK"
36+
fi
37+
return 0
38+
else
39+
if [ "${to_stdout}" -eq "0" ]; then
40+
printf "${red}[%s]${reset}\n" "NO" >&2
41+
else
42+
printf "${red}[%s]${reset}\n" "NO"
43+
fi
44+
return 1
45+
fi
46+
}
47+
48+
49+
###
50+
### Run (must fail in order to succeed)
51+
###
52+
function run_fail() {
53+
local cmd="${1}"
54+
local to_stdout=0
55+
56+
# Output to stdout instead?
57+
if [ "${#}" -eq "2" ]; then
58+
to_stdout="${2}"
59+
fi
60+
61+
local red="\033[0;31m"
62+
local green="\033[0;32m"
63+
local yellow="\033[0;33m"
64+
local reset="\033[0m"
65+
66+
if [ "${to_stdout}" -eq "0" ]; then
67+
printf "${yellow}[%s] ${red}%s \$ ${yellow}[NOT] ${green}${cmd}${reset}\n" "$(hostname)" "$(whoami)" >&2
68+
else
69+
printf "${yellow}[%s] ${red}%s \$ ${yellow}[NOT] ${green}${cmd}${reset}\n" "$(hostname)" "$(whoami)"
70+
fi
71+
72+
if ! sh -c "${cmd}"; then
73+
if [ "${to_stdout}" -eq "0" ]; then
74+
printf "${green}[%s]${reset}\n" "OK" >&2
75+
else
76+
printf "${green}[%s]${reset}\n" "OK"
77+
fi
78+
return 0
79+
else
80+
if [ "${to_stdout}" -eq "0" ]; then
81+
printf "${red}[%s]${reset}\n" "NO" >&2
82+
else
83+
printf "${red}[%s]${reset}\n" "NO"
84+
fi
85+
return 1
86+
fi
87+
}

tests/00-entrypoint.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ IMAGE="devilbox/mysql"
99
TAG="${3}"
1010
#ARCH="${4}"
1111

12+
CWD="$(cd -P -- "$(dirname -- "$0")" && pwd -P)"
13+
# shellcheck disable=SC1090
14+
. "${CWD}/.lib.sh"
1215

1316
# Ensure they all have a common entrypoint
14-
if ! docker image inspect "${IMAGE}:${TAG}" \
17+
if ! run "docker image inspect ${IMAGE}:${TAG}" \
1518
| grep '"Entrypoint":' -A 1 \
1619
| grep -E '"/?docker-entrypoint.sh"'; then
1720
docker image inspect "${IMAGE}:${TAG}" | grep -i "entrypoint" || true

tests/01-version.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ VERSION="${2}"
99
TAG="${3}"
1010
ARCH="${4}"
1111

12+
CWD="$(cd -P -- "$(dirname -- "$0")" && pwd -P)"
13+
# shellcheck disable=SC1090
14+
. "${CWD}/.lib.sh"
15+
1216

1317
if [ "${NAME}" = "mysql" ]; then
14-
docker run --rm --platform "${ARCH}" "$(tty -s && echo "-it" || echo)" "${IMAGE}:${TAG}" -V | grep 'MySQL' | grep "${VERSION/./\\.}"
18+
run "docker run --rm --platform ${ARCH} $(tty -s && echo "-it" || echo) ${IMAGE}:${TAG} -V | grep 'MySQL' | grep '${VERSION/./\\.}'"
1519
elif [ "${NAME}" = "mariadb" ]; then
16-
docker run --rm --platform "${ARCH}" "$(tty -s && echo "-it" || echo)" "${IMAGE}:${TAG}" -V | grep 'MariaDB' | grep "${VERSION/./\\.}"
20+
run "docker run --rm --platform ${ARCH} $(tty -s && echo "-it" || echo) ${IMAGE}:${TAG} -V | grep 'MariaDB' | grep '${VERSION/./\\.}'"
1721
elif [ "${NAME}" = "percona" ]; then
18-
docker run --rm --platform "${ARCH}" "$(tty -s && echo "-it" || echo)" "${IMAGE}:${TAG}" -V | grep 'Percona' | grep "${VERSION/./\\.}"
22+
run "docker run --rm --platform ${ARCH} $(tty -s && echo "-it" || echo) ${IMAGE}:${TAG} -V | grep 'Percona' | grep '${VERSION/./\\.}'"
1923
else
2024
>&2 echo "[ERROR] Wrong type: ${NAME}"
2125
exit 1

tests/02-mysqli.sh

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,28 @@ TAG="${3}"
1010
ARCH="${4}"
1111
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
1212

13+
CWD="$(cd -P -- "$(dirname -- "$0")" && pwd -P)"
14+
# shellcheck disable=SC1090
15+
. "${CWD}/.lib.sh"
16+
1317

1418
# Start MySQL
1519
echo "1/5 Starting MySQL"
16-
docker run \
17-
-d \
18-
--platform "${ARCH}" \
19-
"$(tty -s && echo "-it" || echo)" \
20-
--rm \
21-
--hostname=mysql \
22-
--name devilbox-test-mysql \
23-
-e MYSQL_ALLOW_EMPTY_PASSWORD=yes \
24-
"${IMAGE}:${TAG}"
20+
run "docker run -d --rm --platform ${ARCH} $(tty -s && echo "-it" || echo) --hostname=mysql --name devilbox-test-mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=yes ${IMAGE}:${TAG}"
2521

2622
# Pull PHP image
2723
echo "2/5 Pulling PHP image "
28-
while ! docker pull php:7.2; do
24+
while ! run "docker pull php:7.2"; do
2925
sleep 1
3026
done
3127

3228
# Start PHP 7.2
3329
echo "3/5 Starting PHP"
34-
docker run \
35-
-d \
36-
--platform "${ARCH}" \
37-
"$(tty -s && echo "-it" || echo)" \
38-
--rm \
39-
--hostname=php \
40-
--name devilbox-test-php \
41-
--volume="${SCRIPTPATH}:/tmp" \
42-
--link devilbox-test-mysql php:7.2 sh -c 'sleep 9000'
30+
run "docker run -d --rm --platform ${ARCH} $(tty -s && echo "-it" || echo) --hostname=php --name devilbox-test-php -v ${SCRIPTPATH}:/tmp --link devilbox-test-mysql php:7.2 sh -c 'sleep 9000'"
4331

4432
# Install PHP mysqli module
4533
echo "4/5 Installing mysqli extension"
46-
if ! docker exec "$(tty -s && echo "-it" || echo)" devilbox-test-php sh -c 'docker-php-ext-install mysqli'; then
34+
if ! run "docker exec $(tty -s && echo "-it" || echo) devilbox-test-php sh -c 'docker-php-ext-install mysqli'"; then
4735
docker logs devilbox-test-php 2>/dev/null || true
4836
docker logs devilbox-test-mysql 2>/dev/null || true
4937
docker stop devilbox-test-php 2>/dev/null || true
@@ -58,8 +46,8 @@ fi
5846
# Test MySQL connectivity
5947
max=100
6048
i=0
61-
printf "5/5 Testing mysqli extension "
62-
while ! docker exec "$(tty -s && echo "-it" || echo)" devilbox-test-php php /tmp/mysql.php >/dev/null 2>&1; do
49+
echo "5/5 Testing mysqli extension"
50+
while ! run "docker exec $(tty -s && echo "-it" || echo) devilbox-test-php php /tmp/mysql.php >/dev/null 2>&1"; do
6351
printf "."
6452
sleep 1
6553
i=$(( i + 1))
@@ -80,10 +68,10 @@ while ! docker exec "$(tty -s && echo "-it" || echo)" devilbox-test-php php /tmp
8068
done
8169
printf "\\n"
8270

83-
docker stop devilbox-test-php 2>/dev/null || true
84-
docker stop devilbox-test-mysql 2>/dev/null || true
85-
docker kill devilbox-test-php 2>/dev/null || true
86-
docker kill devilbox-test-mysql 2>/dev/null || true
87-
docker rm -f devilbox-test-php 2>/dev/null || true
88-
docker rm -f devilbox-test-mysql 2>/dev/null || true
71+
run "docker stop devilbox-test-php" || true
72+
run "docker stop devilbox-test-mysql" || true
73+
run "docker kill devilbox-test-php" || true
74+
run "docker kill devilbox-test-mysql" || true
75+
run "docker rm -f devilbox-test-php" || true
76+
run "docker rm -f devilbox-test-mysql" || true
8977
echo "Success"

tests/03-config.sh

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ IMAGE="devilbox/mysql"
1010
TAG="${3}"
1111
ARCH="${4}"
1212

13+
CWD="$(cd -P -- "$(dirname -- "$0")" && pwd -P)"
14+
# shellcheck disable=SC1090
15+
. "${CWD}/.lib.sh"
16+
1317

1418
# Custom MySQL configuration
1519
CNF_DIR="$( mktemp -d )"
@@ -20,21 +24,12 @@ echo "[mysqld]" > "${CNF_DIR}/config.cnf"
2024
echo "${CNF_KEY} = ${CNF_VAL}" >> "${CNF_DIR}/config.cnf"
2125

2226
# Start MySQL
23-
docker run \
24-
-d \
25-
--platform "${ARCH}" \
26-
"$(tty -s && echo "-it" || echo)" \
27-
--rm \
28-
--hostname=mysql \
29-
--name devilbox-test-mysql \
30-
-e MYSQL_ALLOW_EMPTY_PASSWORD=yes \
31-
-v "${CNF_DIR}:/etc/mysql/docker-default.d" \
32-
"${IMAGE}:${TAG}"
27+
run "docker run -d --rm --platform ${ARCH} $(tty -s && echo "-it" || echo) --hostname=mysql --name devilbox-test-mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -v ${CNF_DIR}:/etc/mysql/docker-default.d ${IMAGE}:${TAG}"
3328

3429
# Test MySQL connectivity
3530
max=100
3631
i=0
37-
while ! docker exec "$(tty -s && echo "-it" || echo)" devilbox-test-mysql mysql -uroot --password="" -h 127.0.0.1 -e "SHOW VARIABLES LIKE '%${CNF_KEY}%';" | grep "${CNF_VAL}"; do
32+
while ! run "docker exec $(tty -s && echo "-it" || echo) devilbox-test-mysql mysql -uroot --password='' -h 127.0.0.1 -e \"SHOW VARIABLES LIKE '%${CNF_KEY}%';\" | grep '${CNF_VAL}'"; do
3833
sleep 1
3934
i=$(( i + 1))
4035
if [ "${i}" -ge "${max}" ]; then
@@ -48,8 +43,8 @@ while ! docker exec "$(tty -s && echo "-it" || echo)" devilbox-test-mysql mysql
4843
fi
4944
done
5045

51-
docker stop devilbox-test-mysql 2>/dev/null || true
52-
docker kill devilbox-test-mysql 2>/dev/null || true
53-
docker rm -f devilbox-test-mysql 2>/dev/null || true
46+
run "docker stop devilbox-test-mysql" || true
47+
run "docker kill devilbox-test-mysql" || true
48+
run "docker rm -f devilbox-test-mysql" || true
5449
rm -rf "${CNF_DIR}" || true
5550
echo "Success"

tests/tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ ARCH="${4}"
1212
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
1313

1414
for f in $( find "${SCRIPTPATH}" -name \*.sh | grep -E '[0-9]+' | sort -u ); do
15+
echo
1516
echo "# --------------------------------------------------------------------------------------------------"
1617
echo "# $( basename "${f}" )"
1718
echo "# --------------------------------------------------------------------------------------------------"

0 commit comments

Comments
 (0)