279 lines
9.7 KiB
Go
279 lines
9.7 KiB
Go
package main
|
|
|
|
// Wiring tests for the per-connection response-deadline extensions on the
|
|
// long-running FS handlers (uploadUrl, extract, files/archive). The mechanism
|
|
// — that a handler writing past a short server-wide WriteTimeout loses the
|
|
// response, and that a SetWriteDeadline extension beats it — is proven
|
|
// end-to-end in upload_url_write_deadline_test.go on a real listener. These
|
|
// tests pin the WIRING: that each handler actually arms the extension, with
|
|
// the right budget, at the right place (after the fast validation writes, so
|
|
// early-400 paths keep the stock deadline).
|
|
//
|
|
// The recording writer works because http.NewResponseController calls
|
|
// SetWriteDeadline directly on the ResponseWriter when it implements
|
|
// interface{ SetWriteDeadline(time.Time) error } — no real connection needed.
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type deadlineRecordingWriter struct {
|
|
*httptest.ResponseRecorder
|
|
deadlines []time.Time
|
|
observed []time.Time // wall clock at each recording
|
|
}
|
|
|
|
func (w *deadlineRecordingWriter) SetWriteDeadline(t time.Time) error {
|
|
w.deadlines = append(w.deadlines, t)
|
|
w.observed = append(w.observed, time.Now())
|
|
return nil
|
|
}
|
|
|
|
// assertSingleExtension checks exactly one extension was armed, roughly
|
|
// budget in the future, and returns the response status for further checks.
|
|
func assertSingleExtension(t *testing.T, w *deadlineRecordingWriter, budget time.Duration, what string) int {
|
|
t.Helper()
|
|
if len(w.deadlines) != 1 {
|
|
t.Fatalf("%s: %d deadline extensions recorded, want exactly 1", what, len(w.deadlines))
|
|
}
|
|
delta := w.deadlines[0].Sub(w.observed[0])
|
|
if delta < budget-time.Minute || delta > budget+time.Minute {
|
|
t.Fatalf("%s: extension armed %.0fs out, want ~%.0fs (caller budget)", what, delta.Seconds(), budget.Seconds())
|
|
}
|
|
return w.Code
|
|
}
|
|
|
|
func wiringJSONRequest(t *testing.T, path string, body map[string]any) *http.Request {
|
|
t.Helper()
|
|
b, err := json.Marshal(body)
|
|
if err != nil {
|
|
t.Fatalf("marshal body: %v", err)
|
|
}
|
|
return httptest.NewRequest("POST", path, bytes.NewReader(b))
|
|
}
|
|
|
|
func seedZipArchive(t *testing.T, dir, name string) string {
|
|
t.Helper()
|
|
p := filepath.Join(dir, name)
|
|
f, err := os.Create(p)
|
|
if err != nil {
|
|
t.Fatalf("create zip: %v", err)
|
|
}
|
|
zw := zip.NewWriter(f)
|
|
fw, err := zw.Create("inner.txt")
|
|
if err != nil {
|
|
t.Fatalf("zip create: %v", err)
|
|
}
|
|
if _, err := fw.Write([]byte("hello extract")); err != nil {
|
|
t.Fatalf("zip write: %v", err)
|
|
}
|
|
if err := zw.Close(); err != nil {
|
|
t.Fatalf("zip close: %v", err)
|
|
}
|
|
if err := f.Close(); err != nil {
|
|
t.Fatalf("zip file close: %v", err)
|
|
}
|
|
return p
|
|
}
|
|
|
|
func TestFSExtractHandler_ExtendsWriteDeadlineToCallerBudget(t *testing.T) {
|
|
a, srvDir, _ := newFSScopeTestAgent(t)
|
|
zipPath := seedZipArchive(t, srvDir, "pkg.zip")
|
|
|
|
w := &deadlineRecordingWriter{ResponseRecorder: httptest.NewRecorder()}
|
|
a.handleFSExtract(w, wiringJSONRequest(t, "/v1/fs/extract", map[string]any{
|
|
"path": zipPath,
|
|
"server": "alpha",
|
|
}))
|
|
if code := assertSingleExtension(t, w, extractCallerWaitBudget, "extract"); code != 200 {
|
|
t.Fatalf("extract: status %d (%s)", code, w.Body.String())
|
|
}
|
|
}
|
|
|
|
// The extension must sit after the fast validation writes: a request that
|
|
// fails validation keeps the stock 120s deadline (zero extensions recorded).
|
|
func TestFSExtractHandler_NoExtensionBeforeValidation(t *testing.T) {
|
|
a, srvDir, _ := newFSScopeTestAgent(t)
|
|
|
|
w := &deadlineRecordingWriter{ResponseRecorder: httptest.NewRecorder()}
|
|
a.handleFSExtract(w, wiringJSONRequest(t, "/v1/fs/extract", map[string]any{
|
|
"path": filepath.Join(srvDir, "missing.zip"),
|
|
"server": "alpha",
|
|
}))
|
|
if len(w.deadlines) != 0 {
|
|
t.Fatalf("extract 404 path: %d extensions recorded, want 0", len(w.deadlines))
|
|
}
|
|
|
|
w = &deadlineRecordingWriter{ResponseRecorder: httptest.NewRecorder()}
|
|
a.handleFSExtract(w, wiringJSONRequest(t, "/v1/fs/extract", map[string]any{
|
|
"path": filepath.Join(srvDir, "data", "file.txt"),
|
|
"server": "alpha",
|
|
}))
|
|
if len(w.deadlines) != 0 {
|
|
t.Fatalf("extract unsupported-type path: %d extensions recorded, want 0", len(w.deadlines))
|
|
}
|
|
}
|
|
|
|
func TestFilesArchiveHandler_ExtendsWriteDeadlineToCallerBudget(t *testing.T) {
|
|
a, srvDir, _ := newFSScopeTestAgent(t)
|
|
|
|
w := &deadlineRecordingWriter{ResponseRecorder: httptest.NewRecorder()}
|
|
a.handleFilesArchive(w, wiringJSONRequest(t, "/v1/servers/alpha/files/archive", map[string]any{
|
|
"paths": []string{"data"},
|
|
"destDir": "",
|
|
}), "alpha")
|
|
if code := assertSingleExtension(t, w, archiveCallerWaitBudget, "archive"); code != 200 {
|
|
t.Fatalf("archive: status %d (%s)", code, w.Body.String())
|
|
}
|
|
// The archive itself must still have been committed.
|
|
ents, err := os.ReadDir(srvDir)
|
|
if err != nil {
|
|
t.Fatalf("readdir: %v", err)
|
|
}
|
|
found := false
|
|
for _, e := range ents {
|
|
if filepath.Ext(e.Name()) == ".gz" {
|
|
found = true
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatalf("archive not committed: %v", ents)
|
|
}
|
|
}
|
|
|
|
func TestFilesArchiveHandler_NoExtensionBeforeValidation(t *testing.T) {
|
|
a, _, _ := newFSScopeTestAgent(t)
|
|
|
|
w := &deadlineRecordingWriter{ResponseRecorder: httptest.NewRecorder()}
|
|
a.handleFilesArchive(w, wiringJSONRequest(t, "/v1/servers/alpha/files/archive", map[string]any{
|
|
"paths": []string{},
|
|
"destDir": "",
|
|
}), "alpha")
|
|
if len(w.deadlines) != 0 {
|
|
t.Fatalf("archive empty-paths path: %d extensions recorded, want 0", len(w.deadlines))
|
|
}
|
|
}
|
|
|
|
// The uploadUrl extension must be derived from the fetch context's deadline
|
|
// (30m budget + 2m headroom from request time, one single arming): deriving
|
|
// from wall-clock accumulation instead would let a post-fetch scope-lock wait
|
|
// push the response past the armed deadline.
|
|
func TestUploadURLHandler_ExtendsDeadlineFromFetchContext(t *testing.T) {
|
|
a, serverDir := newURLUploadFilenameTestAgent(t)
|
|
t.Setenv("REMOTE_FETCH_ALLOW_ANY_PORT", "1")
|
|
baseURL := serveURLUploadFixture(t, "", bytes.Repeat([]byte("Z"), 1024))
|
|
|
|
w := &deadlineRecordingWriter{ResponseRecorder: httptest.NewRecorder()}
|
|
a.handleFSUploadURL(w, wiringJSONRequest(t, "/v1/fs/uploadUrl", map[string]any{
|
|
"dir": serverDir,
|
|
"server": "alpha",
|
|
"url": baseURL + "/f.bin",
|
|
"filename": "f.bin",
|
|
}))
|
|
if len(w.deadlines) != 1 {
|
|
t.Fatalf("uploadUrl: %d deadline extensions recorded, want exactly 1", len(w.deadlines))
|
|
}
|
|
delta := w.deadlines[0].Sub(w.observed[0])
|
|
if delta < 31*time.Minute || delta > 33*time.Minute {
|
|
t.Fatalf("uploadUrl: extension armed %.0fs out, want ~32m (fetch budget + headroom)", delta.Seconds())
|
|
}
|
|
if w.Code != 200 {
|
|
t.Fatalf("uploadUrl: status %d (%s)", w.Code, w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestFSDeleteHandler_ExtendsWriteDeadlineToCallerBudget(t *testing.T) {
|
|
a, srvDir, _ := newFSScopeTestAgent(t)
|
|
p := filepath.Join(srvDir, "doomed.txt")
|
|
if err := os.WriteFile(p, []byte("bye"), 0o644); err != nil {
|
|
t.Fatalf("seed doomed.txt: %v", err)
|
|
}
|
|
|
|
w := &deadlineRecordingWriter{ResponseRecorder: httptest.NewRecorder()}
|
|
a.handleFSDelete(w, wiringJSONRequest(t, "/v1/fs/delete", map[string]any{
|
|
"path": p,
|
|
"server": "alpha",
|
|
"isDir": false,
|
|
}))
|
|
if code := assertSingleExtension(t, w, deleteCallerWaitBudget, "delete"); code != 200 {
|
|
t.Fatalf("delete: status %d (%s)", code, w.Body.String())
|
|
}
|
|
}
|
|
|
|
// The already-missing fast path and invalid paths return before the arm: they
|
|
// keep the stock deadline.
|
|
func TestFSDeleteHandler_NoExtensionBeforeValidation(t *testing.T) {
|
|
a, srvDir, _ := newFSScopeTestAgent(t)
|
|
|
|
w := &deadlineRecordingWriter{ResponseRecorder: httptest.NewRecorder()}
|
|
a.handleFSDelete(w, wiringJSONRequest(t, "/v1/fs/delete", map[string]any{
|
|
"path": filepath.Join(srvDir, "missing.txt"),
|
|
"server": "alpha",
|
|
"isDir": false,
|
|
}))
|
|
if w.Code != 200 || len(w.deadlines) != 0 {
|
|
t.Fatalf("already-missing delete: status %d, %d extensions, want 200/0", w.Code, len(w.deadlines))
|
|
}
|
|
|
|
w = &deadlineRecordingWriter{ResponseRecorder: httptest.NewRecorder()}
|
|
a.handleFSDelete(w, wiringJSONRequest(t, "/v1/fs/delete", map[string]any{
|
|
"path": "/etc/passwd",
|
|
"server": "alpha",
|
|
"isDir": false,
|
|
}))
|
|
if len(w.deadlines) != 0 {
|
|
t.Fatalf("invalid-path delete: %d extensions recorded, want 0", len(w.deadlines))
|
|
}
|
|
}
|
|
|
|
func TestFSDeleteBatchHandler_ExtendsWriteDeadlineToCallerBudget(t *testing.T) {
|
|
a, srvDir, _ := newFSScopeTestAgent(t)
|
|
p := filepath.Join(srvDir, "doomed-batch.txt")
|
|
if err := os.WriteFile(p, []byte("bye"), 0o644); err != nil {
|
|
t.Fatalf("seed doomed-batch.txt: %v", err)
|
|
}
|
|
|
|
w := &deadlineRecordingWriter{ResponseRecorder: httptest.NewRecorder()}
|
|
a.handleFSDeleteBatch(w, wiringJSONRequest(t, "/v1/fs/delete-batch", map[string]any{
|
|
"server": "alpha",
|
|
"items": []map[string]any{{"path": p, "isDir": false}},
|
|
}))
|
|
if code := assertSingleExtension(t, w, deleteCallerWaitBudget, "delete-batch"); code != 200 {
|
|
t.Fatalf("delete-batch: status %d (%s)", code, w.Body.String())
|
|
}
|
|
}
|
|
|
|
// The download stream is client-paced: the deadline must be CLEARED (zero),
|
|
// not extended, or a slow transfer is truncated mid-body at the stock 120s.
|
|
func TestFSDownloadHandler_ClearsWriteDeadlineForStream(t *testing.T) {
|
|
a, srvDir, _ := newFSScopeTestAgent(t)
|
|
want := bytes.Repeat([]byte("D"), 4096)
|
|
p := filepath.Join(srvDir, "dl.bin")
|
|
if err := os.WriteFile(p, want, 0o644); err != nil {
|
|
t.Fatalf("seed dl.bin: %v", err)
|
|
}
|
|
|
|
w := &deadlineRecordingWriter{ResponseRecorder: httptest.NewRecorder()}
|
|
a.handleFSDownload(w, wiringJSONRequest(t, "/v1/fs/download", map[string]any{
|
|
"path": p,
|
|
"server": "alpha",
|
|
}))
|
|
if len(w.deadlines) != 1 {
|
|
t.Fatalf("download: %d deadline changes recorded, want exactly 1 (cleared)", len(w.deadlines))
|
|
}
|
|
if !w.deadlines[0].IsZero() {
|
|
t.Fatalf("download: deadline set to %v, want zero (cleared for the client-paced stream)", w.deadlines[0])
|
|
}
|
|
if w.Code != 200 || !bytes.Equal(w.Body.Bytes(), want) {
|
|
t.Fatalf("download: status %d, %d bytes", w.Code, w.Body.Len())
|
|
}
|
|
}
|