From 31dc2f81c63cebfb32e2ce33997944e61a4c3b9b Mon Sep 17 00:00:00 2001 From: Jason Roselander Date: Fri, 28 Jan 2022 18:52:53 -0800 Subject: [PATCH] Moved Cassandra test utility methods to a new file so they can be used 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. --- common/persistence/tests/cassandra_test.go | 119 ++--------------- .../persistence/tests/cassandra_test_util.go | 126 ++++++++++++++++++ 2 files changed, 140 insertions(+), 105 deletions(-) create mode 100644 common/persistence/tests/cassandra_test_util.go diff --git a/common/persistence/tests/cassandra_test.go b/common/persistence/tests/cassandra_test.go index e9796888dfd..e638fb6d522 100644 --- a/common/persistence/tests/cassandra_test.go +++ b/common/persistence/tests/cassandra_test.go @@ -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" @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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)) - } -} diff --git a/common/persistence/tests/cassandra_test_util.go b/common/persistence/tests/cassandra_test_util.go new file mode 100644 index 00000000000..635c807ec70 --- /dev/null +++ b/common/persistence/tests/cassandra_test_util.go @@ -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)) + } +}