aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/lib
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lib')
-rw-r--r--scripts/lib/docker.sh60
-rw-r--r--scripts/lib/test.sh28
2 files changed, 65 insertions, 23 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