Files
tasks/common/persistence/tests/mysql_test_util.go
Alex ShtinandGitHub 8e8f1b0f7c Remove license header from every file (#7689)
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.

## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
2025-05-01 18:50:21 -07:00

162 lines
4.3 KiB
Go

package tests
import (
"net"
"path/filepath"
"strconv"
"testing"
"go.temporal.io/server/common/config"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/metrics"
"go.temporal.io/server/common/metrics/metricstest"
p "go.temporal.io/server/common/persistence"
"go.temporal.io/server/common/persistence/sql"
"go.temporal.io/server/common/persistence/sql/sqlplugin"
"go.temporal.io/server/common/persistence/sql/sqlplugin/mysql"
"go.temporal.io/server/common/resolver"
"go.temporal.io/server/common/shuffle"
"go.temporal.io/server/temporal/environment"
"go.uber.org/zap/zaptest"
)
// TODO merge the initialization with existing persistence setup
const (
testMySQLClusterName = "temporal_mysql_cluster"
testMySQLUser = "temporal"
testMySQLPassword = "temporal"
testMySQLConnectionProtocol = "tcp"
testMySQLDatabaseNamePrefix = "test_"
testMySQLDatabaseNameSuffix = "temporal_persistence"
// TODO hard code this dir for now
// need to merge persistence test config / initialization in one place
testMySQLExecutionSchema = "../../../schema/mysql/v8/temporal/schema.sql"
testMySQLVisibilitySchema = "../../../schema/mysql/v8/visibility/schema.sql"
)
type (
MySQLTestData struct {
Cfg *config.SQL
Factory *sql.Factory
Logger log.Logger
Metrics *metricstest.Capture
}
)
func setUpMySQLTest(t *testing.T) (MySQLTestData, func()) {
var testData MySQLTestData
testData.Cfg = NewMySQLConfig()
testData.Logger = log.NewZapLogger(zaptest.NewLogger(t))
mh := metricstest.NewCaptureHandler()
testData.Metrics = mh.StartCapture()
SetupMySQLDatabase(t, testData.Cfg)
SetupMySQLSchema(t, testData.Cfg)
testData.Factory = sql.NewFactory(
*testData.Cfg,
resolver.NewNoopResolver(),
testMySQLClusterName,
testData.Logger,
mh,
)
tearDown := func() {
testData.Factory.Close()
mh.StopCapture(testData.Metrics)
TearDownMySQLDatabase(t, testData.Cfg)
}
return testData, tearDown
}
// NewMySQLConfig returns a new MySQL config for test
func NewMySQLConfig() *config.SQL {
return &config.SQL{
User: testMySQLUser,
Password: testMySQLPassword,
ConnectAddr: net.JoinHostPort(
environment.GetMySQLAddress(),
strconv.Itoa(environment.GetMySQLPort()),
),
ConnectProtocol: testMySQLConnectionProtocol,
PluginName: mysql.PluginName,
DatabaseName: testMySQLDatabaseNamePrefix + shuffle.String(testMySQLDatabaseNameSuffix),
}
}
func SetupMySQLDatabase(t *testing.T, cfg *config.SQL) {
adminCfg := *cfg
// NOTE need to connect with empty name to create new database
adminCfg.DatabaseName = ""
db, err := sql.NewSQLAdminDB(sqlplugin.DbKindUnknown, &adminCfg, resolver.NewNoopResolver(), log.NewTestLogger(), metrics.NoopMetricsHandler)
if err != nil {
t.Fatalf("unable to create MySQL admin DB: %v", err)
}
defer func() { _ = db.Close() }()
err = db.CreateDatabase(cfg.DatabaseName)
if err != nil {
t.Fatalf("unable to create MySQL database: %v", err)
}
}
func SetupMySQLSchema(t *testing.T, cfg *config.SQL) {
db, err := sql.NewSQLAdminDB(sqlplugin.DbKindUnknown, cfg, resolver.NewNoopResolver(), log.NewTestLogger(), metrics.NoopMetricsHandler)
if err != nil {
t.Fatalf("unable to create MySQL admin DB: %v", err)
}
defer func() { _ = db.Close() }()
schemaPath, err := filepath.Abs(testMySQLExecutionSchema)
if err != nil {
t.Fatal(err)
}
statements, err := p.LoadAndSplitQuery([]string{schemaPath})
if err != nil {
t.Fatal(err)
}
for _, stmt := range statements {
if err = db.Exec(stmt); err != nil {
t.Fatal(err)
}
}
schemaPath, err = filepath.Abs(testMySQLVisibilitySchema)
if err != nil {
t.Fatal(err)
}
statements, err = p.LoadAndSplitQuery([]string{schemaPath})
if err != nil {
t.Fatal(err)
}
for _, stmt := range statements {
if err = db.Exec(stmt); err != nil {
t.Fatal(err)
}
}
}
func TearDownMySQLDatabase(t *testing.T, cfg *config.SQL) {
adminCfg := *cfg
// NOTE need to connect with empty name to create new database
adminCfg.DatabaseName = ""
db, err := sql.NewSQLAdminDB(sqlplugin.DbKindUnknown, &adminCfg, resolver.NewNoopResolver(), log.NewTestLogger(), metrics.NoopMetricsHandler)
if err != nil {
t.Fatalf("unable to create MySQL admin DB: %v", err)
}
defer func() { _ = db.Close() }()
err = db.DropDatabase(cfg.DatabaseName)
if err != nil {
t.Fatalf("unable to drop MySQL database: %v", err)
}
}