Skip to content

Commit

Permalink
Moved Cassandra test utility methods to a new file so they can be use…
Browse files Browse the repository at this point in the history
…d outside of temporal repo (#2434)

* Changed NewCassandraConfig to private function

* Moved Cassandra test utility methods to a new file so they can be used outside of temporal repo.
  • Loading branch information
Jason Roselander authored Jan 29, 2022
1 parent f4c4ab6 commit 31dc2f8
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 105 deletions.
119 changes: 14 additions & 105 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()
SetupCassandraDatabase(cfg)
SetupCassandraSchema(cfg)
cfg := newCassandraConfig()
SetUpCassandraDatabase(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()
SetupCassandraDatabase(cfg)
SetupCassandraSchema(cfg)
cfg := newCassandraConfig()
SetUpCassandraDatabase(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()
SetupCassandraDatabase(cfg)
SetupCassandraSchema(cfg)
cfg := newCassandraConfig()
SetUpCassandraDatabase(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()
SetupCassandraDatabase(cfg)
SetupCassandraSchema(cfg)
cfg := newCassandraConfig()
SetUpCassandraDatabase(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))
}
}
126 changes: 126 additions & 0 deletions common/persistence/tests/cassandra_test_util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// 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 (
"fmt"
"path/filepath"

"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"
)

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))
}
}

0 comments on commit 31dc2f8

Please sign in to comment.