Skip to content

Commit

Permalink
test: generate tmp work dirs
Browse files Browse the repository at this point in the history
Generate tmp work directories for each new Tarantool instance. It
prevents possible directory clash issues.

Part of #215
  • Loading branch information
DifferentialOrange committed Dec 12, 2022
1 parent 9648c4f commit 18855f1
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 45 deletions.
6 changes: 1 addition & 5 deletions connection_pool/connection_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2379,18 +2379,14 @@ func runTestMain(m *testing.M) int {
waitStart := 100 * time.Millisecond
connectRetry := 3
retryTimeout := 500 * time.Millisecond
workDirs := []string{
"work_dir1", "work_dir2",
"work_dir3", "work_dir4",
"work_dir5"}

// Tarantool supports streams and interactive transactions since version 2.10.0
isStreamUnsupported, err := test_helpers.IsTarantoolVersionLess(2, 10, 0)
if err != nil {
log.Fatalf("Could not check the Tarantool version")
}

instances, err = test_helpers.StartTarantoolInstances(servers, workDirs, test_helpers.StartOpts{
instances, err = test_helpers.StartTarantoolInstances(servers, test_helpers.StartOpts{
InitScript: initScript,
User: connOpts.User,
Pass: connOpts.Pass,
Expand Down
1 change: 0 additions & 1 deletion datetime/datetime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,6 @@ func runTestMain(m *testing.M) int {
instance, err := test_helpers.StartTarantool(test_helpers.StartOpts{
InitScript: "config.lua",
Listen: server,
WorkDir: "work_dir",
User: opts.User,
Pass: opts.Pass,
WaitStart: 100 * time.Millisecond,
Expand Down
1 change: 0 additions & 1 deletion decimal/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,6 @@ func runTestMain(m *testing.M) int {
instance, err := test_helpers.StartTarantool(test_helpers.StartOpts{
InitScript: "config.lua",
Listen: server,
WorkDir: "work_dir",
User: opts.User,
Pass: opts.Pass,
WaitStart: 100 * time.Millisecond,
Expand Down
2 changes: 0 additions & 2 deletions multi/multi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,6 @@ func runTestMain(m *testing.M) int {
inst1, err := test_helpers.StartTarantool(test_helpers.StartOpts{
InitScript: initScript,
Listen: server1,
WorkDir: "work_dir1",
User: connOpts.User,
Pass: connOpts.Pass,
WaitStart: waitStart,
Expand All @@ -609,7 +608,6 @@ func runTestMain(m *testing.M) int {
inst2, err := test_helpers.StartTarantool(test_helpers.StartOpts{
InitScript: initScript,
Listen: server2,
WorkDir: "work_dir2",
User: connOpts.User,
Pass: connOpts.Pass,
WaitStart: waitStart,
Expand Down
4 changes: 1 addition & 3 deletions queue/queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,6 @@ func runTestMain(m *testing.M) int {
inst, err := test_helpers.StartTarantool(test_helpers.StartOpts{
InitScript: "testdata/config.lua",
Listen: server,
WorkDir: "work_dir",
User: opts.User,
Pass: opts.Pass,
WaitStart: 100 * time.Millisecond,
Expand All @@ -913,15 +912,14 @@ func runTestMain(m *testing.M) int {

defer test_helpers.StopTarantoolWithCleanup(inst)

workDirs := []string{"work_dir1", "work_dir2"}
poolOpts := test_helpers.StartOpts{
InitScript: "testdata/pool.lua",
User: opts.User,
Pass: opts.Pass,
WaitStart: 3 * time.Second, // replication_timeout * 3
ConnectRetry: -1,
}
instances, err = test_helpers.StartTarantoolInstances(serversPool, workDirs, poolOpts)
instances, err = test_helpers.StartTarantoolInstances(serversPool, poolOpts)

if err != nil {
log.Fatalf("Failed to prepare test tarantool pool: %s", err)
Expand Down
1 change: 0 additions & 1 deletion ssl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ func serverTnt(serverOpts, clientOpts SslOpts) (test_helpers.TarantoolInstance,
ClientServer: tntHost,
ClientTransport: "ssl",
ClientSsl: clientOpts,
WorkDir: "work_dir_ssl",
User: "test",
Pass: "test",
WaitStart: 100 * time.Millisecond,
Expand Down
2 changes: 0 additions & 2 deletions tarantool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
var startOpts test_helpers.StartOpts = test_helpers.StartOpts{
InitScript: "config.lua",
Listen: server,
WorkDir: "work_dir",
User: opts.User,
Pass: opts.Pass,
WaitStart: 100 * time.Millisecond,
Expand Down Expand Up @@ -3317,7 +3316,6 @@ func TestConnection_NewWatcher_reconnect(t *testing.T) {
inst, err := test_helpers.StartTarantool(test_helpers.StartOpts{
InitScript: "config.lua",
Listen: server,
WorkDir: "work_dir",
User: opts.User,
Pass: opts.Pass,
WaitStart: 100 * time.Millisecond,
Expand Down
38 changes: 16 additions & 22 deletions test_helpers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ type StartOpts struct {
// a Tarantool instance.
ClientSsl tarantool.SslOpts

// WorkDir is box.cfg work_dir parameter for tarantool.
// Specify folder to store tarantool data files.
// Folder must be unique for each tarantool process used simultaneously.
// https://www.tarantool.io/en/doc/latest/reference/configuration/#confval-work_dir
WorkDir string

// SslCertsDir is a path to a directory with SSL certificates. It will be
// copied to the working directory.
SslCertsDir string
Expand Down Expand Up @@ -84,6 +78,10 @@ type TarantoolInstance struct {
// Cmd is a Tarantool command. Used to kill Tarantool process.
Cmd *exec.Cmd

// WorkDir is box.cfg work_dir parameter for Tarantool.
// https://www.tarantool.io/en/doc/latest/reference/configuration/#confval-work_dir
WorkDir string

// Options for restarting a tarantool instance.
Opts StartOpts
}
Expand Down Expand Up @@ -189,30 +187,26 @@ func RestartTarantool(inst *TarantoolInstance) error {
func StartTarantool(startOpts StartOpts) (TarantoolInstance, error) {
// Prepare tarantool command.
var inst TarantoolInstance

// Create work_dir for a new instance.
dir, err := os.MkdirTemp("", "work_dir")
if err != nil {
return inst, err
}
inst.WorkDir = dir

inst.Cmd = exec.Command("tarantool", startOpts.InitScript)

inst.Cmd.Env = append(
os.Environ(),
fmt.Sprintf("TEST_TNT_WORK_DIR=%s", startOpts.WorkDir),
fmt.Sprintf("TEST_TNT_WORK_DIR=%s", inst.WorkDir),
fmt.Sprintf("TEST_TNT_LISTEN=%s", startOpts.Listen),
fmt.Sprintf("TEST_TNT_MEMTX_USE_MVCC_ENGINE=%t", startOpts.MemtxUseMvccEngine),
)

// Clean up existing work_dir.
err := os.RemoveAll(startOpts.WorkDir)
if err != nil {
return inst, err
}

// Create work_dir.
err = os.Mkdir(startOpts.WorkDir, 0755)
if err != nil {
return inst, err
}

// Copy SSL certificates.
if startOpts.SslCertsDir != "" {
err = copySslCerts(startOpts.WorkDir, startOpts.SslCertsDir)
err = copySslCerts(inst.WorkDir, startOpts.SslCertsDir)
if err != nil {
return inst, err
}
Expand Down Expand Up @@ -289,8 +283,8 @@ func StopTarantool(inst TarantoolInstance) {
func StopTarantoolWithCleanup(inst TarantoolInstance) {
StopTarantool(inst)

if inst.Opts.WorkDir != "" {
if err := os.RemoveAll(inst.Opts.WorkDir); err != nil {
if inst.WorkDir != "" {
if err := os.RemoveAll(inst.WorkDir); err != nil {
log.Fatalf("Failed to clean work directory, got %s", err)
}
}
Expand Down
9 changes: 2 additions & 7 deletions test_helpers/pool_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,11 @@ func SetClusterRO(servers []string, connOpts tarantool.Opts, roles []bool) error
return nil
}

func StartTarantoolInstances(servers []string, workDirs []string, opts StartOpts) ([]TarantoolInstance, error) {
if len(servers) != len(workDirs) {
return nil, fmt.Errorf("number of servers should be equal to number of workDirs")
}

func StartTarantoolInstances(servers []string, opts StartOpts) ([]TarantoolInstance, error) {
instances := make([]TarantoolInstance, 0, len(servers))

for i, server := range servers {
for _, server := range servers {
opts.Listen = server
opts.WorkDir = workDirs[i]

instance, err := StartTarantool(opts)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion uuid/uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ func runTestMain(m *testing.M) int {
inst, err := test_helpers.StartTarantool(test_helpers.StartOpts{
InitScript: "config.lua",
Listen: server,
WorkDir: "work_dir",
User: opts.User,
Pass: opts.Pass,
WaitStart: 100 * time.Millisecond,
Expand Down

0 comments on commit 18855f1

Please sign in to comment.