Skip to content

Commit 8a86719

Browse files
committed
test: try fix test
1 parent 52e6388 commit 8a86719

File tree

3 files changed

+83
-76
lines changed

3 files changed

+83
-76
lines changed

test_helpers/main.go

Lines changed: 80 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -86,38 +86,35 @@ type TarantoolInstance struct {
8686
// Dialer to check that connection established.
8787
Dialer tarantool.Dialer
8888

89-
done chan error
89+
is_stopping bool
9090
is_done bool
9191
result error
92-
is_stopping bool
93-
}
94-
95-
// Status checks if Tarantool instance is still running.
96-
// Return true if it is running, false if it is not.
97-
// If instance was exit and error is nil - process completed success with zero status code.
98-
func (t *TarantoolInstance) Status() (bool, error) {
99-
if t.is_done {
100-
return false, t.result
101-
}
102-
103-
select {
104-
case t.result = <-t.done:
105-
t.is_done = true
106-
return false, t.result
107-
default:
108-
return true, nil
109-
}
11092
}
11193

11294
func (t *TarantoolInstance) checkDone() {
11395
t.is_done = false
11496
t.is_stopping = false
115-
t.done = make(chan error, 1)
116-
t.done <- t.Cmd.Wait()
97+
t.result = t.Cmd.Wait()
98+
t.is_done = true
11799
if !t.is_stopping {
118-
_, err := t.Status()
119-
log.Printf("Tarantool was unexpected terminated: %s", err)
100+
log.Printf("Tarantool %q was unexpected terminated: %s", t.Opts.Listen, t.result)
101+
}
102+
}
103+
104+
func (t *TarantoolInstance) Stop() error {
105+
log.Printf("Stopping Tarantool instance %q", t.Opts.Listen)
106+
t.is_stopping = true
107+
if t.is_done {
108+
return nil
109+
}
110+
if t.Cmd != nil && t.Cmd.Process != nil {
111+
if err := t.Cmd.Process.Kill(); err != nil && !t.is_done {
112+
return fmt.Errorf("failed to kill tarantool %q (pid %d), got %s",
113+
t.Opts.Listen, t.Cmd.Process.Pid, err)
114+
}
115+
t.Cmd.Process = nil
120116
}
117+
return nil
121118
}
122119

