2026-05-23 13:20:28 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2026-12-01 20:28:13 +00:00
|
|
|
"io"
|
2026-05-23 13:20:28 +00:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestDownloadObjectToTempSuccess(t *testing.T) {
|
|
|
|
|
store := &FakeBackend{}
|
2026-09-02 14:18:51 +00:00
|
|
|
store.SeedObject(FakeObject{Key: "sessions/a/runs/run-123/metadata.txt", Data: []byte("run-123\n")})
|
2026-05-23 13:20:28 +00:00
|
|
|
|
2026-12-01 20:28:13 +00:00
|
|
|
path, err := DownloadObjectToTemp(context.Background(), store, "sessions/a/runs/run-123/metadata.txt", "narratio-test-*.txt", nil)
|
2026-05-23 13:20:28 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("DownloadObjectToTemp() error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
t.Cleanup(func() { _ = os.Remove(path) })
|
|
|
|
|
|
|
|
|
|
data, err := os.ReadFile(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("ReadFile() error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
if string(data) != "run-123\n" {
|
|
|
|
|
t.Fatalf("downloaded data = %q, want %q", string(data), "run-123\n")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDownloadObjectToTempFailedDownloadRemovesTempFile(t *testing.T) {
|
|
|
|
|
sentinel := errors.New("download failed")
|
2026-12-01 20:47:59 +00:00
|
|
|
store := &FakeBackend{ReadErr: sentinel}
|
2026-05-23 13:20:28 +00:00
|
|
|
pattern := "narratio-test-fail-*.txt"
|
|
|
|
|
before, err := filepath.Glob(filepath.Join(os.TempDir(), "narratio-test-fail-*.txt"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Glob(before) error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 20:28:13 +00:00
|
|
|
path, err := DownloadObjectToTemp(context.Background(), store, "sessions/a/runs/run-123/metadata.txt", pattern, nil)
|
2026-05-23 13:20:28 +00:00
|
|
|
if !errors.Is(err, sentinel) {
|
|
|
|
|
t.Fatalf("DownloadObjectToTemp() error = %v, want %v", err, sentinel)
|
|
|
|
|
}
|
|
|
|
|
if strings.TrimSpace(path) != "" {
|
|
|
|
|
t.Fatalf("DownloadObjectToTemp() path = %q, want empty on failure", path)
|
|
|
|
|
}
|
|
|
|
|
after, err := filepath.Glob(filepath.Join(os.TempDir(), "narratio-test-fail-*.txt"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Glob(after) error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
if len(after) != len(before) {
|
|
|
|
|
t.Fatalf("temp file count changed after failed download: before=%d after=%d", len(before), len(after))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDownloadObjectToTempCallerContextWrappingPreservesCause(t *testing.T) {
|
|
|
|
|
sentinel := errors.New("object missing")
|
2026-12-01 20:47:59 +00:00
|
|
|
store := &FakeBackend{ReadErr: sentinel}
|
2026-05-23 13:20:28 +00:00
|
|
|
|
2026-12-01 20:28:13 +00:00
|
|
|
_, err := DownloadObjectToTemp(context.Background(), store, "sessions/a/runs/run-123/metadata.txt", "narratio-test-*.txt", nil)
|
2026-05-23 13:20:28 +00:00
|
|
|
if err == nil {
|
|
|
|
|
t.Fatal("DownloadObjectToTemp() error = nil, want error")
|
|
|
|
|
}
|
|
|
|
|
err = fmt.Errorf("download run pointer failed: %w", err)
|
|
|
|
|
if !errors.Is(err, sentinel) {
|
|
|
|
|
t.Fatalf("wrapped error does not preserve sentinel cause: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-12-01 20:28:13 +00:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|