Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved Cassandra test utility methods to a new file so they can be used outside of temporal repo #2434

Merged
merged 5 commits into from
Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 10 additions & 101 deletions common/persistence/tests/cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@
package tests

import (
"fmt"
"path/filepath"
"testing"

"github.com/stretchr/testify/suite"

"go.temporal.io/server/common/config"
"go.temporal.io/server/common/log"
p "go.temporal.io/server/common/persistence"
"go.temporal.io/server/common/persistence/cassandra"
"go.temporal.io/server/common/persistence/nosql/nosqlplugin/cassandra/gocql"
_ "go.temporal.io/server/common/persistence/sql/sqlplugin/mysql"
"go.temporal.io/server/common/resolver"
"go.temporal.io/server/common/shuffle"
Expand All @@ -50,17 +46,12 @@ const (
testCassandraPassword = "temporal"
testCassandraDatabaseNamePrefix = "test_"
testCassandraDatabaseNameSuffix = "temporal_persistence"

// TODO hard code this dir for now
// need to merge persistence test config / initialization in one place
testCassandraExecutionSchema = "../../../schema/cassandra/temporal/schema.cql"
testCassandraVisibilitySchema = "../../../schema/cassandra/visibility/schema.cql"
)

func TestCassandraExecutionMutableStateStoreSuite(t *testing.T) {
cfg := NewCassandraConfig()
cfg := newCassandraConfig()
SetupCassandraDatabase(cfg)
SetupCassandraSchema(cfg)
SetUpCassandraSchema(cfg)
logger := log.NewNoopLogger()
factory := cassandra.NewFactory(
*cfg,
Expand All @@ -86,9 +77,9 @@ func TestCassandraExecutionMutableStateStoreSuite(t *testing.T) {
}

func TestCassandraHistoryStoreSuite(t *testing.T) {
cfg := NewCassandraConfig()
cfg := newCassandraConfig()
SetupCassandraDatabase(cfg)
SetupCassandraSchema(cfg)
SetUpCassandraSchema(cfg)
logger := log.NewNoopLogger()
factory := cassandra.NewFactory(
*cfg,
Expand All @@ -110,9 +101,9 @@ func TestCassandraHistoryStoreSuite(t *testing.T) {
}

func TestCassandraTaskQueueSuite(t *testing.T) {
cfg := NewCassandraConfig()
cfg := newCassandraConfig()
SetupCassandraDatabase(cfg)
SetupCassandraSchema(cfg)
SetUpCassandraSchema(cfg)
logger := log.NewNoopLogger()
factory := cassandra.NewFactory(
*cfg,
Expand All @@ -134,9 +125,9 @@ func TestCassandraTaskQueueSuite(t *testing.T) {
}

func TestCassandraTaskQueueTaskSuite(t *testing.T) {
cfg := NewCassandraConfig()
cfg := newCassandraConfig()
SetupCassandraDatabase(cfg)
SetupCassandraSchema(cfg)
SetUpCassandraSchema(cfg)
logger := log.NewNoopLogger()
factory := cassandra.NewFactory(
*cfg,
Expand All @@ -157,8 +148,8 @@ func TestCassandraTaskQueueTaskSuite(t *testing.T) {
suite.Run(t, s)
}

// NewCassandraConfig returns a new Cassandra config for test
func NewCassandraConfig() *config.Cassandra {
// newCassandraConfig returns a new Cassandra config for test
func newCassandraConfig() *config.Cassandra {
return &config.Cassandra{
User: testCassandraUser,
Password: testCassandraPassword,
Expand All @@ -167,85 +158,3 @@ func NewCassandraConfig() *config.Cassandra {
Keyspace: testCassandraDatabaseNamePrefix + shuffle.String(testCassandraDatabaseNameSuffix),
}
}

func SetupCassandraDatabase(cfg *config.Cassandra) {
adminCfg := *cfg
// NOTE need to connect with empty name to create new database
adminCfg.Keyspace = "system"

session, err := gocql.NewSession(adminCfg, resolver.NewNoopResolver(), log.NewNoopLogger())
if err != nil {
panic(fmt.Sprintf("unable to create Cassandra session: %v", err))
}
defer session.Close()

if err := cassandra.CreateCassandraKeyspace(
session,
cfg.Keyspace,
1,
true,
log.NewNoopLogger(),
); err != nil {
panic(fmt.Sprintf("unable to create Cassandra keyspace: %v", err))
}
}

func SetupCassandraSchema(cfg *config.Cassandra) {
session, err := gocql.NewSession(*cfg, resolver.NewNoopResolver(), log.NewNoopLogger())
if err != nil {
panic(fmt.Sprintf("unable to create Cassandra session: %v", err))
}
defer session.Close()

schemaPath, err := filepath.Abs(testCassandraExecutionSchema)
if err != nil {
panic(err)
}

statements, err := p.LoadAndSplitQuery([]string{schemaPath})
if err != nil {
panic(err)
}

for _, stmt := range statements {
if err = session.Query(stmt).Exec(); err != nil {
panic(err)
}
}

schemaPath, err = filepath.Abs(testCassandraVisibilitySchema)
if err != nil {
panic(err)
}

statements, err = p.LoadAndSplitQuery([]string{schemaPath})
if err != nil {
panic(err)
}

for _, stmt := range statements {
if err = session.Query(stmt).Exec(); err != nil {
panic(err)
}
}
}

func TearDownCassandraKeyspace(cfg *config.Cassandra) {
adminCfg := *cfg
// NOTE need to connect with empty name to create new database
adminCfg.Keyspace = "system"

session, err := gocql.NewSession(adminCfg, resolver.NewNoopResolver(), log.NewNoopLogger())
if err != nil {
panic(fmt.Sprintf("unable to create Cassandra session: %v", err))
}
defer session.Close()

if err := cassandra.DropCassandraKeyspace(
session,
cfg.Keyspace,
log.NewNoopLogger(),
); err != nil {
panic(fmt.Sprintf("unable to drop Cassandra keyspace: %v", err))
}
}
101 changes: 101 additions & 0 deletions common/persistence/tests/cassandra_test_util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package tests

import (
"fmt"
"go.temporal.io/server/common/config"
"go.temporal.io/server/common/log"
p "go.temporal.io/server/common/persistence"
"go.temporal.io/server/common/persistence/cassandra"
"go.temporal.io/server/common/persistence/nosql/nosqlplugin/cassandra/gocql"
"go.temporal.io/server/common/resolver"
"path/filepath"
)

const (
// TODO hard code this dir for now
// need to merge persistence test config / initialization in one place
testCassandraExecutionSchema = "../../../schema/cassandra/temporal/schema.cql"
testCassandraVisibilitySchema = "../../../schema/cassandra/visibility/schema.cql"
)

func SetupCassandraDatabase(cfg *config.Cassandra) {
adminCfg := *cfg
// NOTE need to connect with empty name to create new database
adminCfg.Keyspace = "system"

session, err := gocql.NewSession(adminCfg, resolver.NewNoopResolver(), log.NewNoopLogger())
if err != nil {
panic(fmt.Sprintf("unable to create Cassandra session: %v", err))
}
defer session.Close()

if err := cassandra.CreateCassandraKeyspace(
session,
cfg.Keyspace,
1,
true,
log.NewNoopLogger(),
); err != nil {
panic(fmt.Sprintf("unable to create Cassandra keyspace: %v", err))
}
}

func SetUpCassandraSchema(cfg *config.Cassandra) {
session, err := gocql.NewSession(*cfg, resolver.NewNoopResolver(), log.NewNoopLogger())
if err != nil {
panic(fmt.Sprintf("unable to create Cassandra session: %v", err))
}
defer session.Close()

schemaPath, err := filepath.Abs(testCassandraExecutionSchema)
if err != nil {
panic(err)
}

statements, err := p.LoadAndSplitQuery([]string{schemaPath})
if err != nil {
panic(err)
}

for _, stmt := range statements {
if err = session.Query(stmt).Exec(); err != nil {
panic(err)
}
}

schemaPath, err = filepath.Abs(testCassandraVisibilitySchema)
if err != nil {
panic(err)
}

statements, err = p.LoadAndSplitQuery([]string{schemaPath})
if err != nil {
panic(err)
}

for _, stmt := range statements {
if err = session.Query(stmt).Exec(); err != nil {
panic(err)
}
}
}

func TearDownCassandraKeyspace(cfg *config.Cassandra) {
adminCfg := *cfg
// NOTE need to connect with empty name to create new database
adminCfg.Keyspace = "system"

session, err := gocql.NewSession(adminCfg, resolver.NewNoopResolver(), log.NewNoopLogger())
if err != nil {
panic(fmt.Sprintf("unable to create Cassandra session: %v", err))
}
defer session.Close()

if err := cassandra.DropCassandraKeyspace(
session,
cfg.Keyspace,
log.NewNoopLogger(),
); err != nil {
panic(fmt.Sprintf("unable to drop Cassandra keyspace: %v", err))
}
}