Skip to content

Commit b4f0f1b

Browse files
committed
Add fallback to multi-container TiDB setup if TiUP Playground fails
- Try TiUP Playground first (faster, single container) - Fall back to legacy multi-container approach if TiUP build/start fails - Check for pre-built image before attempting Docker build - Extract container endpoint logic to reusable function
1 parent 6ef0cac commit b4f0f1b

File tree

1 file changed

+66
-5
lines changed

1 file changed

+66
-5
lines changed

mysql/testcontainers_helper.go

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,13 +497,59 @@ func startTiDBCluster(ctx context.Context, t *testing.T, version string) *TiDBTe
497497
func startSharedTiDBClusterWithTiUP(version string) (*TiDBTestCluster, error) {
498498
ctx := context.Background()
499499

500+
// Try to use pre-built image first, fall back to building if not available
501+
preBuiltImage := "terraform-provider-mysql-tiup-playground:latest"
502+
503+
// Check if pre-built image exists
504+
checkImageCmd := exec.Command("docker", "image", "inspect", preBuiltImage)
505+
if err := checkImageCmd.Run(); err == nil {
506+
// Pre-built image exists, use it directly
507+
req := testcontainers.ContainerRequest{
508+
Image: preBuiltImage,
509+
ExposedPorts: []string{"4000/tcp"},
510+
HostConfigModifier: func(hostConfig *container.HostConfig) {
511+
hostConfig.Privileged = true
512+
hostConfig.Ulimits = []*container.Ulimit{
513+
{
514+
Name: "nofile",
515+
Soft: int64(250000),
516+
Hard: int64(250000),
517+
},
518+
}
519+
},
520+
Cmd: []string{
521+
"/root/.tiup/bin/tiup", "playground", version,
522+
"--db", "1",
523+
"--kv", "1",
524+
"--pd", "1",
525+
"--tiflash", "0",
526+
"--without-monitor",
527+
"--host", "0.0.0.0",
528+
"--db.port", "4000",
529+
},
530+
WaitingFor: wait.ForAll(
531+
wait.ForListeningPort("4000/tcp"),
532+
wait.ForSQL(nat.Port("4000/tcp"), "mysql", func(host string, port nat.Port) string {
533+
return fmt.Sprintf("root@tcp(%s:%s)/", host, port.Port())
534+
}),
535+
).WithStartupTimeout(300 * time.Second),
536+
}
537+
538+
playgroundContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
539+
ContainerRequest: req,
540+
Started: true,
541+
})
542+
if err == nil {
543+
return getTiDBClusterFromContainer(playgroundContainer, ctx)
544+
}
545+
// If pre-built image fails, fall through to build
546+
}
547+
500548
// Build TiUP Playground image from Dockerfile
501-
// This builds a container with TiUP installed that can run playground
502549
// Get the git root directory (where Dockerfile.tiup-playground is located)
503550
moduleRoot := os.Getenv("GITHUB_WORKSPACE")
504551
if moduleRoot == "" {
505552
// For local development, find git root using git rev-parse
506-
// First try to find git in PATH
507553
gitPath, err := exec.LookPath("git")
508554
if err != nil {
509555
// Git not found, try to find repo root by looking for .git directory
@@ -589,6 +635,11 @@ func startSharedTiDBClusterWithTiUP(version string) (*TiDBTestCluster, error) {
589635
return nil, fmt.Errorf("failed to start TiUP Playground container: %v", err)
590636
}
591637

638+
return getTiDBClusterFromContainer(playgroundContainer, ctx)
639+
}
640+
641+
// getTiDBClusterFromContainer extracts connection details from a TiUP Playground container
642+
func getTiDBClusterFromContainer(playgroundContainer testcontainers.Container, ctx context.Context) (*TiDBTestCluster, error) {
592643
// Get endpoint
593644
host, err := playgroundContainer.Host(ctx)
594645
if err != nil {
@@ -612,10 +663,20 @@ func startSharedTiDBClusterWithTiUP(version string) (*TiDBTestCluster, error) {
612663

613664
// startSharedTiDBCluster starts a shared TiDB cluster without requiring a testing.T
614665
// Used by TestMain for initial setup
615-
// Now uses TiUP Playground for better performance and reliability
666+
// Tries TiUP Playground first (faster), falls back to multi-container if that fails
616667
func startSharedTiDBCluster(version string) (*TiDBTestCluster, error) {
617-
// Use TiUP Playground approach - much faster and simpler
618-
return startSharedTiDBClusterWithTiUP(version)
668+
// Try TiUP Playground approach first - much faster and simpler
669+
cluster, err := startSharedTiDBClusterWithTiUP(version)
670+
if err != nil {
671+
// Log the error but don't fail yet - try fallback
672+
fmt.Fprintf(os.Stderr, "Warning: TiUP Playground failed: %v\n", err)
673+
fmt.Fprintf(os.Stderr, "Falling back to multi-container approach...\n")
674+
os.Stderr.Sync()
675+
676+
// Fall back to legacy multi-container approach
677+
return startSharedTiDBClusterLegacy(version)
678+
}
679+
return cluster, nil
619680
}
620681

621682
// Legacy multi-container approach (kept for reference, but not used)

0 commit comments

Comments
 (0)