From 535145fc07aca013c119acd99d30f5ef35286e18 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 11 Oct 2025 09:06:00 -0400 Subject: Add --keepdb flag to tests script --- scripts/lib/docker.sh | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ scripts/lib/test.sh | 28 +++++------------------- scripts/test.sh | 59 ++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 110 insertions(+), 37 deletions(-) diff --git a/scripts/lib/docker.sh b/scripts/lib/docker.sh index 494ccbf..d2efaf5 100644 --- a/scripts/lib/docker.sh +++ b/scripts/lib/docker.sh @@ -185,3 +185,63 @@ docker_wait_for_healthy_container() { log_info "$(docker ps -a)" return 1 } + +# Ensure a database container is running and healthy +# +# DESCRIPTION: +# Checks if a container is already running and healthy. If keepdb is true +# and container is healthy, reuses it. Otherwise, stops any existing container +# and starts a fresh one. +# +# ARGUMENTS: +# $1: Container name +# $2: Database type (postgresql, mysql, mssql) +# $3: Keep database running (true/false) +# +# RETURN: +# 0 - Container is running and healthy +# 1 - Failed to start or wait for container +# +docker_ensure_container_running() { + local container_name="$1" + local db_type="$2" + local keepdb="$3" + + local container_status + container_status=$(docker inspect -f '{{.State.Health.Status}}' "$container_name" 2>/dev/null || echo "not_found") + + if [[ $keepdb == "true" && $container_status == "healthy" ]]; then + log_info "Reusing existing healthy container '$container_name'" + return 0 + fi + + # Stop existing container if it exists but isn't healthy + if [[ $container_status != "not_found" ]]; then + log_info "Removing existing container '$container_name'..." + docker_stop_container "$container_name" + fi + + log_step "Starting $container_name..." + + case "$db_type" in + "postgresql") + docker_start_postgresql "$container_name" + ;; + "mysql") + docker_start_mysql "$container_name" + ;; + "mssql") + docker_start_mssql "$container_name" + ;; + *) + log_error "Unknown database type: $db_type" + return 1 + ;; + esac + + if ! docker_wait_for_healthy_container "$container_name"; then + log_error "Failed to start $container_name container" + docker_stop_container "$container_name" + return 1 + fi +} diff --git a/scripts/lib/test.sh b/scripts/lib/test.sh index 65134d7..1cef37a 100644 --- a/scripts/lib/test.sh +++ b/scripts/lib/test.sh @@ -17,6 +17,7 @@ source "$(dirname "${BASH_SOURCE[0]}")/path.sh" # # ARGUMENTS: # $1: Name of the database test suite to run (e.g., "mssql", "postgresql", "sqlite3", or "mysql") +# $2: Keep database running (true/false, default: false) # # RETURN: # 0 - Tests passed successfully @@ -24,10 +25,11 @@ source "$(dirname "${BASH_SOURCE[0]}")/path.sh" # # EXAMPLES: # test_run_suite "mssql" -# test_run_suite "postgresql" +# test_run_suite "postgresql" true # test_run_suite() { local db_test_suite_name="$1" + local keepdb="${2:-false}" local container_name="bolt_${db_test_suite_name}_db" local coverage_file="${PATH_PROJECT_DIR}/coverage-${db_test_suite_name}.out" local test_result=0 @@ -35,27 +37,7 @@ test_run_suite() { log_header "$db_test_suite_name Tests" if [[ $db_test_suite_name != "sqlite3" ]]; then - log_step "Starting $container_name..." - - case "$db_test_suite_name" in - "postgresql") - docker_start_postgresql "$container_name" - ;; - "mysql") - docker_start_mysql "$container_name" - ;; - "mssql") - docker_start_mssql "$container_name" - ;; - *) - log_error "Unknown database test suite: $db_test_suite_name" - return 1 - ;; - esac - - if ! docker_wait_for_healthy_container "$container_name"; then - log_error "Failed to start $container_name container" - docker_stop_container "$container_name" + if ! docker_ensure_container_running "$container_name" "$db_test_suite_name" "$keepdb"; then return 1 fi fi @@ -66,7 +48,7 @@ test_run_suite() { local test_result=1 fi - if [[ $db_test_suite_name != "sqlite3" ]]; then + if [[ $db_test_suite_name != "sqlite3" && $keepdb == "false" ]]; then log_step "Cleaning up..." docker_stop_container "$container_name" || log_warn "Unable to clean up container" fi diff --git a/scripts/test.sh b/scripts/test.sh index 7937359..c31038f 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -5,39 +5,42 @@ source "$(dirname "${BASH_SOURCE[0]}")/lib/log.sh" source "$(dirname "${BASH_SOURCE[0]}")/lib/test.sh" usage() { - echo "Usage: $0 [TEST_SUITE]" + echo "Usage: $0 [OPTIONS] [TEST_SUITE]" echo "" echo "Run database test suites" echo "" + echo "Options:" + echo " --keepdb Keep database containers running after tests" + echo " Reuses existing containers if already running" + echo " Useful for rapid iteration when running tests repeatedly" + echo "" echo "Arguments:" - echo " TEST_SUITE Test suite to run (mssql, sqlite, postgres, mysql)" + echo " TEST_SUITE Test suite to run (mssql, sqlite3, postgresql, mysql)" echo " If not specified, runs all test suites" echo "" echo "Examples:" - echo " $0 mssql # Run only MSSQL tests" - echo " $0 # Run all test suites" + echo " $0 mssql # Run only MSSQL tests" + echo " $0 --keepdb mssql # Run MSSQL tests, keep container running" + echo " $0 --keepdb # Run all test suites, keep containers running" + echo " $0 # Run all test suites" } main() { - local suite="${1:-}" - if [[ $suite == "-h" || $suite == "--help" ]]; then - usage - exit 0 - fi + parse_args "$@" - if [[ -n "$suite" ]]; then - if ! test_is_valid_suite "$suite"; then - log_error "Invalid test suite: $suite" + if [[ -n "$SUITE" ]]; then + if ! test_is_valid_suite "$SUITE"; then + log_error "Invalid test suite: $SUITE" log_newline usage exit 1 fi - test_run_suite "$suite" + test_run_suite "$SUITE" "$KEEPDB" else log_header "Running all test suites" local failed_suites=() for suite in "${BOLT_TEST_SUITES[@]}"; do - if ! test_run_suite "$suite"; then + if ! test_run_suite "$suite" "$KEEPDB"; then failed_suites+=("$suite") fi done @@ -51,4 +54,32 @@ main() { fi } +parse_args() { + KEEPDB=false + SUITE="" + + while [[ $# -gt 0 ]]; do + case "$1" in + -h|--help) + usage + exit 0 + ;; + --keepdb) + KEEPDB=true + shift + ;; + -*) + log_error "Unknown option: $1" + log_newline + usage + exit 1 + ;; + *) + SUITE="$1" + shift + ;; + esac + done +} + main "$@" -- cgit v1.2.3