diff --git a/management/server/store.go b/management/server/store.go index 63163168a..7c288d19f 100644 --- a/management/server/store.go +++ b/management/server/store.go @@ -291,7 +291,7 @@ func NewTestStoreFromSQL(ctx context.Context, filename string, dataDir string) ( func getSqlStoreEngine(ctx context.Context, store *SqlStore, kind StoreEngine) (Store, func(), error) { if kind == PostgresStoreEngine { - cleanUp, err := testutil.CreatePGDB() + cleanUp, err := testutil.CreatePostgresTestContainer() if err != nil { return nil, nil, err } @@ -310,7 +310,7 @@ func getSqlStoreEngine(ctx context.Context, store *SqlStore, kind StoreEngine) ( } if kind == MysqlStoreEngine { - cleanUp, err := testutil.CreateMyDB() + cleanUp, err := testutil.CreateMysqlTestContainer() if err != nil { return nil, nil, err } diff --git a/management/server/testutil/store.go b/management/server/testutil/store.go index a2e9e01c3..9bae1b58d 100644 --- a/management/server/testutil/store.go +++ b/management/server/testutil/store.go @@ -8,66 +8,91 @@ 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" ) -func CreatePGDB() (func(), error) { +var ( + mysqlContainer = (*mysql.MySQLContainer)(nil) + mysqlContainerString = "" + mysqlContainerConfigPath = "../../management/server/testdata/mysql.cnf" + postgresContainer = (*postgres.PostgresContainer)(nil) + postgresContainerString = "" +) - log.Printf("[DEBUG] CreatePGDB") - - ctx := context.Background() - c, err := postgres.Run(ctx, "postgres:16-alpine", testcontainers.WithWaitStrategy( - wait.ForLog("database system is ready to accept connections"). - WithOccurrence(2).WithStartupTimeout(15*time.Second)), - ) - if err != nil { - return nil, err - } - - talksConn, err := c.ConnectionString(ctx) - - return GetContextDB(ctx, c, talksConn, err, "NETBIRD_STORE_ENGINE_POSTGRES_DSN") +func emptyCleanup() { + // Empty function, don't do anything. } -func CreateMyDB() (func(), error) { - - mysqlConfigPath := "../../management/server/testdata/mysql.cnf" +func CreateMysqlTestContainer() (func(), error) { ctx := context.Background() - c, err := mysql.Run(ctx, + + 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(mysqlConfigPath), + mysql.WithConfigFile(mysqlContainerConfigPath), mysql.WithDatabase("netbird"), - mysql.WithUsername("netbird"), - mysql.WithPassword("mysql"), + mysql.WithUsername("root"), + mysql.WithPassword(""), ) if err != nil { return nil, err } - talksConn, err := c.ConnectionString(ctx) + talksConn, _ := container.ConnectionString(ctx) - return GetContextDB(ctx, c, talksConn, err, "NETBIRD_STORE_ENGINE_MYSQL_DSN") + mysqlContainer = container + mysqlContainerString = talksConn + + RefreshMysqlDatabase(ctx) + return emptyCleanup, os.Setenv("NETBIRD_STORE_ENGINE_MYSQL_DSN", talksConn) } -func GetContextDB(ctx context.Context, c testcontainers.Container, talksConn string, err error, dsn string) (func(), error) { +func CreatePostgresTestContainer() (func(), error) { - cleanup := func() { - timeout := 10 * time.Second - err = c.Stop(ctx, &timeout) - if err != nil { - log.WithContext(ctx).Warnf("failed to stop container: %s", err) - } + 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)), + ) if err != nil { - return cleanup, err + return nil, err } - return cleanup, os.Setenv(dsn, talksConn) + talksConn, _ := container.ConnectionString(ctx) + + postgresContainerString = talksConn + postgresContainer = container + + RefreshPostgresDatabase(ctx) + 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"}) } diff --git a/management/server/testutil/store_ios.go b/management/server/testutil/store_ios.go index af2cf7a3f..c0aebadc4 100644 --- a/management/server/testutil/store_ios.go +++ b/management/server/testutil/store_ios.go @@ -3,4 +3,6 @@ package testutil -func CreatePGDB() (func(), error) { return func() {}, nil } +func CreatePostgresTestContainer() (func(), error) { return func() {}, nil } + +func CreateMysqlTestContainer() (func(), error) { return func() {}, nil }