Skip to content

Commit

Permalink
Refactor test for internal integration tests (cadence-workflow#4437)
Browse files Browse the repository at this point in the history
- Export integration test suite definitions
- Allow specifying schema dir when setting up nosql test cluster
  • Loading branch information
yycptt authored Sep 1, 2021
1 parent 4969e35 commit 88549dd
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 110 deletions.
40 changes: 24 additions & 16 deletions common/persistence/nosql/nosqlPersistenceTest.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,35 @@ import (

// testCluster allows executing cassandra operations in testing.
type testCluster struct {
keyspace string
cfg config.NoSQL
keyspace string
schemaBaseDir string
cfg config.NoSQL
}

var _ testcluster.PersistenceTestCluster = (*testCluster)(nil)

// NewTestCluster returns a new cassandra test cluster
func NewTestCluster(pluginName, keyspace, username, password, host string, port int, protoVersion int) testcluster.PersistenceTestCluster {
var tc testCluster
tc.keyspace = keyspace
tc.cfg = config.NoSQL{
PluginName: pluginName,
User: username,
Password: password,
Hosts: host,
Port: port,
MaxConns: 2,
Keyspace: keyspace,
ProtoVersion: protoVersion,
// if schemaBaseDir is empty, it will be auto-resolved based on os.Getwd()
// otherwise the specified value will be used (used by internal tests)
func NewTestCluster(
pluginName, keyspace, username, password, host string,
port, protoVersion int,
schemaBaseDir string,
) testcluster.PersistenceTestCluster {
return &testCluster{
keyspace: keyspace,
schemaBaseDir: schemaBaseDir,
cfg: config.NoSQL{
PluginName: pluginName,
User: username,
Password: password,
Hosts: host,
Port: port,
MaxConns: 2,
Keyspace: keyspace,
ProtoVersion: protoVersion,
},
}
return &tc
}

// Config returns the persistence config for connecting to this test cluster
Expand All @@ -78,7 +86,7 @@ func (s *testCluster) SetupTestDatabase() {
if err != nil {
log.Fatal(err)
}
err = adminDB.SetupTestDatabase()
err = adminDB.SetupTestDatabase(s.schemaBaseDir)
if err != nil {
log.Fatal(err)
}
Expand Down
24 changes: 13 additions & 11 deletions common/persistence/nosql/nosqlplugin/cassandra/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,33 @@ const (

var _ nosqlplugin.AdminDB = (*cdb)(nil)

func (db *cdb) SetupTestDatabase() error {
func (db *cdb) SetupTestDatabase(schemaBaseDir string) error {
err := createCassandraKeyspace(db.session, db.cfg.Keyspace, 1, true)
if err != nil {
return err
}
cadencePackageDir, err := getCadencePackageDir()
if err != nil {
log.Fatal(err)
if schemaBaseDir == "" {
cadencePackageDir, err := getCadencePackageDir()
if err != nil {
log.Fatal(err)
}
schemaBaseDir = cadencePackageDir + testSchemaDir
}
schemaDir := cadencePackageDir + testSchemaDir

err = db.loadSchema([]string{"schema.cql"}, schemaDir)
err = db.loadSchema([]string{"schema.cql"}, schemaBaseDir)
if err != nil {
return err
}
err = db.loadVisibilitySchema([]string{"schema.cql"}, schemaDir)
err = db.loadVisibilitySchema([]string{"schema.cql"}, schemaBaseDir)
if err != nil {
return err
}
return nil
}

// loadSchema from PersistenceTestCluster interface
func (db *cdb) loadSchema(fileNames []string, schemaDir string) error {
workflowSchemaDir := schemaDir + "/cadence"
func (db *cdb) loadSchema(fileNames []string, schemaBaseDir string) error {
workflowSchemaDir := schemaBaseDir + "/cadence"
err := loadCassandraSchema(workflowSchemaDir, fileNames, db.cfg.Hosts, db.cfg.Port, db.cfg.Keyspace, true, nil, db.cfg.ProtoVersion)
if err != nil && !strings.Contains(err.Error(), "AlreadyExists") {
// TODO: should we remove the second condition?
Expand All @@ -76,8 +78,8 @@ func (db *cdb) loadSchema(fileNames []string, schemaDir string) error {
}

// loadVisibilitySchema from PersistenceTestCluster interface
func (db *cdb) loadVisibilitySchema(fileNames []string, schemaDir string) error {
workflowSchemaDir := schemaDir + "visibility"
func (db *cdb) loadVisibilitySchema(fileNames []string, schemaBaseDir string) error {
workflowSchemaDir := schemaBaseDir + "/visibility"
err := loadCassandraSchema(workflowSchemaDir, fileNames, db.cfg.Hosts, db.cfg.Port, db.cfg.Keyspace, false, nil, db.cfg.ProtoVersion)
if err != nil && !strings.Contains(err.Error(), "AlreadyExists") {
// TODO: should we remove the second condition?
Expand Down
2 changes: 1 addition & 1 deletion common/persistence/nosql/nosqlplugin/dynamodb/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

var _ nosqlplugin.AdminDB = (*ddb)(nil)

func (db *ddb) SetupTestDatabase() error {
func (db *ddb) SetupTestDatabase(schemaBaseDir string) error {
panic("TODO")
}

Expand Down
2 changes: 1 addition & 1 deletion common/persistence/nosql/nosqlplugin/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type (

// AdminDB is for tooling and testing
AdminDB interface {
SetupTestDatabase() error
SetupTestDatabase(schemaBaseDir string) error
TeardownTestDatabase() error
}

Expand Down
8 changes: 4 additions & 4 deletions common/persistence/nosql/nosqlplugin/interfaces_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func NewTestBaseWithNoSQL(options *TestBaseOptions) TestBase {
if options.DBName == "" {
options.DBName = "test_" + GenerateRandomDBName(10)
}
testCluster := nosql.NewTestCluster(options.DBPluginName, options.DBName, options.DBUsername, options.DBPassword, options.DBHost, options.DBPort, options.ProtoVersion)
testCluster := nosql.NewTestCluster(options.DBPluginName, options.DBName, options.DBUsername, options.DBPassword, options.DBHost, options.DBPort, options.ProtoVersion, "")
metadata := options.ClusterMetadata
if metadata == nil {
metadata = cluster.GetTestClusterMetadata(false, false)
Expand Down
23 changes: 5 additions & 18 deletions host/client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (
"go.uber.org/cadence/activity"
"go.uber.org/cadence/client"
"go.uber.org/cadence/encoded"
cworker "go.uber.org/cadence/worker"
"go.uber.org/cadence/worker"
"go.uber.org/cadence/workflow"
"go.uber.org/yarpc"
"go.uber.org/yarpc/transport/tchannel"
Expand All @@ -57,19 +57,6 @@ func init() {
workflow.Register(testChildWorkflow)
}

type (
ClientIntegrationSuite struct {
// override suite.Suite.Assertions with require.Assertions; this means that s.NotNil(nil) will stop the test,
// not merely log an error
*require.Assertions
IntegrationBase
wfService workflowserviceclient.Interface
wfClient client.Client
worker cworker.Worker
taskList string
}
)

func TestClientIntegrationSuite(t *testing.T) {
flag.Parse()

Expand Down Expand Up @@ -100,7 +87,7 @@ func (s *ClientIntegrationSuite) SetupSuite() {
s.wfClient = client.NewClient(s.wfService, s.domainName, nil)

s.taskList = "client-integration-test-tasklist"
s.worker = cworker.New(s.wfService, s.domainName, s.taskList, cworker.Options{})
s.worker = worker.New(s.wfService, s.domainName, s.taskList, worker.Options{})
if err := s.worker.Start(); err != nil {
s.Logger.Fatal("Error when start worker", tag.Error(err))
}
Expand Down Expand Up @@ -208,12 +195,12 @@ func testDataConverterWorkflow(ctx workflow.Context, tl string) (string, error)
return result + "," + result1, nil
}

func (s *ClientIntegrationSuite) startWorkerWithDataConverter(tl string, dataConverter encoded.DataConverter) cworker.Worker {
opts := cworker.Options{}
func (s *ClientIntegrationSuite) startWorkerWithDataConverter(tl string, dataConverter encoded.DataConverter) worker.Worker {
opts := worker.Options{}
if dataConverter != nil {
opts.DataConverter = dataConverter
}
worker := cworker.New(s.wfService, s.domainName, tl, opts)
worker := worker.New(s.wfService, s.domainName, tl, opts)
if err := worker.Start(); err != nil {
s.Logger.Fatal("Error when start worker with data converter", tag.Error(err))
}
Expand Down
9 changes: 0 additions & 9 deletions host/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,6 @@ import (
"github.com/uber/cadence/service/matching"
)

type (
IntegrationSuite struct {
// override suite.Suite.Assertions with require.Assertions; this means that s.NotNil(nil) will stop the test,
// not merely log an error
*require.Assertions
IntegrationBase
}
)

func TestIntegrationSuite(t *testing.T) {
flag.Parse()

Expand Down
41 changes: 0 additions & 41 deletions host/ndc/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,48 +39,15 @@ import (
adminClient "github.com/uber/cadence/client/admin"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/cache"
"github.com/uber/cadence/common/log"
"github.com/uber/cadence/common/log/loggerimpl"
"github.com/uber/cadence/common/log/tag"
"github.com/uber/cadence/common/persistence"
pt "github.com/uber/cadence/common/persistence/persistence-tests"
"github.com/uber/cadence/common/persistence/persistence-tests/testcluster"
test "github.com/uber/cadence/common/testing"
"github.com/uber/cadence/common/types"
"github.com/uber/cadence/host"
)

type (
NDCIntegrationTestSuite struct {
// override suite.Suite.Assertions with require.Assertions; this means that s.NotNil(nil) will stop the test,
// not merely log an error
*require.Assertions
suite.Suite
active *host.TestCluster
generator test.Generator
serializer persistence.PayloadSerializer
logger log.Logger

domainName string
domainID string
version int64
versionIncrement int64
mockAdminClient map[string]adminClient.Client
standByReplicationTasksChan chan *types.ReplicationTask
standByTaskID int64

clusterConfigs []*host.TestClusterConfig
defaultTestCluster testcluster.PersistenceTestCluster
visibilityTestCluster testcluster.PersistenceTestCluster
}

NDCIntegrationTestSuiteParams struct {
ClusterConfigs []*host.TestClusterConfig
DefaultTestCluster testcluster.PersistenceTestCluster
VisibilityTestCluster testcluster.PersistenceTestCluster
}
)

var (
clusterName = []string{"active", "standby", "other"}
clusterReplicationConfig = []*types.ClusterReplicationConfiguration{
Expand Down Expand Up @@ -109,14 +76,6 @@ func TestNDCIntegrationTestSuite(t *testing.T) {
suite.Run(t, s)
}

func NewNDCIntegrationTestSuite(params NDCIntegrationTestSuiteParams) *NDCIntegrationTestSuite {
return &NDCIntegrationTestSuite{
clusterConfigs: params.ClusterConfigs,
defaultTestCluster: params.DefaultTestCluster,
visibilityTestCluster: params.VisibilityTestCluster,
}
}

func (s *NDCIntegrationTestSuite) SetupSuite() {
zapLogger, err := zap.NewDevelopment()
// cannot use s.Nil since it is not initialized
Expand Down
76 changes: 76 additions & 0 deletions host/ndc/test_suites.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) 2021 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 ndc

import (
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"github.com/uber/cadence/client/admin"
"github.com/uber/cadence/common/log"
"github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/common/persistence/persistence-tests/testcluster"
"github.com/uber/cadence/common/testing"
"github.com/uber/cadence/common/types"
"github.com/uber/cadence/host"
)

// NOTE: the following definitions can't be defined in *_test.go
// since they need to be exported and used by our internal tests

type (
NDCIntegrationTestSuite struct {
// override suite.Suite.Assertions with require.Assertions; this means that s.NotNil(nil) will stop the test,
// not merely log an error
*require.Assertions
suite.Suite
active *host.TestCluster
generator testing.Generator
serializer persistence.PayloadSerializer
logger log.Logger

domainName string
domainID string
version int64
versionIncrement int64
mockAdminClient map[string]admin.Client
standByReplicationTasksChan chan *types.ReplicationTask
standByTaskID int64

clusterConfigs []*host.TestClusterConfig
defaultTestCluster testcluster.PersistenceTestCluster
visibilityTestCluster testcluster.PersistenceTestCluster
}

NDCIntegrationTestSuiteParams struct {
ClusterConfigs []*host.TestClusterConfig
DefaultTestCluster testcluster.PersistenceTestCluster
VisibilityTestCluster testcluster.PersistenceTestCluster
}
)

func NewNDCIntegrationTestSuite(params NDCIntegrationTestSuiteParams) *NDCIntegrationTestSuite {
return &NDCIntegrationTestSuite{
clusterConfigs: params.ClusterConfigs,
defaultTestCluster: params.DefaultTestCluster,
visibilityTestCluster: params.VisibilityTestCluster,
}
}
7 changes: 0 additions & 7 deletions host/size_limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ import (
"github.com/uber/cadence/common/types"
)

type SizeLimitIntegrationSuite struct {
// override suite.Suite.Assertions with require.Assertions; this means that s.NotNil(nil) will stop the test,
// not merely log an error
*require.Assertions
IntegrationBase
}

func TestSizeLimitIntegrationSuite(t *testing.T) {
flag.Parse()

Expand Down
Loading

0 comments on commit 88549dd

Please sign in to comment.