mirror of
https://github.com/netbirdio/netbird.git
synced 2025-01-26 07:48:48 +01:00
Revert caching test containers
Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
This commit is contained in:
parent
9ee234ac35
commit
1adab0d06d
@ -86,12 +86,6 @@ func NewSqlStore(ctx context.Context, db *gorm.DB, storeEngine Engine, metrics t
|
|||||||
|
|
||||||
sql.SetMaxOpenConns(conns)
|
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)
|
log.WithContext(ctx).Infof("Set max open db connections to %d", conns)
|
||||||
|
|
||||||
if err := migrate(ctx, db); err != nil {
|
if err := migrate(ctx, db); err != nil {
|
||||||
@ -977,7 +971,6 @@ func getGormConfig() *gorm.Config {
|
|||||||
Logger: logger.Default.LogMode(logger.Silent),
|
Logger: logger.Default.LogMode(logger.Silent),
|
||||||
CreateBatchSize: 400,
|
CreateBatchSize: 400,
|
||||||
PrepareStmt: true,
|
PrepareStmt: true,
|
||||||
SkipDefaultTransaction: true,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/testcontainers/testcontainers-go"
|
"github.com/testcontainers/testcontainers-go"
|
||||||
"github.com/testcontainers/testcontainers-go/modules/mysql"
|
"github.com/testcontainers/testcontainers-go/modules/mysql"
|
||||||
"github.com/testcontainers/testcontainers-go/modules/postgres"
|
"github.com/testcontainers/testcontainers-go/modules/postgres"
|
||||||
@ -19,23 +20,12 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
mysqlContainerConfigPath = "../testdata/mysql.cnf"
|
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.
|
// CreateMysqlTestContainer creates a new MySQL container for testing.
|
||||||
func CreateMysqlTestContainer() (func(), error) {
|
func CreateMysqlTestContainer() (func(), error) {
|
||||||
ctx := context.Background()
|
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)
|
_, caller, _, ok := runtime.Caller(0)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("failed to get caller information")
|
return nil, fmt.Errorf("failed to get caller information")
|
||||||
@ -52,25 +42,25 @@ func CreateMysqlTestContainer() (func(), error) {
|
|||||||
return nil, err
|
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)
|
talksConn, err := container.ConnectionString(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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.
|
// CreatePostgresTestContainer creates a new PostgreSQL container for testing.
|
||||||
func CreatePostgresTestContainer() (func(), error) {
|
func CreatePostgresTestContainer() (func(), error) {
|
||||||
ctx := context.Background()
|
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,
|
container, err := postgres.RunContainer(ctx,
|
||||||
testcontainers.WithImage("postgres:16-alpine"),
|
testcontainers.WithImage("postgres:16-alpine"),
|
||||||
postgres.WithDatabase("netbird"),
|
postgres.WithDatabase("netbird"),
|
||||||
@ -85,22 +75,17 @@ func CreatePostgresTestContainer() (func(), error) {
|
|||||||
return nil, err
|
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)
|
talksConn, err := container.ConnectionString(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
postgresContainer = container
|
|
||||||
postgresConnStr = talksConn
|
|
||||||
|
|
||||||
return emptyCleanup, os.Setenv("NETBIRD_STORE_ENGINE_POSTGRES_DSN", talksConn)
|
return cleanUp, 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"})
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user