Revert caching test containers

Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
This commit is contained in:
bcmmbaga 2024-12-30 18:38:51 +03:00
parent 9ee234ac35
commit 1adab0d06d
No known key found for this signature in database
GPG Key ID: 511EED5C928AD547
2 changed files with 20 additions and 42 deletions

View File

@ -86,12 +86,6 @@ func NewSqlStore(ctx context.Context, db *gorm.DB, storeEngine Engine, metrics t
sql.SetMaxOpenConns(conns)
if storeEngine == MysqlStoreEngine {
sql.SetConnMaxLifetime(time.Minute * 2)
sql.SetConnMaxIdleTime(time.Minute * 2)
sql.SetMaxIdleConns(conns)
}
log.WithContext(ctx).Infof("Set max open db connections to %d", conns)
if err := migrate(ctx, db); err != nil {
@ -974,10 +968,9 @@ func NewMysqlStore(ctx context.Context, dsn string, metrics telemetry.AppMetrics
func getGormConfig() *gorm.Config {
return &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
CreateBatchSize: 400,
PrepareStmt: true,
SkipDefaultTransaction: true,
Logger: logger.Default.LogMode(logger.Silent),
CreateBatchSize: 400,
PrepareStmt: true,
}
}

View File

@ -11,6 +11,7 @@ import (
"runtime"
"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"
@ -19,23 +20,12 @@ import (
var (
mysqlContainerConfigPath = "../testdata/mysql.cnf"
mysqlContainer = (*mysql.MySQLContainer)(nil)
postgresContainer = (*postgres.PostgresContainer)(nil)
mysqlConnStr = ""
postgresConnStr = ""
)
func emptyCleanup() { return }
// CreateMysqlTestContainer creates a new MySQL container for testing.
func CreateMysqlTestContainer() (func(), error) {
ctx := context.Background()
if mysqlContainer != nil && mysqlContainer.IsRunning() {
refreshMysqlDatabase(ctx)
return emptyCleanup, os.Setenv("NETBIRD_STORE_ENGINE_MYSQL_DSN", mysqlConnStr)
}
_, caller, _, ok := runtime.Caller(0)
if !ok {
return nil, fmt.Errorf("failed to get caller information")
@ -52,25 +42,25 @@ func CreateMysqlTestContainer() (func(), error) {
return nil, err
}
cleanUp := func() {
timeout := 2 * time.Second
if err = container.Stop(ctx, &timeout); err != nil {
log.WithContext(ctx).Warnf("failed to stop container: %s", err)
}
}
talksConn, err := container.ConnectionString(ctx)
if err != nil {
return nil, err
}
mysqlContainer = container
mysqlConnStr = talksConn
return emptyCleanup, os.Setenv("NETBIRD_STORE_ENGINE_MYSQL_DSN", talksConn)
return cleanUp, os.Setenv("NETBIRD_STORE_ENGINE_MYSQL_DSN", talksConn)
}
// CreatePostgresTestContainer creates a new PostgreSQL container for testing.
func CreatePostgresTestContainer() (func(), error) {
ctx := context.Background()
if postgresContainer != nil && postgresContainer.IsRunning() {
refreshPostgresDatabase(ctx)
return emptyCleanup, os.Setenv("NETBIRD_STORE_ENGINE_POSTGRES_DSN", postgresConnStr)
}
container, err := postgres.RunContainer(ctx,
testcontainers.WithImage("postgres:16-alpine"),
postgres.WithDatabase("netbird"),
@ -85,22 +75,17 @@ func CreatePostgresTestContainer() (func(), error) {
return nil, err
}
cleanUp := func() {
timeout := 2 * time.Second
if err = container.Stop(ctx, &timeout); err != nil {
log.WithContext(ctx).Warnf("failed to stop container: %s", err)
}
}
talksConn, err := container.ConnectionString(ctx)
if err != nil {
return nil, err
}
postgresContainer = container
postgresConnStr = talksConn
return emptyCleanup, os.Setenv("NETBIRD_STORE_ENGINE_POSTGRES_DSN", talksConn)
}
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"})
return cleanUp, os.Setenv("NETBIRD_STORE_ENGINE_POSTGRES_DSN", talksConn)
}