Skip to content

Commit 062d194

Browse files
committed
Fix MySQL tests - add fallback to environment variables if sharedContainer is nil
If sharedContainer is nil (which shouldn't happen but can in edge cases), fall back to using environment variables that TestMain sets. This makes the code more resilient and works consistently for both MySQL and TiDB.
1 parent 2eb46b6 commit 062d194

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

mysql/testcontainers_helper.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,20 @@ func getSharedMySQLContainer(t *testing.T, image string) *MySQLTestContainer {
202202
}
203203

204204
// TestMain should have already created sharedContainer
205-
// If it's nil, something went wrong in TestMain
205+
// If it's nil, fall back to using environment variables (TestMain sets these)
206206
if sharedContainer == nil {
207-
t.Fatalf("ERROR: sharedContainer is nil. TestMain should have created it using DOCKER_IMAGE='%s'.\n"+
208-
"This indicates a problem with TestMain initialization.", dockerImage)
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+
}
209219
}
210220

211221
return sharedContainer

0 commit comments

Comments
 (0)