Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Container ¶
type Container struct {
testcontainers.Container
// contains filtered or unexported fields
}
Container represents the TiDB container type used in the module
func Run ¶
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error)
Run creates an instance of the TiDB container type
Example ¶
// runTiDBContainer {
ctx := context.Background()
ctr, err := tidb.Run(ctx,
"pingcap/tidb:v8.4.0",
)
defer func() {
if err := testcontainers.TerminateContainer(ctr); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Printf("failed to start container: %s", err)
return
}
// }
state, err := ctr.State(ctx)
if err != nil {
log.Printf("failed to get container state: %s", err)
return
}
fmt.Println(state.Running)
Output: true
Example (Connect) ¶
ctx := context.Background()
ctr, err := tidb.Run(ctx,
"pingcap/tidb:v8.4.0",
)
defer func() {
if err := testcontainers.TerminateContainer(ctr); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Printf("failed to start container: %s", err)
return
}
connectionString, err := ctr.ConnectionString(ctx)
if err != nil {
log.Printf("failed to get connection string: %s", err)
return
}
db, err := sql.Open("mysql", connectionString)
if err != nil {
log.Printf("failed to connect to TiDB: %s", err)
return
}
defer db.Close()
if err = db.Ping(); err != nil {
log.Printf("failed to ping TiDB: %s", err)
return
}
var result int
row := db.QueryRow("SELECT 1")
if err = row.Scan(&result); err != nil {
log.Printf("failed to scan row: %s", err)
return
}
fmt.Println(result)
Output: 1
func (*Container) ConnectionString ¶
ConnectionString returns a DSN connection string for the TiDB container, using the MySQL driver format. It is possible to pass extra parameters to the connection string, e.g. "tls=skip-verify".
Click to show internal directories.
Click to hide internal directories.