Skip to content

Commit 1dffc80

Browse files
committed
Simplify getSharedMySQLContainer - always use environment variables as source of truth
TestMain always sets MYSQL_ENDPOINT, MYSQL_USERNAME, and MYSQL_PASSWORD for both MySQL and TiDB tests. Use these environment variables as the primary source of truth, with sharedContainer as an optimization when available.
1 parent 062d194 commit 1dffc80

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

mysql/testcontainers_helper.go

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ func getSharedMySQLContainer(t *testing.T, image string) *MySQLTestContainer {
176176
"The 'image' parameter to getSharedMySQLContainer is ignored - use DOCKER_IMAGE env var instead.")
177177
}
178178

179+
// Validate that the provided image matches DOCKER_IMAGE (if provided)
180+
if image != "" && image != dockerImage {
181+
t.Fatalf("ERROR: getSharedMySQLContainer called with image '%s' but DOCKER_IMAGE is set to '%s'.\n"+
182+
"Remove the hardcoded image parameter - TestMain uses DOCKER_IMAGE env var to create the shared container.",
183+
image, dockerImage)
184+
}
185+
179186
// Check if we're in TiDB mode
180187
// For TiDB, TestMain already set up the cluster and environment variables
181188
// Just validate that the environment variables are set and return a dummy container
@@ -194,31 +201,27 @@ func getSharedMySQLContainer(t *testing.T, image string) *MySQLTestContainer {
194201
}
195202
}
196203

197-
// Validate that the provided image matches DOCKER_IMAGE (if provided)
198-
if image != "" && image != dockerImage {
199-
t.Fatalf("ERROR: getSharedMySQLContainer called with image '%s' but DOCKER_IMAGE is set to '%s'.\n"+
200-
"Remove the hardcoded image parameter - TestMain uses DOCKER_IMAGE env var to create the shared container.",
201-
image, dockerImage)
204+
// For MySQL/Percona/MariaDB, TestMain should have set MYSQL_ENDPOINT
205+
// Use environment variables as the source of truth (TestMain always sets these)
206+
endpoint := os.Getenv("MYSQL_ENDPOINT")
207+
if endpoint == "" {
208+
t.Fatalf("ERROR: MYSQL_ENDPOINT not set. TestMain should have set this using DOCKER_IMAGE='%s'.\n"+
209+
"This indicates TestMain did not run or failed to initialize.", dockerImage)
202210
}
203211

204-
// TestMain should have already created sharedContainer
205-
// If it's nil, fall back to using environment variables (TestMain sets these)
206-
if sharedContainer == nil {
207-
endpoint := os.Getenv("MYSQL_ENDPOINT")
208-
if endpoint == "" {
209-
t.Fatalf("ERROR: sharedContainer is nil and MYSQL_ENDPOINT is not set. TestMain should have created the container or set environment variables using DOCKER_IMAGE='%s'.\n"+
210-
"This indicates a problem with TestMain initialization.", dockerImage)
211-
}
212-
// Fallback: use environment variables set by TestMain
213-
return &MySQLTestContainer{
214-
Container: nil, // Not available, but tests use environment variables
215-
Endpoint: endpoint,
216-
Username: os.Getenv("MYSQL_USERNAME"),
217-
Password: os.Getenv("MYSQL_PASSWORD"),
218-
}
212+
// If sharedContainer is available, use it; otherwise use environment variables
213+
if sharedContainer != nil {
214+
return sharedContainer
219215
}
220216

221-
return sharedContainer
217+
// Fallback: use environment variables set by TestMain
218+
// This handles cases where sharedContainer might be nil but environment variables are set
219+
return &MySQLTestContainer{
220+
Container: nil, // Not available, but tests use environment variables
221+
Endpoint: endpoint,
222+
Username: os.Getenv("MYSQL_USERNAME"),
223+
Password: os.Getenv("MYSQL_PASSWORD"),
224+
}
222225
}
223226

224227
// startSharedMySQLContainer starts a shared MySQL container without requiring a testing.T

0 commit comments

Comments
 (0)