Refactor tests containers

Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
This commit is contained in:
bcmmbaga 2024-12-24 17:03:09 +03:00
parent 0daccaeabb
commit 626b36f6c5
No known key found for this signature in database
GPG Key ID: 511EED5C928AD547

View File

@ -8,63 +8,47 @@ import (
"os" "os"
"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"
"github.com/testcontainers/testcontainers-go/wait" "github.com/testcontainers/testcontainers-go/wait"
) )
var ( var mysqlContainerConfigPath = "../testdata/mysql.cnf"
mysqlContainer = (*mysql.MySQLContainer)(nil)
mysqlContainerString = ""
mysqlContainerConfigPath = "../../management/server/testdata/mysql.cnf"
postgresContainer = (*postgres.PostgresContainer)(nil)
postgresContainerString = ""
)
func emptyCleanup() {
// Empty function, don't do anything.
}
func CreateMysqlTestContainer() (func(), error) { func CreateMysqlTestContainer() (func(), error) {
ctx := context.Background() ctx := context.Background()
if mysqlContainerString != "" && mysqlContainer != nil && mysqlContainer.IsRunning() {
RefreshMysqlDatabase(ctx)
return emptyCleanup, os.Setenv("NETBIRD_STORE_ENGINE_MYSQL_DSN", mysqlContainerString)
}
container, err := mysql.Run(ctx, container, err := mysql.Run(ctx,
"mysql:8.0.40", "mysql:8.0.40",
mysql.WithConfigFile(mysqlContainerConfigPath), mysql.WithConfigFile(mysqlContainerConfigPath),
mysql.WithDatabase("netbird"), mysql.WithDatabase("netbird"),
mysql.WithUsername("root"), mysql.WithUsername("root"),
mysql.WithPassword(""), mysql.WithPassword("netbird"),
) )
if err != nil { if err != nil {
return nil, err return nil, err
} }
talksConn, _ := container.ConnectionString(ctx) cleanup := func() {
timeout := 10 * time.Second
if err = container.Stop(ctx, &timeout); err != nil {
log.WithContext(ctx).Warnf("failed to stop container: %s", err)
}
}
mysqlContainer = container talksConn, err := container.ConnectionString(ctx)
mysqlContainerString = talksConn if err != nil {
return cleanup, err
}
RefreshMysqlDatabase(ctx) return cleanup, os.Setenv("NETBIRD_STORE_ENGINE_MYSQL_DSN", talksConn)
return emptyCleanup, os.Setenv("NETBIRD_STORE_ENGINE_MYSQL_DSN", talksConn)
} }
func CreatePostgresTestContainer() (func(), error) { func CreatePostgresTestContainer() (func(), error) {
ctx := context.Background() 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, container, err := postgres.Run(ctx,
"postgres:16-alpine", "postgres:16-alpine",
postgres.WithDatabase("netbird"), postgres.WithDatabase("netbird"),
@ -72,27 +56,24 @@ func CreatePostgresTestContainer() (func(), error) {
postgres.WithPassword("netbird"), postgres.WithPassword("netbird"),
testcontainers.WithWaitStrategy( testcontainers.WithWaitStrategy(
wait.ForLog("database system is ready to accept connections"). wait.ForLog("database system is ready to accept connections").
WithOccurrence(2).WithStartupTimeout(15*time.Second)), WithOccurrence(2).WithStartupTimeout(15*time.Second),
),
) )
if err != nil { if err != nil {
return nil, err return nil, err
} }
talksConn, _ := container.ConnectionString(ctx) cleanup := func() {
timeout := 10 * time.Second
if err = container.Stop(ctx, &timeout); err != nil {
log.WithContext(ctx).Warnf("failed to stop container: %s", err)
}
}
postgresContainerString = talksConn talksConn, err := container.ConnectionString(ctx)
postgresContainer = container if err != nil {
return cleanup, err
}
RefreshPostgresDatabase(ctx) return cleanup, os.Setenv("NETBIRD_STORE_ENGINE_POSTGRES_DSN", talksConn)
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"})
} }