Skip to content

Commit 770ad3a

Browse files
DifferentialOrangeoleg-jukovec
authored andcommitted
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
1 parent 3dc36de commit 770ad3a

File tree

10 files changed

+39
-33
lines changed

10 files changed

+39
-33
lines changed

connection_pool/connection_pool_test.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -2379,18 +2379,14 @@ func runTestMain(m *testing.M) int {
23792379
waitStart := 100 * time.Millisecond
23802380
connectRetry := 3
23812381
retryTimeout := 500 * time.Millisecond
2382-
workDirs := []string{
2383-
"work_dir1", "work_dir2",
2384-
"work_dir3", "work_dir4",
2385-
"work_dir5"}
23862382

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

2393-
instances, err = test_helpers.StartTarantoolInstances(servers, workDirs, test_helpers.StartOpts{
2389+
instances, err = test_helpers.StartTarantoolInstances(servers, nil, test_helpers.StartOpts{
23942390
InitScript: initScript,
23952391
User: connOpts.User,
23962392
Pass: connOpts.Pass,

datetime/datetime_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,6 @@ func runTestMain(m *testing.M) int {
11411141
instance, err := test_helpers.StartTarantool(test_helpers.StartOpts{
11421142
InitScript: "config.lua",
11431143
Listen: server,
1144-
WorkDir: "work_dir",
11451144
User: opts.User,
11461145
Pass: opts.Pass,
11471146
WaitStart: 100 * time.Millisecond,

decimal/decimal_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,6 @@ func runTestMain(m *testing.M) int {
613613
instance, err := test_helpers.StartTarantool(test_helpers.StartOpts{
614614
InitScript: "config.lua",
615615
Listen: server,
616-
WorkDir: "work_dir",
617616
User: opts.User,
618617
Pass: opts.Pass,
619618
WaitStart: 100 * time.Millisecond,

multi/multi_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,6 @@ func runTestMain(m *testing.M) int {
592592
inst1, err := test_helpers.StartTarantool(test_helpers.StartOpts{
593593
InitScript: initScript,
594594
Listen: server1,
595-
WorkDir: "work_dir1",
596595
User: connOpts.User,
597596
Pass: connOpts.Pass,
598597
WaitStart: waitStart,
@@ -609,7 +608,6 @@ func runTestMain(m *testing.M) int {
609608
inst2, err := test_helpers.StartTarantool(test_helpers.StartOpts{
610609
InitScript: initScript,
611610
Listen: server2,
612-
WorkDir: "work_dir2",
613611
User: connOpts.User,
614612
Pass: connOpts.Pass,
615613
WaitStart: waitStart,

queue/queue_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,6 @@ func runTestMain(m *testing.M) int {
899899
inst, err := test_helpers.StartTarantool(test_helpers.StartOpts{
900900
InitScript: "testdata/config.lua",
901901
Listen: server,
902-
WorkDir: "work_dir",
903902
User: opts.User,
904903
Pass: opts.Pass,
905904
WaitStart: 100 * time.Millisecond,
@@ -913,15 +912,14 @@ func runTestMain(m *testing.M) int {
913912

914913
defer test_helpers.StopTarantoolWithCleanup(inst)
915914

916-
workDirs := []string{"work_dir1", "work_dir2"}
917915
poolOpts := test_helpers.StartOpts{
918916
InitScript: "testdata/pool.lua",
919917
User: opts.User,
920918
Pass: opts.Pass,
921919
WaitStart: 3 * time.Second, // replication_timeout * 3
922920
ConnectRetry: -1,
923921
}
924-
instances, err = test_helpers.StartTarantoolInstances(serversPool, workDirs, poolOpts)
922+
instances, err = test_helpers.StartTarantoolInstances(serversPool, nil, poolOpts)
925923

926924
if err != nil {
927925
log.Fatalf("Failed to prepare test tarantool pool: %s", err)

ssl_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ func serverTnt(serverOpts, clientOpts SslOpts) (test_helpers.TarantoolInstance,
126126
ClientServer: tntHost,
127127
ClientTransport: "ssl",
128128
ClientSsl: clientOpts,
129-
WorkDir: "work_dir_ssl",
130129
User: "test",
131130
Pass: "test",
132131
WaitStart: 100 * time.Millisecond,

tarantool_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
var startOpts test_helpers.StartOpts = test_helpers.StartOpts{
2424
InitScript: "config.lua",
2525
Listen: server,
26-
WorkDir: "work_dir",
2726
User: opts.User,
2827
Pass: opts.Pass,
2928
WaitStart: 100 * time.Millisecond,
@@ -3317,7 +3316,6 @@ func TestConnection_NewWatcher_reconnect(t *testing.T) {
33173316
inst, err := test_helpers.StartTarantool(test_helpers.StartOpts{
33183317
InitScript: "config.lua",
33193318
Listen: server,
3320-
WorkDir: "work_dir",
33213319
User: opts.User,
33223320
Pass: opts.Pass,
33233321
WaitStart: 100 * time.Millisecond,

test_helpers/main.go

+30-15
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ type StartOpts struct {
4747
// a Tarantool instance.
4848
ClientSsl tarantool.SslOpts
4949

50-
// WorkDir is box.cfg work_dir parameter for tarantool.
51-
// Specify folder to store tarantool data files.
52-
// Folder must be unique for each tarantool process used simultaneously.
50+
// WorkDir is box.cfg work_dir parameter for a Tarantool instance:
51+
// a folder to store data files. If not specified, helpers create a
52+
// new temporary directory.
53+
// Folder must be unique for each Tarantool process used simultaneously.
5354
// https://www.tarantool.io/en/doc/latest/reference/configuration/#confval-work_dir
5455
WorkDir string
5556

@@ -189,6 +190,32 @@ func RestartTarantool(inst *TarantoolInstance) error {
189190
func StartTarantool(startOpts StartOpts) (TarantoolInstance, error) {
190191
// Prepare tarantool command.
191192
var inst TarantoolInstance
193+
var dir string
194+
var err error
195+
196+
if startOpts.WorkDir == "" {
197+
// Create work_dir for a new instance.
198+
// TO DO: replace with `os.MkdirTemp` when we drop support of
199+
// Go 1.16 an older
200+
dir, err = ioutil.TempDir("", "work_dir")
201+
if err != nil {
202+
return inst, err
203+
}
204+
startOpts.WorkDir = dir
205+
} else {
206+
// Clean up existing work_dir.
207+
err = os.RemoveAll(startOpts.WorkDir)
208+
if err != nil {
209+
return inst, err
210+
}
211+
212+
// Create work_dir.
213+
err = os.Mkdir(startOpts.WorkDir, 0755)
214+
if err != nil {
215+
return inst, err
216+
}
217+
}
218+
192219
inst.Cmd = exec.Command("tarantool", startOpts.InitScript)
193220

194221
inst.Cmd.Env = append(
@@ -198,18 +225,6 @@ func StartTarantool(startOpts StartOpts) (TarantoolInstance, error) {
198225
fmt.Sprintf("TEST_TNT_MEMTX_USE_MVCC_ENGINE=%t", startOpts.MemtxUseMvccEngine),
199226
)
200227

201-
// Clean up existing work_dir.
202-
err := os.RemoveAll(startOpts.WorkDir)
203-
if err != nil {
204-
return inst, err
205-
}
206-
207-
// Create work_dir.
208-
err = os.Mkdir(startOpts.WorkDir, 0755)
209-
if err != nil {
210-
return inst, err
211-
}
212-
213228
// Copy SSL certificates.
214229
if startOpts.SslCertsDir != "" {
215230
err = copySslCerts(startOpts.WorkDir, startOpts.SslCertsDir)

test_helpers/pool_helper.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,20 @@ func SetClusterRO(servers []string, connOpts tarantool.Opts, roles []bool) error
219219
}
220220

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

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

228229
for i, server := range servers {
229230
opts.Listen = server
230-
opts.WorkDir = workDirs[i]
231+
if isUserWorkDirs {
232+
opts.WorkDir = workDirs[i]
233+
} else {
234+
opts.WorkDir = ""
235+
}
231236

232237
instance, err := StartTarantool(opts)
233238
if err != nil {

uuid/uuid_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ func runTestMain(m *testing.M) int {
155155
inst, err := test_helpers.StartTarantool(test_helpers.StartOpts{
156156
InitScript: "config.lua",
157157
Listen: server,
158-
WorkDir: "work_dir",
159158
User: opts.User,
160159
Pass: opts.Pass,
161160
WaitStart: 100 * time.Millisecond,

0 commit comments

Comments
 (0)