Skip to content

Commit

Permalink
Revert caching test containers
Browse files Browse the repository at this point in the history
Signed-off-by: bcmmbaga <[email protected]>
  • Loading branch information
bcmmbaga committed Dec 30, 2024
1 parent 9ee234a commit 1adab0d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 42 deletions.
13 changes: 3 additions & 10 deletions management/server/store/sql_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}
}

Expand Down
49 changes: 17 additions & 32 deletions management/server/testutil/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
Expand All @@ -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"),
Expand All @@ -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)
}

0 comments on commit 1adab0d

Please sign in to comment.