aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/lib
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lib')
-rw-r--r--scripts/lib/docker.sh63
-rw-r--r--scripts/lib/test.sh51
2 files changed, 89 insertions, 25 deletions
diff --git a/scripts/lib/docker.sh b/scripts/lib/docker.sh
index d2efaf5..ae037e7 100644
--- a/scripts/lib/docker.sh
+++ b/scripts/lib/docker.sh
@@ -15,6 +15,32 @@ readonly BOLT_MYSQL_ROOT_PASSWORD="${MYSQL_ROOT_PASSWORD:-testpassword}"
readonly BOLT_MSSQL_SA_PASSWORD="${MSSQL_SA_PASSWORD:-TestPass123!}"
+# Parse database type and version from suite name
+#
+# DESCRIPTION:
+# Extracts base database type and version from suite names.
+# Handles both simple names (postgresql) and versioned names (postgresql-15).
+#
+# ARGUMENTS:
+# $1: Suite name (e.g., "postgresql", "postgresql-15", "mysql-8.0")
+#
+# OUTPUTS:
+# Prints "db_type version" to stdout (version is "latest" if not specified)
+#
+# EXAMPLES:
+# docker_parse_suite_name "postgresql" # outputs: postgresql latest
+# docker_parse_suite_name "postgresql-15" # outputs: postgresql 15
+#
+docker_parse_suite_name() {
+ local suite_name="$1"
+
+ if [[ "$suite_name" =~ ^([a-z0-9]+)-(.+)$ ]]; then
+ echo "${BASH_REMATCH[1]} ${BASH_REMATCH[2]}"
+ else
+ echo "$suite_name latest"
+ fi
+}
+
# Generate database DSN string for a given database type
#
# DESCRIPTION:
@@ -22,7 +48,7 @@ readonly BOLT_MSSQL_SA_PASSWORD="${MSSQL_SA_PASSWORD:-TestPass123!}"
# This ensures perfect synchronization between container setup and Go test configuration.
#
# ARGUMENTS:
-# $1: Database type (postgresql, mysql, mssql, sqlite3)
+# $1: Database suite name (postgresql, mysql, mssql, sqlite3, or versioned like postgresql-15)
#
# OUTPUTS:
# Prints the DSN string to stdout
@@ -32,7 +58,9 @@ readonly BOLT_MSSQL_SA_PASSWORD="${MSSQL_SA_PASSWORD:-TestPass123!}"
# 1 - Unknown database type
#
docker_generate_dsn() {
- local db_type="$1"
+ local suite_name="$1"
+ local db_type version
+ read -r db_type version <<< "$(docker_parse_suite_name "$suite_name")"
case "$db_type" in
"postgresql")
@@ -60,11 +88,13 @@ docker_generate_dsn() {
#
# ARGUMENTS:
# $1: Container name (default: bolt_postgresql_db)
+# $2: Version tag (default: latest)
#
docker_start_postgresql() {
local container_name="${1:-bolt_postgresql_db}"
+ local version="${2:-latest}"
- log_info "Starting PostgreSQL container '$container_name'..."
+ log_info "Starting PostgreSQL $version container '$container_name'..."
docker run --detach \
--name "$container_name" \
@@ -76,18 +106,20 @@ docker_start_postgresql() {
--env POSTGRES_DB="$BOLT_POSTGRES_DB" \
--env POSTGRES_USER="$BOLT_POSTGRES_USER" \
--env POSTGRES_PASSWORD="$BOLT_POSTGRES_PASSWORD" \
- postgres:18-alpine
+ "postgres:${version}"
}
# Start a MySQL database container
#
# ARGUMENTS:
# $1: Container name (default: bolt_mysql_db)
+# $2: Version tag (default: latest)
#
docker_start_mysql() {
local container_name="${1:-bolt_mysql_db}"
+ local version="${2:-latest}"
- log_info "Starting MySQL container '$container_name'..."
+ log_info "Starting MySQL $version container '$container_name'..."
docker run --detach \
--name "$container_name" \
@@ -100,18 +132,20 @@ docker_start_mysql() {
--env MYSQL_USER="$BOLT_MYSQL_USER" \
--env MYSQL_PASSWORD="$BOLT_MYSQL_PASSWORD" \
--env MYSQL_ROOT_PASSWORD="$BOLT_MYSQL_ROOT_PASSWORD" \
- mysql:9.4
+ "mysql:${version}"
}
# Start a MSSQL database container
#
# ARGUMENTS:
# $1: Container name (default: bolt_mssql_db)
+# $2: Version tag (default: latest)
#
docker_start_mssql() {
local container_name="${1:-bolt_mssql_db}"
+ local version="${2:-latest}"
- log_info "Starting MSSQL container '$container_name'..."
+ log_info "Starting MSSQL $version container '$container_name'..."
docker run --detach \
--name "$container_name" \
@@ -123,7 +157,7 @@ docker_start_mssql() {
--health-retries 10 \
--env ACCEPT_EULA=Y \
--env MSSQL_SA_PASSWORD="$BOLT_MSSQL_SA_PASSWORD" \
- mcr.microsoft.com/mssql/server:2022-latest
+ "mcr.microsoft.com/mssql/server:${version}"
}
# Stop and remove a database container
@@ -195,7 +229,7 @@ docker_wait_for_healthy_container() {
#
# ARGUMENTS:
# $1: Container name
-# $2: Database type (postgresql, mysql, mssql)
+# $2: Database suite name (postgresql, mysql, mssql, or versioned like postgresql-15)
# $3: Keep database running (true/false)
#
# RETURN:
@@ -204,9 +238,12 @@ docker_wait_for_healthy_container() {
#
docker_ensure_container_running() {
local container_name="$1"
- local db_type="$2"
+ local suite_name="$2"
local keepdb="$3"
+ local db_type version
+ read -r db_type version <<< "$(docker_parse_suite_name "$suite_name")"
+
local container_status
container_status=$(docker inspect -f '{{.State.Health.Status}}' "$container_name" 2>/dev/null || echo "not_found")
@@ -225,13 +262,13 @@ docker_ensure_container_running() {
case "$db_type" in
"postgresql")
- docker_start_postgresql "$container_name"
+ docker_start_postgresql "$container_name" "$version"
;;
"mysql")
- docker_start_mysql "$container_name"
+ docker_start_mysql "$container_name" "$version"
;;
"mssql")
- docker_start_mssql "$container_name"
+ docker_start_mssql "$container_name" "$version"
;;
*)
log_error "Unknown database type: $db_type"
diff --git a/scripts/lib/test.sh b/scripts/lib/test.sh
index 02dd6a8..accc9e4 100644
--- a/scripts/lib/test.sh
+++ b/scripts/lib/test.sh
@@ -14,9 +14,11 @@ source "$(dirname "${BASH_SOURCE[0]}")/path.sh"
# Starts database, waits for health checks, runs tests with
# environment variables, and cleans up afterward. Automatically handles
# database lifecycle and generates connection strings from Docker configuration.
+# Supports versioned suite names (e.g., "postgresql-15") for testing against
+# specific database versions.
#
# ARGUMENTS:
-# $1: Name of the database test suite to run (e.g., "mssql", "postgresql", "sqlite3", or "mysql")
+# $1: Name of the database test suite to run (e.g., "mssql", "postgresql", "postgresql-15", "sqlite3", "mysql", "mysql-8.0")
# $2: Keep database running (true/false, default: false)
#
# RETURN:
@@ -26,6 +28,8 @@ source "$(dirname "${BASH_SOURCE[0]}")/path.sh"
# EXAMPLES:
# test_run_suite "mssql"
# test_run_suite "postgresql" true
+# test_run_suite "postgresql-15" false
+# test_run_suite "mysql-8.0"
#
test_run_suite() {
local db_test_suite_name="$1"
@@ -34,9 +38,12 @@ test_run_suite() {
local coverage_file="${PATH_PROJECT_DIR}/coverage-${db_test_suite_name}.out"
local test_result=0
+ local db_type version
+ read -r db_type version <<< "$(docker_parse_suite_name "$db_test_suite_name")"
+
log_header "$db_test_suite_name Tests"
- if [[ $db_test_suite_name != "sqlite3" ]]; then
+ if [[ $db_type != "sqlite3" ]]; then
if ! docker_ensure_container_running "$container_name" "$db_test_suite_name" "$keepdb"; then
return 1
fi
@@ -48,7 +55,7 @@ test_run_suite() {
local test_result=1
fi
- if [[ $db_test_suite_name != "sqlite3" && $keepdb == "false" ]]; then
+ if [[ $db_type != "sqlite3" && $keepdb == "false" ]]; then
log_step "Cleaning up..."
docker_stop_container "$container_name" || log_warn "Unable to clean up container"
fi
@@ -58,16 +65,30 @@ test_run_suite() {
# Check if a test suite name is valid
#
+# DESCRIPTION:
+# Validates test suite names. Accepts both simple names (postgresql) and
+# versioned names (postgresql-15). Validates that the base database type
+# is in the list of supported test suites.
+#
# ARGUMENTS:
# $1: Test suite name to validate
#
# RETURN:
# 0 - Valid test suite name
# 1 - Invalid test suite name
+#
+# EXAMPLES:
+# test_is_valid_suite "postgresql" # valid
+# test_is_valid_suite "postgresql-15" # valid
+# test_is_valid_suite "invalid" # invalid
+#
test_is_valid_suite() {
local suite="$1"
+ local db_type version
+ read -r db_type version <<< "$(docker_parse_suite_name "$suite")"
+
for valid in "${BOLT_TEST_SUITES[@]}"; do
- [[ "$suite" == "$valid" ]] && return 0
+ [[ "$db_type" == "$valid" ]] && return 0
done
return 1
}
@@ -78,9 +99,11 @@ test_is_valid_suite() {
# Executes Go tests with optional build tags and coverage reporting.
# Tests are run with parallelism disabled (-p 1). For database tests,
# automatically generates connection strings from Docker configuration.
+# Supports versioned suite names (e.g., "postgresql-15") which use the
+# base database type for build tags.
#
# ARGUMENTS:
-# $1 - database_type: Database type (e.g., "mssql", "postgresql", "mysql", "sqlite3"). Defaults to empty string.
+# $1 - suite_name: Suite name (e.g., "mssql", "postgresql", "postgresql-15"). Defaults to empty string.
# $2 - coverage_file: Output file for coverage profile. Defaults to empty string.
#
# RETURN:
@@ -90,18 +113,22 @@ test_is_valid_suite() {
# EXAMPLES:
# test_run_go_tests
# test_run_go_tests "mssql" "coverage-mssql.out"
+# test_run_go_tests "postgresql-15" "coverage-postgresql-15.out"
#
test_run_go_tests() {
- local database_type="${1:-}"
+ local suite_name="${1:-}"
local coverage_file="${2:-}"
# NOTE: -p 1 is used because we want to run tests sequentially.
# This is because our tests depend on shared resources e.g. a database.
local cmd="go test -p 1"
- if [[ -n "$database_type" ]]; then
- cmd="$cmd -tags $database_type"
- log_step "Running tests with tags: $database_type"
+ local db_type=""
+ if [[ -n "$suite_name" ]]; then
+ local version
+ read -r db_type version <<< "$(docker_parse_suite_name "$suite_name")"
+ cmd="$cmd -tags $db_type"
+ log_step "Running tests with tags: $db_type"
else
log_step "Running tests"
fi
@@ -112,10 +139,10 @@ test_run_go_tests() {
cmd="$cmd ./..."
- if [[ -n "$database_type" ]]; then
+ if [[ -n "$suite_name" ]]; then
local dsn
- dsn=$(docker_generate_dsn "$database_type")
- cmd="BOLT_DB_DSN='$dsn' BOLT_DB_DRIVER='$database_type' BOLT_DB_MIGRATIONS_TABLE='bolt_migrations' $cmd"
+ dsn=$(docker_generate_dsn "$suite_name")
+ cmd="BOLT_DB_DSN='$dsn' BOLT_DB_DRIVER='$db_type' BOLT_DB_MIGRATIONS_TABLE='bolt_migrations' $cmd"
fi
if eval "$cmd"; then