netbird/management/server/testutil/store.go

99 lines
2.7 KiB
Go
Raw Normal View History

//go:build !ios
// +build !ios
package testutil
import (
"context"
"os"
"time"
"github.com/testcontainers/testcontainers-go"
MySQL Support (#2837) * Update store.go * Update sql_store.go * Update store.go * Update golang-test-linux.yml * Update store.go * Update go.mod * Update go.mod * Update go.sum * Update store.go * Update sql_store.go * TestContainer * Update go.sum * Update store.go * TestUtil Duplicate * dsn fix * go mod tidy * NETBIRD_STORE_ENGINE_MYSQL_DSN * Skip Test * Update test-infrastructure-files.yml * Update test-infrastructure-files.yml * MYSQL_ROOT_PASSWORD added * Update test-infrastructure-files.yml * Update store.go * Debug + Mysql JSON Query * swicth/case convert * Update store.go * Update store.go * Debug * MySQL Test Version Change * Root Test * Ignore other sql tests. * MySQL Connection Fix * enable other tests * The word "key" is a reserved word in MySQL. * Remove Debugs * Update sql_store.go * Added default null value for datetime. * Added default null value for datetime. * MySQL Hooks * MySQL Config File * remove default values * test timeout change * MySQL max lifetime change * WithConfigFile * disable other tests * Update mysql.cnf * Update golang-test-linux.yml * Delete sql_hooks.go * enable other tests * test timeout change * update packets * Fix the Inactivity Expiration problem * Update sql_store.go * Update mysql.cnf * Update sql_store.go * Update sql_store.go * timeout change * MySQL Connection LifeTime Change * TestContainers have been optimized. * Update store_ios.go * Update sql_store.go * timeout fix * fix migration (setup keys) * Update event.go * Add disable option for event activities. * Revert "Update event.go" * Update event.go * Fix Gorm Mysql Bug * update go-jose module * containerd module update * containerd downgrade * Revert commits * Revert "Revert commits" This reverts commit 62b3eac799825e0d3624904401fe67587ad8e780. * Revert "containerd downgrade" This reverts commit 4e46108915ea3b70f8a0234d4860c308a843c5a0. * Revert "containerd module update" This reverts commit e8cfa87d1688e0feeebf0c1ea0127578eba30bd3. * Revert "update go-jose module" This reverts commit 1fabdc760601e389589750daa6b2089148dd29fb.
2024-12-23 11:06:13 +01:00
"github.com/testcontainers/testcontainers-go/modules/mysql"
"github.com/testcontainers/testcontainers-go/modules/postgres"
"github.com/testcontainers/testcontainers-go/wait"
)
MySQL Support (#2837) * Update store.go * Update sql_store.go * Update store.go * Update golang-test-linux.yml * Update store.go * Update go.mod * Update go.mod * Update go.sum * Update store.go * Update sql_store.go * TestContainer * Update go.sum * Update store.go * TestUtil Duplicate * dsn fix * go mod tidy * NETBIRD_STORE_ENGINE_MYSQL_DSN * Skip Test * Update test-infrastructure-files.yml * Update test-infrastructure-files.yml * MYSQL_ROOT_PASSWORD added * Update test-infrastructure-files.yml * Update store.go * Debug + Mysql JSON Query * swicth/case convert * Update store.go * Update store.go * Debug * MySQL Test Version Change * Root Test * Ignore other sql tests. * MySQL Connection Fix * enable other tests * The word "key" is a reserved word in MySQL. * Remove Debugs * Update sql_store.go * Added default null value for datetime. * Added default null value for datetime. * MySQL Hooks * MySQL Config File * remove default values * test timeout change * MySQL max lifetime change * WithConfigFile * disable other tests * Update mysql.cnf * Update golang-test-linux.yml * Delete sql_hooks.go * enable other tests * test timeout change * update packets * Fix the Inactivity Expiration problem * Update sql_store.go * Update mysql.cnf * Update sql_store.go * Update sql_store.go * timeout change * MySQL Connection LifeTime Change * TestContainers have been optimized. * Update store_ios.go * Update sql_store.go * timeout fix * fix migration (setup keys) * Update event.go * Add disable option for event activities. * Revert "Update event.go" * Update event.go * Fix Gorm Mysql Bug * update go-jose module * containerd module update * containerd downgrade * Revert commits * Revert "Revert commits" This reverts commit 62b3eac799825e0d3624904401fe67587ad8e780. * Revert "containerd downgrade" This reverts commit 4e46108915ea3b70f8a0234d4860c308a843c5a0. * Revert "containerd module update" This reverts commit e8cfa87d1688e0feeebf0c1ea0127578eba30bd3. * Revert "update go-jose module" This reverts commit 1fabdc760601e389589750daa6b2089148dd29fb.
2024-12-23 11:06:13 +01:00
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.
}
func CreateMysqlTestContainer() (func(), error) {
ctx := context.Background()
MySQL Support (#2837) * Update store.go * Update sql_store.go * Update store.go * Update golang-test-linux.yml * Update store.go * Update go.mod * Update go.mod * Update go.sum * Update store.go * Update sql_store.go * TestContainer * Update go.sum * Update store.go * TestUtil Duplicate * dsn fix * go mod tidy * NETBIRD_STORE_ENGINE_MYSQL_DSN * Skip Test * Update test-infrastructure-files.yml * Update test-infrastructure-files.yml * MYSQL_ROOT_PASSWORD added * Update test-infrastructure-files.yml * Update store.go * Debug + Mysql JSON Query * swicth/case convert * Update store.go * Update store.go * Debug * MySQL Test Version Change * Root Test * Ignore other sql tests. * MySQL Connection Fix * enable other tests * The word "key" is a reserved word in MySQL. * Remove Debugs * Update sql_store.go * Added default null value for datetime. * Added default null value for datetime. * MySQL Hooks * MySQL Config File * remove default values * test timeout change * MySQL max lifetime change * WithConfigFile * disable other tests * Update mysql.cnf * Update golang-test-linux.yml * Delete sql_hooks.go * enable other tests * test timeout change * update packets * Fix the Inactivity Expiration problem * Update sql_store.go * Update mysql.cnf * Update sql_store.go * Update sql_store.go * timeout change * MySQL Connection LifeTime Change * TestContainers have been optimized. * Update store_ios.go * Update sql_store.go * timeout fix * fix migration (setup keys) * Update event.go * Add disable option for event activities. * Revert "Update event.go" * Update event.go * Fix Gorm Mysql Bug * update go-jose module * containerd module update * containerd downgrade * Revert commits * Revert "Revert commits" This reverts commit 62b3eac799825e0d3624904401fe67587ad8e780. * Revert "containerd downgrade" This reverts commit 4e46108915ea3b70f8a0234d4860c308a843c5a0. * Revert "containerd module update" This reverts commit e8cfa87d1688e0feeebf0c1ea0127578eba30bd3. * Revert "update go-jose module" This reverts commit 1fabdc760601e389589750daa6b2089148dd29fb.
2024-12-23 11:06:13 +01:00
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 Support (#2837) * Update store.go * Update sql_store.go * Update store.go * Update golang-test-linux.yml * Update store.go * Update go.mod * Update go.mod * Update go.sum * Update store.go * Update sql_store.go * TestContainer * Update go.sum * Update store.go * TestUtil Duplicate * dsn fix * go mod tidy * NETBIRD_STORE_ENGINE_MYSQL_DSN * Skip Test * Update test-infrastructure-files.yml * Update test-infrastructure-files.yml * MYSQL_ROOT_PASSWORD added * Update test-infrastructure-files.yml * Update store.go * Debug + Mysql JSON Query * swicth/case convert * Update store.go * Update store.go * Debug * MySQL Test Version Change * Root Test * Ignore other sql tests. * MySQL Connection Fix * enable other tests * The word "key" is a reserved word in MySQL. * Remove Debugs * Update sql_store.go * Added default null value for datetime. * Added default null value for datetime. * MySQL Hooks * MySQL Config File * remove default values * test timeout change * MySQL max lifetime change * WithConfigFile * disable other tests * Update mysql.cnf * Update golang-test-linux.yml * Delete sql_hooks.go * enable other tests * test timeout change * update packets * Fix the Inactivity Expiration problem * Update sql_store.go * Update mysql.cnf * Update sql_store.go * Update sql_store.go * timeout change * MySQL Connection LifeTime Change * TestContainers have been optimized. * Update store_ios.go * Update sql_store.go * timeout fix * fix migration (setup keys) * Update event.go * Add disable option for event activities. * Revert "Update event.go" * Update event.go * Fix Gorm Mysql Bug * update go-jose module * containerd module update * containerd downgrade * Revert commits * Revert "Revert commits" This reverts commit 62b3eac799825e0d3624904401fe67587ad8e780. * Revert "containerd downgrade" This reverts commit 4e46108915ea3b70f8a0234d4860c308a843c5a0. * Revert "containerd module update" This reverts commit e8cfa87d1688e0feeebf0c1ea0127578eba30bd3. * Revert "update go-jose module" This reverts commit 1fabdc760601e389589750daa6b2089148dd29fb.
2024-12-23 11:06:13 +01:00
if err != nil {
return nil, err
}
MySQL Support (#2837) * Update store.go * Update sql_store.go * Update store.go * Update golang-test-linux.yml * Update store.go * Update go.mod * Update go.mod * Update go.sum * Update store.go * Update sql_store.go * TestContainer * Update go.sum * Update store.go * TestUtil Duplicate * dsn fix * go mod tidy * NETBIRD_STORE_ENGINE_MYSQL_DSN * Skip Test * Update test-infrastructure-files.yml * Update test-infrastructure-files.yml * MYSQL_ROOT_PASSWORD added * Update test-infrastructure-files.yml * Update store.go * Debug + Mysql JSON Query * swicth/case convert * Update store.go * Update store.go * Debug * MySQL Test Version Change * Root Test * Ignore other sql tests. * MySQL Connection Fix * enable other tests * The word "key" is a reserved word in MySQL. * Remove Debugs * Update sql_store.go * Added default null value for datetime. * Added default null value for datetime. * MySQL Hooks * MySQL Config File * remove default values * test timeout change * MySQL max lifetime change * WithConfigFile * disable other tests * Update mysql.cnf * Update golang-test-linux.yml * Delete sql_hooks.go * enable other tests * test timeout change * update packets * Fix the Inactivity Expiration problem * Update sql_store.go * Update mysql.cnf * Update sql_store.go * Update sql_store.go * timeout change * MySQL Connection LifeTime Change * TestContainers have been optimized. * Update store_ios.go * Update sql_store.go * timeout fix * fix migration (setup keys) * Update event.go * Add disable option for event activities. * Revert "Update event.go" * Update event.go * Fix Gorm Mysql Bug * update go-jose module * containerd module update * containerd downgrade * Revert commits * Revert "Revert commits" This reverts commit 62b3eac799825e0d3624904401fe67587ad8e780. * Revert "containerd downgrade" This reverts commit 4e46108915ea3b70f8a0234d4860c308a843c5a0. * Revert "containerd module update" This reverts commit e8cfa87d1688e0feeebf0c1ea0127578eba30bd3. * Revert "update go-jose module" This reverts commit 1fabdc760601e389589750daa6b2089148dd29fb.
2024-12-23 11:06:13 +01:00
talksConn, _ := container.ConnectionString(ctx)
mysqlContainer = container
mysqlContainerString = talksConn
RefreshMysqlDatabase(ctx)
return emptyCleanup, 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)
}
MySQL Support (#2837) * Update store.go * Update sql_store.go * Update store.go * Update golang-test-linux.yml * Update store.go * Update go.mod * Update go.mod * Update go.sum * Update store.go * Update sql_store.go * TestContainer * Update go.sum * Update store.go * TestUtil Duplicate * dsn fix * go mod tidy * NETBIRD_STORE_ENGINE_MYSQL_DSN * Skip Test * Update test-infrastructure-files.yml * Update test-infrastructure-files.yml * MYSQL_ROOT_PASSWORD added * Update test-infrastructure-files.yml * Update store.go * Debug + Mysql JSON Query * swicth/case convert * Update store.go * Update store.go * Debug * MySQL Test Version Change * Root Test * Ignore other sql tests. * MySQL Connection Fix * enable other tests * The word "key" is a reserved word in MySQL. * Remove Debugs * Update sql_store.go * Added default null value for datetime. * Added default null value for datetime. * MySQL Hooks * MySQL Config File * remove default values * test timeout change * MySQL max lifetime change * WithConfigFile * disable other tests * Update mysql.cnf * Update golang-test-linux.yml * Delete sql_hooks.go * enable other tests * test timeout change * update packets * Fix the Inactivity Expiration problem * Update sql_store.go * Update mysql.cnf * Update sql_store.go * Update sql_store.go * timeout change * MySQL Connection LifeTime Change * TestContainers have been optimized. * Update store_ios.go * Update sql_store.go * timeout fix * fix migration (setup keys) * Update event.go * Add disable option for event activities. * Revert "Update event.go" * Update event.go * Fix Gorm Mysql Bug * update go-jose module * containerd module update * containerd downgrade * Revert commits * Revert "Revert commits" This reverts commit 62b3eac799825e0d3624904401fe67587ad8e780. * Revert "containerd downgrade" This reverts commit 4e46108915ea3b70f8a0234d4860c308a843c5a0. * Revert "containerd module update" This reverts commit e8cfa87d1688e0feeebf0c1ea0127578eba30bd3. * Revert "update go-jose module" This reverts commit 1fabdc760601e389589750daa6b2089148dd29fb.
2024-12-23 11:06:13 +01:00
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 {
MySQL Support (#2837) * Update store.go * Update sql_store.go * Update store.go * Update golang-test-linux.yml * Update store.go * Update go.mod * Update go.mod * Update go.sum * Update store.go * Update sql_store.go * TestContainer * Update go.sum * Update store.go * TestUtil Duplicate * dsn fix * go mod tidy * NETBIRD_STORE_ENGINE_MYSQL_DSN * Skip Test * Update test-infrastructure-files.yml * Update test-infrastructure-files.yml * MYSQL_ROOT_PASSWORD added * Update test-infrastructure-files.yml * Update store.go * Debug + Mysql JSON Query * swicth/case convert * Update store.go * Update store.go * Debug * MySQL Test Version Change * Root Test * Ignore other sql tests. * MySQL Connection Fix * enable other tests * The word "key" is a reserved word in MySQL. * Remove Debugs * Update sql_store.go * Added default null value for datetime. * Added default null value for datetime. * MySQL Hooks * MySQL Config File * remove default values * test timeout change * MySQL max lifetime change * WithConfigFile * disable other tests * Update mysql.cnf * Update golang-test-linux.yml * Delete sql_hooks.go * enable other tests * test timeout change * update packets * Fix the Inactivity Expiration problem * Update sql_store.go * Update mysql.cnf * Update sql_store.go * Update sql_store.go * timeout change * MySQL Connection LifeTime Change * TestContainers have been optimized. * Update store_ios.go * Update sql_store.go * timeout fix * fix migration (setup keys) * Update event.go * Add disable option for event activities. * Revert "Update event.go" * Update event.go * Fix Gorm Mysql Bug * update go-jose module * containerd module update * containerd downgrade * Revert commits * Revert "Revert commits" This reverts commit 62b3eac799825e0d3624904401fe67587ad8e780. * Revert "containerd downgrade" This reverts commit 4e46108915ea3b70f8a0234d4860c308a843c5a0. * Revert "containerd module update" This reverts commit e8cfa87d1688e0feeebf0c1ea0127578eba30bd3. * Revert "update go-jose module" This reverts commit 1fabdc760601e389589750daa6b2089148dd29fb.
2024-12-23 11:06:13 +01:00
return nil, err
}
MySQL Support (#2837) * Update store.go * Update sql_store.go * Update store.go * Update golang-test-linux.yml * Update store.go * Update go.mod * Update go.mod * Update go.sum * Update store.go * Update sql_store.go * TestContainer * Update go.sum * Update store.go * TestUtil Duplicate * dsn fix * go mod tidy * NETBIRD_STORE_ENGINE_MYSQL_DSN * Skip Test * Update test-infrastructure-files.yml * Update test-infrastructure-files.yml * MYSQL_ROOT_PASSWORD added * Update test-infrastructure-files.yml * Update store.go * Debug + Mysql JSON Query * swicth/case convert * Update store.go * Update store.go * Debug * MySQL Test Version Change * Root Test * Ignore other sql tests. * MySQL Connection Fix * enable other tests * The word "key" is a reserved word in MySQL. * Remove Debugs * Update sql_store.go * Added default null value for datetime. * Added default null value for datetime. * MySQL Hooks * MySQL Config File * remove default values * test timeout change * MySQL max lifetime change * WithConfigFile * disable other tests * Update mysql.cnf * Update golang-test-linux.yml * Delete sql_hooks.go * enable other tests * test timeout change * update packets * Fix the Inactivity Expiration problem * Update sql_store.go * Update mysql.cnf * Update sql_store.go * Update sql_store.go * timeout change * MySQL Connection LifeTime Change * TestContainers have been optimized. * Update store_ios.go * Update sql_store.go * timeout fix * fix migration (setup keys) * Update event.go * Add disable option for event activities. * Revert "Update event.go" * Update event.go * Fix Gorm Mysql Bug * update go-jose module * containerd module update * containerd downgrade * Revert commits * Revert "Revert commits" This reverts commit 62b3eac799825e0d3624904401fe67587ad8e780. * Revert "containerd downgrade" This reverts commit 4e46108915ea3b70f8a0234d4860c308a843c5a0. * Revert "containerd module update" This reverts commit e8cfa87d1688e0feeebf0c1ea0127578eba30bd3. * Revert "update go-jose module" This reverts commit 1fabdc760601e389589750daa6b2089148dd29fb.
2024-12-23 11:06:13 +01:00
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"})
}