diff --git a/management/server/testutil/store.go b/management/server/testutil/store.go index 9bae1b58d97..7a9046d6d8a 100644 --- a/management/server/testutil/store.go +++ b/management/server/testutil/store.go @@ -8,63 +8,47 @@ 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"), @@ -72,27 +56,24 @@ func CreatePostgresTestContainer() (func(), error) { 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) }