|
|
@ -2,14 +2,10 @@ package os_test |
|
|
|
|
|
|
|
import ( |
|
|
|
"bytes" |
|
|
|
"context" |
|
|
|
"fmt" |
|
|
|
"os" |
|
|
|
"os/exec" |
|
|
|
"path/filepath" |
|
|
|
"syscall" |
|
|
|
"testing" |
|
|
|
"time" |
|
|
|
|
|
|
|
"github.com/stretchr/testify/require" |
|
|
|
tmos "github.com/tendermint/tendermint/libs/os" |
|
|
@ -43,32 +39,6 @@ func TestCopyFile(t *testing.T) { |
|
|
|
os.Remove(copyfile) |
|
|
|
} |
|
|
|
|
|
|
|
func TestTrapSignal(t *testing.T) { |
|
|
|
if os.Getenv("TM_TRAP_SIGNAL_TEST") == "1" { |
|
|
|
t.Log("inside test process") |
|
|
|
killer() |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
cmd, _, mockStderr := newTestProgram(t, "TM_TRAP_SIGNAL_TEST") |
|
|
|
|
|
|
|
err := cmd.Run() |
|
|
|
if err == nil { |
|
|
|
wantStderr := "exiting" |
|
|
|
if mockStderr.String() != wantStderr { |
|
|
|
t.Fatalf("stderr: want %q, got %q", wantStderr, mockStderr.String()) |
|
|
|
} |
|
|
|
|
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
if e, ok := err.(*exec.ExitError); ok && !e.Success() { |
|
|
|
t.Fatalf("wrong exit code, want 0, got %d", e.ExitCode()) |
|
|
|
} |
|
|
|
|
|
|
|
t.Fatal("this error should not be triggered") |
|
|
|
} |
|
|
|
|
|
|
|
func TestEnsureDir(t *testing.T) { |
|
|
|
tmp, err := os.MkdirTemp("", "ensure-dir") |
|
|
|
require.NoError(t, err) |
|
|
@ -102,43 +72,6 @@ func TestEnsureDir(t *testing.T) { |
|
|
|
require.Error(t, err) |
|
|
|
} |
|
|
|
|
|
|
|
type mockLogger struct{} |
|
|
|
|
|
|
|
func (ml mockLogger) Info(msg string, keyvals ...interface{}) {} |
|
|
|
|
|
|
|
func killer() { |
|
|
|
logger := mockLogger{} |
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background()) |
|
|
|
defer cancel() |
|
|
|
|
|
|
|
tmos.TrapSignal(ctx, logger, func() { _, _ = fmt.Fprintf(os.Stderr, "exiting") }) |
|
|
|
time.Sleep(1 * time.Second) |
|
|
|
|
|
|
|
p, err := os.FindProcess(os.Getpid()) |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
|
} |
|
|
|
|
|
|
|
if err := p.Signal(syscall.SIGTERM); err != nil { |
|
|
|
panic(err) |
|
|
|
} |
|
|
|
|
|
|
|
time.Sleep(1 * time.Second) |
|
|
|
} |
|
|
|
|
|
|
|
func newTestProgram(t *testing.T, environVar string) (cmd *exec.Cmd, stdout *bytes.Buffer, stderr *bytes.Buffer) { |
|
|
|
t.Helper() |
|
|
|
|
|
|
|
cmd = exec.Command(os.Args[0], "-test.run="+t.Name()) |
|
|
|
stdout, stderr = bytes.NewBufferString(""), bytes.NewBufferString("") |
|
|
|
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", environVar)) |
|
|
|
cmd.Stdout = stdout |
|
|
|
cmd.Stderr = stderr |
|
|
|
|
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// Ensure that using CopyFile does not truncate the destination file before
|
|
|
|
// the origin is positively a non-directory and that it is ready for copying.
|
|
|
|
// See https://github.com/tendermint/tendermint/issues/6427
|
|
|
|