@@ -22,16 +22,20 @@ import (
2222 "fmt"
2323 "strings"
2424 "sync"
25+ "sync/atomic"
2526 "testing"
2627 "time"
2728
29+ "cloud.google.com/go/storage"
2830 "github.com/agent-substrate/substrate/cmd/ateapi/internal/store"
2931 "github.com/agent-substrate/substrate/internal/actoridjwt"
3032 "github.com/agent-substrate/substrate/internal/ateattr"
33+ "github.com/agent-substrate/substrate/internal/objectstore"
3134 "github.com/agent-substrate/substrate/internal/proto/ateletpb"
3235 "github.com/agent-substrate/substrate/internal/resources"
3336 "github.com/agent-substrate/substrate/internal/volume"
3437 "github.com/agent-substrate/substrate/pkg/proto/ateapipb"
38+ "github.com/aws/aws-sdk-go-v2/service/s3/types"
3539 "github.com/google/go-cmp/cmp"
3640 "github.com/google/go-cmp/cmp/cmpopts"
3741 "go.opentelemetry.io/otel/attribute"
@@ -1080,6 +1084,81 @@ func TestDeleteActor_Success(t *testing.T) {
10801084 assertGrpcError (t , err , codes .NotFound , "Actor test-atespace/id1 not found" )
10811085}
10821086
1087+ func TestDeleteActor_MissingSnapshotBucket (t * testing.T ) {
1088+ for _ , provider := range []struct {
1089+ name string
1090+ err error
1091+ }{
1092+ {name : "S3" , err : & types.NoSuchBucket {}},
1093+ {name : "GCS" , err : storage .ErrBucketNotExist },
1094+ } {
1095+ for _ , state := range []ateapipb.ActorState {
1096+ ateapipb .ActorState_ACTOR_STATE_SUSPENDED ,
1097+ ateapipb .ActorState_ACTOR_STATE_DELETING ,
1098+ } {
1099+ t .Run (provider .name + "/" + state .String (), func (t * testing.T ) {
1100+ ns := namespaceForTest ("ns-delete-missing-bucket" )
1101+ tc := setupTest (t , ns )
1102+ defer tc .cleanup ()
1103+ createTemplate (t , tc , ns )
1104+ ctx := t .Context ()
1105+ ref := & ateapipb.ObjectRef {Atespace : testAtespace , Name : testActorID }
1106+ actor , err := tc .client .CreateActor (ctx , & ateapipb.CreateActorRequest {Actor : & ateapipb.Actor {
1107+ Metadata : & ateapipb.ResourceMetadata {Atespace : ref .GetAtespace (), Name : ref .GetName ()},
1108+ ActorTemplate : & ateapipb.ObjectRef {Atespace : testAtespace , Name : "tmpl1" },
1109+ }})
1110+ if err != nil {
1111+ t .Fatalf ("CreateActor: %v" , err )
1112+ }
1113+ uri , err := resources .NewActorSnapshotURI (testStorageLocation , testAtespace , actor .GetMetadata ().GetUid (), "snapshot" )
1114+ if err != nil {
1115+ t .Fatal (err )
1116+ }
1117+ if _ , err := tc .persistence .UpdateActor (ctx , resources .ActorRefFromActor (actor ), store .PreconditionFrom (actor ), func (toUpdate * ateapipb.Actor ) error {
1118+ toUpdate .Status .State = state
1119+ toUpdate .Status .ExternalSnapshot = nil
1120+ if state == ateapipb .ActorState_ACTOR_STATE_SUSPENDED {
1121+ toUpdate .Status .ExternalSnapshot = & ateapipb.ExternalSnapshot {
1122+ SnapshotUri : uri .String (), ContentScope : ateapipb .SnapshotContentScope_SNAPSHOT_CONTENT_SCOPE_FULL ,
1123+ }
1124+ } else {
1125+ // A failed upload leaves only the in-progress URI, even
1126+ // when a prior deletion already marked the actor DELETING.
1127+ toUpdate .Status .InProgressSnapshotUri = uri .String ()
1128+ }
1129+ return nil
1130+ }); err != nil {
1131+ t .Fatalf ("UpdateActor: %v" , err )
1132+ }
1133+ bucket , prefix , err := objectstore .BucketPrefix (uri .OwnerPrefix ())
1134+ if err != nil {
1135+ t .Fatal (err )
1136+ }
1137+ var listCalls atomic.Int32
1138+ tc .objectStore .OnList = func (gotBucket , gotPrefix string ) error {
1139+ listCalls .Add (1 )
1140+ if gotBucket != bucket || gotPrefix != prefix {
1141+ return fmt .Errorf ("unexpected cleanup prefix: %s/%s" , gotBucket , gotPrefix )
1142+ }
1143+ return fmt .Errorf ("snapshot bucket removed: %w" , provider .err )
1144+ }
1145+ deleted , err := tc .client .DeleteActor (ctx , & ateapipb.DeleteActorRequest {Actor : ref })
1146+ if err != nil {
1147+ t .Fatalf ("DeleteActor with missing snapshot bucket: %v" , err )
1148+ }
1149+ if deleted .GetMetadata ().GetUid () != actor .GetMetadata ().GetUid () {
1150+ t .Errorf ("DeleteActor returned a different actor: %v" , deleted )
1151+ }
1152+ if got := listCalls .Load (); got != 1 {
1153+ t .Errorf ("snapshot cleanup list calls = %d, want 1" , got )
1154+ }
1155+ _ , err = tc .client .GetActor (ctx , & ateapipb.GetActorRequest {Actor : ref })
1156+ assertGrpcError (t , err , codes .NotFound , "Actor test-atespace/id1 not found" )
1157+ })
1158+ }
1159+ }
1160+ }
1161+
10831162func TestDeleteActor_NotSuspended (t * testing.T ) {
10841163 ns := namespaceForTest ("ns-delete-notsuspended" )
10851164 tc := setupTest (t , ns )
0 commit comments