From d092de68096ccc4efd8a256120224f0c7a9ca5c7 Mon Sep 17 00:00:00 2001 From: Georgy Moiseev Date: Mon, 12 Dec 2022 18:01:31 +0300 Subject: [PATCH] test: generate tmp work dirs 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 --- connection_pool/connection_pool_test.go | 6 +--- datetime/datetime_test.go | 1 - decimal/decimal_test.go | 1 - multi/multi_test.go | 2 -- queue/queue_test.go | 4 +-- ssl_test.go | 1 - tarantool_test.go | 2 -- test_helpers/main.go | 45 ++++++++++++++++--------- test_helpers/pool_helper.go | 9 +++-- uuid/uuid_test.go | 1 - 10 files changed, 39 insertions(+), 33 deletions(-) diff --git a/connection_pool/connection_pool_test.go b/connection_pool/connection_pool_test.go index 8dc8cc9da..970612b5e 100644 --- a/connection_pool/connection_pool_test.go +++ b/connection_pool/connection_pool_test.go @@ -2379,10 +2379,6 @@ 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) @@ -2390,7 +2386,7 @@ func runTestMain(m *testing.M) int { 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, diff --git a/datetime/datetime_test.go b/datetime/datetime_test.go index 5c63d0f4c..f4cc8b2a1 100644 --- a/datetime/datetime_test.go +++ b/datetime/datetime_test.go @@ -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, diff --git a/decimal/decimal_test.go b/decimal/decimal_test.go index 6fba0cc8f..d81e6c488 100644 --- a/decimal/decimal_test.go +++ b/decimal/decimal_test.go @@ -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, diff --git a/multi/multi_test.go b/multi/multi_test.go index d3a111a9f..3b395864c 100644 --- a/multi/multi_test.go +++ b/multi/multi_test.go @@ -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, @@ -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, diff --git a/queue/queue_test.go b/queue/queue_test.go index 85276e4a8..905fefc32 100644 --- a/queue/queue_test.go +++ b/queue/queue_test.go @@ -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, @@ -913,7 +912,6 @@ 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, @@ -921,7 +919,7 @@ func runTestMain(m *testing.M) int { 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) diff --git a/ssl_test.go b/ssl_test.go index 36ce4905f..769508284 100644 --- a/ssl_test.go +++ b/ssl_test.go @@ -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, diff --git a/tarantool_test.go b/tarantool_test.go index fdb129d07..96bf02254 100644 --- a/tarantool_test.go +++ b/tarantool_test.go @@ -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, @@ -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, diff --git a/test_helpers/main.go b/test_helpers/main.go index 77e22c535..f6e745617 100644 --- a/test_helpers/main.go +++ b/test_helpers/main.go @@ -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 @@ -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( @@ -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) diff --git a/test_helpers/pool_helper.go b/test_helpers/pool_helper.go index 0061870de..8c3a7e6ff 100644 --- a/test_helpers/pool_helper.go +++ b/test_helpers/pool_helper.go @@ -219,7 +219,8 @@ 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") } @@ -227,7 +228,11 @@ func StartTarantoolInstances(servers []string, workDirs []string, opts StartOpts 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 { diff --git a/uuid/uuid_test.go b/uuid/uuid_test.go index 6224a938b..e04ab5ab9 100644 --- a/uuid/uuid_test.go +++ b/uuid/uuid_test.go @@ -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,