From 6103885c0fbd9e2159f35d928000956d4e0cd9b0 Mon Sep 17 00:00:00 2001 From: apotek <279278+apotek@users.noreply.github.com> Date: Fri, 12 Jan 2024 14:13:11 -0500 Subject: [PATCH] Create first thorough tests of stable addon --- commands/db/dblog | 8 ++++-- tests/test.bats | 63 ++++++++++++++++++++++++++++------------------- 2 files changed, 44 insertions(+), 27 deletions(-) diff --git a/commands/db/dblog b/commands/db/dblog index cea4f56..1c01213 100755 --- a/commands/db/dblog +++ b/commands/db/dblog @@ -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 diff --git a/tests/test.bats b/tests/test.bats index f5715e6..75dd8b4 100644 --- a/tests/test.bats +++ b/tests/test.bats @@ -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" { @@ -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 } -