Skip to content

Commit 101c1ce

Browse files
committed
Standardize on DOCKER_IMAGE for all database types
Use single DOCKER_IMAGE environment variable for all database types: - MySQL/Percona/MariaDB: mysql:8.0, percona:5.7, mariadb:10.10 - TiDB: tidb:6.1.7, tidb:8.5.3 (format: tidb:VERSION) This eliminates the need for separate TIDB_VERSION variable. TestMain detects TiDB mode by checking if DOCKER_IMAGE starts with 'tidb:'.
1 parent bdcc960 commit 101c1ce

File tree

3 files changed

+42
-40
lines changed

3 files changed

+42
-40
lines changed

.github/workflows/main.yml

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,22 @@ jobs:
130130
# TiDB versions - must match env.TIDB_VERSIONS: 6.1.7 6.5.12 7.1.6 7.5.7 8.1.2 8.5.3
131131
- db_type: tidb
132132
db_version: "6.1.7"
133+
docker_image: "tidb:6.1.7"
133134
- db_type: tidb
134135
db_version: "6.5.12"
136+
docker_image: "tidb:6.5.12"
135137
- db_type: tidb
136138
db_version: "7.1.6"
139+
docker_image: "tidb:7.1.6"
137140
- db_type: tidb
138141
db_version: "7.5.7"
142+
docker_image: "tidb:7.5.7"
139143
- db_type: tidb
140144
db_version: "8.1.2"
145+
docker_image: "tidb:8.1.2"
141146
- db_type: tidb
142147
db_version: "8.5.3"
148+
docker_image: "tidb:8.5.3"
143149
steps:
144150
- name: Checkout Git repo
145151
uses: actions/checkout@v4
@@ -187,30 +193,15 @@ jobs:
187193
TF_ACC: 1
188194
GOTOOLCHAIN: auto
189195
DOCKER_IMAGE: ${{ matrix.docker_image }}
190-
TIDB_VERSION: ${{ matrix.db_version }}
191196
run: |
192197
export PATH="${{ github.workspace }}/bin:$PATH"
193-
if [ "${{ matrix.db_type }}" == "tidb" ]; then
194-
# TiDB tests - validate TIDB_VERSION is set
195-
if [ -z "$TIDB_VERSION" ]; then
196-
echo "ERROR: TIDB_VERSION is not set for TiDB test"
197-
exit 1
198-
fi
199-
# Ensure DOCKER_IMAGE is not set for TiDB tests
200-
unset DOCKER_IMAGE
201-
echo "Running TiDB tests with version: $TIDB_VERSION"
202-
go test -tags=testcontainers -v ./mysql/... -run WithTestcontainers -timeout=30m
203-
else
204-
# MySQL/Percona/MariaDB tests - validate DOCKER_IMAGE is set
205-
if [ -z "$DOCKER_IMAGE" ]; then
206-
echo "ERROR: DOCKER_IMAGE is not set for ${{ matrix.db_type }} test"
207-
exit 1
208-
fi
209-
# Ensure TIDB_VERSION is not set for MySQL tests
210-
unset TIDB_VERSION
211-
echo "Running ${{ matrix.db_type }} tests with Docker image: $DOCKER_IMAGE"
212-
go test -tags=testcontainers -v ./mysql/... -run WithTestcontainers -timeout=30m
198+
# Validate DOCKER_IMAGE is set for all test types
199+
if [ -z "$DOCKER_IMAGE" ]; then
200+
echo "ERROR: DOCKER_IMAGE is not set for ${{ matrix.db_type }} test"
201+
exit 1
213202
fi
203+
echo "Running ${{ matrix.db_type }} tests with Docker image: $DOCKER_IMAGE"
204+
go test -tags=testcontainers -v ./mysql/... -run WithTestcontainers -timeout=30m
214205
release:
215206
name: Release
216207
needs: [tests]

