Files
tasks/common/persistence/tests/mysql_test_util.go
Tim Deeb-SwihartandGitHub ff946f14d8 Replace test setup panics with t.Fatalf (#5981)
## What changed?
I replaced all panics in the persistence tests with calls to
`testing.T.Fatalf` so they're easier to find.

I also updated the SQLite setup to log the error itself and not `%+v` of
the error tag wrapper

## Why?

To make these failures easier to find and/or possible to diagnose. We
ate the error in
https://github.com/temporalio/temporal/actions/runs/9199422279/job/25304258612
so I don't know why it flaked

## How did you test it?

## Potential risks

## Documentation

## Is hotfix candidate?
No
2024-05-23 11:50:54 -07:00

187 lines
5.5 KiB
Go

// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package tests
import (
"net"
"path/filepath"
"strconv"
"testing"
"go.uber.org/zap/zaptest"
"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/environment"
)
// 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)
}
}