Files
substrate/cmd/ateapi/internal/controlapi/workflow_delete_test.go
T
Michelle Au ac3f08f7ad Handle partial volume creation failures (#527)
Part of #232

1. During CreateActor, the volumes to be created get instantiated in the
Actor API in PENDING status. The Actor is still instantiated as
SUSPENDED.

However, the CreateVolume operation is moved to the beginning of
ResumeActor. This is because CreateActor is intentionally designed to
not be idempotent. The downside is that the user won't know dependent
resource creation failed until the first resume.

2. Changes volume id to use actor uid instead of atespace+actorname and
adds "substrate" prefix

3. Adds a new DELETING status that gets persisted before we start to
delete volumes.

This shifts the existing DB DELETE precondition checks to the new
DELETING status. And now you can only delete the actor in DB from
DELETING status.

4. Refactor DeleteActor to use a workflow

In the future, we will also add a workflow that will do mandatory node
cleanup of volumes before starting to delete the volumes.

- [x] Tests pass
- [x] Appropriate changes to documentation are included in the PR
2026-07-30 14:21:45 -04:00

141 lines
3.9 KiB
Go

// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controlapi
import (
"context"
"testing"
"github.com/agent-substrate/substrate/cmd/ateapi/internal/store/storetest"
"github.com/agent-substrate/substrate/internal/resources"
"github.com/agent-substrate/substrate/pkg/proto/ateapipb"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func TestDeleteActorWorkflow_ExecutionPaths(t *testing.T) {
tests := []struct {
name string
seedStatus ateapipb.Actor_Status
wantErr bool
wantCode codes.Code
}{
{
name: "delete suspended actor succeeds",
seedStatus: ateapipb.Actor_STATUS_SUSPENDED,
wantErr: false,
},
{
name: "delete crashed actor succeeds",
seedStatus: ateapipb.Actor_STATUS_CRASHED,
wantErr: false,
},
{
name: "delete deleting actor succeeds",
seedStatus: ateapipb.Actor_STATUS_DELETING,
wantErr: false,
},
{
name: "delete running actor rejected",
seedStatus: ateapipb.Actor_STATUS_RUNNING,
wantErr: true,
wantCode: codes.FailedPrecondition,
},
{
name: "delete paused actor rejected",
seedStatus: ateapipb.Actor_STATUS_PAUSED,
wantErr: true,
wantCode: codes.FailedPrecondition,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
st, cleanup := storetest.SetupTestStore(t)
defer cleanup()
w := newTestActorWorkflow(t, st, "ns", "tmpl1")
actorRef := resources.ActorRef{Atespace: "team-a", Name: "id1"}
seedWorkflowActor(t, ctx, st, actorRef, "ns", "tmpl1", tc.seedStatus)
deleted, err := w.DeleteActor(ctx, "team-a", "id1")
if tc.wantErr {
if got := status.Code(err); got != tc.wantCode {
t.Fatalf("status.Code(err) = %v, want %v (err: %v)", got, tc.wantCode, err)
}
} else {
if err != nil {
t.Fatalf("DeleteActor failed: %v", err)
}
if deleted == nil {
t.Fatalf("expected non-nil deleted actor")
}
if _, err := st.GetActor(ctx, actorRef); err == nil {
t.Errorf("expected actor to be deleted from store, but it still exists")
}
}
})
}
}
func TestDeleteSteps_CheckPrerequisite(t *testing.T) {
tests := []struct {
name string
step WorkflowStep[*DeleteInput, *DeleteState]
allowed map[ateapipb.Actor_Status]bool
}{
{
name: "LoadActorForDeleteStep",
step: &LoadActorForDeleteStep{},
allowed: nil,
},
{
name: "MarkDeletingStep",
step: &MarkDeletingStep{},
allowed: map[ateapipb.Actor_Status]bool{
ateapipb.Actor_STATUS_SUSPENDED: true,
ateapipb.Actor_STATUS_CRASHED: true,
ateapipb.Actor_STATUS_DELETING: true,
},
},
{
name: "DeleteVolumesStep",
step: &DeleteVolumesStep{},
allowed: map[ateapipb.Actor_Status]bool{
ateapipb.Actor_STATUS_DELETING: true,
},
},
{
name: "FinalizeDeletedStep",
step: &FinalizeDeletedStep{},
allowed: map[ateapipb.Actor_Status]bool{
ateapipb.Actor_STATUS_DELETING: true,
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
actorRef := resources.ActorRef{Atespace: "team-a", Name: "id1"}
for _, st := range allActorStatuses {
err := tc.step.CheckPrerequisite(ctx, &DeleteInput{ActorRef: actorRef}, &DeleteState{Actor: &ateapipb.Actor{Status: st}})
assertPrerequisiteResult(t, st, err, tc.allowed == nil || tc.allowed[st])
}
})
}
}