diff options
| -rw-r--r-- | .env.mssql | 3 | ||||
| -rw-r--r-- | .env.mysql | 3 | ||||
| -rw-r--r-- | .env.postgresql | 3 | ||||
| -rw-r--r-- | .env.sqlite3 | 3 | ||||
| -rw-r--r-- | scripts/lib/docker.sh | 92 | ||||
| -rw-r--r-- | scripts/lib/env.sh | 20 | ||||
| -rw-r--r-- | scripts/lib/test.sh | 66 |
7 files changed, 93 insertions, 97 deletions
diff --git a/.env.mssql b/.env.mssql deleted file mode 100644 index a479ed1..0000000 --- a/.env.mssql +++ /dev/null @@ -1,3 +0,0 @@ -BOLT_DB_DSN=sqlserver://sa:TestPass123%21@localhost:1433?database=master -BOLT_DB_DRIVER=mssql -BOLT_DB_MIGRATIONS_TABLE=bolt_migrations diff --git a/.env.mysql b/.env.mysql deleted file mode 100644 index 44782d6..0000000 --- a/.env.mysql +++ /dev/null @@ -1,3 +0,0 @@ -BOLT_DB_DSN='test_user:testpassword@tcp(localhost:3306)/test_db?parseTime=true' -BOLT_DB_DRIVER=mysql -BOLT_DB_MIGRATIONS_TABLE=bolt_migrations diff --git a/.env.postgresql b/.env.postgresql deleted file mode 100644 index aba5b61..0000000 --- a/.env.postgresql +++ /dev/null @@ -1,3 +0,0 @@ -BOLT_DB_DSN=postgresql://test_user:testpassword@localhost:5432/test_db?sslmode=disable -BOLT_DB_DRIVER=postgresql -BOLT_DB_MIGRATIONS_TABLE=bolt_migrations
\ No newline at end of file diff --git a/.env.sqlite3 b/.env.sqlite3 deleted file mode 100644 index ee3b319..0000000 --- a/.env.sqlite3 +++ /dev/null @@ -1,3 +0,0 @@ -BOLT_DB_DSN=/tmp/tmp.db -BOLT_DB_DRIVER=sqlite3 -BOLT_DB_MIGRATIONS_TABLE=bolt_migrations
\ No newline at end of file diff --git a/scripts/lib/docker.sh b/scripts/lib/docker.sh index 6488bb0..c6f3c34 100644 --- a/scripts/lib/docker.sh +++ b/scripts/lib/docker.sh @@ -4,21 +4,65 @@ readonly BOLT_DOCKER_LOADED=1 source "$(dirname "${BASH_SOURCE[0]}")/log.sh" +readonly BOLT_POSTGRES_DB="${POSTGRES_DB:-test_db}" +readonly BOLT_POSTGRES_USER="${POSTGRES_USER:-test_user}" +readonly BOLT_POSTGRES_PASSWORD="${POSTGRES_PASSWORD:-testpassword}" + +readonly BOLT_MYSQL_DATABASE="${MYSQL_DATABASE:-test_db}" +readonly BOLT_MYSQL_USER="${MYSQL_USER:-test_user}" +readonly BOLT_MYSQL_PASSWORD="${MYSQL_PASSWORD:-testpassword}" +readonly BOLT_MYSQL_ROOT_PASSWORD="${MYSQL_ROOT_PASSWORD:-testpassword}" + +readonly BOLT_MSSQL_SA_PASSWORD="${MSSQL_SA_PASSWORD:-TestPass123!}" + +# Generate database DSN string for a given database type +# +# DESCRIPTION: +# Creates a connection string using the same credentials as the Docker containers. +# This ensures perfect synchronization between container setup and Go test configuration. +# +# ARGUMENTS: +# $1: Database type (postgresql, mysql, mssql, sqlite3) +# +# OUTPUTS: +# Prints the DSN string to stdout +# +# RETURN: +# 0 - DSN generated successfully +# 1 - Unknown database type +# +docker_generate_dsn() { + local db_type="$1" + + case "$db_type" in + "postgresql") + echo "postgresql://${BOLT_POSTGRES_USER}:${BOLT_POSTGRES_PASSWORD}@localhost:5432/${BOLT_POSTGRES_DB}?sslmode=disable" + ;; + "mysql") + echo "${BOLT_MYSQL_USER}:${BOLT_MYSQL_PASSWORD}@tcp(localhost:3306)/${BOLT_MYSQL_DATABASE}?parseTime=true" + ;; + "mssql") + local url_encoded_password + url_encoded_password=$(printf %s "$BOLT_MSSQL_SA_PASSWORD" | jq --slurp --raw-input --raw-output @uri) + echo "sqlserver://sa:${url_encoded_password}@localhost:1433?database=master" + ;; + "sqlite3") + echo "/tmp/bolt_test.db" + ;; + *) + log_error "Unknown database type: $db_type" + return 1 + ;; + esac +} + # Start a PostgreSQL database container # # ARGUMENTS: # $1: Container name (default: bolt_postgresql_db) # -# ENVIRONMENT: -# - POSTGRES_DB (default: test_db) -# - POSTGRES_USER (default: test_user) -# - POSTGRES_PASSWORD (default: testpassword) -# docker_start_postgresql() { local container_name="${1:-bolt_postgresql_db}" - local postgres_db="${POSTGRES_DB:-test_db}" - local postgres_user="${POSTGRES_USER:-test_user}" - local postgres_password="${POSTGRES_PASSWORD:-testpassword}" log_info "Starting PostgreSQL container '$container_name'..." @@ -29,9 +73,9 @@ docker_start_postgresql() { --health-interval 10s \ --health-timeout 5s \ --health-retries 5 \ - --env POSTGRES_DB="$postgres_db" \ - --env POSTGRES_USER="$postgres_user" \ - --env POSTGRES_PASSWORD="$postgres_password" \ + --env POSTGRES_DB="$BOLT_POSTGRES_DB" \ + --env POSTGRES_USER="$BOLT_POSTGRES_USER" \ + --env POSTGRES_PASSWORD="$BOLT_POSTGRES_PASSWORD" \ postgres:16.1-alpine3.19 } @@ -40,18 +84,8 @@ docker_start_postgresql() { # ARGUMENTS: # $1: Container name (default: bolt_mysql_db) # -# ENVIRONMENT: -# - MYSQL_DATABASE (default: test_db) -# - MYSQL_USER (default: test_user) -# - MYSQL_PASSWORD (default: testpassword) -# - MYSQL_ROOT_PASSWORD (default: testpassword) -# docker_start_mysql() { local container_name="${1:-bolt_mysql_db}" - local mysql_database="${MYSQL_DATABASE:-test_db}" - local mysql_user="${MYSQL_USER:-test_user}" - local mysql_password="${MYSQL_PASSWORD:-testpassword}" - local mysql_root_password="${MYSQL_ROOT_PASSWORD:-testpassword}" log_info "Starting MySQL container '$container_name'..." @@ -62,10 +96,10 @@ docker_start_mysql() { --health-interval 10s \ --health-timeout 5s \ --health-retries 5 \ - --env MYSQL_DATABASE="$mysql_database" \ - --env MYSQL_USER="$mysql_user" \ - --env MYSQL_PASSWORD="$mysql_password" \ - --env MYSQL_ROOT_PASSWORD="$mysql_root_password" \ + --env MYSQL_DATABASE="$BOLT_MYSQL_DATABASE" \ + --env MYSQL_USER="$BOLT_MYSQL_USER" \ + --env MYSQL_PASSWORD="$BOLT_MYSQL_PASSWORD" \ + --env MYSQL_ROOT_PASSWORD="$BOLT_MYSQL_ROOT_PASSWORD" \ mysql:8.3 } @@ -74,25 +108,21 @@ docker_start_mysql() { # ARGUMENTS: # $1: Container name (default: bolt_mssql_db) # -# ENVIRONMENT: -# - MSSQL_SA_PASSWORD (default: TestPass123!) -# docker_start_mssql() { local container_name="${1:-bolt_mssql_db}" - local mssql_sa_password="${MSSQL_SA_PASSWORD:-TestPass123!}" log_info "Starting MSSQL container '$container_name'..." docker run --detach \ --name "$container_name" \ --publish 1433:1433 \ - --health-cmd "/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P $mssql_sa_password -Q 'SELECT 1' -C || exit 1" \ + --health-cmd "/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P $BOLT_MSSQL_SA_PASSWORD -Q 'SELECT 1' -C || exit 1" \ --health-interval 10s \ --health-timeout 3s \ --health-start-period 10s \ --health-retries 10 \ --env ACCEPT_EULA=Y \ - --env MSSQL_SA_PASSWORD="$mssql_sa_password" \ + --env MSSQL_SA_PASSWORD="$BOLT_MSSQL_SA_PASSWORD" \ mcr.microsoft.com/mssql/server:2022-latest } diff --git a/scripts/lib/env.sh b/scripts/lib/env.sh deleted file mode 100644 index e598584..0000000 --- a/scripts/lib/env.sh +++ /dev/null @@ -1,20 +0,0 @@ -# shellcheck shell=bash -[[ -n "${BOLT_ENV_LOADED:-}" ]] && return -readonly BOLT_ENV_LOADED=1 - -source "$(dirname "${BASH_SOURCE[0]}")/log.sh" - -load_env_file() { - local -r env_file="$1" - if [[ ! -f "$env_file" ]]; then - log_error "Environment file not found: $env_file" - return 1 - fi - - set -a - # shellcheck disable=SC1090 - source "$env_file" - set +a - - log_info "Loaded environment from $env_file" -} diff --git a/scripts/lib/test.sh b/scripts/lib/test.sh index 22fa8eb..dab2241 100644 --- a/scripts/lib/test.sh +++ b/scripts/lib/test.sh @@ -7,21 +7,17 @@ readonly BOLT_TEST_SUITES=("mssql" "sqlite3" "postgresql" "mysql") source "$(dirname "${BASH_SOURCE[0]}")/log.sh" source "$(dirname "${BASH_SOURCE[0]}")/docker.sh" source "$(dirname "${BASH_SOURCE[0]}")/path.sh" -source "$(dirname "${BASH_SOURCE[0]}")/env.sh" # Run a full test suite with database setup and teardown # # DESCRIPTION: # Starts database, waits for health checks, runs tests with # environment variables, and cleans up afterward. Automatically handles -# database lifecycle and environment variable loading. +# database lifecycle and generates connection strings from Docker configuration. # # ARGUMENTS: # $1: Name of the database test suite to run (e.g., "mssql", "postgresql", "sqlite3", or "mysql") # -# FILES REQUIRED: -# .env.{test_name} - Environment file with database configuration -# # RETURN: # 0 - Tests passed successfully # 1 - Tests failed or setup/teardown failed. @@ -33,7 +29,6 @@ source "$(dirname "${BASH_SOURCE[0]}")/env.sh" test_run_suite() { local db_test_suite_name="$1" local container_name="bolt_${db_test_suite_name}_db" - local env_file="${PATH_PROJECT_DIR}/.env.${db_test_suite_name}" local coverage_file="${PATH_PROJECT_DIR}/coverage-${db_test_suite_name}.out" local test_result=0 @@ -43,19 +38,19 @@ test_run_suite() { 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 - ;; + "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 @@ -66,7 +61,7 @@ test_run_suite() { fi log_step "Running $db_test_suite_name tests..." - load_env_file "$env_file" + if ! test_run_go_tests "$db_test_suite_name" "$coverage_file"; then local test_result=1 fi @@ -95,20 +90,16 @@ test_is_valid_suite() { return 1 } -# Run Go tests with optional tags and coverage +# Run Go tests with optional database type and coverage # # DESCRIPTION: # Executes Go tests with optional build tags and coverage reporting. -# Tests are run with parallelism disabled (-p 1). +# Tests are run with parallelism disabled (-p 1). For database tests, +# automatically generates connection strings from Docker configuration. # # ARGUMENTS: -# $1 - test_tags (optional): Build tags to include (e.g., "mssql", "postgres") -# $2 - coverage_file (optional): Output file for coverage profile -# -# ENVIRONMENT VARIABLES (required for database tests): -# BOLT_DB_DSN - Database connection string -# BOLT_DB_DRIVER - Database driver name (e.g., "mssql", "postgres") -# BOLT_DB_MIGRATIONS_TABLE - Table name for migration tracking +# $1 - database_type: Database type (e.g., "mssql", "postgresql", "mysql", "sqlite3"). Defaults to empty string. +# $2 - coverage_file: Output file for coverage profile. Defaults to empty string. # # RETURN: # 0 - Tests passed successfully @@ -119,16 +110,16 @@ test_is_valid_suite() { # test_run_go_tests "mssql" "coverage-mssql.out" # test_run_go_tests() { - local test_tags="$1" - local coverage_file="$2" + local database_type="${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 "$test_tags" ]]; then - cmd="$cmd -tags $test_tags" - log_step "Running tests with tags: $test_tags" + if [[ -n "$database_type" ]]; then + cmd="$cmd -tags $database_type" + log_step "Running tests with tags: $database_type" else log_step "Running tests" fi @@ -139,6 +130,13 @@ test_run_go_tests() { cmd="$cmd ./..." + # For database tests, generate DSN and set environment variables + if [[ -n "$database_type" ]]; 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" + fi + if eval "$cmd"; then log_success "Tests completed successfully!" else |