Skip to content

Commit

Permalink
Create first thorough tests of stable addon
Browse files Browse the repository at this point in the history
  • Loading branch information
apotek committed Jan 12, 2024
1 parent cf791d4 commit 6103885
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 27 deletions.
8 changes: 6 additions & 2 deletions commands/db/dblog
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ case "$@" in
off)
state=0
;;
ping)
echo "OK"
exit 0
;;
tail)
state=1
previous_state=$(mysql -u root -proot -e "SHOW VARIABLES LIKE 'general_log'\G;" | awk '/Value:/ { print $2 }')
trap ctrl_c INT
;;
*)
echo "Tell us whether you want the query log on or off, or if you want to tail it."
echo "Usage: querylog on|off|tail"
echo "Tell us whether you want the db query log on or off, or if you want to tail it."
echo "Usage: " $(basename $0) " on|off|tail"
exit 1
;;
esac
Expand Down
63 changes: 38 additions & 25 deletions tests/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,62 @@ setup() {
ddev start -y >/dev/null
}

capture_state() {
state=$(ddev mysql -u root -proot -e "show variables like 'general_log';" | grep general_log | cut -f 2)
echo "# dblog state: $state" >&3
}

health_checks() {
# Do something useful here that verifies the add-on
# ddev exec "curl -s elasticsearch:9200" | grep "${PROJNAME}-elasticsearch"
state=$(ddev mysql -u root -proot -e "show variables like 'general_log';" | grep general_log | cut -f 2)
echo "# Captured state is $state"
echo "# Running health checks..." >&3
ddev dblog ping | grep 'OK'
}

teardown() {
set -eu -o pipefail
cd ${TESTDIR} || ( printf "unable to cd to ${TESTDIR}\n" && exit 1 )
ddev delete -Oy ${PROJNAME} >/dev/null 2>&1
[ "${TESTDIR}" != "" ] && rm -rf ${TESTDIR}
}

toggle() {
echo "# Test toggle of dblog state..." >&3
capture_state
case "$state" in
OFF)
echo "# Turning dblog on..."
echo "# Turning dblog on..." >&3
ddev dblog on
on=$(ddev mysql -u root -proot -e "show variables like 'general_log';" | grep general_log | cut -f 2)
if [ "$on" != "ON"]; then
echo "Failed to enable db query log"
capture_state
echo "# dblog state is now $state" >&3
if [ "$state" != "ON"]; then
echo "Failed to enable db query log" >&3
failed_dblog_command
fi
;;
ON)
echo "# Turning dblog off..."
echo "# Turning dblog off..." >&3
ddev dblog off
off=$(ddev mysql -u root -proot -e "show variables like 'general_log';" | grep general_log | cut -f 2)
if [ "$off" != "OFF"]; then
echo "Failed to disable db query log"
capture_state
echo "# dblog state is now $state" >&3
if [ "$state" != "OFF"]; then
echo "Failed to disable db query log" >&3
failed_dblog_command
fi
;;
*)
echo "# Could not determine log state"
echo "# Unexpected log state: $state" >&3
failed_db_log_test
;;
esac
}

teardown() {
@test "install from release" {
set -eu -o pipefail
cd ${TESTDIR} || ( printf "unable to cd to ${TESTDIR}\n" && exit 1 )
ddev delete -Oy ${PROJNAME} >/dev/null 2>&1
[ "${TESTDIR}" != "" ] && rm -rf ${TESTDIR}
echo "# ddev get chromatichq/ddev-dblog with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3
ddev get chromatichq/ddev-dblog
ddev restart >/dev/null
health_checks
}

@test "install from directory" {
Expand All @@ -59,17 +79,10 @@ teardown() {
echo "# ddev get ${DIR} with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3
ddev get ${DIR}
ddev restart
echo "# Running health checks..."
health_checks
}

@test "install from release" {
set -eu -o pipefail
cd ${TESTDIR} || ( printf "unable to cd to ${TESTDIR}\n" && exit 1 )
echo "# ddev get chromatichq/ddev-dblog with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3
ddev get chromatichq/ddev-dblog
ddev restart #>/dev/null
echo "# Running health checks..."
health_checks
@test "toggle dblog state" {
toggle
toggle
}

0 comments on commit 6103885

Please sign in to comment.