1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
# shellcheck shell=bash
[[ -n "${BOLT_DOCKER_LOADED:-}" ]] && return
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!}"
# 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:
# 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 suite name (postgresql, mysql, mssql, sqlite3, or versioned like postgresql-15)
#
# OUTPUTS:
# Prints the DSN string to stdout
#
# RETURN:
# 0 - DSN generated successfully
# 1 - Unknown database type
#
docker_generate_dsn() {
local suite_name="$1"
local db_type version
read -r db_type version <<< "$(docker_parse_suite_name "$suite_name")"
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)
# $2: Version tag (default: latest)
#
docker_start_postgresql() {
local container_name="${1:-bolt_postgresql_db}"
local version="${2:-latest}"
log_info "Starting PostgreSQL $version container '$container_name'..."
docker run --detach \
--name "$container_name" \
--publish 5432:5432 \
--health-cmd "pg_isready -U postgres" \
--health-interval 10s \
--health-timeout 5s \
--health-retries 5 \
--env POSTGRES_DB="$BOLT_POSTGRES_DB" \
--env POSTGRES_USER="$BOLT_POSTGRES_USER" \
--env POSTGRES_PASSWORD="$BOLT_POSTGRES_PASSWORD" \
"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 $version container '$container_name'..."
docker run --detach \
--name "$container_name" \
--publish 3306:3306 \
--health-cmd "mysqladmin ping -h localhost" \
--health-interval 10s \
--health-timeout 5s \
--health-retries 5 \
--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:${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 $version container '$container_name'..."
docker run --detach \
--name "$container_name" \
--publish 1433:1433 \
--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="$BOLT_MSSQL_SA_PASSWORD" \
"mcr.microsoft.com/mssql/server:${version}"
}
# Stop and remove a database container
#
# ARGUMENTS:
# $1: Container name
#
docker_stop_container() {
local container_name="$1"
log_info "Stopping and removing container '$container_name'..."
docker stop "$container_name" >/dev/null 2>&1 || true
docker rm "$container_name" >/dev/null 2>&1 || true
}
# Wait for a container to be healthy
#
# ARGUMENTS:
# $1: Container name
# $2: Timeout in seconds (default: 60)
#
# RETURN:
# 0 - Container is healthy
# 1 - Container not found, unhealthy, or timeout
#
docker_wait_for_healthy_container() {
local container_name="$1"
local timeout="${2:-60}"
local elapsed=0
log_info "Waiting for container '$container_name' to be healthy..."
while [[ $elapsed -lt $timeout ]]; do
local status
status=$(docker inspect -f '{{.State.Health.Status}}' "$container_name" 2>/dev/null || echo "not_found")
case "$status" in
"healthy")
log_success "Container '$container_name' is healthy!"
return 0
;;
"not_found")
log_error "Error: Container '$container_name' not found"
return 1
;;
"unhealthy")
log_error "Error: Container '$container_name' is unhealthy"
return 1
;;
*)
log_progress
sleep 1
((elapsed++))
;;
esac
done
log_progress_end
log_error "Timeout: Container '$container_name' did not become healthy within ${timeout} seconds"
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 suite name (postgresql, mysql, mssql, or versioned like postgresql-15)
# $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 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")
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" "$version"
;;
"mysql")
docker_start_mysql "$container_name" "$version"
;;
"mssql")
docker_start_mssql "$container_name" "$version"
;;
*)
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
}
|