139 lines
4.8 KiB
Go
139 lines
4.8 KiB
Go
package subprocess
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestRunCapturesResultSeparatelyFromDiagnostics(t *testing.T) {
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
const secret = "synthetic-result-secret"
|
|
dir := t.TempDir()
|
|
var result bytes.Buffer
|
|
req := RunRequest{
|
|
Executable: exe, Args: []string{"-test.run=^TestSubprocessHelper$", "--", "echoenv"},
|
|
EnvOverrides: map[string]string{"GO_WANT_SUBPROCESS_HELPER": "1", "SUBPROCESS_HELPER_ENV_KEY": "API_KEY", "API_KEY": secret},
|
|
StdoutResult: &result, StdoutResultLimit: 1024,
|
|
StdoutLogPath: filepath.Join(dir, "stdout.log"), StderrLogPath: filepath.Join(dir, "stderr.log"),
|
|
}
|
|
runResult, err := Run(context.Background(), req)
|
|
if err == nil || strings.Contains(err.Error(), secret) || !strings.Contains(err.Error(), "<redacted>") {
|
|
t.Fatalf("error = %v", err)
|
|
}
|
|
if result.String() != secret {
|
|
t.Fatalf("result = %q", result.String())
|
|
}
|
|
if runResult.StdoutLogPath != "" {
|
|
t.Fatalf("stdout log = %q", runResult.StdoutLogPath)
|
|
}
|
|
if _, err := os.Stat(req.StdoutLogPath); !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("stdout log exists: %v", err)
|
|
}
|
|
stderr, err := os.ReadFile(req.StderrLogPath)
|
|
if err != nil || strings.Contains(string(stderr), secret) || !strings.Contains(string(stderr), "<redacted>") {
|
|
t.Fatalf("stderr = %q, %v", stderr, err)
|
|
}
|
|
}
|
|
|
|
func TestRunResultBudget(t *testing.T) {
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, tc := range []struct {
|
|
name string
|
|
limit int64
|
|
want string
|
|
overflow bool
|
|
}{
|
|
{"exact", 6, "hello\n", false}, {"overflow", 5, "hello", true},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var result bytes.Buffer
|
|
req := RunRequest{Executable: exe, Args: []string{"-test.run=^TestSubprocessHelper$", "--", "success"}, EnvOverrides: map[string]string{"GO_WANT_SUBPROCESS_HELPER": "1", "SUBPROCESS_HELPER_STDOUT": "hello"}, DiagnosticOwner: "example adapter", StdoutResult: &result, StdoutResultLimit: tc.limit}
|
|
_, err := Run(context.Background(), req)
|
|
if (err != nil) != tc.overflow {
|
|
t.Fatalf("Run error = %v", err)
|
|
}
|
|
if tc.overflow && (!strings.Contains(err.Error(), "example adapter") || !strings.Contains(err.Error(), "stdout") || !strings.Contains(err.Error(), "5 bytes")) {
|
|
t.Fatalf("error = %v", err)
|
|
}
|
|
if result.String() != tc.want {
|
|
t.Fatalf("result = %q, want %q", result.String(), tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestRunResultPreservesBinaryBytes(t *testing.T) {
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var result bytes.Buffer
|
|
req := RunRequest{Executable: exe, Args: []string{"-test.run=^TestSubprocessHelper$", "--", "binary-result"}, EnvOverrides: map[string]string{"GO_WANT_SUBPROCESS_HELPER": "1"}, StdoutResult: &result, StdoutResultLimit: 4}
|
|
if _, err := Run(context.Background(), req); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !bytes.Equal(result.Bytes(), []byte{0, 255, '\n', 128}) {
|
|
t.Fatalf("result = %v", result.Bytes())
|
|
}
|
|
}
|
|
|
|
func TestRunResultCanExceedDiagnosticLimit(t *testing.T) {
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var result bytes.Buffer
|
|
req := RunRequest{Executable: exe, Args: []string{"-test.run=^TestSubprocessHelper$", "--", "spam"}, EnvOverrides: map[string]string{"GO_WANT_SUBPROCESS_HELPER": "1", "SUBPROCESS_HELPER_CHUNKS": "129"}, StdoutResult: &result, StdoutResultLimit: 129 * 64 * 1024}
|
|
if _, err := Run(context.Background(), req); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if int64(result.Len()) <= MaxStdoutDiagnosticBytes || !bytes.Equal(result.Bytes(), bytes.Repeat([]byte("x"), 129*64*1024)) {
|
|
t.Fatalf("unexpected result length/content: %d", result.Len())
|
|
}
|
|
}
|
|
|
|
type failingResultWriter struct{ err error }
|
|
|
|
func (w failingResultWriter) Write([]byte) (int, error) { return 0, w.err }
|
|
|
|
func TestRunResultWriterFailureAbortsChild(t *testing.T) {
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := errors.New("synthetic write failure")
|
|
req := RunRequest{Executable: exe, Args: []string{"-test.run=^TestSubprocessHelper$", "--", "result-wait"}, EnvOverrides: map[string]string{"GO_WANT_SUBPROCESS_HELPER": "1"}, StdoutResult: failingResultWriter{want}, StdoutResultLimit: 100, Timeout: 8 * time.Second}
|
|
start := time.Now()
|
|
_, err = Run(context.Background(), req)
|
|
if !errors.Is(err, want) {
|
|
t.Fatalf("error = %v, want writer error", err)
|
|
}
|
|
if time.Since(start) > 5*time.Second {
|
|
t.Fatalf("child was not terminated promptly")
|
|
}
|
|
}
|
|
|
|
func TestResultWriterReportsShortWrite(t *testing.T) {
|
|
w := &boundedResultWriter{target: shortResultWriter{}, limit: 5, abort: make(chan error, 1)}
|
|
if _, err := w.Write([]byte("abc")); !errors.Is(err, io.ErrShortWrite) {
|
|
t.Fatalf("error = %v", err)
|
|
}
|
|
}
|
|
|
|
type shortResultWriter struct{}
|
|
|
|
func (shortResultWriter) Write(p []byte) (int, error) { return len(p) - 1, nil }
|