123120
func isReady(dialer tarantool.Dialer, opts *tarantool.Opts) error {
@@ -235,46 +232,77 @@ func RestartTarantool(inst *TarantoolInstance) error {
235232
return err
236233
}
237234

235+
func removeByMask(dir string, masks ...string) error {
236+
for _, mask := range masks {
237+
files, err := filepath.Glob(filepath.Join(dir, mask))
238+
if err != nil {
239+
return err
240+
}
241+
for _, f := range files {
242+
if err = os.Remove(f); err != nil {
243+
return err
244+
}
245+
}
246+
}
247+
return nil
248+
}
249+
250+
func prepareDir(workDir string) (string, error) {
251+
if workDir == "" {
252+
dir, err := os.MkdirTemp("", "work_dir")
253+
if err != nil {
254+
return "", err
255+
}
256+
return dir, nil
257+
}
258+
// Create work_dir.
259+
err := os.MkdirAll(workDir, 0755)
260+
if err != nil {
261+
return "", err
262+
}
263+
264+
// Clean up existing work_dir.
265+
// TODO: Ensure that nested files will be removed.
266+
err = removeByMask(workDir, "*.snap", "*.xlog")
267+
if err != nil {
268+
return "", err
269+
}
270+
return workDir, nil
271+
}
272+
238273
// StartTarantool starts a tarantool instance for tests
239274
// with specifies parameters (refer to StartOpts).
240275
// Process must be stopped with StopTarantool.
241276
func StartTarantool(startOpts StartOpts) (TarantoolInstance, error) {
242277
// Prepare tarantool command.
243278
var inst TarantoolInstance
244-
var dir string
245279
var err error
246280

247281
inst.Dialer = startOpts.Dialer
248-
249-
if startOpts.WorkDir == "" {
250-
dir, err = os.MkdirTemp("", "work_dir")
251-
if err != nil {
252-
return inst, err
253-
}
254-
startOpts.WorkDir = dir
255-
} else {
256-
// Clean up existing work_dir.
257-
err = os.RemoveAll(startOpts.WorkDir)
258-
if err != nil {
259-
return inst, err
260-
}
261-
262-
// Create work_dir.
263-
err = os.Mkdir(startOpts.WorkDir, 0755)
264-
if err != nil {
265-
return inst, err
266-
}
282+
startOpts.WorkDir, err = prepareDir(startOpts.WorkDir)
283+
if err != nil {
284+
return inst, fmt.Errorf("failed prepare working dir %q: %w", startOpts.WorkDir, err)
267285
}
268-
args := []string{}
269286

287+
args := []string{}
270288
if startOpts.InitScript != "" {
289+
if !filepath.IsAbs(startOpts.InitScript) {
290+
cwd, err := os.Getwd()
291+
if err != nil {
292+
return inst, fmt.Errorf("failed to get current working directory: %w", err)
293+
}
294+
startOpts.InitScript = filepath.Join(cwd, startOpts.InitScript)
295+
}
271296
args = append(args, startOpts.InitScript)
272297
}
273298
if startOpts.ConfigFile != "" && startOpts.InstanceName != "" {
274299
args = append(args, "--config", startOpts.ConfigFile)
275300
args = append(args, "--name", startOpts.InstanceName)
276301
}
277302
inst.Cmd = exec.Command(getTarantoolExec(), args...)
303+
inst.Cmd.Dir = startOpts.WorkDir
304+
inst.Cmd.Stdout = os.Stderr
305+
inst.Cmd.Stderr = os.Stderr
278306

279307
inst.Cmd.Env = append(
280308
os.Environ(),
@@ -327,15 +355,16 @@ func StartTarantool(startOpts StartOpts) (TarantoolInstance, error) {
327355
}
328356
}
329357

330-
working, err_st := inst.Status()
331-
if !working || err_st != nil {
358+
if inst.is_done && inst.result != nil {
332359
StopTarantool(inst)
333-
return TarantoolInstance{}, fmt.Errorf("unexpected terminated Tarantool: %w", err_st)
360+
return TarantoolInstance{}, fmt.Errorf("unexpected terminated Tarantool %q: %w",
361+
inst.Opts.Listen, inst.result)
334362
}
335363

336364
if err != nil {
337365
StopTarantool(inst)
338-
return TarantoolInstance{}, fmt.Errorf("failed to connect Tarantool: %w", err)
366+
return TarantoolInstance{}, fmt.Errorf("failed to connect Tarantool %q: %w",
367+
inst.Opts.Listen, err)
339368
}
340369

341370
return inst, nil
@@ -345,25 +374,9 @@ func StartTarantool(startOpts StartOpts) (TarantoolInstance, error) {
345374
// with StartTarantool. Waits until any resources
346375
// associated with the process is released. If something went wrong, fails.
347376
func StopTarantool(inst TarantoolInstance) {
348-
log.Printf("Stopping Tarantool instance")
349-
inst.is_stopping = true
350-
if inst.Cmd != nil && inst.Cmd.Process != nil {
351-
if err := inst.Cmd.Process.Kill(); err != nil {
352-
is_running, _ := inst.Status()
353-
if is_running {
354-
log.Fatalf("Failed to kill tarantool (pid %d), got %s", inst.Cmd.Process.Pid, err)
355-
}
356-
}
357-
358-
// Wait releases any resources associated with the Process.
359-
if _, err := inst.Cmd.Process.Wait(); err != nil {
360-
is_running, _ := inst.Status()
361-
if is_running {
362-
log.Fatalf("Failed to wait for Tarantool process to exit, got %s", err)
363-
}
364-
}
365-
366-
inst.Cmd.Process = nil
377+
err := inst.Stop()
378+
if err != nil {
379+
log.Fatal(err)
367380
}
368381
}
369382

test_helpers/tcs/prepare.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ func writeConfig(name string, port int) error {
3939

4040
func makeOpts(port int) (test_helpers.StartOpts, error) {
4141
opts := test_helpers.StartOpts{}
42-
dir, err := os.MkdirTemp("", "tcs_dir")
42+
var err error
43+
opts.WorkDir, err = os.MkdirTemp("", "tcs_dir")
4344
if err != nil {
4445
return opts, err
4546
}
46-
opts.ConfigFile = filepath.Join(dir, "config.yaml")
47+
opts.ConfigFile = filepath.Join(opts.WorkDir, "config.yaml")
4748
err = writeConfig(opts.ConfigFile, port)
4849
if err != nil {
4950
return opts, fmt.Errorf("can't save file %q: %w", opts.ConfigFile, err)

test_helpers/tcs/tcs.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"log"
87
"net"
9-
"os"
10-
"path/filepath"
118
"testing"
129

1310
"github.com/tarantool/go-tarantool/v2"
@@ -133,10 +130,6 @@ func (t *TCS) Stop() {
133130
t.conn.Close()
134131
}
135132
test_helpers.StopTarantoolWithCleanup(t.inst)
136-
tcs_dir := filepath.Dir(t.inst.Opts.ConfigFile)
137-
if err := os.RemoveAll(tcs_dir); err != nil {
138-
log.Fatalf("Failed to clean TcS directory, got %s", err)
139-
}
140133
}
141134

142135
// Put implements "config.storage.put" method.

0 commit comments

Comments
 (0)