fix: complete P2 host workspace contract
This commit is contained in:
@@ -6,8 +6,10 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRunnerPassesEachArgumentWithoutShellSplitting(t *testing.T) {
|
||||
@@ -65,18 +67,78 @@ func writeExecutable(t *testing.T, contents string) string {
|
||||
return path
|
||||
}
|
||||
|
||||
|
||||
func TestRunnerBoundedCapturesExactlyAtLimit(t *testing.T) {
|
||||
runner := NewRunner(writeExecutable(t, "#!/bin/sh\nprintf 1234567890\nprintf error12345 >&2\n"))
|
||||
result, err := runner.RunBounded(context.Background(), nil, nil, CaptureLimits{StdoutBytes: 10, StderrBytes: 10})
|
||||
if err != nil { t.Fatal(err) }
|
||||
if result.Stdout != "1234567890" || result.Stderr != "error12345" { t.Fatalf("bounded result = %#v", result) }
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Stdout != "1234567890" || result.Stderr != "error12345" {
|
||||
t.Fatalf("bounded result = %#v", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerBoundedRejectsOneByteOverEachStream(t *testing.T) {
|
||||
for _, script := range []string{"#!/bin/sh\nprintf 12345678901\n", "#!/bin/sh\nprintf 12345678901 >&2\n"} {
|
||||
runner := NewRunner(writeExecutable(t, script)); result, err := runner.RunBounded(context.Background(), nil, nil, CaptureLimits{StdoutBytes: 10, StderrBytes: 10})
|
||||
if !errors.Is(err, ErrOutputLimit) { t.Fatalf("err = %v result=%#v", err, result) }
|
||||
if len(result.Stdout) > 10 || len(result.Stderr) > 10 { t.Fatalf("retained overflow: %#v", result) }
|
||||
runner := NewRunner(writeExecutable(t, script))
|
||||
result, err := runner.RunBounded(context.Background(), nil, nil, CaptureLimits{StdoutBytes: 10, StderrBytes: 10})
|
||||
if !errors.Is(err, ErrOutputLimit) {
|
||||
t.Fatalf("err = %v result=%#v", err, result)
|
||||
}
|
||||
if len(result.Stdout) > 10 || len(result.Stderr) > 10 {
|
||||
t.Fatalf("retained overflow: %#v", result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerBoundedTerminatesInfiniteStreams(t *testing.T) {
|
||||
for _, script := range []string{"#!/bin/sh\nwhile :; do printf x; done", "#!/bin/sh\nwhile :; do printf x >&2; done"} {
|
||||
runner := NewRunner(writeExecutable(t, script))
|
||||
result, err := runner.RunBounded(context.Background(), nil, nil, CaptureLimits{StdoutBytes: 1024, StderrBytes: 1024})
|
||||
if !errors.Is(err, ErrOutputLimit) {
|
||||
t.Fatalf("err = %v result = %#v", err, result)
|
||||
}
|
||||
if len(result.Stdout) > 1024 || len(result.Stderr) > 1024 {
|
||||
t.Fatalf("retained overflow: %#v", result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerBoundedCancellationWinsOverChildKill(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
runner := NewRunner(writeExecutable(t, "#!/bin/sh\nsleep 30\n"))
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := runner.RunBounded(ctx, nil, nil, CaptureLimits{StdoutBytes: 32, StderrBytes: 32})
|
||||
done <- err
|
||||
}()
|
||||
cancel()
|
||||
select {
|
||||
case err := <-done:
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("cancellation did not terminate process")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerBoundedTerminatesGrandchildHoldingPipe(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("shell process-group fixture is Unix-specific")
|
||||
}
|
||||
runner := NewRunner(writeExecutable(t, "#!/bin/sh\n(sleep 30; printf survivor) &\nwhile :; do printf x; done\n"))
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := runner.RunBounded(context.Background(), nil, nil, CaptureLimits{StdoutBytes: 64, StderrBytes: 64})
|
||||
done <- err
|
||||
}()
|
||||
select {
|
||||
case err := <-done:
|
||||
if !errors.Is(err, ErrOutputLimit) {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("grandchild kept owned pipe alive")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user