Verify S3 upload payloads and clarify recovery

This commit is contained in:
2026-12-01 20:11:56 +00:00
parent a92a117ad9
commit 8e4627e262
3 changed files with 43 additions and 3 deletions

View File

@@ -8,6 +8,7 @@ import (
"path/filepath"
"strings"
"testing"
"testing/iotest"
"time"
"github.com/aws/aws-sdk-go-v2/service/s3"
@@ -32,6 +33,7 @@ type fakeS3API struct {
putOut *s3.PutObjectOutput
putErr error
putBody []byte
deleteErr error
headErr error
@@ -110,6 +112,11 @@ func (f *fakeS3API) GetObject(_ context.Context, params *s3.GetObjectInput, _ ..
func (f *fakeS3API) PutObject(_ context.Context, params *s3.PutObjectInput, _ ...func(*s3.Options)) (*s3.PutObjectOutput, error) {
f.lastPut = params
var err error
f.putBody, err = io.ReadAll(params.Body)
if err != nil {
return nil, err
}
if f.putErr != nil {
return nil, f.putErr
}
@@ -236,6 +243,9 @@ func TestS3BackendUploadAndExists(t *testing.T) {
if client.lastPut == nil || *client.lastPut.Key != "runs/id/artifact.txt" {
t.Fatalf("PutObject key = %#v, want normalized key", client.lastPut)
}
if string(client.putBody) != "artifact" || client.lastPut.Metadata["kind"] != "artifact" {
t.Fatalf("PutObject body = %q, metadata = %#v", client.putBody, client.lastPut.Metadata)
}
ok, err := backend.Exists(context.Background(), "runs/id/artifact.txt")
if err != nil {
@@ -255,11 +265,26 @@ func TestS3BackendConditionalUploadUsesProviderPrecondition(t *testing.T) {
if client.lastPut == nil || client.lastPut.IfMatch == nil || *client.lastPut.IfMatch != "before" || client.lastPut.IfNoneMatch != nil {
t.Fatalf("PutObject conditional input = %#v", client.lastPut)
}
if string(client.putBody) != "payload" || *client.lastPut.Key != "control.json" {
t.Fatalf("PutObject body = %q, key = %q", client.putBody, *client.lastPut.Key)
}
client.putErr = &smithy.GenericAPIError{Code: "PreconditionFailed", Message: "changed"}
_, err := backend.UploadConditional(context.Background(), strings.NewReader("payload"), "control.json", UploadOptions{}, WriteCondition{RequireAbsent: true})
if !errors.Is(err, ErrConditionNotMet) {
t.Fatalf("UploadConditional() error = %v, want ErrConditionNotMet", err)
}
if client.lastPut.IfNoneMatch == nil || *client.lastPut.IfNoneMatch != "*" {
t.Fatalf("PutObject IfNoneMatch = %v, want *", client.lastPut.IfNoneMatch)
}
}
func TestS3BackendUploadPropagatesReaderFailure(t *testing.T) {
readErr := errors.New("synthetic reader failure")
backend := &S3Backend{bucket: "bucket-1", client: &fakeS3API{}}
_, err := backend.UploadConditional(context.Background(), iotest.ErrReader(readErr), "control.json", UploadOptions{}, WriteCondition{RequireAbsent: true})
if !errors.Is(err, readErr) {
t.Fatalf("UploadConditional() error = %v, want reader failure", err)
}
}
func TestS3BackendUploadMissingLocalFile(t *testing.T) {