GetTestMySQLContainer creates a MySQL container for testing
(ctx context.Context)
| 53 | |
| 54 | // GetTestMySQLContainer creates a MySQL container for testing |
| 55 | func GetTestMySQLContainer(ctx context.Context) (retc *Container, retErr error) { |
| 56 | req := testcontainers.ContainerRequest{ |
| 57 | Image: "mysql:8.0.33", |
| 58 | Env: map[string]string{ |
| 59 | "MYSQL_ROOT_PASSWORD": "root-password", |
| 60 | }, |
| 61 | ExposedPorts: []string{"3306/tcp"}, |
| 62 | WaitingFor: wait.ForLog("ready for connections").WithOccurrence(2).WithStartupTimeout(5 * time.Minute), |
| 63 | } |
| 64 | c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ |
| 65 | ContainerRequest: req, |
| 66 | Started: true, |
| 67 | }) |
| 68 | if err != nil { |
| 69 | return nil, err |
| 70 | } |
| 71 | |
| 72 | host, err := c.Host(ctx) |
| 73 | if err != nil { |
| 74 | return nil, err |
| 75 | } |
| 76 | port, err := c.MappedPort(ctx, "3306/tcp") |
| 77 | if err != nil { |
| 78 | return nil, err |
| 79 | } |
| 80 | |
| 81 | dsn := fmt.Sprintf("root:root-password@tcp(%s:%s)/?multiStatements=true", host, port.Port()) |
| 82 | db, err := sql.Open("mysql", dsn) |
| 83 | if err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | defer func() { |
| 87 | if retErr != nil { |
| 88 | db.Close() |
| 89 | } |
| 90 | }() |
| 91 | |
| 92 | if err := waitDBPing(ctx, db); err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | |
| 96 | return &Container{ |
| 97 | container: c, |
| 98 | host: host, |
| 99 | port: port.Port(), |
| 100 | db: db, |
| 101 | }, nil |
| 102 | } |
| 103 | |
| 104 | // GetPgContainer creates a PostgreSQL 16 container for testing. |
| 105 | func GetPgContainer(ctx context.Context) (*Container, error) { |