MCPcopy Create free account
hub / github.com/bytebase/bytebase / GetTiDBContainer

Function GetTiDBContainer

backend/common/testcontainer/testcontainer.go:472–518  ·  view source on GitHub ↗

GetTiDBContainer creates a TiDB container for testing

(ctx context.Context)

Source from the content-addressed store, hash-verified

470
471// GetTiDBContainer creates a TiDB container for testing
472func GetTiDBContainer(ctx context.Context) (retC *Container, retErr error) {
473 req := testcontainers.ContainerRequest{
474 Image: "pingcap/tidb:v8.5.0",
475 ExposedPorts: []string{"4000/tcp"},
476 WaitingFor: wait.ForLog("server is running MySQL protocol").WithStartupTimeout(5 * time.Minute),
477 }
478
479 c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
480 ContainerRequest: req,
481 Started: true,
482 })
483 if err != nil {
484 return nil, err
485 }
486
487 host, err := c.Host(ctx)
488 if err != nil {
489 return nil, err
490 }
491 port, err := c.MappedPort(ctx, "4000/tcp")
492 if err != nil {
493 return nil, err
494 }
495
496 // TiDB uses MySQL protocol, so we use MySQL driver
497 dsn := fmt.Sprintf("root@tcp(%s:%s)/?multiStatements=true&tls=false", host, port.Port())
498 db, err := sql.Open("mysql", dsn)
499 if err != nil {
500 return nil, err
501 }
502 defer func() {
503 if retErr != nil {
504 db.Close()
505 }
506 }()
507
508 if err := waitDBPing(ctx, db); err != nil {
509 return nil, err
510 }
511
512 return &Container{
513 container: c,
514 host: host,
515 port: port.Port(),
516 db: db,
517 }, nil
518}
519
520// GetTestTiDBContainer is a helper function for tests that creates a TiDB container
521// and handles the error by failing the test if container creation fails

Callers 1

GetTestTiDBContainerFunction · 0.85

Calls 4

waitDBPingFunction · 0.85
PortMethod · 0.80
OpenMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected