|
@ -1,11 +1,15 @@ |
|
|
package os |
|
|
|
|
|
|
|
|
package os_test |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
"bytes" |
|
|
"bytes" |
|
|
"fmt" |
|
|
"fmt" |
|
|
"io/ioutil" |
|
|
"io/ioutil" |
|
|
"os" |
|
|
"os" |
|
|
|
|
|
"os/exec" |
|
|
"testing" |
|
|
"testing" |
|
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
tmos "github.com/tendermint/tendermint/libs/os" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
func TestCopyFile(t *testing.T) { |
|
|
func TestCopyFile(t *testing.T) { |
|
@ -20,7 +24,7 @@ func TestCopyFile(t *testing.T) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
copyfile := fmt.Sprintf("%s.copy", tmpfile.Name()) |
|
|
copyfile := fmt.Sprintf("%s.copy", tmpfile.Name()) |
|
|
if err := CopyFile(tmpfile.Name(), copyfile); err != nil { |
|
|
|
|
|
|
|
|
if err := tmos.CopyFile(tmpfile.Name(), copyfile); err != nil { |
|
|
t.Fatal(err) |
|
|
t.Fatal(err) |
|
|
} |
|
|
} |
|
|
if _, err := os.Stat(copyfile); os.IsNotExist(err) { |
|
|
if _, err := os.Stat(copyfile); os.IsNotExist(err) { |
|
@ -35,3 +39,51 @@ func TestCopyFile(t *testing.T) { |
|
|
} |
|
|
} |
|
|
os.Remove(copyfile) |
|
|
os.Remove(copyfile) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestTrapSignal(t *testing.T) { |
|
|
|
|
|
if os.Getenv("TM_TRAP_SIGNAL_TEST") == "1" { |
|
|
|
|
|
t.Log("inside test process") |
|
|
|
|
|
killer() |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
cmd := exec.Command(os.Args[0], "-test.run="+t.Name()) |
|
|
|
|
|
mockStderr := bytes.NewBufferString("") |
|
|
|
|
|
cmd.Env = append(os.Environ(), "TM_TRAP_SIGNAL_TEST=1") |
|
|
|
|
|
cmd.Stderr = mockStderr |
|
|
|
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type mockLogger struct{} |
|
|
|
|
|
|
|
|
|
|
|
func (ml mockLogger) Info(msg string, keyvals ...interface{}) {} |
|
|
|
|
|
|
|
|
|
|
|
func killer() { |
|
|
|
|
|
logger := mockLogger{} |
|
|
|
|
|
|
|
|
|
|
|
tmos.TrapSignal(logger, func() { _, _ = fmt.Fprintf(os.Stderr, "exiting") }) |
|
|
|
|
|
time.Sleep(1 * time.Second) |
|
|
|
|
|
|
|
|
|
|
|
// use Kill() to test SIGTERM
|
|
|
|
|
|
if err := tmos.Kill(); err != nil { |
|
|
|
|
|
panic(err) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second) |
|
|
|
|
|
} |