forked from goodrain/rainbond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testcontainer.go
61 lines (55 loc) · 1.34 KB
/
testcontainer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package fixtures
import (
"context"
"fmt"
"time"
"github.com/goodrain/rainbond/db"
dbconfig "github.com/goodrain/rainbond/db/config"
"github.com/testcontainers/testcontainers-go"
)
// InitDBManager initiates a db manager with a real mysql provided by testcontainers-go.
func InitDBManager() error {
dbname := "region"
rootpw := "rainbond"
ctx := context.Background()
req := testcontainers.ContainerRequest{
Image: "mariadb",
ExposedPorts: []string{"3306/tcp"},
Env: map[string]string{
"MYSQL_ROOT_PASSWORD": rootpw,
"MYSQL_DATABASE": dbname,
},
Cmd: "--character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci",
}
mariadb, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
return err
}
defer mariadb.Terminate(ctx)
host, err := mariadb.Host(ctx)
if err != nil {
return err
}
port, err := mariadb.MappedPort(ctx, "3306")
if err != nil {
return err
}
connInfo := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", "root",
rootpw, host, port.Int(), dbname)
tryTimes := 3
for {
if err := db.CreateManager(dbconfig.Config{
DBType: "mysql",
MysqlConnectionInfo: connInfo,
}); err != nil {
tryTimes = tryTimes - 1
time.Sleep(10 * time.Second)
continue
}
break
}
return nil
}