mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-22 02:06:39 +00:00
TestContainers have been optimized.
This commit is contained in:
@@ -8,66 +8,91 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/testcontainers/testcontainers-go"
|
||||
"github.com/testcontainers/testcontainers-go/modules/mysql"
|
||||
"github.com/testcontainers/testcontainers-go/modules/postgres"
|
||||
"github.com/testcontainers/testcontainers-go/wait"
|
||||
)
|
||||
|
||||
func CreatePGDB() (func(), error) {
|
||||
var (
|
||||
mysqlContainer = (*mysql.MySQLContainer)(nil)
|
||||
mysqlContainerString = ""
|
||||
mysqlContainerConfigPath = "../../management/server/testdata/mysql.cnf"
|
||||
postgresContainer = (*postgres.PostgresContainer)(nil)
|
||||
postgresContainerString = ""
|
||||
)
|
||||
|
||||
log.Printf("[DEBUG] CreatePGDB")
|
||||
|
||||
ctx := context.Background()
|
||||
c, err := postgres.Run(ctx, "postgres:16-alpine", testcontainers.WithWaitStrategy(
|
||||
wait.ForLog("database system is ready to accept connections").
|
||||
WithOccurrence(2).WithStartupTimeout(15*time.Second)),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
talksConn, err := c.ConnectionString(ctx)
|
||||
|
||||
return GetContextDB(ctx, c, talksConn, err, "NETBIRD_STORE_ENGINE_POSTGRES_DSN")
|
||||
func emptyCleanup() {
|
||||
// Empty function, don't do anything.
|
||||
}
|
||||
|
||||
func CreateMyDB() (func(), error) {
|
||||
|
||||
mysqlConfigPath := "../../management/server/testdata/mysql.cnf"
|
||||
func CreateMysqlTestContainer() (func(), error) {
|
||||
|
||||
ctx := context.Background()
|
||||
c, err := mysql.Run(ctx,
|
||||
|
||||
if mysqlContainerString != "" && mysqlContainer != nil && mysqlContainer.IsRunning() {
|
||||
RefreshMysqlDatabase(ctx)
|
||||
return emptyCleanup, os.Setenv("NETBIRD_STORE_ENGINE_MYSQL_DSN", mysqlContainerString)
|
||||
}
|
||||
|
||||
container, err := mysql.Run(ctx,
|
||||
"mysql:8.0.40",
|
||||
mysql.WithConfigFile(mysqlConfigPath),
|
||||
mysql.WithConfigFile(mysqlContainerConfigPath),
|
||||
mysql.WithDatabase("netbird"),
|
||||
mysql.WithUsername("netbird"),
|
||||
mysql.WithPassword("mysql"),
|
||||
mysql.WithUsername("root"),
|
||||
mysql.WithPassword(""),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
talksConn, err := c.ConnectionString(ctx)
|
||||
talksConn, _ := container.ConnectionString(ctx)
|
||||
|
||||
return GetContextDB(ctx, c, talksConn, err, "NETBIRD_STORE_ENGINE_MYSQL_DSN")
|
||||
mysqlContainer = container
|
||||
mysqlContainerString = talksConn
|
||||
|
||||
RefreshMysqlDatabase(ctx)
|
||||
return emptyCleanup, os.Setenv("NETBIRD_STORE_ENGINE_MYSQL_DSN", talksConn)
|
||||
}
|
||||
|
||||
func GetContextDB(ctx context.Context, c testcontainers.Container, talksConn string, err error, dsn string) (func(), error) {
|
||||
func CreatePostgresTestContainer() (func(), error) {
|
||||
|
||||
cleanup := func() {
|
||||
timeout := 10 * time.Second
|
||||
err = c.Stop(ctx, &timeout)
|
||||
if err != nil {
|
||||
log.WithContext(ctx).Warnf("failed to stop container: %s", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
if postgresContainerString != "" && postgresContainer != nil && postgresContainer.IsRunning() {
|
||||
RefreshPostgresDatabase(ctx)
|
||||
return emptyCleanup, os.Setenv("NETBIRD_STORE_ENGINE_POSTGRES_DSN", postgresContainerString)
|
||||
}
|
||||
|
||||
container, err := postgres.Run(ctx,
|
||||
"postgres:16-alpine",
|
||||
postgres.WithDatabase("netbird"),
|
||||
postgres.WithUsername("root"),
|
||||
postgres.WithPassword("netbird"),
|
||||
testcontainers.WithWaitStrategy(
|
||||
wait.ForLog("database system is ready to accept connections").
|
||||
WithOccurrence(2).WithStartupTimeout(15*time.Second)),
|
||||
)
|
||||
if err != nil {
|
||||
return cleanup, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return cleanup, os.Setenv(dsn, talksConn)
|
||||
talksConn, _ := container.ConnectionString(ctx)
|
||||
|
||||
postgresContainerString = talksConn
|
||||
postgresContainer = container
|
||||
|
||||
RefreshPostgresDatabase(ctx)
|
||||
return emptyCleanup, os.Setenv("NETBIRD_STORE_ENGINE_POSTGRES_DSN", postgresContainerString)
|
||||
}
|
||||
|
||||
func RefreshMysqlDatabase(ctx context.Context) {
|
||||
_, _, _ = mysqlContainer.Exec(ctx, []string{"mysqladmin", "--user=root", "drop", "netbird", "-f"})
|
||||
_, _, _ = mysqlContainer.Exec(ctx, []string{"mysqladmin", "--user=root", "create", "netbird"})
|
||||
}
|
||||
|
||||
func RefreshPostgresDatabase(ctx context.Context) {
|
||||
_, _, _ = postgresContainer.Exec(ctx, []string{"dropdb", "-f", "netbird"})
|
||||
_, _, _ = postgresContainer.Exec(ctx, []string{"createdb", "netbird"})
|
||||
}
|
||||
|
||||
@@ -3,4 +3,6 @@
|
||||
|
||||
package testutil
|
||||
|
||||
func CreatePGDB() (func(), error) { return func() {}, nil }
|
||||
func CreatePostgresTestContainer() (func(), error) { return func() {}, nil }
|
||||
|
||||
func CreateMysqlTestContainer() (func(), error) { return func() {}, nil }
|
||||
|
||||
Reference in New Issue
Block a user