2017-06-01 02:39:35 -04:00
|
|
|
// +build linux
|
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package signal // import "github.com/docker/docker/pkg/signal"
|
2017-06-01 02:39:35 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
2017-06-01 02:39:35 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func buildTestBinary(t *testing.T, tmpdir string, prefix string) (string, string) {
|
|
|
|
tmpDir, err := ioutil.TempDir(tmpdir, prefix)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-06-01 02:39:35 -04:00
|
|
|
exePath := tmpDir + "/" + prefix
|
|
|
|
wd, _ := os.Getwd()
|
|
|
|
testHelperCode := wd + "/testfiles/main.go"
|
|
|
|
cmd := exec.Command("go", "build", "-o", exePath, testHelperCode)
|
|
|
|
err = cmd.Run()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-06-01 02:39:35 -04:00
|
|
|
return exePath, tmpDir
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTrap(t *testing.T) {
|
|
|
|
var sigmap = []struct {
|
|
|
|
name string
|
|
|
|
signal os.Signal
|
|
|
|
multiple bool
|
|
|
|
}{
|
|
|
|
{"TERM", syscall.SIGTERM, false},
|
|
|
|
{"QUIT", syscall.SIGQUIT, true},
|
|
|
|
{"INT", os.Interrupt, false},
|
|
|
|
{"TERM", syscall.SIGTERM, true},
|
|
|
|
{"INT", os.Interrupt, true},
|
|
|
|
}
|
|
|
|
exePath, tmpDir := buildTestBinary(t, "", "main")
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
for _, v := range sigmap {
|
|
|
|
cmd := exec.Command(exePath)
|
|
|
|
cmd.Env = append(os.Environ(), fmt.Sprintf("SIGNAL_TYPE=%s", v.name))
|
|
|
|
if v.multiple {
|
|
|
|
cmd.Env = append(cmd.Env, "IF_MULTIPLE=1")
|
|
|
|
}
|
|
|
|
err := cmd.Start()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-06-01 02:39:35 -04:00
|
|
|
err = cmd.Wait()
|
|
|
|
if e, ok := err.(*exec.ExitError); ok {
|
|
|
|
code := e.Sys().(syscall.WaitStatus).ExitStatus()
|
|
|
|
if v.multiple {
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(128+int(v.signal.(syscall.Signal)), code))
|
2017-06-01 02:39:35 -04:00
|
|
|
} else {
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(99, code))
|
2017-06-01 02:39:35 -04:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.Fatal("process didn't end with any error")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDumpStacks(t *testing.T) {
|
|
|
|
directory, err := ioutil.TempDir("", "test-dump-tasks")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err)
|
2017-06-01 02:39:35 -04:00
|
|
|
defer os.RemoveAll(directory)
|
|
|
|
dumpPath, err := DumpStacks(directory)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err)
|
2017-06-01 02:39:35 -04:00
|
|
|
readFile, _ := ioutil.ReadFile(dumpPath)
|
|
|
|
fileData := string(readFile)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Contains(fileData, "goroutine"))
|
2017-06-01 02:39:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDumpStacksWithEmptyInput(t *testing.T) {
|
|
|
|
path, err := DumpStacks("")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err)
|
|
|
|
assert.Check(t, is.Equal(os.Stderr.Name(), path))
|
2017-06-01 02:39:35 -04:00
|
|
|
}
|