| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/exec" |
| 6 | "runtime" |
| 7 | "syscall" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestRelayRecognizesOnlyTheRequestedTermination(t *testing.T) { |
| 12 | if runtime.GOOS == "windows" { |
| 13 | t.Skip("POSIX signal fixture; Windows build is checked separately") |
| 14 | } |
| 15 | for _, tc := range []struct { |
| 16 | child string |
| 17 | forwarded os.Signal |
| 18 | want bool |
| 19 | }{ |
| 20 | {"kill -TERM $$", syscall.SIGTERM, true}, |
| 21 | {"exit 143", syscall.SIGTERM, true}, |
| 22 | {"exit 143", nil, false}, |
| 23 | {"kill -TERM $$", nil, false}, |
| 24 | {"exit 7", syscall.SIGTERM, false}, |
| 25 | {"kill -KILL $$", syscall.SIGTERM, false}, |
| 26 | } { |
| 27 | err := exec.Command("sh", "-c", tc.child).Run() |
| 28 | if got := expectedRelayStop(err, tc.forwarded); got != tc.want { |
| 29 | t.Fatalf("child=%q signal=%v: got %t, want %t", tc.child, tc.forwarded, got, tc.want) |
| 30 | } |
| 31 | } |
| 32 | } |