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, if not
specified. It prevents possible directory clash issues.

Later `ioutil.TempDir` (deprecated since Go 1.17) must be replaced with
`os.MkdirTemp` (introduced in Go 1.16 as a replacement) when we drop
support of Go 1.16 an older.

Part of #215
  • Loading branch information
DifferentialOrange committed Dec 14, 2022
1 parent 6cfd45b commit d092de6
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 33 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, nil, 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, nil, 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
45 changes: 30 additions & 15 deletions test_helpers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ 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.
// WorkDir is box.cfg work_dir parameter for a Tarantool instance:
// a folder to store data files. If not specified, helpers create a
// new temporary directory.
// Folder must be unique for each Tarantool process used simultaneously.
// https://www.tarantool.io/en/doc/latest/reference/configuration/#confval-work_dir
WorkDir string

Expand Down Expand Up @@ -189,6 +190,32 @@ func RestartTarantool(inst *TarantoolInstance) error {
func StartTarantool(startOpts StartOpts) (TarantoolInstance, error) {
// Prepare tarantool command.
var inst TarantoolInstance
var dir string
var err error

if startOpts.WorkDir == "" {
// Create work_dir for a new instance.
// TO DO: replace with `os.MkdirTemp` when we drop support of
// Go 1.16 an older
dir, err = ioutil.TempDir("", "work_dir")
if err != nil {
return inst, err
}
startOpts.WorkDir = dir
} else {
// 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
}
}

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

inst.Cmd.Env = append(
Expand All @@ -198,18 +225,6 @@ func StartTarantool(startOpts StartOpts) (TarantoolInstance, error) {
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)
Expand Down
9 changes: 7 additions & 2 deletions test_helpers/pool_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,20 @@ func SetClusterRO(servers []string, connOpts tarantool.Opts, roles []bool) error
}

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

instances := make([]TarantoolInstance, 0, len(servers))

for i, server := range servers {
opts.Listen = server
opts.WorkDir = workDirs[i]
if isUserWorkDirs {
opts.WorkDir = workDirs[i]
} else {
opts.WorkDir = ""
}

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 d092de6

Please sign in to comment.