Skip to content

Commit

Permalink
Refactor tests containers
Browse files Browse the repository at this point in the history
Signed-off-by: bcmmbaga <[email protected]>
  • Loading branch information
bcmmbaga committed Dec 24, 2024
1 parent 0daccae commit a25e299
Showing 1 changed file with 27 additions and 46 deletions.
73 changes: 27 additions & 46 deletions management/server/testutil/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,91 +8,72 @@ 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"
)

var (
mysqlContainer = (*mysql.MySQLContainer)(nil)
mysqlContainerString = ""
mysqlContainerConfigPath = "../../management/server/testdata/mysql.cnf"
postgresContainer = (*postgres.PostgresContainer)(nil)
postgresContainerString = ""
)

func emptyCleanup() {
// Empty function, don't do anything.
}
var mysqlContainerConfigPath = "./testdata/mysql.cnf"

func CreateMysqlTestContainer() (func(), error) {

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,
"mysql:8.0.40",
mysql.WithConfigFile(mysqlContainerConfigPath),
mysql.WithDatabase("netbird"),
mysql.WithUsername("root"),
mysql.WithPassword(""),
mysql.WithPassword("netbird"),
)

if err != nil {
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
mysqlContainerString = talksConn
talksConn, err := container.ConnectionString(ctx)
if err != nil {
return cleanup, err
}

RefreshMysqlDatabase(ctx)
return emptyCleanup, os.Setenv("NETBIRD_STORE_ENGINE_MYSQL_DSN", talksConn)
return cleanup, os.Setenv("NETBIRD_STORE_ENGINE_MYSQL_DSN", talksConn)
}

func CreatePostgresTestContainer() (func(), error) {

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)),
WithOccurrence(2).WithStartupTimeout(15*time.Second),
),
)
if err != nil {
return nil, err
}

talksConn, _ := container.ConnectionString(ctx)

postgresContainerString = talksConn
postgresContainer = container

RefreshPostgresDatabase(ctx)
return emptyCleanup, os.Setenv("NETBIRD_STORE_ENGINE_POSTGRES_DSN", postgresContainerString)
}
cleanup := func() {
timeout := 10 * time.Second
if err = container.Stop(ctx, &timeout); err != nil {
log.WithContext(ctx).Warnf("failed to stop container: %s", err)
}
}

func RefreshMysqlDatabase(ctx context.Context) {
_, _, _ = mysqlContainer.Exec(ctx, []string{"mysqladmin", "--user=root", "drop", "netbird", "-f"})
_, _, _ = mysqlContainer.Exec(ctx, []string{"mysqladmin", "--user=root", "create", "netbird"})
}
talksConn, err := container.ConnectionString(ctx)
if err != nil {
return cleanup, err
}

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 a25e299

Please sign in to comment.