Bound restore and publication verification downloads
This commit is contained in:
@@ -33,6 +33,7 @@ type FakeBackend struct {
|
||||
ExistsErr error
|
||||
UploadHook func(FakeUploadCall) error
|
||||
DownloadHook func(FakeDownloadCall) error
|
||||
ReadHook func(FakeReadCall) error
|
||||
}
|
||||
|
||||
// FakeUploadCall captures one upload invocation in call order.
|
||||
@@ -126,6 +127,11 @@ func (f *FakeBackend) Read(ctx context.Context, key string) (ObjectInfo, io.Read
|
||||
return ObjectInfo{}, nil, f.DownloadErr
|
||||
}
|
||||
normalizedKey := normalizeObjectKey(key)
|
||||
if f.ReadHook != nil {
|
||||
if err := f.ReadHook(FakeReadCall{Key: normalizedKey}); err != nil {
|
||||
return ObjectInfo{}, nil, err
|
||||
}
|
||||
}
|
||||
f.mu.RLock()
|
||||
obj, ok := f.Objects[normalizedKey]
|
||||
if ok {
|
||||
|
||||
@@ -2,6 +2,7 @@ package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -10,7 +11,7 @@ import (
|
||||
|
||||
// DownloadObjectToTemp downloads an object into a temporary file and returns
|
||||
// the cleaned local path.
|
||||
func DownloadObjectToTemp(ctx context.Context, store ObjectStore, key, pattern string) (string, error) {
|
||||
func DownloadObjectToTemp(ctx context.Context, store ObjectStore, key, pattern string, expected *int64) (string, error) {
|
||||
if store == nil {
|
||||
return "", fmt.Errorf("object store is required")
|
||||
}
|
||||
@@ -23,12 +24,9 @@ func DownloadObjectToTemp(ctx context.Context, store ObjectStore, key, pattern s
|
||||
return "", fmt.Errorf("create temp file: %w", err)
|
||||
}
|
||||
path := tmp.Name()
|
||||
if err := tmp.Close(); err != nil {
|
||||
_ = os.Remove(path)
|
||||
return "", fmt.Errorf("close temp file: %w", err)
|
||||
}
|
||||
|
||||
if err := store.Download(ctx, key, path); err != nil {
|
||||
_, transferErr := StreamObject(ctx, store, key, tmp, expected)
|
||||
closeErr := tmp.Close()
|
||||
if err := errors.Join(transferErr, closeErr); err != nil {
|
||||
_ = os.Remove(path)
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -14,7 +15,7 @@ func TestDownloadObjectToTempSuccess(t *testing.T) {
|
||||
store := &FakeBackend{}
|
||||
store.SeedObject(FakeObject{Key: "sessions/a/runs/run-123/metadata.txt", Data: []byte("run-123\n")})
|
||||
|
||||
path, err := DownloadObjectToTemp(context.Background(), store, "sessions/a/runs/run-123/metadata.txt", "narratio-test-*.txt")
|
||||
path, err := DownloadObjectToTemp(context.Background(), store, "sessions/a/runs/run-123/metadata.txt", "narratio-test-*.txt", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("DownloadObjectToTemp() error = %v", err)
|
||||
}
|
||||
@@ -38,7 +39,7 @@ func TestDownloadObjectToTempFailedDownloadRemovesTempFile(t *testing.T) {
|
||||
t.Fatalf("Glob(before) error = %v", err)
|
||||
}
|
||||
|
||||
path, err := DownloadObjectToTemp(context.Background(), store, "sessions/a/runs/run-123/metadata.txt", pattern)
|
||||
path, err := DownloadObjectToTemp(context.Background(), store, "sessions/a/runs/run-123/metadata.txt", pattern, nil)
|
||||
if !errors.Is(err, sentinel) {
|
||||
t.Fatalf("DownloadObjectToTemp() error = %v, want %v", err, sentinel)
|
||||
}
|
||||
@@ -58,7 +59,7 @@ func TestDownloadObjectToTempCallerContextWrappingPreservesCause(t *testing.T) {
|
||||
sentinel := errors.New("object missing")
|
||||
store := &FakeBackend{DownloadErr: sentinel}
|
||||
|
||||
_, err := DownloadObjectToTemp(context.Background(), store, "sessions/a/runs/run-123/metadata.txt", "narratio-test-*.txt")
|
||||
_, err := DownloadObjectToTemp(context.Background(), store, "sessions/a/runs/run-123/metadata.txt", "narratio-test-*.txt", nil)
|
||||
if err == nil {
|
||||
t.Fatal("DownloadObjectToTemp() error = nil, want error")
|
||||
}
|
||||
@@ -67,3 +68,20 @@ func TestDownloadObjectToTempCallerContextWrappingPreservesCause(t *testing.T) {
|
||||
t.Fatalf("wrapped error does not preserve sentinel cause: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDownloadObjectToTempStopsOversizedResponse(t *testing.T) {
|
||||
t.Setenv("TMPDIR", t.TempDir())
|
||||
expected := int64(3)
|
||||
body := &trackingReadCloser{reader: strings.NewReader("abcdefghi"), chunkSize: 1}
|
||||
store := &boundedReadStore{read: func(context.Context, string) (ObjectInfo, io.ReadCloser, error) {
|
||||
return ObjectInfo{Size: expected, SizeKnown: true}, body, nil
|
||||
}}
|
||||
path, err := DownloadObjectToTemp(context.Background(), store, "key", "narratio-test-*.tmp", &expected)
|
||||
if err == nil || !strings.Contains(err.Error(), "oversized") || path != "" || body.bytesRead != 4 || !body.closed {
|
||||
t.Fatalf("path=%q err=%v read=%d closed=%v", path, err, body.bytesRead, body.closed)
|
||||
}
|
||||
entries, err := os.ReadDir(os.TempDir())
|
||||
if err != nil || len(entries) != 0 {
|
||||
t.Fatalf("temp entries=%v err=%v", entries, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user