mysql/testcontainers_helper.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,21 +163,22 @@ func contains(s, substr string) bool {
163163
// The image parameter is ignored - TestMain uses DOCKER_IMAGE env var
164164
// This function validates that DOCKER_IMAGE is set and fails early if not
165165
func getSharedMySQLContainer(t *testing.T, image string) *MySQLTestContainer {
166-
// Validate that DOCKER_IMAGE is set (required by TestMain for MySQL/Percona/MariaDB)
166+
// Validate that DOCKER_IMAGE is set (required by TestMain)
167167
// This validation must always be present - fail early if DOCKER_IMAGE is empty
168168
dockerImage := os.Getenv("DOCKER_IMAGE")
169169
if dockerImage == "" {
170-
t.Fatalf("ERROR: DOCKER_IMAGE environment variable is not set. This is required for MySQL/Percona/MariaDB tests.\n" +
171-
"Please set DOCKER_IMAGE to the appropriate Docker image (e.g., mysql:5.6, percona:8.0, mariadb:10.10)\n" +
170+
t.Fatalf("ERROR: DOCKER_IMAGE environment variable is not set.\n" +
171+
"Please set DOCKER_IMAGE to the appropriate Docker image:\n" +
172+
" - MySQL/Percona/MariaDB: mysql:5.6, percona:8.0, mariadb:10.10\n" +
173+
" - TiDB: tidb:6.1.7, tidb:8.5.3\n" +
172174
"The 'image' parameter to getSharedMySQLContainer is ignored - use DOCKER_IMAGE env var instead.")
173175
}
174176

175177
// Check if we're in TiDB mode - if so, this function shouldn't be called
176178
// TiDB tests use sharedTiDBCluster, not sharedContainer
177-
tidbVersion := os.Getenv("TIDB_VERSION")
178-
if tidbVersion != "" {
179-
t.Fatalf("ERROR: getSharedMySQLContainer called but TIDB_VERSION is set. " +
180-
"TiDB tests should use the shared TiDB cluster from TestMain, not getSharedMySQLContainer.")
179+
if strings.HasPrefix(dockerImage, "tidb:") {
180+
t.Fatalf("ERROR: getSharedMySQLContainer called but DOCKER_IMAGE is set to '%s' (TiDB format). "+
181+
"TiDB tests should use the shared TiDB cluster from TestMain, not getSharedMySQLContainer.", dockerImage)
181182
}
182183

183184
// Validate that the provided image matches DOCKER_IMAGE (if provided)

mysql/testcontainers_testmain.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,32 @@ package mysql
66
import (
77
"fmt"
88
"os"
9+
"strings"
910
"testing"
1011
)
1112

1213
// TestMain sets up a shared MySQL/TiDB container for all testcontainers tests
1314
// This is more efficient than starting a container for each test
1415
func TestMain(m *testing.M) {
15-
// Check if we're testing TiDB (requires multi-container setup)
16-
tidbVersion := os.Getenv("TIDB_VERSION")
17-
if tidbVersion != "" {
16+
// Require DOCKER_IMAGE to be set - fail early if missing
17+
dockerImage := os.Getenv("DOCKER_IMAGE")
18+
if dockerImage == "" {
19+
os.Stderr.WriteString("ERROR: DOCKER_IMAGE environment variable is not set.\n")
20+
os.Stderr.WriteString("Please set DOCKER_IMAGE to the appropriate Docker image:\n")
21+
os.Stderr.WriteString(" - MySQL/Percona/MariaDB: mysql:5.6, percona:8.0, mariadb:10.10\n")
22+
os.Stderr.WriteString(" - TiDB: tidb:6.1.7, tidb:8.5.3\n")
23+
os.Exit(1)
24+
}
25+
26+
// Check if we're testing TiDB (format: tidb:VERSION)
27+
// TiDB requires multi-container setup
28+
if strings.HasPrefix(dockerImage, "tidb:") {
29+
tidbVersion := strings.TrimPrefix(dockerImage, "tidb:")
30+
if tidbVersion == "" {
31+
os.Stderr.WriteString("ERROR: DOCKER_IMAGE format for TiDB must be 'tidb:VERSION' (e.g., tidb:6.1.7)\n")
32+
os.Exit(1)
33+
}
34+
1835
// Start shared TiDB cluster before running tests
1936
var err error
2037
sharedTiDBClusterMtx.Lock()
@@ -42,18 +59,11 @@ func TestMain(m *testing.M) {
4259
os.Exit(code)
4360
}
4461

45-
// Require DOCKER_IMAGE to be set - fail early if missing
46-
mysqlImage := os.Getenv("DOCKER_IMAGE")
47-
if mysqlImage == "" {
48-
os.Stderr.WriteString("ERROR: DOCKER_IMAGE environment variable is not set. This is required for MySQL/Percona/MariaDB tests.\n")
49-
os.Stderr.WriteString("Please set DOCKER_IMAGE to the appropriate Docker image (e.g., mysql:5.6, percona:8.0, mariadb:10.10)\n")
50-
os.Exit(1)
51-
}
52-
62+
// MySQL/Percona/MariaDB mode - use single container
5363
// Start shared container before running tests
5464
var err error
5565
sharedContainerMtx.Lock()
56-
sharedContainer, err = startSharedMySQLContainer(mysqlImage)
66+
sharedContainer, err = startSharedMySQLContainer(dockerImage)
5767
sharedContainerMtx.Unlock()
5868

5969
if err != nil {

0 commit comments

Comments
 (0)